Skip to content

Commit

Permalink
Merge pull request #27 from ckaznocha/replacement_files
Browse files Browse the repository at this point in the history
Replacement files
  • Loading branch information
ckaznocha authored Jan 26, 2017
2 parents 7c2e473 + 7c2e223 commit 20a1141
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Given a JSON file specified by `app_json`, post it to Marathon to deploy the app

* `replacements`: *Optional.* A `name`/`value` list of templated strings in the app.json to replace during the deploy. Useful for things such as passwords or urls that change.

* `replacement_files`: *Optional.* Similar to `replacements` except value is a path to a file who's content will be used in the replace.

* `restart_if_no_update`: *Optional.* If Marathon doesn't detect any change in your app.json it won't deploy a new version. Setting this to `true` will restart an existing app causing a new version. Default is `false`.

## Example Configuration
Expand Down
1 change: 1 addition & 0 deletions cmd/marathon-resource/behaviors/behaviors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Params struct {
AppJSON string `json:"app_json"`
TimeOut int `json:"time_out"`
Replacements []Metadata `json:"replacements"`
ReplacementFiles []Metadata `json:"replacement_files"`
RestartIfNoUpdate bool `json:"restart_if_no_update"`
}

Expand Down
47 changes: 42 additions & 5 deletions cmd/marathon-resource/behaviors/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@ package behaviors

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"strings"

"github.com/aymerick/raymond"
)

func parsePayload(p Params, path string) (io.Reader, error) {
var (
replacments = map[string]string{}
buf = bytes.NewBuffer([]byte{})
replacements = map[string]string{}
buf = bytes.NewBuffer([]byte{})
)
for _, v := range p.Replacements {
replacments[v.Name] = v.Value
replacements = replaceStrings(p.Replacements, replacements)
replacements, err := replaceFiles(p.ReplacementFiles, replacements, path)
if err != nil {
return nil, err
}

tmpl, err := raymond.ParseFile(filepath.Join(path, p.AppJSON))
if err != nil {
return nil, err
}
app, err := tmpl.Exec(replacments)
app, err := tmpl.Exec(replacements)
if err != nil {
return nil, err
}
Expand All @@ -30,3 +36,34 @@ func parsePayload(p Params, path string) (io.Reader, error) {
}
return buf, nil
}

func replaceStrings(
metadata []Metadata,
replacements map[string]string,
) map[string]string {
for _, v := range metadata {
replacements[v.Name] = v.Value
}

return replacements
}

func replaceFiles(
metadata []Metadata,
replacements map[string]string,
path string,
) (map[string]string, error) {
for _, v := range metadata {
fileValue, err := ioutil.ReadFile(filepath.Join(path, v.Value))
if err != nil {
return replacements, fmt.Errorf(
"Error replaceing %s from replacement_files: %v",
v.Name,
err,
)
}
replacements[v.Name] = strings.TrimSpace(string(fileValue))
}

return replacements, nil
}
2 changes: 2 additions & 0 deletions cmd/marathon-resource/behaviors/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func Test_parsePayload(t *testing.T) {
}{
{"Reads file with no replacements", args{Params{AppJSON: "app.json"}, "../fixtures"}, []byte{123, 10, 32, 32, 32, 32, 34, 102, 111, 111, 34, 58, 32, 34, 98, 97, 114, 34, 10, 125, 10}, false},
{"Reads file with replacements", args{Params{AppJSON: "app_template.json", Replacements: []Metadata{{"foo", "bar"}}}, "../fixtures"}, []byte{123, 10, 32, 32, 32, 32, 34, 102, 111, 111, 34, 58, 32, 34, 98, 97, 114, 34, 10, 125, 10}, false},
{"Reads file with replacement files", args{Params{AppJSON: "app_template.json", ReplacementFiles: []Metadata{{"foo", "foo.txt"}}}, "../fixtures"}, []byte{123, 10, 32, 32, 32, 32, 34, 102, 111, 111, 34, 58, 32, 34, 98, 97, 114, 34, 10, 125, 10}, false},
{"Reads file with missing replacement files", args{Params{AppJSON: "app_template.json", ReplacementFiles: []Metadata{{"foo", "baz.txt"}}}, "../fixtures"}, nil, true},
{"Reads file with bad tmpl", args{Params{AppJSON: "app_template_bad.json", Replacements: []Metadata{{"foo", "bar"}}}, "../fixtures"}, nil, true},
}
for _, tt := range tests {
Expand Down
2 changes: 2 additions & 0 deletions cmd/marathon-resource/fixtures/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

bar

0 comments on commit 20a1141

Please sign in to comment.