2014-05-24 21:04:43 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-06-10 19:28:47 +02:00
|
|
|
"io"
|
2014-05-24 21:04:43 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2014-10-03 22:02:16 +02:00
|
|
|
"sync"
|
2014-05-24 21:04:43 +02:00
|
|
|
|
2014-06-10 06:44:13 +02:00
|
|
|
"github.com/hashicorp/terraform/plugin"
|
2014-05-24 21:04:43 +02:00
|
|
|
"github.com/mitchellh/cli"
|
2014-05-31 01:07:26 +02:00
|
|
|
"github.com/mitchellh/panicwrap"
|
2014-06-10 19:28:47 +02:00
|
|
|
"github.com/mitchellh/prefixedio"
|
2014-05-24 21:04:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
os.Exit(realMain())
|
|
|
|
}
|
|
|
|
|
|
|
|
func realMain() int {
|
2014-05-31 01:07:26 +02:00
|
|
|
var wrapConfig panicwrap.WrapConfig
|
|
|
|
|
|
|
|
if !panicwrap.Wrapped(&wrapConfig) {
|
|
|
|
// Determine where logs should go in general (requested by the user)
|
|
|
|
logWriter, err := logOutput()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Couldn't setup log output: %s", err)
|
|
|
|
return 1
|
|
|
|
}
|
2014-06-10 19:28:47 +02:00
|
|
|
if logWriter == nil {
|
|
|
|
logWriter = ioutil.Discard
|
|
|
|
}
|
2014-05-31 01:07:26 +02:00
|
|
|
|
|
|
|
// We always send logs to a temporary file that we use in case
|
|
|
|
// there is a panic. Otherwise, we delete it.
|
|
|
|
logTempFile, err := ioutil.TempFile("", "terraform-log")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Couldn't setup logging tempfile: %s", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
defer os.Remove(logTempFile.Name())
|
2014-06-10 19:28:47 +02:00
|
|
|
defer logTempFile.Close()
|
2014-05-31 01:07:26 +02:00
|
|
|
|
|
|
|
// Tell the logger to log to this file
|
2014-06-10 19:28:47 +02:00
|
|
|
os.Setenv(EnvLog, "")
|
|
|
|
os.Setenv(EnvLogFile, "")
|
|
|
|
|
|
|
|
// Setup the prefixed readers that send data properly to
|
|
|
|
// stdout/stderr.
|
2014-10-03 22:02:16 +02:00
|
|
|
doneCh := make(chan struct{})
|
2014-06-10 19:28:47 +02:00
|
|
|
outR, outW := io.Pipe()
|
2014-10-03 22:02:16 +02:00
|
|
|
go copyOutput(outR, doneCh)
|
2014-05-31 01:07:26 +02:00
|
|
|
|
|
|
|
// Create the configuration for panicwrap and wrap our executable
|
|
|
|
wrapConfig.Handler = panicHandler(logTempFile)
|
2014-06-10 19:28:47 +02:00
|
|
|
wrapConfig.Writer = io.MultiWriter(logTempFile, logWriter)
|
|
|
|
wrapConfig.Stdout = outW
|
2014-05-31 01:07:26 +02:00
|
|
|
exitStatus, err := panicwrap.Wrap(&wrapConfig)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Couldn't start Terraform: %s", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// If >= 0, we're the parent, so just exit
|
|
|
|
if exitStatus >= 0 {
|
2014-10-03 22:02:16 +02:00
|
|
|
// Close the stdout writer so that our copy process can finish
|
|
|
|
outW.Close()
|
|
|
|
|
|
|
|
// Wait for the output copying to finish
|
|
|
|
<-doneCh
|
|
|
|
|
2014-05-31 01:07:26 +02:00
|
|
|
return exitStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're the child, so just close the tempfile we made in order to
|
|
|
|
// save file handles since the tempfile is only used by the parent.
|
|
|
|
logTempFile.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the real main
|
|
|
|
return wrappedMain()
|
|
|
|
}
|
|
|
|
|
|
|
|
func wrappedMain() int {
|
2014-06-10 19:28:47 +02:00
|
|
|
log.SetOutput(os.Stderr)
|
2014-05-24 21:04:43 +02:00
|
|
|
|
2014-06-10 06:57:37 +02:00
|
|
|
// Load the configuration
|
|
|
|
config := BuiltinConfig
|
2014-08-29 02:27:15 +02:00
|
|
|
if err := config.Discover(); err != nil {
|
2014-10-11 22:03:11 +02:00
|
|
|
Ui.Error(fmt.Sprintf("Error discovering plugins: %s", err))
|
2014-08-29 02:27:15 +02:00
|
|
|
return 1
|
|
|
|
}
|
2014-06-10 06:57:37 +02:00
|
|
|
|
|
|
|
// Make sure we clean up any managed plugins at the end of this
|
|
|
|
defer plugin.CleanupClients()
|
|
|
|
|
|
|
|
// Initialize the TFConfig settings for the commands...
|
2014-07-03 21:01:20 +02:00
|
|
|
ContextOpts.Providers = config.ProviderFactories()
|
2014-07-09 23:47:37 +02:00
|
|
|
ContextOpts.Provisioners = config.ProvisionerFactories()
|
2014-06-10 06:57:37 +02:00
|
|
|
|
2014-05-24 21:04:43 +02:00
|
|
|
// Get the command line args. We shortcut "--version" and "-v" to
|
|
|
|
// just show the version.
|
|
|
|
args := os.Args[1:]
|
|
|
|
for _, arg := range args {
|
2014-07-13 19:37:25 +02:00
|
|
|
if arg == "-v" || arg == "-version" || arg == "--version" {
|
2014-05-24 21:04:43 +02:00
|
|
|
newArgs := make([]string, len(args)+1)
|
|
|
|
newArgs[0] = "version"
|
|
|
|
copy(newArgs[1:], args)
|
|
|
|
args = newArgs
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cli := &cli.CLI{
|
2014-06-26 19:24:32 +02:00
|
|
|
Args: args,
|
|
|
|
Commands: Commands,
|
|
|
|
HelpFunc: cli.BasicHelpFunc("terraform"),
|
|
|
|
HelpWriter: os.Stdout,
|
2014-05-24 21:04:43 +02:00
|
|
|
}
|
|
|
|
|
2014-08-19 19:58:23 +02:00
|
|
|
// Load the configuration file if we have one, that can be used to
|
|
|
|
// define extra providers and provisioners.
|
2014-08-14 18:45:58 +02:00
|
|
|
clicfgFile, err := cliConfigFile()
|
|
|
|
if err != nil {
|
2014-10-11 22:03:11 +02:00
|
|
|
Ui.Error(fmt.Sprintf("Error loading CLI configuration: \n\n%s", err))
|
2014-08-14 18:45:58 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-08-19 19:58:23 +02:00
|
|
|
if clicfgFile != "" {
|
|
|
|
usrcfg, err := LoadConfig(clicfgFile)
|
|
|
|
if err != nil {
|
2014-10-11 22:03:11 +02:00
|
|
|
Ui.Error(fmt.Sprintf("Error loading CLI configuration: \n\n%s", err))
|
2014-08-19 19:58:23 +02:00
|
|
|
return 1
|
|
|
|
}
|
2014-08-14 18:45:58 +02:00
|
|
|
|
2014-08-19 19:58:23 +02:00
|
|
|
config = *config.Merge(usrcfg)
|
|
|
|
}
|
2014-08-14 18:45:58 +02:00
|
|
|
|
|
|
|
// Initialize the TFConfig settings for the commands...
|
|
|
|
ContextOpts.Providers = config.ProviderFactories()
|
|
|
|
ContextOpts.Provisioners = config.ProvisionerFactories()
|
|
|
|
|
2014-05-24 21:04:43 +02:00
|
|
|
exitCode, err := cli.Run()
|
|
|
|
if err != nil {
|
2014-10-11 22:03:11 +02:00
|
|
|
Ui.Error(fmt.Sprintf("Error executing CLI: %s", err.Error()))
|
2014-05-24 21:04:43 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return exitCode
|
|
|
|
}
|
2014-06-10 19:28:47 +02:00
|
|
|
|
2014-08-14 18:45:58 +02:00
|
|
|
func cliConfigFile() (string, error) {
|
|
|
|
mustExist := true
|
|
|
|
configFilePath := os.Getenv("TERRAFORM_CONFIG")
|
|
|
|
if configFilePath == "" {
|
|
|
|
var err error
|
2014-10-11 22:03:11 +02:00
|
|
|
configFilePath, err = ConfigFile()
|
2014-08-14 18:45:58 +02:00
|
|
|
mustExist = false
|
|
|
|
|
|
|
|
if err != nil {
|
2014-08-19 19:58:23 +02:00
|
|
|
log.Printf(
|
|
|
|
"[ERROR] Error detecting default CLI config file path: %s",
|
|
|
|
err)
|
2014-08-14 18:45:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-19 19:58:23 +02:00
|
|
|
log.Printf("[DEBUG] Attempting to open CLI config file: %s", configFilePath)
|
2014-08-14 18:45:58 +02:00
|
|
|
f, err := os.Open(configFilePath)
|
2014-08-19 19:58:23 +02:00
|
|
|
if err == nil {
|
|
|
|
f.Close()
|
|
|
|
return configFilePath, nil
|
|
|
|
}
|
2014-08-14 18:45:58 +02:00
|
|
|
|
2014-08-19 19:58:23 +02:00
|
|
|
if mustExist || !os.IsNotExist(err) {
|
|
|
|
return "", err
|
2014-08-14 18:45:58 +02:00
|
|
|
}
|
|
|
|
|
2014-08-19 19:58:23 +02:00
|
|
|
log.Println("[DEBUG] File doesn't exist, but doesn't need to. Ignoring.")
|
|
|
|
return "", nil
|
2014-08-14 18:45:58 +02:00
|
|
|
}
|
|
|
|
|
2014-06-10 19:32:59 +02:00
|
|
|
// copyOutput uses output prefixes to determine whether data on stdout
|
|
|
|
// should go to stdout or stderr. This is due to panicwrap using stderr
|
|
|
|
// as the log and error channel.
|
2014-10-03 22:02:16 +02:00
|
|
|
func copyOutput(r io.Reader, doneCh chan<- struct{}) {
|
|
|
|
defer close(doneCh)
|
|
|
|
|
2014-06-10 19:28:47 +02:00
|
|
|
pr, err := prefixedio.NewReader(r)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stderrR, err := pr.Prefix(ErrorPrefix)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
stdoutR, err := pr.Prefix(OutputPrefix)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-06-26 19:24:32 +02:00
|
|
|
defaultR, err := pr.Prefix("")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-06-10 19:28:47 +02:00
|
|
|
|
2014-10-03 22:02:16 +02:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(3)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
io.Copy(os.Stderr, stderrR)
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
io.Copy(os.Stdout, stdoutR)
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
io.Copy(os.Stdout, defaultR)
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
2014-06-10 19:28:47 +02:00
|
|
|
}
|