Skip to content
This repository has been archived by the owner on Jun 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #25 from C2FO/dvo-5728
Browse files Browse the repository at this point in the history
DVO-5728 -> master | Convert WFC from existing config management to gull
  • Loading branch information
tim-kretschmer-c2fo committed Jun 8, 2016
2 parents 42e1194 + 8b9b7d3 commit 0b2384d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 10 deletions.
31 changes: 30 additions & 1 deletion source/lib/gull/migration.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package gull

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"text/template"
"time"

"github.com/go-yaml/yaml"
Expand All @@ -23,7 +26,12 @@ type Migration struct {
func NewMigrationFromGull(name string, source string) (*Migration, error) {
migration := newMigration(name)

err := yaml.Unmarshal([]byte(source), &migration.Content)
sourceBytes, err := ingestMigrationTemplate(source)
if err != nil {
return nil, err
}

err = yaml.Unmarshal(sourceBytes, &migration.Content)

return migration, err
}
Expand Down Expand Up @@ -90,6 +98,27 @@ func (m *Migration) WriteToFile(filePath string) error {
return ioutil.WriteFile(filePath, []byte(rawYaml), 0644)
}

// ingestMigrationTemplate searches environment variables prepended with 'GULL_TEMPLATE_VAR_' to token swap migration file contents.
func ingestMigrationTemplate(source string) ([]byte, error) {
parsedTemplate, err := template.New("").Parse(source)
if err != nil {
return nil, err
}
var renderedTemplate bytes.Buffer
templateVariables := map[string]string{}
environmentVariables := os.Environ()
for _, env := range environmentVariables {
parts := strings.Split(env, "=")
envKey := parts[0]
envValue := parts[1]
if strings.Contains(envKey, "GULL_TEMPLATE_VAR_") {
templateVariables[strings.Replace(envKey, "GULL_TEMPLATE_VAR_", "", 1)] = envValue
}
}
err = parsedTemplate.Execute(&renderedTemplate, templateVariables)
return renderedTemplate.Bytes(), err
}

func migrationNameFromPath(filePath string) string {
return strings.Replace(strings.Replace(path.Base(filePath), ".yaml", "", 1), ".yml", "", 1)
}
Expand Down
4 changes: 2 additions & 2 deletions source/lib/ui/destroy_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func (dc *DestroyCommand) GetFlags() []cli.Flag {
Name: "environment, e",
Usage: "system to target for configuration migration",
EnvVar: "GULL_ENVIRONMENT",
Value: "default",
},
cli.StringFlag{
Name: "etcdhost, s",
Expand Down Expand Up @@ -60,8 +59,9 @@ func (dc *DestroyCommand) ParseOptions(context *cli.Context) {
}

dc.Environment = context.String("environment")
if dc.Environment == "default" {
if dc.Environment == "" {
dc.Logger.Info("No target environment was provided, using 'default'")
dc.Environment = "default"
}

dc.EtcdHostUrl = context.String("etcdhost")
Expand Down
4 changes: 2 additions & 2 deletions source/lib/ui/down_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func (dc *DownCommand) GetFlags() []cli.Flag {
Name: "environment, e",
Usage: "system to target for configuration migration",
EnvVar: "GULL_ENVIRONMENT",
Value: "default",
},
cli.StringFlag{
Name: "etcdhost, s",
Expand Down Expand Up @@ -74,9 +73,10 @@ func (dc *DownCommand) ParseOptions(context *cli.Context) {
}

dc.Environment = context.String("environment")
if dc.Environment == "default" {
if dc.Environment == "" {
if dc.Verbose {
fmt.Printf("environment was not found, migrating 'default'\n")
dc.Environment = "default"
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/lib/ui/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type GullCommand interface {
func Launch() {
app := cli.NewApp()
app.Name = "gull"
app.Version = "0.10.0"
app.Version = "0.11.0"
app.Usage = "etcd configuration migration management system"
app.Commands = []cli.Command{
new(ConvertCommand).GetCliCommand(),
Expand Down
4 changes: 2 additions & 2 deletions source/lib/ui/status_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func (sc *StatusCommand) GetFlags() []cli.Flag {
Name: "environment, e",
Usage: "system to target for configuration migration",
EnvVar: "GULL_ENVIRONMENT",
Value: "default",
},
cli.StringFlag{
Name: "etcdhost, s",
Expand Down Expand Up @@ -60,8 +59,9 @@ func (sc *StatusCommand) ParseOptions(context *cli.Context) {
}

sc.Environment = context.String("environment")
if sc.Environment == "default" {
if sc.Environment == "" {
sc.Logger.Info("No target environment was provided, using 'default'")
sc.Environment = "default"
}

sc.EtcdHostUrl = context.String("etcdhost")
Expand Down
4 changes: 2 additions & 2 deletions source/lib/ui/up_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func (uc *UpCommand) GetFlags() []cli.Flag {
Name: "environment, e",
Usage: "environment to target for configuration migration",
EnvVar: "GULL_ENVIRONMENT",
Value: "default",
},
cli.StringFlag{
Name: "etcdhost, s",
Expand Down Expand Up @@ -89,9 +88,10 @@ func (uc *UpCommand) ParseOptions(context *cli.Context) {
}

uc.Environment = context.String("environment")
if uc.Environment == "default" {
if uc.Environment == "" {
if uc.Verbose {
fmt.Printf("environment was not found, migrating 'default'\n")
uc.Environment = "default"
}
}

Expand Down

0 comments on commit 0b2384d

Please sign in to comment.