Skip to content

Commit

Permalink
Add init_run_reconfigure CLI config. Update stack_name_pattern (#131
Browse files Browse the repository at this point in the history
)

* Add `init_run_reconfigure`

* Add `init_run_reconfigure`

* Update `stack_name_pattern`

* Update `stack_name_pattern`

* Update `stack_name_pattern`
  • Loading branch information
aknysh authored Apr 5, 2022
1 parent e7cfb9b commit 8c0796e
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 25 deletions.
2 changes: 2 additions & 0 deletions atmos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ components:
apply_auto_approve: false
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_DEPLOY_RUN_INIT` ENV var, or `--deploy-run-init` command-line argument
deploy_run_init: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE` ENV var, or `--init-run-reconfigure` command-line argument
init_run_reconfigure: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_AUTO_GENERATE_BACKEND_FILE` ENV var, or `--auto-generate-backend-file` command-line argument
auto_generate_backend_file: false
helmfile:
Expand Down
2 changes: 1 addition & 1 deletion examples/complete/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
ARG GEODESIC_VERSION=0.152.2
ARG GEODESIC_OS=debian
# atmos: https://github.com/cloudposse/atmos
ARG ATMOS_VERSION=1.4.1
ARG ATMOS_VERSION=1.4.2
# Terraform
ARG TF_VERSION=1.1.4

Expand Down
2 changes: 2 additions & 0 deletions examples/complete/atmos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ components:
apply_auto_approve: false
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_DEPLOY_RUN_INIT` ENV var, or `--deploy-run-init` command-line argument
deploy_run_init: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE` ENV var, or `--init-run-reconfigure` command-line argument
init_run_reconfigure: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_AUTO_GENERATE_BACKEND_FILE` ENV var, or `--auto-generate-backend-file` command-line argument
auto_generate_backend_file: false
helmfile:
Expand Down
2 changes: 2 additions & 0 deletions examples/complete/rootfs/usr/local/etc/atmos/atmos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ components:
apply_auto_approve: false
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_DEPLOY_RUN_INIT` ENV var, or `--deploy-run-init` command-line argument
deploy_run_init: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE` ENV var, or `--init-run-reconfigure` command-line argument
init_run_reconfigure: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_AUTO_GENERATE_BACKEND_FILE` ENV var, or `--auto-generate-backend-file` command-line argument
auto_generate_backend_file: false
helmfile:
Expand Down
13 changes: 2 additions & 11 deletions internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func ExecuteTerraform(cmd *cobra.Command, args []string) error {
}

// Check if the component is allowed to be provisioned (`metadata.type` attribute)
if (info.SubCommand == "apply" || info.SubCommand == "deploy") && info.ComponentIsAbstract {
if (info.SubCommand == "plan" || info.SubCommand == "apply" || info.SubCommand == "deploy" || info.SubCommand == "workspace") && info.ComponentIsAbstract {
return errors.New(fmt.Sprintf("Abstract component '%s' cannot be provisioned since it's explicitly prohibited from being deployed "+
"by 'metadata.type: abstract' attribute", path.Join(info.ComponentFolderPrefix, info.Component)))
}
Expand Down Expand Up @@ -153,7 +153,7 @@ func ExecuteTerraform(cmd *cobra.Command, args []string) error {
}
if runTerraformInit == true {
initCommandWithArguments := []string{"init"}
if info.SubCommand == "workspace" {
if info.SubCommand == "workspace" || c.Config.Components.Terraform.InitRunReconfigure == true {
initCommandWithArguments = []string{"init", "-reconfigure"}
}
err = execCommand(info.Command, initCommandWithArguments, componentPath, info.ComponentEnvList)
Expand Down Expand Up @@ -302,12 +302,3 @@ func ExecuteTerraform(cmd *cobra.Command, args []string) error {

return nil
}

func checkTerraformConfig() error {
if len(c.Config.Components.Terraform.BasePath) < 1 {
return errors.New("Base path to terraform components must be provided in 'components.terraform.base_path' config or " +
"'ATMOS_COMPONENTS_TERRAFORM_BASE_PATH' ENV variable")
}

return nil
}
15 changes: 15 additions & 0 deletions internal/exec/terraform_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package exec

import (
c "github.com/cloudposse/atmos/pkg/config"
"github.com/pkg/errors"
)

func checkTerraformConfig() error {
if len(c.Config.Components.Terraform.BasePath) < 1 {
return errors.New("Base path to terraform components must be provided in 'components.terraform.base_path' config or " +
"'ATMOS_COMPONENTS_TERRAFORM_BASE_PATH' ENV variable")
}

return nil
}
15 changes: 15 additions & 0 deletions internal/exec/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
g.BasePathFlag,
g.GlobalOptionsFlag,
g.DeployRunInitFlag,
g.InitRunReconfigure,
g.AutoGenerateBackendFileFlag,
g.FromPlanFlag,
g.HelpFlag1,
Expand Down Expand Up @@ -169,6 +170,7 @@ func processArgsConfigAndStacks(componentType string, cmd *cobra.Command, args [
configAndStacksInfo.ConfigDir = argsAndFlagsInfo.ConfigDir
configAndStacksInfo.WorkflowsDir = argsAndFlagsInfo.WorkflowsDir
configAndStacksInfo.DeployRunInit = argsAndFlagsInfo.DeployRunInit
configAndStacksInfo.InitRunReconfigure = argsAndFlagsInfo.InitRunReconfigure
configAndStacksInfo.AutoGenerateBackendFile = argsAndFlagsInfo.AutoGenerateBackendFile
configAndStacksInfo.UseTerraformPlan = argsAndFlagsInfo.UseTerraformPlan
configAndStacksInfo.NeedHelp = argsAndFlagsInfo.NeedHelp
Expand Down Expand Up @@ -549,6 +551,19 @@ func processArgsAndFlags(inputArgsAndFlags []string) (c.ArgsAndFlagsInfo, error)
info.WorkflowsDir = workflowDirFlagParts[1]
}

if arg == g.InitRunReconfigure {
if len(inputArgsAndFlags) <= (i + 1) {
return info, errors.New(fmt.Sprintf("invalid flag: %s", arg))
}
info.InitRunReconfigure = inputArgsAndFlags[i+1]
} else if strings.HasPrefix(arg+"=", g.InitRunReconfigure) {
var initRunReconfigureParts = strings.Split(arg, "=")
if len(initRunReconfigureParts) != 2 {
return info, errors.New(fmt.Sprintf("invalid flag: %s", arg))
}
info.InitRunReconfigure = initRunReconfigureParts[1]
}

if arg == g.FromPlanFlag {
info.UseTerraformPlan = true
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/component/atmos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ components:
apply_auto_approve: false
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_DEPLOY_RUN_INIT` ENV var, or `--deploy-run-init` command-line argument
deploy_run_init: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE` ENV var, or `--init-run-reconfigure` command-line argument
init_run_reconfigure: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_AUTO_GENERATE_BACKEND_FILE` ENV var, or `--auto-generate-backend-file` command-line argument
auto_generate_backend_file: false
helmfile:
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
BasePath: "components/terraform",
ApplyAutoApprove: false,
DeployRunInit: true,
InitRunReconfigure: true,
AutoGenerateBackendFile: false,
},
Helmfile: Helmfile{
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type Terraform struct {
BasePath string `yaml:"base_path" json:"base_path" mapstructure:"base_path"`
ApplyAutoApprove bool `yaml:"apply_auto_approve" json:"apply_auto_approve" mapstructure:"apply_auto_approve"`
DeployRunInit bool `yaml:"deploy_run_init" json:"deploy_run_init" mapstructure:"deploy_run_init"`
InitRunReconfigure bool `yaml:"init_run_reconfigure" json:"init_run_reconfigure" mapstructure:"init_run_reconfigure"`
AutoGenerateBackendFile bool `yaml:"auto_generate_backend_file" json:"auto_generate_backend_file" mapstructure:"auto_generate_backend_file"`
}

Expand Down Expand Up @@ -75,6 +76,7 @@ type ArgsAndFlagsInfo struct {
WorkflowsDir string
BasePath string
DeployRunInit string
InitRunReconfigure string
AutoGenerateBackendFile string
UseTerraformPlan bool
NeedHelp bool
Expand Down Expand Up @@ -109,6 +111,7 @@ type ConfigAndStacksInfo struct {
Context Context
ContextPrefix string
DeployRunInit string
InitRunReconfigure string
AutoGenerateBackendFile string
UseTerraformPlan bool
ComponentInheritanceChain []string
Expand Down
33 changes: 32 additions & 1 deletion pkg/config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ func processEnvVars() error {
Config.Components.Terraform.DeployRunInit = deployRunInitBool
}

componentsInitRunReconfigure := os.Getenv("ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE")
if len(componentsInitRunReconfigure) > 0 {
color.Cyan("Found ENV var ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE=%s", componentsInitRunReconfigure)
initRunReconfigureBool, err := strconv.ParseBool(componentsInitRunReconfigure)
if err != nil {
return err
}
Config.Components.Terraform.InitRunReconfigure = initRunReconfigureBool
}

componentsTerraformAutoGenerateBackendFile := os.Getenv("ATMOS_COMPONENTS_TERRAFORM_AUTO_GENERATE_BACKEND_FILE")
if len(componentsTerraformAutoGenerateBackendFile) > 0 {
color.Cyan("Found ENV var ATMOS_COMPONENTS_TERRAFORM_AUTO_GENERATE_BACKEND_FILE=%s", componentsTerraformAutoGenerateBackendFile)
Expand Down Expand Up @@ -297,6 +307,14 @@ func processCommandLineArgs(configAndStacksInfo ConfigAndStacksInfo) error {
Config.Workflows.BasePath = configAndStacksInfo.WorkflowsDir
color.Cyan(fmt.Sprintf("Using command line argument '%s' as workflows directory", configAndStacksInfo.WorkflowsDir))
}
if len(configAndStacksInfo.InitRunReconfigure) > 0 {
initRunReconfigureBool, err := strconv.ParseBool(configAndStacksInfo.InitRunReconfigure)
if err != nil {
return err
}
Config.Components.Terraform.InitRunReconfigure = initRunReconfigureBool
color.Cyan(fmt.Sprintf("Using command line argument '%s=%s'", g.InitRunReconfigure, configAndStacksInfo.InitRunReconfigure))
}
return nil
}

Expand Down Expand Up @@ -352,7 +370,20 @@ func GetContextPrefix(stack string, context Context, stackNamePattern string) (s
stackNamePatternParts := strings.Split(stackNamePattern, "-")

for _, part := range stackNamePatternParts {
if part == "{tenant}" {
if part == "{namespace}" {
if len(context.Namespace) == 0 {
return "",
errors.New(fmt.Sprintf("The stack name pattern '%s' specifies 'namespace`, but the stack %s does not have a namespace defined",
stackNamePattern,
stack,
))
}
if len(contextPrefix) == 0 {
contextPrefix = context.Namespace
} else {
contextPrefix = contextPrefix + "-" + context.Namespace
}
} else if part == "{tenant}" {
if len(context.Tenant) == 0 {
return "",
errors.New(fmt.Sprintf("The stack name pattern '%s' specifies 'tenant`, but the stack %s does not have a tenant defined",
Expand Down
1 change: 1 addition & 0 deletions pkg/globals/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (

DeployRunInitFlag = "--deploy-run-init"
AutoGenerateBackendFileFlag = "--auto-generate-backend-file"
InitRunReconfigure = "--init-run-reconfigure"

FromPlanFlag = "--from-plan"

Expand Down
2 changes: 2 additions & 0 deletions pkg/spacelift/atmos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ components:
apply_auto_approve: false
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_DEPLOY_RUN_INIT` ENV var, or `--deploy-run-init` command-line argument
deploy_run_init: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE` ENV var, or `--init-run-reconfigure` command-line argument
init_run_reconfigure: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_AUTO_GENERATE_BACKEND_FILE` ENV var, or `--auto-generate-backend-file` command-line argument
auto_generate_backend_file: false
helmfile:
Expand Down
24 changes: 12 additions & 12 deletions pkg/spacelift/spacelift_stack_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"strings"
)

const defaultSpaceliftStackNamePattern = "{tenant}-{environment}-{stage}-{component}"

// CreateSpaceliftStacks takes a list of paths to YAML config files, processes and deep-merges all imports,
// and returns a map of Spacelift stack configs
func CreateSpaceliftStacks(
Expand Down Expand Up @@ -248,7 +246,7 @@ func TransformStackConfigToSpaceliftStacks(
res := map[string]interface{}{}
var allStackNames []string

for _, stackConfig := range stacks {
for stackName, stackConfig := range stacks {
config := stackConfig.(map[interface{}]interface{})

if i, ok := config["components"]; ok {
Expand Down Expand Up @@ -277,8 +275,13 @@ func TransformStackConfigToSpaceliftStacks(

// Spacelift stack name
context := c.GetContextFromVars(componentVars)
contextPrefix, err := c.GetContextPrefix(stackName, context, stackNamePattern)
if err != nil {
return nil, err
}

context.Component = component
spaceliftStackName, _ := buildSpaceliftStackName(spaceliftSettings, context)
spaceliftStackName, _ := buildSpaceliftStackName(spaceliftSettings, context, contextPrefix)
allStackNames = append(allStackNames, strings.Replace(spaceliftStackName, "/", "-", -1))
}
}
Expand Down Expand Up @@ -458,7 +461,7 @@ func TransformStackConfigToSpaceliftStacks(

// Spacelift stack name
context.Component = component
spaceliftStackName, spaceliftStackNamePattern := buildSpaceliftStackName(spaceliftSettings, context)
spaceliftStackName, spaceliftStackNamePattern := buildSpaceliftStackName(spaceliftSettings, context, contextPrefix)

// Add Spacelift stack config to the final map
spaceliftStackNameKey := strings.Replace(spaceliftStackName, "/", "-", -1)
Expand Down Expand Up @@ -511,14 +514,11 @@ func buildSpaceliftDependsOnStackName(
}

// buildSpaceliftStackName build a Spacelift stack name from the provided context and state name pattern
func buildSpaceliftStackName(spaceliftSettings map[interface{}]interface{}, context c.Context) (string, string) {
var finalSpaceliftStackNamePattern string

func buildSpaceliftStackName(spaceliftSettings map[interface{}]interface{}, context c.Context, contextPrefix string) (string, string) {
if spaceliftStackNamePattern, ok := spaceliftSettings["stack_name_pattern"].(string); ok {
finalSpaceliftStackNamePattern = spaceliftStackNamePattern
return c.ReplaceContextTokens(context, spaceliftStackNamePattern), spaceliftStackNamePattern
} else {
finalSpaceliftStackNamePattern = defaultSpaceliftStackNamePattern
defaultSpaceliftStackNamePattern := fmt.Sprintf("%s-%s", contextPrefix, context.Component)
return strings.Replace(defaultSpaceliftStackNamePattern, "/", "-", -1), contextPrefix
}

return c.ReplaceContextTokens(context, finalSpaceliftStackNamePattern), finalSpaceliftStackNamePattern
}

0 comments on commit 8c0796e

Please sign in to comment.