infra

package
v0.2.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AWSAccessKeyID string
	AWSSecretKey   string
	AWSRegion      string
)
View Source
var Cmd = &cobra.Command{
	Version: "v0.0.1",
	Use:     "infra",
	Long:    `The 'infra' command automate and perform several infra-related actions using either Terraform or Terragrunt.`,
	Example: `
  stiletto infra terragrunt --plan --target-module=module1`,
	Run: func(cmd *cobra.Command, args []string) {
		_ = cmd.Help()
	},
}
View Source
var TgCmd = &cobra.Command{
	Version: "v0.0.1",
	Use:     "terragrunt",
	Long:    `The 'terragrunt' command automate and perform several infra-related actions using Terragrunt.`,
	Example: `
	  stiletto infra terragrunt --plan --target-module=module1
      # Also, the task can be set instead of the flag:
      stiletto infra terragrunt --task=plan --target-module=module1
`,
	Args: func(cmd *cobra.Command, args []string) error {
		// Fail-fast for inconsistent flags
		var tempTgCustomCommands []string
		var argErr error

		if viper.GetStringSlice("tg-commands") != nil {
			tempTgCustomCommands = viper.GetStringSlice("tg-commands")
		}

		tempTaskName := viper.GetString("task")

		if len(tempTgCustomCommands) > 0 && tempTaskName != "" {
			msg.ShowError("", "The task name and custom commands cannot be set at the same time",
				nil)
			argErr = fmt.Errorf("the task name and custom commands cannot be set at the same time")
		}

		if !tgApply && !tgPlan && !tgDestroy && !tgPlanAll && !tgApplyAll && !tgDestroyAll && len(tempTgCustomCommands) == 0 && tempTaskName == "" {
			msg.ShowError("", "No task was set. Please set a task to run.", nil)
			argErr = fmt.Errorf("no task was set. Please set a task to run")
		}

		if tgPlan && tgApply && tgDestroy && tgPlanAll && tgApplyAll && tgDestroyAll {
			msg.ShowError("", "Only one task can be set at a time", nil)
			argErr = fmt.Errorf("only one task can be set at a time")
		}

		if (tgPlan || tgApply || tgDestroy || tgPlanAll || tgApplyAll || tgDestroyAll) && tempTaskName != "" {
			msg.ShowError("", "The task name and task flags cannot be set at the same time", nil)
			argErr = fmt.Errorf("the task name and task flags cannot be set at the same time")
		}

		if tempTaskName == "" {
			if tgPlan {
				viper.Set("task", "plan")
				viper.Set("tg-commands", []string{"plan"})
			}

			if tgApply {
				viper.Set("task", "apply")
				viper.Set("tg-commands", []string{"apply"})
			}

			if tgDestroy {
				viper.Set("task", "destroy")
				viper.Set("tg-commands", []string{"destroy"})
			}

			if tgPlanAll {
				viper.Set("task", "plan-all")
				viper.Set("tg-commands", []string{"run-all, plan"})
			}

			if tgApplyAll {
				viper.Set("task", "apply-all")
				viper.Set("tg-commands", []string{"run-all, apply"})
			}

			if tgDestroyAll {
				viper.Set("task", "destroy-all")
				viper.Set("tg-commands", []string{"run-all, destroy"})
			}
		} else {
			viper.Set("task", tempTaskName)

			if tempTaskName == "plan-all" || tempTaskName == "apply-all" || tempTaskName == "destroy-all" {
				viper.Set("tg-commands", []string{"run-all", tempTaskName})
			} else {
				viper.Set("tg-commands", []string{tempTaskName})
			}
		}

		if len(tempTgCustomCommands) > 0 {
			viper.Set("tg-commands", append(viper.GetStringSlice("tg-commands"), tempTgCustomCommands...))
		}

		return argErr
	},
	Run: func(cmd *cobra.Command, args []string) {
		stackName := "INFRA:TERRAGRUNT"
		jobName := "IAC"

		cliGlobalArgs, err := config.GetCLIGlobalArgs()

		if err != nil {
			panic(err)
		}

		p, j, err := api.New(&cliGlobalArgs, stackName, jobName)
		if err != nil {
			panic(err)
		}

		ux.ShowSubTitle("TASK:", cliGlobalArgs.TaskName)
		ux.ShowTaskDetails(jobName, cliGlobalArgs.TaskName, j.WorkDirPath,
			j.TargetDirPath,
			j.MountDirPath)

		err = task.RunTaskInfraTerraGrunt(task.InitOptions{
			Task:           cliGlobalArgs.TaskName,
			Stack:          stackName,
			PipelineCfg:    p,
			JobCfg:         j,
			WorkDir:        p.PipelineOpts.WorkDir,
			MountDir:       p.PipelineOpts.MountDir,
			TargetDir:      p.PipelineOpts.TargetDir,
			ActionCommands: cliGlobalArgs.CustomCommands,
		})

		if err != nil {
			msg.ShowError("", fmt.Sprintf("Failed to run task '%s' as part of job %s on stack '%s'",
				cliGlobalArgs.TaskName, jobName, stackName), err)
			os.Exit(1)
		}

	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL