forked from rancher/os
/
config.go
310 lines (279 loc) · 6.32 KB
/
config.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
package control
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"sort"
"strings"
"text/template"
"github.com/burmilla/os/config"
"github.com/burmilla/os/pkg/log"
"github.com/burmilla/os/pkg/util"
yaml "github.com/cloudfoundry-incubator/candiedyaml"
"github.com/codegangsta/cli"
"github.com/pkg/errors"
)
func configSubcommands() []cli.Command {
return []cli.Command{
{
Name: "get",
Usage: "get value",
Action: configGet,
},
{
Name: "set",
Usage: "set a value",
Action: configSet,
},
{
Name: "images",
Usage: "List Docker images for a configuration from a file",
Action: runImages,
Flags: []cli.Flag{
cli.StringFlag{
Name: "input, i",
Usage: "File from which to read config",
},
},
},
{
Name: "generate",
Usage: "Generate a configuration file from a template",
Action: runGenerate,
HideHelp: true,
},
{
Name: "export",
Usage: "export configuration",
Flags: []cli.Flag{
cli.StringFlag{
Name: "output, o",
Usage: "File to which to save",
},
cli.BoolFlag{
Name: "private, p",
Usage: "Include the generated private keys",
},
cli.BoolFlag{
Name: "full, f",
Usage: "Export full configuration, including internal and default settings",
},
},
Action: export,
},
{
Name: "merge",
Usage: "merge configuration from stdin",
Action: merge,
Flags: []cli.Flag{
cli.StringFlag{
Name: "input, i",
Usage: "File from which to read",
},
},
},
{
Name: "syslinux",
Usage: "edit Syslinux boot global.cfg",
Action: editSyslinux,
},
{
Name: "validate",
Usage: "validate configuration from stdin",
Action: validate,
Flags: []cli.Flag{
cli.StringFlag{
Name: "input, i",
Usage: "File from which to read",
},
},
},
}
}
func imagesFromConfig(cfg *config.CloudConfig) []string {
imagesMap := map[string]int{}
for _, service := range cfg.Rancher.BootstrapContainers {
imagesMap[service.Image] = 1
}
for _, service := range cfg.Rancher.Services {
imagesMap[service.Image] = 1
}
images := make([]string, len(imagesMap))
i := 0
for image := range imagesMap {
images[i] = image
i++
}
sort.Strings(images)
return images
}
func runImages(c *cli.Context) error {
configFile := c.String("input")
cfg, err := config.ReadConfig(nil, false, configFile)
if err != nil {
log.WithFields(log.Fields{"err": err, "file": configFile}).Fatalf("Could not read config from file")
}
images := imagesFromConfig(cfg)
fmt.Println(strings.Join(images, " "))
return nil
}
func runGenerate(c *cli.Context) error {
if err := genTpl(os.Stdin, os.Stdout); err != nil {
log.Fatalf("Failed to generate config, err: '%s'", err)
}
return nil
}
func genTpl(in io.Reader, out io.Writer) error {
bytes, err := ioutil.ReadAll(in)
if err != nil {
log.Fatal("Could not read from stdin")
}
tpl := template.Must(template.New("osconfig").Parse(string(bytes)))
return tpl.Execute(out, env2map(os.Environ()))
}
func env2map(env []string) map[string]string {
m := make(map[string]string, len(env))
for _, s := range env {
d := strings.Split(s, "=")
m[d[0]] = d[1]
}
return m
}
func editSyslinux(c *cli.Context) error {
// check whether is Raspberry Pi or not
bytes, err := ioutil.ReadFile("/proc/device-tree/model")
if err == nil && strings.Contains(strings.ToLower(string(bytes)), "raspberry") {
buf := bufio.NewWriter(os.Stdout)
fmt.Fprintln(buf, "raspberry pi can not use this command")
buf.Flush()
return errors.New("raspberry pi can not use this command")
}
if isExist := checkGlobalCfg(); !isExist {
buf := bufio.NewWriter(os.Stdout)
fmt.Fprintln(buf, "global.cfg can not be found")
buf.Flush()
return errors.New("global.cfg can not be found")
}
cmd := exec.Command("system-docker", "run", "--rm", "-it",
"-v", "/:/host",
"-w", "/host",
"--entrypoint=nano",
"burmilla/os-console:"+config.Version,
"boot/global.cfg")
cmd.Stdout, cmd.Stderr, cmd.Stdin = os.Stdout, os.Stderr, os.Stdin
return cmd.Run()
}
func configSet(c *cli.Context) error {
if c.NArg() < 2 {
return nil
}
key := c.Args().Get(0)
value := c.Args().Get(1)
if key == "" {
return nil
}
err := config.Set(key, value)
if err != nil {
log.Fatal(err)
}
return nil
}
func configGet(c *cli.Context) error {
arg := c.Args().Get(0)
if arg == "" {
return nil
}
val, err := config.Get(arg)
if err != nil {
log.WithFields(log.Fields{"key": arg, "val": val, "err": err}).Fatal("config get: failed to retrieve value")
}
printYaml := false
switch val.(type) {
case []interface{}:
printYaml = true
case map[interface{}]interface{}:
printYaml = true
}
if printYaml {
bytes, err := yaml.Marshal(val)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(bytes))
} else {
fmt.Println(val)
}
return nil
}
func merge(c *cli.Context) error {
bytes, err := inputBytes(c)
if err != nil {
log.Fatal(err)
}
if err = config.Merge(bytes); err != nil {
log.Error(err)
validationErrors, err := config.ValidateBytes(bytes)
if err != nil {
log.Fatal(err)
}
for _, validationError := range validationErrors.Errors() {
log.Error(validationError)
}
log.Fatal("EXITING: Failed to parse configuration")
}
return nil
}
func export(c *cli.Context) error {
content, err := config.Export(c.Bool("private"), c.Bool("full"))
if err != nil {
log.Fatal(err)
}
output := c.String("output")
if output == "" {
fmt.Println(content)
} else {
err := util.WriteFileAtomic(output, []byte(content), 0400)
if err != nil {
log.Fatal(err)
}
}
return nil
}
func validate(c *cli.Context) error {
bytes, err := inputBytes(c)
if err != nil {
log.Fatal(err)
}
validationErrors, err := config.ValidateBytes(bytes)
if err != nil {
log.Fatal(err)
}
for _, validationError := range validationErrors.Errors() {
log.Error(validationError)
}
return nil
}
func inputBytes(c *cli.Context) ([]byte, error) {
input := os.Stdin
inputFile := c.String("input")
if inputFile != "" {
var err error
input, err = os.Open(inputFile)
if err != nil {
return nil, err
}
defer input.Close()
}
content, err := ioutil.ReadAll(input)
if err != nil {
return nil, err
}
if bytes.Contains(content, []byte{13, 10}) {
return nil, errors.New("file format shouldn't contain CRLF characters")
}
return content, nil
}