-
Notifications
You must be signed in to change notification settings - Fork 15
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
Changes from 3 commits
e2da7ce
be0f2c5
6bfca22
f9cc291
bd704b2
e00c717
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,14 @@ package main | |
type ConsulPlaybookProvider struct { | ||
consulAddress string | ||
consulKey string | ||
variables CLIVariables | ||
} | ||
|
||
func NewConsulPlaybookProvider(consulAddress, consulKey string) *ConsulPlaybookProvider { | ||
func NewConsulPlaybookProvider(options Options) *ConsulPlaybookProvider { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same, don't hesitate to run |
||
} | ||
} | ||
|
||
|
@@ -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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,11 +14,13 @@ package main | |
|
||
type YAMLFilePlaybookProvider struct { | ||
playbookPath string | ||
variables CLIVariables | ||
} | ||
|
||
func NewYAMLFilePlaybookProvider(playbookPath string) *YAMLFilePlaybookProvider { | ||
func NewYAMLFilePlaybookProvider(options Options) *YAMLFilePlaybookProvider { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
} | ||
} | ||
|
||
|
@@ -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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ import ( | |
"gopkg.in/yaml.v1" | ||
"regexp" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
var ( | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be worth it to restrict ourselves to just a |
||
// 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 | ||
} | ||
|
@@ -51,3 +57,17 @@ func cleanYaml(rawYaml []byte) []byte { | |
} | ||
return buffer.Bytes() | ||
} | ||
|
||
func fillPlaybookTemplate(playbookStr string, variables CLIVariables) (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same I think we can drop |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the wrapping of a string in another string? |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the indent seems off