Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add the possibility to template playbooks #103

Merged
merged 6 commits into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions integration/resources/good-postgres-with-template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
:targets:
- :name: "My Postgres database 1"
:type: postgres
:host: {{.host}}
:database: sql_runner_tests_1
:port: 5432
:username: {{.username}}
:password: {{.password}}
:ssl: false # SSL disabled by default
:variables:
:test_schema: sql_runner_tests
:timeFormat: "2006_01_02"
:steps:
- :name: Create schema and table
:queries:
- :name: Create schema and table
:file: postgres-sql/good/1.sql
:template: true
6 changes: 6 additions & 0 deletions integration/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ assert_ExitCodeForCommand "0" "${root}/sql-runner -checkLock ${root}/dist/integr
assert_ExitCodeForCommand "5" "${root}/sql-runner -playbook ${root_key}/bad-mixed.yml -lock ${root}/dist/integration-lock -dryRun"
assert_ExitCodeForCommand "0" "${root}/sql-runner -playbook ${root_key}/good-postgres.yml -var test_date=`date "+%Y_%m_%d"` -lock ${root}/dist/integration-lock -dryRun"

# Test: Valid playbook which uses playbook template variables
assert_ExitCodeForCommand "6" "${root}/sql-runner -playbook ${root_key}/good-postgres-with-template.yml -var password=,host=localhost"
assert_ExitCodeForCommand "6" "${root}/sql-runner -playbook ${root_key}/good-postgres-with-template.yml"
assert_ExitCodeForCommand "0" "${root}/sql-runner -playbook ${root_key}/good-postgres-with-template.yml -var username=postgres,password=,host=localhost"


printf "==========================================================\n"
printf " INTEGRATION TESTS SUCCESSFUL\n"
printf "==========================================================\n"
10 changes: 6 additions & 4 deletions sql_runner/consul_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ package main
type ConsulPlaybookProvider struct {
consulAddress string
consulKey string
variables CLIVariables

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the indent seems off

}

func NewConsulPlaybookProvider(consulAddress, consulKey string) *ConsulPlaybookProvider {
func NewConsulPlaybookProvider(options Options) *ConsulPlaybookProvider {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure we want to pass the whole options bundle and not just the address, key and variables?

return &ConsulPlaybookProvider{
consulAddress: consulAddress,
consulKey: consulKey,
consulAddress: options.consul,
consulKey: options.playbook,
variables: options.variables,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, don't hesitate to run gofmt

}
}

Expand All @@ -30,6 +32,6 @@ func (p ConsulPlaybookProvider) GetPlaybook() (*Playbook, error) {
return nil, err
}

playbook, pbErr := parsePlaybookYaml(lines)
playbook, pbErr := parsePlaybookYaml(lines, p.variables)
return &playbook, pbErr
}
4 changes: 2 additions & 2 deletions sql_runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ func processFlags() Options {
// based on flags passed in
func PlaybookProviderFromOptions(options Options) (PlaybookProvider, error) {
if options.consul != "" {
return NewConsulPlaybookProvider(options.consul, options.playbook), nil
return NewConsulPlaybookProvider(options), nil
} else if options.playbook != "" {
return NewYAMLFilePlaybookProvider(options.playbook), nil
return NewYAMLFilePlaybookProvider(options), nil
} else {
return nil, errors.New("Cannot determine provider for playbook")
}
Expand Down
11 changes: 6 additions & 5 deletions sql_runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ func (i *CLIVariables) String() string {
}

func (i *CLIVariables) Set(value string) error {
var split = strings.SplitN(value, "=", 2)
if len(split) > 1 {
key := split[0]
val := split[1]
(*i)[key] = val
var split = strings.Split(value, ",")

for value := range split {
kv := strings.SplitN(split[value], "=", 2)

(*i)[kv[0]] = kv[1]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we check the length of kv and send an error accordingly to avoid index out of range?

}
return nil
}
Expand Down
8 changes: 5 additions & 3 deletions sql_runner/yaml_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ package main

type YAMLFilePlaybookProvider struct {
playbookPath string
variables CLIVariables
}

func NewYAMLFilePlaybookProvider(playbookPath string) *YAMLFilePlaybookProvider {
func NewYAMLFilePlaybookProvider(options Options) *YAMLFilePlaybookProvider {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same remark as above

return &YAMLFilePlaybookProvider{
playbookPath: playbookPath,
playbookPath: options.playbook,
variables: options.variables,
}
}

Expand All @@ -28,6 +30,6 @@ func (p YAMLFilePlaybookProvider) GetPlaybook() (*Playbook, error) {
return nil, err
}

playbook, pbErr := parsePlaybookYaml(lines)
playbook, pbErr := parsePlaybookYaml(lines, p.variables)
return &playbook, pbErr
}
24 changes: 22 additions & 2 deletions sql_runner/yaml_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"gopkg.in/yaml.v1"
"regexp"
"strings"
"text/template"
)

var (
Expand All @@ -26,13 +27,18 @@ var (

// Parses a playbook.yml to return the targets
// to execute against and the steps to execute
func parsePlaybookYaml(playbookBytes []byte) (Playbook, error) {
func parsePlaybookYaml(playbookBytes []byte, variables CLIVariables) (Playbook, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be worth it to restrict ourselves to just a map[string]string

// Define and initialize the Playbook struct
var playbook Playbook = NewPlaybook()

// Clean up the YAML
cleaned := cleanYaml(playbookBytes)
err := yaml.Unmarshal(cleaned, &playbook)

// Run the yaml through the template engine
str, err := fillPlaybookTemplate(string(cleaned[:]), variables)

// Unmarshal the yaml into the playbook
err = yaml.Unmarshal([]byte(str), &playbook)

return playbook, err
}
Expand All @@ -51,3 +57,17 @@ func cleanYaml(rawYaml []byte) []byte {
}
return buffer.Bytes()
}

func fillPlaybookTemplate(playbookStr string, variables CLIVariables) (string, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same I think we can drop CLIVariables in favour of map[string]string

t, err := template.New("playbook").Parse(playbookStr)
if err != nil {
return "", err
}

var filled bytes.Buffer
if err := t.Execute(&filled, variables); err != nil {
return "", err
}

return string(filled.String()), err

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the wrapping of a string in another string?

}
26 changes: 24 additions & 2 deletions sql_runner/yaml_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
func TestParsePlaybookYaml(t *testing.T) {
assert := assert.New(t)

playbook, err := parsePlaybookYaml(nil)
playbook, err := parsePlaybookYaml(nil, nil)
assert.Nil(err)
assert.NotNil(playbook)
assert.Equal(0, len(playbook.Targets))
Expand All @@ -30,7 +30,7 @@ func TestParsePlaybookYaml(t *testing.T) {
assert.Nil(err1)
assert.NotNil(playbookBytes)

playbook, err = parsePlaybookYaml(playbookBytes)
playbook, err = parsePlaybookYaml(playbookBytes, nil)
assert.Nil(err)
assert.NotNil(playbook)
assert.Equal(2, len(playbook.Targets))
Expand All @@ -51,3 +51,25 @@ func TestCleanYaml(t *testing.T) {
cleanYamlStr = string(cleanYaml(nil))
assert.Equal("\n", cleanYamlStr)
}


func TestTemplateYaml(t *testing.T) {
assert := assert.New(t)

playbookBytes, err1 := loadLocalFile("../integration/resources/good-postgres-with-template.yml")
assert.Nil(err1)

var m map[string]string = make(map[string]string)

m["password"] = "qwerty123"
m["username"] = "animoto"
m["host"] = "theinternetz"

playbook, err := parsePlaybookYaml(playbookBytes, CLIVariables(m))

assert.Nil(err)

assert.Equal("qwerty123", playbook.Targets[0].Password)
assert.Equal("animoto", playbook.Targets[0].Username)
assert.Equal("theinternetz", playbook.Targets[0].Host)
}