Skip to content

Commit

Permalink
feat: render compose file as a Go template
Browse files Browse the repository at this point in the history
  • Loading branch information
m-adawi committed Jul 21, 2024
1 parent 9636070 commit cc9544c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
10 changes: 9 additions & 1 deletion docs/stacks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,13 @@ stack-name:
branch: main
# The path to the docker compose file where stack
# is defined
compose_file: compose.yaml
compose_file: /path/to/compose.yaml
# Path to values file to use when rendering
# compose file as a Go template. If empty, compose
# file will be treated as a regular compose file
values_file: /path/to/values.yaml
# Paths to files encrypted using sops to decrypt
# before updating stack
sops_files:
- path/to/sops/encrypted/file

37 changes: 37 additions & 0 deletions swarmcd/swarmcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path"
"sync"
"text/template"
"time"

"github.com/docker/cli/cli/command/stack"
Expand Down Expand Up @@ -61,6 +62,11 @@ func updateStack(stackName string) (revision string, err error) {
return "", fmt.Errorf("failed to decrypt one or more sops files for %s stack: %w", stackName, err)
}

err = renderComposeTemplate(stackName)
if err != nil {
return
}

err = rotateConfigsAndSecrets(stackName)
if err != nil {
return
Expand Down Expand Up @@ -206,6 +212,37 @@ func rotateObjects(objects map[string]any, objectsDir string, stackName string)
return nil
}


func renderComposeTemplate(stackName string) error {
stackConfig := config.StackConfigs[stackName]
if stackConfig.ValuesFile == "" {
return nil
}
composeFile := path.Join(config.ReposPath, stackConfig.Repo, stackConfig.ComposeFile)
valuesFile := path.Join(config.ReposPath, stackConfig.Repo, stackConfig.ValuesFile)
valuesBytes, err := os.ReadFile(valuesFile)
if err != nil {
return fmt.Errorf("could not read %s stack values file: %w", stackName, err)
}
var valuesMap map[string]any
yaml.Unmarshal(valuesBytes, &valuesMap)
templ, err := template.New(path.Base(composeFile)).ParseFiles(composeFile)
if err != nil {
return fmt.Errorf("could not parse %s stack compose file as a Go template: %w", stackName, err)
}
composeFileWriter, err := os.Create(composeFile)
if err != nil {
return fmt.Errorf("could not open %s stack compose file: %w",stackName, err)
}
err = templ.Execute(composeFileWriter, map[string]map[string]any{"Values": valuesMap})
if err != nil {
return fmt.Errorf("error rending %s stack compose template: %w", stackName, err)
}
return nil
}



func GetStackStatus() map[string]*StackStatus {
return stackStatus
}
1 change: 1 addition & 0 deletions util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type StackConfig struct {
Repo string
Branch string
ComposeFile string `mapstructure:"compose_file"`
ValuesFile string `mapstructure:"values_file"`
SopsFiles []string `mapstructure:"sops_files"`
}

Expand Down

0 comments on commit cc9544c

Please sign in to comment.