Skip to content

Commit 20a1141

Browse files
authored
Merge pull request #27 from ckaznocha/replacement_files
Replacement files
2 parents 7c2e473 + 7c2e223 commit 20a1141

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Given a JSON file specified by `app_json`, post it to Marathon to deploy the app
4747

4848
* `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.
4949

50+
* `replacement_files`: *Optional.* Similar to `replacements` except value is a path to a file who's content will be used in the replace.
51+
5052
* `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`.
5153

5254
## Example Configuration

cmd/marathon-resource/behaviors/behaviors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Params struct {
1414
AppJSON string `json:"app_json"`
1515
TimeOut int `json:"time_out"`
1616
Replacements []Metadata `json:"replacements"`
17+
ReplacementFiles []Metadata `json:"replacement_files"`
1718
RestartIfNoUpdate bool `json:"restart_if_no_update"`
1819
}
1920

cmd/marathon-resource/behaviors/template.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,31 @@ package behaviors
22

33
import (
44
"bytes"
5+
"fmt"
56
"io"
7+
"io/ioutil"
68
"path/filepath"
9+
"strings"
710

811
"github.com/aymerick/raymond"
912
)
1013

1114
func parsePayload(p Params, path string) (io.Reader, error) {
1215
var (
13-
replacments = map[string]string{}
14-
buf = bytes.NewBuffer([]byte{})
16+
replacements = map[string]string{}
17+
buf = bytes.NewBuffer([]byte{})
1518
)
16-
for _, v := range p.Replacements {
17-
replacments[v.Name] = v.Value
19+
replacements = replaceStrings(p.Replacements, replacements)
20+
replacements, err := replaceFiles(p.ReplacementFiles, replacements, path)
21+
if err != nil {
22+
return nil, err
1823
}
24+
1925
tmpl, err := raymond.ParseFile(filepath.Join(path, p.AppJSON))
2026
if err != nil {
2127
return nil, err
2228
}
23-
app, err := tmpl.Exec(replacments)
29+
app, err := tmpl.Exec(replacements)
2430
if err != nil {
2531
return nil, err
2632
}
@@ -30,3 +36,34 @@ func parsePayload(p Params, path string) (io.Reader, error) {
3036
}
3137
return buf, nil
3238
}
39+
40+
func replaceStrings(
41+
metadata []Metadata,
42+
replacements map[string]string,
43+
) map[string]string {
44+
for _, v := range metadata {
45+
replacements[v.Name] = v.Value
46+
}
47+
48+
return replacements
49+
}
50+
51+
func replaceFiles(
52+
metadata []Metadata,
53+
replacements map[string]string,
54+
path string,
55+
) (map[string]string, error) {
56+
for _, v := range metadata {
57+
fileValue, err := ioutil.ReadFile(filepath.Join(path, v.Value))
58+
if err != nil {
59+
return replacements, fmt.Errorf(
60+
"Error replaceing %s from replacement_files: %v",
61+
v.Name,
62+
err,
63+
)
64+
}
65+
replacements[v.Name] = strings.TrimSpace(string(fileValue))
66+
}
67+
68+
return replacements, nil
69+
}

cmd/marathon-resource/behaviors/template_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ func Test_parsePayload(t *testing.T) {
1919
}{
2020
{"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},
2121
{"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},
22+
{"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},
23+
{"Reads file with missing replacement files", args{Params{AppJSON: "app_template.json", ReplacementFiles: []Metadata{{"foo", "baz.txt"}}}, "../fixtures"}, nil, true},
2224
{"Reads file with bad tmpl", args{Params{AppJSON: "app_template_bad.json", Replacements: []Metadata{{"foo", "bar"}}}, "../fixtures"}, nil, true},
2325
}
2426
for _, tt := range tests {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
bar

0 commit comments

Comments
 (0)