Skip to content

Commit

Permalink
Detect ENV vars in YAML stack config and set them for command executi…
Browse files Browse the repository at this point in the history
…on. Make `workspace_key_prefix` config DRY (#77)

* Detect ENV vars in YAML stack config and set them for command execution

* Detect ENV vars in YAML stack config and set them for command execution

* Detect ENV vars in YAML stack config and set them for command execution

* Make `workspace_key_prefix` config DRY

* Make `workspace_key_prefix` config DRY

* Make `workspace_key_prefix` config DRY

* Make `workspace_key_prefix` config DRY

* Make `workspace_key_prefix` config DRY
  • Loading branch information
aknysh authored Nov 12, 2021
1 parent 51143a9 commit 84e9fd0
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ components:
component: "test/test-component"
# Other variables can be overridden here
vars: {}
env:
TEST_ENV_VAR1: "val1-override"
TEST_ENV_VAR3: "val3-override"
TEST_ENV_VAR4: "val4"
# Override remote state backend for this component
remote_state_backend_type: static # s3, remote, vault, static, etc.
remote_state_backend:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import:
components:
terraform:
"test/test-component":
backend:
s3:
workspace_key_prefix: test-test-component
settings:
spacelift:
workspace_enabled: true
vars:
enabled: true
env:
TEST_ENV_VAR1: "val1"
TEST_ENV_VAR2: "val2"
TEST_ENV_VAR3: "val3"
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import:
components:
terraform:
top-level-component1:
backend:
s3:
workspace_key_prefix: top-level-component1
settings:
spacelift:
workspace_enabled: true
Expand Down
11 changes: 6 additions & 5 deletions internal/exec/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ func ExecuteDescribeComponent(cmd *cobra.Command, args []string) error {
if c.ProcessedConfig.StackType == "Directory" {
componentSection,
componentVarsSection,
_, _, _, _,
_, _, _, _, _,
err = findComponentConfig(stack, stacksMap, "terraform", component)
if err != nil {
componentSection,
componentVarsSection,
_, _, _, _,
_, _, _, _, _,
err = findComponentConfig(stack, stacksMap, "helmfile", component)
if err != nil {
return err
Expand Down Expand Up @@ -116,12 +116,12 @@ func ExecuteDescribeComponent(cmd *cobra.Command, args []string) error {
for stackName := range stacksMap {
componentSection,
componentVarsSection,
_, _, _, _,
_, _, _, _, _,
err = findComponentConfig(stackName, stacksMap, "terraform", component)
if err != nil {
componentSection,
componentVarsSection,
_, _, _, _,
_, _, _, _, _,
err = findComponentConfig(stackName, stacksMap, "helmfile", component)
if err != nil {
continue
Expand Down Expand Up @@ -174,7 +174,8 @@ func ExecuteDescribeComponent(cmd *cobra.Command, args []string) error {
}

if g.LogVerbose {
color.Cyan("\nComponent config:\n\n")
fmt.Println()
color.Cyan("Component config:\n\n")
}

err = u.PrintAsYAML(componentSection)
Expand Down
11 changes: 8 additions & 3 deletions internal/exec/helmfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ func ExecuteHelmfile(cmd *cobra.Command, args []string) error {

var workingDir string
if len(info.ComponentFolderPrefix) == 0 {
workingDir = fmt.Sprintf("%s/%s", c.Config.Components.Helmfile.BasePath, info.Component)
workingDir = path.Join(c.Config.Components.Helmfile.BasePath, info.Component)
} else {
workingDir = fmt.Sprintf("%s/%s/%s", c.Config.Components.Helmfile.BasePath, info.ComponentFolderPrefix, info.Component)
workingDir = path.Join(c.Config.Components.Helmfile.BasePath, info.ComponentFolderPrefix, info.Component)
}
fmt.Println(fmt.Sprintf("Working dir: %s\n\n", workingDir))

Expand All @@ -135,7 +135,7 @@ func ExecuteHelmfile(cmd *cobra.Command, args []string) error {
allArgsAndFlags = append(allArgsAndFlags, info.AdditionalArgsAndFlags...)

// Prepare ENV vars
envVars := []string{
envVars := append(info.ComponentEnvList, []string{
fmt.Sprintf("AWS_PROFILE=%s", helmAwsProfile),
fmt.Sprintf("KUBECONFIG=%s", kubeconfigPath),
fmt.Sprintf("NAMESPACE=%s", context.Namespace),
Expand All @@ -144,6 +144,11 @@ func ExecuteHelmfile(cmd *cobra.Command, args []string) error {
fmt.Sprintf("STAGE=%s", context.Stage),
fmt.Sprintf("REGION=%s", context.Region),
fmt.Sprintf("STACK=%s", info.Stack),
}...)

color.Cyan("Using ENV vars:\n")
for _, v := range envVars {
fmt.Println(v)
}

err = execCommand(info.Command, allArgsAndFlags, componentPath, envVars)
Expand Down
34 changes: 20 additions & 14 deletions internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,21 @@ func ExecuteTerraform(cmd *cobra.Command, args []string) error {
}

// Auto generate backend file
var backendFileName string
if c.Config.Components.Terraform.AutoGenerateBackendFile == true {
var backendFileName string
fmt.Println()
if len(info.ComponentFolderPrefix) == 0 {
backendFileName = fmt.Sprintf("%s/%s/backend.tf.json",
backendFileName = path.Join(
c.Config.Components.Terraform.BasePath,
finalComponent,
"backend.tf.json",
)
} else {
backendFileName = fmt.Sprintf("%s/%s/%s/backend.tf.json",
backendFileName = path.Join(
c.Config.Components.Terraform.BasePath,
info.ComponentFolderPrefix,
finalComponent,
"backend.tf.json",
)
}
color.Cyan("Writing backend config to file:")
Expand All @@ -167,7 +169,7 @@ func ExecuteTerraform(cmd *cobra.Command, args []string) error {
if info.SubCommand == "workspace" {
initCommandWithArguments = []string{"init", "-reconfigure"}
}
err = execCommand(info.Command, initCommandWithArguments, componentPath, nil)
err = execCommand(info.Command, initCommandWithArguments, componentPath, info.ComponentEnvList)
if err != nil {
return err
}
Expand Down Expand Up @@ -201,12 +203,21 @@ func ExecuteTerraform(cmd *cobra.Command, args []string) error {

var workingDir string
if len(info.ComponentFolderPrefix) == 0 {
workingDir = fmt.Sprintf("%s/%s", c.Config.Components.Terraform.BasePath, finalComponent)
workingDir = path.Join(c.Config.Components.Terraform.BasePath, finalComponent)
} else {
workingDir = fmt.Sprintf("%s/%s/%s", c.Config.Components.Terraform.BasePath, info.ComponentFolderPrefix, finalComponent)
workingDir = path.Join(c.Config.Components.Terraform.BasePath, info.ComponentFolderPrefix, finalComponent)
}
fmt.Println(fmt.Sprintf(fmt.Sprintf("Working dir: %s", workingDir)))

// Print ENV vars if they are found in the component stack config
if len(info.ComponentEnvList) > 0 {
fmt.Println()
color.Cyan("Using ENV vars:\n")
for _, v := range info.ComponentEnvList {
fmt.Println(v)
}
}

var workspaceName string
if len(info.BaseComponent) > 0 {
workspaceName = fmt.Sprintf("%s-%s", info.ContextPrefix, info.Component)
Expand Down Expand Up @@ -238,9 +249,9 @@ func ExecuteTerraform(cmd *cobra.Command, args []string) error {
allArgsAndFlags = append(allArgsAndFlags, info.AdditionalArgsAndFlags...)

// Run `terraform workspace`
err = execCommand(info.Command, []string{"workspace", "select", workspaceName}, componentPath, nil)
err = execCommand(info.Command, []string{"workspace", "select", workspaceName}, componentPath, info.ComponentEnvList)
if err != nil {
err = execCommand(info.Command, []string{"workspace", "new", workspaceName}, componentPath, nil)
err = execCommand(info.Command, []string{"workspace", "new", workspaceName}, componentPath, info.ComponentEnvList)
if err != nil {
return err
}
Expand All @@ -264,7 +275,7 @@ func ExecuteTerraform(cmd *cobra.Command, args []string) error {

// Execute the command
if info.SubCommand != "workspace" {
err = execCommand(info.Command, allArgsAndFlags, componentPath, nil)
err = execCommand(info.Command, allArgsAndFlags, componentPath, info.ComponentEnvList)
if err != nil {
return err
}
Expand All @@ -276,11 +287,6 @@ func ExecuteTerraform(cmd *cobra.Command, args []string) error {
_ = os.Remove(planFilePath)
}

err = os.Remove(varFileName)
if err != nil {
color.Yellow("Error deleting terraform varfile: %s\n", err)
}

return nil
}

Expand Down
29 changes: 21 additions & 8 deletions internal/exec/terraform_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"path"
"strings"
)

Expand Down Expand Up @@ -75,6 +76,7 @@ func ExecuteTerraformGenerateBackend(cmd *cobra.Command, args []string) error {
if config.ProcessedConfig.StackType == "Directory" {
componentSection,
componentVarsSection,
_,
componentBackendSection,
componentBackendType,
_, _,
Expand All @@ -83,7 +85,9 @@ func ExecuteTerraformGenerateBackend(cmd *cobra.Command, args []string) error {
return err
}
} else {
color.Cyan("Searching for stack config where the component '%s' is defined\n", component)
if g.LogVerbose == true {
color.Cyan("Searching for stack config where the component '%s' is defined\n", component)
}

if len(config.Config.Stacks.NamePattern) < 1 {
return errors.New("stack name pattern must be provided in 'stacks.name_pattern' config or 'ATMOS_STACKS_NAME_PATTERN' ENV variable")
Expand Down Expand Up @@ -112,6 +116,7 @@ func ExecuteTerraformGenerateBackend(cmd *cobra.Command, args []string) error {
for stackName := range stacksMap {
componentSection,
componentVarsSection,
_,
componentBackendSection,
componentBackendType,
_, _,
Expand Down Expand Up @@ -146,7 +151,9 @@ func ExecuteTerraformGenerateBackend(cmd *cobra.Command, args []string) error {
}

if tenantFound == true && environmentFound == true && stageFound == true {
color.Green("Found stack config for the '%s' component in the '%s' stack\n\n", component, stackName)
if g.LogVerbose == true {
color.Green("Found stack config for the '%s' component in the '%s' stack\n\n", component, stackName)
}
stack = stackName
break
}
Expand All @@ -173,7 +180,8 @@ func ExecuteTerraformGenerateBackend(cmd *cobra.Command, args []string) error {

var componentBackendConfig = generateComponentBackendConfig(componentBackendType, componentBackendSection)

color.Cyan("\nComponent backend config:\n\n")
fmt.Println()
color.Cyan("Component backend config:\n\n")
err = utils.PrintAsJSON(componentBackendConfig)
if err != nil {
return err
Expand All @@ -197,12 +205,17 @@ func ExecuteTerraformGenerateBackend(cmd *cobra.Command, args []string) error {
finalComponent = component
}

// Write backend to file
var varFileName = fmt.Sprintf("%s/%s/backend.tf.json", config.Config.Components.Terraform.BasePath, finalComponent)
// Write backend config to file
var backendFileName = path.Join(
config.Config.Components.Terraform.BasePath,
finalComponent,
"backend.tf.json",
)

color.Cyan("\nWriting backend config to file:")
fmt.Println(varFileName)
err = utils.WriteToFileAsJSON(varFileName, componentBackendConfig, 0644)
fmt.Println()
color.Cyan("Writing backend config to file:")
fmt.Println(backendFileName)
err = utils.WriteToFileAsJSON(backendFileName, componentBackendConfig, 0644)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 84e9fd0

Please sign in to comment.