This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
/
root.go
339 lines (284 loc) · 8.68 KB
/
root.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package cmd
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"code.cloudfoundry.org/fissile/app"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
cfgFile string
fissile *app.Fissile
version string
)
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "fissile",
Short: "The BOSH disintegrator",
Long: `
Fissile converts existing BOSH final or dev releases into docker images.
It does this using just the releases, without a BOSH deployment, CPIs, or a BOSH
agent.
`,
SilenceErrors: true,
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := validateBasicFlags(); err != nil {
return err
}
return validateReleaseArgs()
},
}
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(f *app.Fissile, v string) error {
fissile = f
version = v
return RootCmd.Execute()
}
func init() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags, which, if defined here,
// will be global for your application.
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.fissile.yaml)")
RootCmd.PersistentFlags().StringP(
"role-manifest",
"m",
"",
"Path to a yaml file that details which jobs are used for each instance group.",
)
// We can't use slices here because of https://github.com/spf13/viper/issues/112
RootCmd.PersistentFlags().StringP(
"release",
"r",
"",
"Path to final or dev BOSH release(s).",
)
// We can't use slices here because of https://github.com/spf13/viper/issues/112
RootCmd.PersistentFlags().StringP(
"release-name",
"n",
"",
"Name of a dev BOSH release; if empty, default configured dev release name will be used; Final release always use the name in release.MF",
)
// We can't use slices here because of https://github.com/spf13/viper/issues/112
RootCmd.PersistentFlags().StringP(
"release-version",
"v",
"",
"Version of a dev BOSH release; if empty, the latest dev release will be used; Final release always use the version in release.MF",
)
RootCmd.PersistentFlags().StringP(
"cache-dir",
"c",
filepath.Join(os.Getenv("HOME"), ".bosh", "cache"),
"Local BOSH cache directory.",
)
RootCmd.PersistentFlags().StringP(
"final-releases-dir",
"",
filepath.Join(os.Getenv("HOME"), ".final-releases"),
"Local final releases directory.",
)
RootCmd.PersistentFlags().StringP(
"work-dir",
"w",
"/var/fissile",
"Path to the location of the work directory.",
)
RootCmd.PersistentFlags().StringP(
"repository",
"p",
"",
"Repository name prefix used to create image names.",
)
RootCmd.PersistentFlags().StringP(
"docker-registry",
"",
"",
"Docker registry used when referencing image names",
)
RootCmd.PersistentFlags().StringP(
"docker-username",
"",
"",
"Username for authenticated docker registry",
)
RootCmd.PersistentFlags().StringP(
"docker-password",
"",
"",
"Password for authenticated docker registry",
)
RootCmd.PersistentFlags().StringP(
"docker-organization",
"",
"",
"Docker organization used when referencing image names",
)
RootCmd.PersistentFlags().IntP(
"workers",
"W",
0,
"Number of workers to use; zero means determine based on CPU count.",
)
RootCmd.PersistentFlags().StringP(
"light-opinions",
"l",
"",
"Path to a BOSH deployment manifest file that contains properties to be used as defaults.",
)
RootCmd.PersistentFlags().StringP(
"dark-opinions",
"d",
"",
"Path to a BOSH deployment manifest file that contains properties that should not have opinionated defaults.",
)
RootCmd.PersistentFlags().StringP(
"metrics",
"M",
"",
"Path to a CSV file to store timing metrics into.",
)
RootCmd.PersistentFlags().StringP(
"output",
"o",
app.OutputFormatHuman,
"Choose output format, one of human, json, or yaml (currently only for 'show properties')",
)
RootCmd.PersistentFlags().BoolP(
"verbose",
"V",
false,
"Enable verbose output.",
)
viper.BindPFlags(RootCmd.PersistentFlags())
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
initViper(viper.GetViper())
}
func initViper(v *viper.Viper) {
if cfgFile != "" { // enable ability to specify config file via flag
v.SetConfigFile(cfgFile)
}
v.SetEnvPrefix("FISSILE")
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
v.SetConfigName(".fissile") // name of config file (without extension)
v.AddConfigPath("$HOME") // adding home directory as first search path
v.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := v.ReadInConfig(); err == nil {
if v == viper.GetViper() {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
}
func validateBasicFlags() error {
fissile.Options.RoleManifest = viper.GetString("role-manifest")
fissile.Options.Releases = splitNonEmpty(viper.GetString("release"), ",")
fissile.Options.ReleaseNames = splitNonEmpty(viper.GetString("release-name"), ",")
fissile.Options.ReleaseVersions = splitNonEmpty(viper.GetString("release-version"), ",")
fissile.Options.CacheDir = viper.GetString("cache-dir")
fissile.Options.FinalReleasesDir = viper.GetString("final-releases-dir")
fissile.Options.WorkDir = viper.GetString("work-dir")
fissile.Options.RepositoryPrefix = viper.GetString("repository")
fissile.Options.DockerRegistry = strings.TrimSuffix(viper.GetString("docker-registry"), "/")
fissile.Options.DockerOrganization = viper.GetString("docker-organization")
fissile.Options.DockerUsername = viper.GetString("docker-username")
fissile.Options.DockerPassword = viper.GetString("docker-password")
fissile.Options.Workers = viper.GetInt("workers")
fissile.Options.LightOpinions = viper.GetString("light-opinions")
fissile.Options.DarkOpinions = viper.GetString("dark-opinions")
fissile.Options.OutputFormat = viper.GetString("output")
fissile.Options.Metrics = viper.GetString("metrics")
fissile.Options.Verbose = viper.GetBool("verbose")
// Set defaults for empty flags
if fissile.Options.RoleManifest == "" {
fissile.Options.RoleManifest = filepath.Join(fissile.Options.WorkDir, "role-manifest.yml")
}
if fissile.Options.LightOpinions == "" {
fissile.Options.LightOpinions = filepath.Join(fissile.Options.WorkDir, "opinions.yml")
}
if fissile.Options.DarkOpinions == "" {
fissile.Options.DarkOpinions = filepath.Join(fissile.Options.WorkDir, "dark-opinions.yml")
}
if fissile.Options.Workers < 1 {
fissile.Options.Workers = runtime.NumCPU()
}
err := absolutePaths(
&fissile.Options.RoleManifest,
&fissile.Options.CacheDir,
&fissile.Options.WorkDir,
&fissile.Options.LightOpinions,
&fissile.Options.DarkOpinions,
&fissile.Options.Metrics,
)
if err == nil {
fissile.Options.Releases, err = absolutePathsForArray(fissile.Options.Releases)
}
return err
}
func validateReleaseArgs() error {
releasePathsCount := len(fissile.Options.Releases)
releaseNamesCount := len(fissile.Options.ReleaseNames)
releaseVersionsCount := len(fissile.Options.ReleaseVersions)
argList := fmt.Sprintf(
"validateDevReleaseArgs: paths:%s (%d), names:%s (%d), versions:%s (%d)\n",
fissile.Options.Releases,
releasePathsCount,
fissile.Options.ReleaseNames,
releaseNamesCount,
fissile.Options.ReleaseVersions,
releaseVersionsCount,
)
if releaseNamesCount != 0 && releaseNamesCount != releasePathsCount {
return fmt.Errorf("If you specify custom release names, you need to do it for all of them. Args: %s", argList)
}
if releaseVersionsCount != 0 && releaseVersionsCount != releasePathsCount {
return fmt.Errorf("If you specify custom release versions, you need to do it for all of them. Args: %s", argList)
}
return nil
}
func absolutePathsForArray(paths []string) ([]string, error) {
absolutePaths := make([]string, len(paths))
for idx, path := range paths {
absPath, err := absolutePath(path)
if err != nil {
return nil, err
}
absolutePaths[idx] = absPath
}
return absolutePaths, nil
}
func absolutePaths(paths ...*string) error {
for _, path := range paths {
absPath, err := absolutePath(*path)
if err != nil {
return err
}
*path = absPath
}
return nil
}
func absolutePath(path string) (string, error) {
path, err := filepath.Abs(path)
if err != nil {
return "", fmt.Errorf("Error getting absolute path for path %s: %v", path, err)
}
return path, nil
}
func splitNonEmpty(value string, separator string) []string {
s := strings.Split(value, separator)
var r []string
for _, str := range s {
if len(str) != 0 {
r = append(r, str)
}
}
return r
}