github.com/lukahartwig/terraform@v0.11.4-0.20180302171601-664391c254ea/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"io/ioutil"
     7  	"log"
     8  	"os"
     9  	"path/filepath"
    10  	"runtime"
    11  	"strings"
    12  	"sync"
    13  
    14  	"github.com/mitchellh/colorstring"
    15  
    16  	"github.com/hashicorp/go-plugin"
    17  	"github.com/hashicorp/terraform/command/format"
    18  	"github.com/hashicorp/terraform/helper/logging"
    19  	"github.com/hashicorp/terraform/terraform"
    20  	"github.com/mattn/go-colorable"
    21  	"github.com/mattn/go-shellwords"
    22  	"github.com/mitchellh/cli"
    23  	"github.com/mitchellh/panicwrap"
    24  	"github.com/mitchellh/prefixedio"
    25  )
    26  
    27  const (
    28  	// EnvCLI is the environment variable name to set additional CLI args.
    29  	EnvCLI = "TF_CLI_ARGS"
    30  )
    31  
    32  func main() {
    33  	// Override global prefix set by go-dynect during init()
    34  	log.SetPrefix("")
    35  	os.Exit(realMain())
    36  }
    37  
    38  func realMain() int {
    39  	var wrapConfig panicwrap.WrapConfig
    40  
    41  	// don't re-exec terraform as a child process for easier debugging
    42  	if os.Getenv("TF_FORK") == "0" {
    43  		return wrappedMain()
    44  	}
    45  
    46  	if !panicwrap.Wrapped(&wrapConfig) {
    47  		// Determine where logs should go in general (requested by the user)
    48  		logWriter, err := logging.LogOutput()
    49  		if err != nil {
    50  			fmt.Fprintf(os.Stderr, "Couldn't setup log output: %s", err)
    51  			return 1
    52  		}
    53  
    54  		// We always send logs to a temporary file that we use in case
    55  		// there is a panic. Otherwise, we delete it.
    56  		logTempFile, err := ioutil.TempFile("", "terraform-log")
    57  		if err != nil {
    58  			fmt.Fprintf(os.Stderr, "Couldn't setup logging tempfile: %s", err)
    59  			return 1
    60  		}
    61  		defer os.Remove(logTempFile.Name())
    62  		defer logTempFile.Close()
    63  
    64  		// Setup the prefixed readers that send data properly to
    65  		// stdout/stderr.
    66  		doneCh := make(chan struct{})
    67  		outR, outW := io.Pipe()
    68  		go copyOutput(outR, doneCh)
    69  
    70  		// Create the configuration for panicwrap and wrap our executable
    71  		wrapConfig.Handler = panicHandler(logTempFile)
    72  		wrapConfig.Writer = io.MultiWriter(logTempFile, logWriter)
    73  		wrapConfig.Stdout = outW
    74  		wrapConfig.IgnoreSignals = ignoreSignals
    75  		wrapConfig.ForwardSignals = forwardSignals
    76  		exitStatus, err := panicwrap.Wrap(&wrapConfig)
    77  		if err != nil {
    78  			fmt.Fprintf(os.Stderr, "Couldn't start Terraform: %s", err)
    79  			return 1
    80  		}
    81  
    82  		// If >= 0, we're the parent, so just exit
    83  		if exitStatus >= 0 {
    84  			// Close the stdout writer so that our copy process can finish
    85  			outW.Close()
    86  
    87  			// Wait for the output copying to finish
    88  			<-doneCh
    89  
    90  			return exitStatus
    91  		}
    92  
    93  		// We're the child, so just close the tempfile we made in order to
    94  		// save file handles since the tempfile is only used by the parent.
    95  		logTempFile.Close()
    96  	}
    97  
    98  	// Call the real main
    99  	return wrappedMain()
   100  }
   101  
   102  func init() {
   103  	Ui = &cli.PrefixedUi{
   104  		AskPrefix:    OutputPrefix,
   105  		OutputPrefix: OutputPrefix,
   106  		InfoPrefix:   OutputPrefix,
   107  		ErrorPrefix:  ErrorPrefix,
   108  		Ui:           &cli.BasicUi{Writer: os.Stdout},
   109  	}
   110  }
   111  
   112  func wrappedMain() int {
   113  	var err error
   114  
   115  	// We always need to close the DebugInfo before we exit.
   116  	defer terraform.CloseDebugInfo()
   117  
   118  	log.SetOutput(os.Stderr)
   119  	log.Printf(
   120  		"[INFO] Terraform version: %s %s %s",
   121  		Version, VersionPrerelease, GitCommit)
   122  	log.Printf("[INFO] Go runtime version: %s", runtime.Version())
   123  	log.Printf("[INFO] CLI args: %#v", os.Args)
   124  
   125  	config, diags := LoadConfig()
   126  	if len(diags) > 0 {
   127  		// Since we haven't instantiated a command.Meta yet, we need to do
   128  		// some things manually here and use some "safe" defaults for things
   129  		// that command.Meta could otherwise figure out in smarter ways.
   130  		Ui.Error("There are some problems with the CLI configuration:")
   131  		for _, diag := range diags {
   132  			earlyColor := &colorstring.Colorize{
   133  				Colors:  colorstring.DefaultColors,
   134  				Disable: true, // Disable color to be conservative until we know better
   135  				Reset:   true,
   136  			}
   137  			Ui.Error(format.Diagnostic(diag, earlyColor, 78))
   138  		}
   139  		if diags.HasErrors() {
   140  			Ui.Error("As a result of the above problems, Terraform may not behave as intended.\n\n")
   141  			// We continue to run anyway, since Terraform has reasonable defaults.
   142  		}
   143  	}
   144  
   145  	// In tests, Commands may already be set to provide mock commands
   146  	if Commands == nil {
   147  		initCommands(config)
   148  	}
   149  
   150  	// Run checkpoint
   151  	go runCheckpoint(config)
   152  
   153  	// Make sure we clean up any managed plugins at the end of this
   154  	defer plugin.CleanupClients()
   155  
   156  	// Get the command line args.
   157  	binName := filepath.Base(os.Args[0])
   158  	args := os.Args[1:]
   159  
   160  	// Build the CLI so far, we do this so we can query the subcommand.
   161  	cliRunner := &cli.CLI{
   162  		Args:       args,
   163  		Commands:   Commands,
   164  		HelpFunc:   helpFunc,
   165  		HelpWriter: os.Stdout,
   166  	}
   167  
   168  	// Prefix the args with any args from the EnvCLI
   169  	args, err = mergeEnvArgs(EnvCLI, cliRunner.Subcommand(), args)
   170  	if err != nil {
   171  		Ui.Error(err.Error())
   172  		return 1
   173  	}
   174  
   175  	// Prefix the args with any args from the EnvCLI targeting this command
   176  	suffix := strings.Replace(strings.Replace(
   177  		cliRunner.Subcommand(), "-", "_", -1), " ", "_", -1)
   178  	args, err = mergeEnvArgs(
   179  		fmt.Sprintf("%s_%s", EnvCLI, suffix), cliRunner.Subcommand(), args)
   180  	if err != nil {
   181  		Ui.Error(err.Error())
   182  		return 1
   183  	}
   184  
   185  	// We shortcut "--version" and "-v" to just show the version
   186  	for _, arg := range args {
   187  		if arg == "-v" || arg == "-version" || arg == "--version" {
   188  			newArgs := make([]string, len(args)+1)
   189  			newArgs[0] = "version"
   190  			copy(newArgs[1:], args)
   191  			args = newArgs
   192  			break
   193  		}
   194  	}
   195  
   196  	// Rebuild the CLI with any modified args.
   197  	log.Printf("[INFO] CLI command args: %#v", args)
   198  	cliRunner = &cli.CLI{
   199  		Name:       binName,
   200  		Args:       args,
   201  		Commands:   Commands,
   202  		HelpFunc:   helpFunc,
   203  		HelpWriter: os.Stdout,
   204  
   205  		Autocomplete:          true,
   206  		AutocompleteInstall:   "install-autocomplete",
   207  		AutocompleteUninstall: "uninstall-autocomplete",
   208  	}
   209  
   210  	// Pass in the overriding plugin paths from config
   211  	PluginOverrides.Providers = config.Providers
   212  	PluginOverrides.Provisioners = config.Provisioners
   213  
   214  	exitCode, err := cliRunner.Run()
   215  	if err != nil {
   216  		Ui.Error(fmt.Sprintf("Error executing CLI: %s", err.Error()))
   217  		return 1
   218  	}
   219  
   220  	return exitCode
   221  }
   222  
   223  func cliConfigFile() (string, error) {
   224  	mustExist := true
   225  	configFilePath := os.Getenv("TERRAFORM_CONFIG")
   226  	if configFilePath == "" {
   227  		var err error
   228  		configFilePath, err = ConfigFile()
   229  		mustExist = false
   230  
   231  		if err != nil {
   232  			log.Printf(
   233  				"[ERROR] Error detecting default CLI config file path: %s",
   234  				err)
   235  		}
   236  	}
   237  
   238  	log.Printf("[DEBUG] Attempting to open CLI config file: %s", configFilePath)
   239  	f, err := os.Open(configFilePath)
   240  	if err == nil {
   241  		f.Close()
   242  		return configFilePath, nil
   243  	}
   244  
   245  	if mustExist || !os.IsNotExist(err) {
   246  		return "", err
   247  	}
   248  
   249  	log.Println("[DEBUG] File doesn't exist, but doesn't need to. Ignoring.")
   250  	return "", nil
   251  }
   252  
   253  // copyOutput uses output prefixes to determine whether data on stdout
   254  // should go to stdout or stderr. This is due to panicwrap using stderr
   255  // as the log and error channel.
   256  func copyOutput(r io.Reader, doneCh chan<- struct{}) {
   257  	defer close(doneCh)
   258  
   259  	pr, err := prefixedio.NewReader(r)
   260  	if err != nil {
   261  		panic(err)
   262  	}
   263  
   264  	stderrR, err := pr.Prefix(ErrorPrefix)
   265  	if err != nil {
   266  		panic(err)
   267  	}
   268  	stdoutR, err := pr.Prefix(OutputPrefix)
   269  	if err != nil {
   270  		panic(err)
   271  	}
   272  	defaultR, err := pr.Prefix("")
   273  	if err != nil {
   274  		panic(err)
   275  	}
   276  
   277  	var stdout io.Writer = os.Stdout
   278  	var stderr io.Writer = os.Stderr
   279  
   280  	if runtime.GOOS == "windows" {
   281  		stdout = colorable.NewColorableStdout()
   282  		stderr = colorable.NewColorableStderr()
   283  
   284  		// colorable is not concurrency-safe when stdout and stderr are the
   285  		// same console, so we need to add some synchronization to ensure that
   286  		// we can't be concurrently writing to both stderr and stdout at
   287  		// once, or else we get intermingled writes that create gibberish
   288  		// in the console.
   289  		wrapped := synchronizedWriters(stdout, stderr)
   290  		stdout = wrapped[0]
   291  		stderr = wrapped[1]
   292  	}
   293  
   294  	var wg sync.WaitGroup
   295  	wg.Add(3)
   296  	go func() {
   297  		defer wg.Done()
   298  		io.Copy(stderr, stderrR)
   299  	}()
   300  	go func() {
   301  		defer wg.Done()
   302  		io.Copy(stdout, stdoutR)
   303  	}()
   304  	go func() {
   305  		defer wg.Done()
   306  		io.Copy(stdout, defaultR)
   307  	}()
   308  
   309  	wg.Wait()
   310  }
   311  
   312  func mergeEnvArgs(envName string, cmd string, args []string) ([]string, error) {
   313  	v := os.Getenv(envName)
   314  	if v == "" {
   315  		return args, nil
   316  	}
   317  
   318  	log.Printf("[INFO] %s value: %q", envName, v)
   319  	extra, err := shellwords.Parse(v)
   320  	if err != nil {
   321  		return nil, fmt.Errorf(
   322  			"Error parsing extra CLI args from %s: %s",
   323  			envName, err)
   324  	}
   325  
   326  	// Find the command to look for in the args. If there is a space,
   327  	// we need to find the last part.
   328  	search := cmd
   329  	if idx := strings.LastIndex(search, " "); idx >= 0 {
   330  		search = cmd[idx+1:]
   331  	}
   332  
   333  	// Find the index to place the flags. We put them exactly
   334  	// after the first non-flag arg.
   335  	idx := -1
   336  	for i, v := range args {
   337  		if v == search {
   338  			idx = i
   339  			break
   340  		}
   341  	}
   342  
   343  	// idx points to the exact arg that isn't a flag. We increment
   344  	// by one so that all the copying below expects idx to be the
   345  	// insertion point.
   346  	idx++
   347  
   348  	// Copy the args
   349  	newArgs := make([]string, len(args)+len(extra))
   350  	copy(newArgs, args[:idx])
   351  	copy(newArgs[idx:], extra)
   352  	copy(newArgs[len(extra)+idx:], args[idx:])
   353  	return newArgs, nil
   354  }