func CmdRun(c *cli.Context) { if len(c.Args()) != 2 { fmt.Printf("Incorrect usage\n") fmt.Printf("run <terraform command> <environment>\n") os.Exit(1) } IsTerraformProject() terraformCommand := c.Args()[0] environment := c.Args()[1] security.Apply(c.String("security"), c) IsSupportedTerraformCommand(terraformCommand) fmt.Println() fmt.Println("Run Terraform command") fmt.Println("Command: ", bold(terraformCommand)) fmt.Println("Environment:", bold(environment)) fmt.Println() configLocation := c.String("config-location") config := terraform_config.LoadConfig(c.String("config-location"), environment) if c.Bool("no-sync") { red("SKIPPING S3 download of current state") if command.InputAffirmative() { red("Skipped syncing with S3") } else { DownloadState(config, environment) } } else { DownloadState(config, environment) } tfVars := terraform_config.TerraformVars(configLocation, environment) tfState := terraform_config.TerraformState(environment) terraformActions := terraformCommands[terraformCommand] // It would be great to use golang terraform so we don't have to install it separately // I think we would need to use "github.com/mitchellh/cli" instead of current cli cmdName := "terraform" cmdArgs := []string{terraformCommand, "-var-file", tfVars, fmt.Sprintf("-state=%s", tfState), "-var", fmt.Sprintf("environment=%s", environment)} if terraformActions.extraArgs != "" { cmdArgs = append(cmdArgs, terraformActions.extraArgs) } fmt.Println("---------------------------------------------") Bold.Println(cmdName, strings.Join(cmdArgs, " ")) fmt.Println("---------------------------------------------") fmt.Println() command.Default().Execute(cmdName, cmdArgs) fmt.Println() fmt.Println("---------------------------------------------") if terraformActions.sync { fmt.Printf("S3 SYNC new changes\n") UploadState(config, environment) } }
func UploadState(config *terraform_config.AwsConfig, environment string) { tfState := terraform_config.TerraformState(environment) s3Key := S3Key(config.S3_key, environment) fmt.Printf("Uploading project state: %s to: %s/%s\n", green(tfState), green(config.S3_bucket), green(s3Key)) err := sync.Upload(config.Aws_region, config.S3_bucket, s3Key, tfState) fmt.Println() if err != nil { command.Error("Failed to upload", err) } else { color.Green("Uploaded successfully to S3") fmt.Println() } }
func DownloadState(config *terraform_config.AwsConfig, environment string) { fmt.Println("Syncing project state with S3") tfState := terraform_config.TerraformState(environment) s3Key := S3Key(config.S3_key, environment) fmt.Printf("Downloading project state: %s/%s to: %s\n", cyan(config.S3_bucket), cyan(s3Key), cyan(tfState)) err := sync.Download(config.Aws_region, config.S3_bucket, s3Key, tfState) fmt.Println() if err != nil { command.Warn("Failed to download", err) } else { color.Green("Downloaded successfully from S3") fmt.Println() } }