Пример #1
0
func initialize(c *cli.Context, terraformCommand string) TerraformOperation {
	if len(c.Args()) < 1 {
		fmt.Printf("Incorrect usage\n")
		fmt.Printf("%s <environment>\n", terraformCommand)
		os.Exit(1)
	}

	fmt.Println(c.Args())
	environment := c.Args()[0]

	security.Apply(c.String("security"), c)

	fmt.Println()
	fmt.Println("Execute Terraform command")
	fmt.Println("Command:    ", command.Bold(terraformCommand))
	fmt.Println("Environment:", command.Bold(environment))
	fmt.Println()

	configLocation := c.String("config-location")
	config := terraform_config.LoadConfig(configLocation, environment)

	getState(c.Bool("no-sync"), config, environment)

	return TerraformOperation{
		command:     terraformCommand,
		environment: environment,
		config:      config,
		args:        os.Args[2:],
	}
}
Пример #2
0
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)
	}
}
Пример #3
0
func CmdDownload(c *cli.Context) {
	if len(c.Args()) != 1 {
		fmt.Printf("Incorrect usage\n")
		fmt.Printf("download <environment>\n")
		return
	}
	environment := c.Args()[0]
	fmt.Println()
	fmt.Println("Download Terraform state")
	fmt.Println("Environment:", command.Bold(environment))
	fmt.Println()
	config := terraform_config.LoadConfig(c.String("config-location"), environment)
	DownloadState(config, environment)
}
Пример #4
0
func CmdUpload(c *cli.Context) {
	if len(c.Args()) != 1 {
		fmt.Printf("Incorrect usage\n")
		fmt.Printf("upload <environment>\n")
		return
	}
	fmt.Println()
	environment := c.Args()[0]
	config := terraform_config.LoadConfig(c.String("config-location"), environment)

	fmt.Println()
	fmt.Println("Upload Terraform state")
	fmt.Println("Environment:", command.Bold(environment))
	fmt.Println()
	fmt.Printf("Upload current project state to s3\n")

	if command.InputAreYouSure() {
		UploadState(config, environment)
	} else {
		fmt.Println(command.Red("Aborted"))
	}
}