From e6c92eccf1e2ee1f688e7ceca20ebc69b70cb2f2 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 2 Oct 2018 17:12:55 -0700 Subject: [PATCH] cmd/openshift-install: Add Terraform version to 'version' output And print the error information if we can't extract it. The .Stderr stuffing is because Terraform just prints usage information to stdout (not stderr!) if you call it with an unrecognized command: $ terraform does-not-exit >stdout 2>stderr $ head -n1 stdout Usage: terraform [-version] [-help] [args] $ ls -l stderr -rw-r--r--. 1 trking trking 0 Oct 2 17:14 stderr and in this case we want to see that usage information. Ideally Terraform would have printed "unrecognized command 'does-no-exist'" or some such to stderr, but that's a bit bigger than we can work around here. --- .github/ISSUE_TEMPLATE.md | 15 +-------------- cmd/openshift-install/main.go | 11 +++++++++++ pkg/terraform/executor.go | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 18e5c27ea1..ce88c85bc8 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -7,26 +7,13 @@ If we can't reproduce a bug we might close your issue. If we're wrong, PLEASE feel free to reopen it and explain why. --> -# Versions - -## Installer version: +# Version ```console $ openshift-install version ``` -## Terraform version - - - -```console -$ terraform version - -``` - # Platform (aws|libvirt|openshift): Enter text here. diff --git a/cmd/openshift-install/main.go b/cmd/openshift-install/main.go index c14d57095d..5a520d5451 100644 --- a/cmd/openshift-install/main.go +++ b/cmd/openshift-install/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "os/exec" "github.com/sirupsen/logrus" "gopkg.in/alecthomas/kingpin.v2" @@ -11,6 +12,7 @@ import ( "github.com/openshift/installer/pkg/asset/stock" "github.com/openshift/installer/pkg/destroy" _ "github.com/openshift/installer/pkg/destroy/libvirt" + "github.com/openshift/installer/pkg/terraform" ) var ( @@ -44,6 +46,15 @@ func main() { if command == versionCommand.FullCommand() { fmt.Printf("%s %s\n", os.Args[0], version) + terraformVersion, err := terraform.Version() + if err != nil { + exitError, ok := err.(*exec.ExitError) + if ok && len(exitError.Stderr) > 0 { + logrus.Error(string(exitError.Stderr)) + } + logrus.Fatalf("Failed to calculate Terraform version: %v", err) + } + fmt.Println(terraformVersion) return } diff --git a/pkg/terraform/executor.go b/pkg/terraform/executor.go index 3c526b8101..52ced7eea4 100644 --- a/pkg/terraform/executor.go +++ b/pkg/terraform/executor.go @@ -7,6 +7,7 @@ import ( "os/exec" "path/filepath" "runtime" + "strings" "github.com/sirupsen/logrus" ) @@ -73,6 +74,24 @@ func (ex *executor) execute(clusterDir string, args ...string) error { return cmd.Run() } +// Version gets the output of 'terrraform version'. +func Version() (version string, err error) { + // Find the Terraform binary. + binPath, err := tfBinaryPath() + if err != nil { + return "", err + } + + output, err := exec.Command(binPath, "version").Output() + if err != nil { + exitError := err.(*exec.ExitError) + if len(exitError.Stderr) == 0 { + exitError.Stderr = output + } + } + return strings.TrimRight(string(output), "\n"), err +} + // tfBinaryPath searches for a Terraform binary on disk: // - in the executing binary's folder, // - in the current working directory,