This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
/
generate.go
183 lines (160 loc) · 4.85 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package tasks
import (
"encoding/json"
"fmt"
printer "github.com/KablamoOSS/go-cli-printer"
"github.com/KablamoOSS/kombustion/internal/cloudformation"
"github.com/KablamoOSS/kombustion/internal/cloudformation/tasks"
"github.com/KablamoOSS/kombustion/internal/core"
"github.com/KablamoOSS/kombustion/internal/manifest"
"github.com/KablamoOSS/kombustion/internal/plugins"
"github.com/KablamoOSS/kombustion/internal/plugins/lock"
"github.com/urfave/cli"
)
// GenerateFlags for kombustion upsert
var GenerateFlags = []cli.Flag{
cli.StringSliceFlag{
Name: "param, p",
Usage: "cloudformation parameters. eg. `--param Env=dev --param BucketName=test`",
},
cli.BoolFlag{
Name: "generate-default-outputs, b",
Usage: "disable generation of outputs for Base AWS types",
},
cli.StringFlag{
Name: "read-parameters",
Usage: "Read parameters from a file",
},
cli.BoolFlag{
Name: "write-parameters, w",
Usage: "Write parameters to a file",
},
cli.StringFlag{
Name: "output-directory, d",
Usage: "Directory to write generated yaml to",
Value: "compiled",
},
}
func init() {
GenerateFlags = append(GenerateFlags, CloudFormationStackFlags...)
}
// Generate a template and save it to disk, without upserting it
func Generate(c *cli.Context) {
objectStore := core.NewFilesystemStore(".")
fileName := c.Args().Get(0)
if fileName == "" {
printer.Fatal(
fmt.Errorf("Can't generate file, no source template provided"),
"Add the path to the source template file you want to generate like: `kombustion generate template.yaml`.",
"https://www.kombustion.io/api/cli/#generate",
)
}
paramsSlice := c.StringSlice("param")
paramsMap := cliSliceMap(paramsSlice)
// if in devMode optionally load a devMode plugin
devPluginPath := c.GlobalString("load-plugin")
outputDirectory := c.String("output-directory")
inputParameters := c.String("read-parameters")
outputParameters := c.Bool("write-parameters")
env := c.String("environment")
generateDefaultOutputs := c.Bool("generate-default-outputs")
manifestFile := c.GlobalString("manifest-file")
generate(
objectStore,
fileName,
paramsMap,
inputParameters,
devPluginPath,
outputDirectory,
outputParameters,
env,
generateDefaultOutputs,
manifestFile,
)
}
func generate(
objectStore core.ObjectStore,
templatePath string,
cliParams map[string]string,
paramsPath string,
devPluginPath string,
outputDirectory string,
outputParameters bool,
envName string,
generateDefaultOutputs bool,
manifestLocation string,
) {
printer.Step("Generate template")
printer.Progress("Kombusting")
lockFile, err := lock.GetLockObject(objectStore, "kombustion.lock")
if err != nil {
printer.Fatal(
fmt.Errorf("Couldn't load lock file: %v", err),
"Check the file exists, and your user has read permissions",
"",
)
}
// manifestFile := manifest.FindAndLoadManifest()
manifestFile, err := manifest.GetManifestObject(objectStore, manifestLocation)
if err != nil {
printer.Fatal(
fmt.Errorf("Couldn't load manifest file: %v", err),
"Check the file exists, and your user has read permissions",
"",
)
}
paramMap := make(map[string]string)
if env, ok := manifestFile.Environments[envName]; ok {
for key, value := range env.Parameters {
paramMap[key] = value
}
}
if paramsPath != "" {
for key, value := range readParamsObject(objectStore, paramsPath) {
paramMap[key] = value
}
}
for key, value := range cliParams {
paramMap[key] = value
}
// load all plugins
loadedPlugins := plugins.LoadPlugins(manifestFile, lockFile)
if devPluginPath != "" {
devPluginLoaded := plugins.LoadDevPlugin(devPluginPath)
loadedPlugins = append(loadedPlugins, devPluginLoaded)
}
printer.Progress("Generating template")
tasks.GenerateTemplate(cloudformation.GenerateParams{
ObjectStore: objectStore,
Filename: templatePath,
Directory: outputDirectory,
WriteParams: outputParameters,
Env: envName,
ParamMap: paramMap,
Plugins: loadedPlugins,
GenerateDefaultOutputs: generateDefaultOutputs || manifestFile.GenerateDefaultOutputs,
})
}
func readParamsObject(objStore core.ObjectStore, path string) map[string]string {
body, err := objStore.Get(path)
if err != nil {
printer.Fatal(
fmt.Errorf("Couldn't read params file: %v", err),
"Check the file exists, and your user has read permissions",
"",
)
}
cfParams := []cloudformation.Parameter{}
if err = json.Unmarshal(body, &cfParams); err != nil {
printer.Fatal(
fmt.Errorf("Couldn't unmarshal params file: %v", err),
"Check the file is valid JSON, in the standard AWS cli format",
"https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_Parameter.html",
)
}
params := make(map[string]string)
for _, param := range cfParams {
params[param.ParameterKey] = param.ParameterValue
}
return params
}