-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
134 lines (106 loc) · 2.63 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
package config
import (
"encoding/base64"
"fmt"
"io/ioutil"
"strings"
"github.com/deis/deis/deisctl/utils"
)
// fileKeys define config keys to be read from local files
var fileKeys = []string{
"/deis/platform/sshPrivateKey",
"/deis/router/sslCert",
"/deis/router/sslKey"}
// b64Keys define config keys to be base64 encoded before stored
var b64Keys = []string{"/deis/platform/sshPrivateKey"}
// Config runs the config subcommand
func Config(args map[string]interface{}) error {
return doConfig(args)
}
// CheckConfig looks for a value at a keyspace path
// and returns an error if a value is not found
func CheckConfig(root string, k string) error {
client, err := getEtcdClient()
if err != nil {
return err
}
_, err = doConfigGet(client, root, []string{k})
if err != nil {
return err
}
return nil
}
func doConfig(args map[string]interface{}) error {
client, err := getEtcdClient()
if err != nil {
return err
}
rootPath := "/deis/" + args["<target>"].(string) + "/"
var vals []string
if args["set"] == true {
vals, err = doConfigSet(client, rootPath, args["<key=val>"].([]string))
} else {
vals, err = doConfigGet(client, rootPath, args["<key>"].([]string))
}
if err != nil {
return err
}
// print results
for _, v := range vals {
fmt.Printf("%v\n", v)
}
return nil
}
func doConfigSet(client *etcdClient, root string, kvs []string) ([]string, error) {
var result []string
for _, kv := range kvs {
// split k/v from args
split := strings.SplitN(kv, "=", 2)
k, v := split[0], split[1]
// prepare path and value
path := root + k
val, err := valueForPath(path, v)
if err != nil {
return result, err
}
// set key/value in etcd
ret, err := client.Set(path, val)
if err != nil {
return result, err
}
result = append(result, ret)
}
return result, nil
}
func doConfigGet(client *etcdClient, root string, keys []string) ([]string, error) {
var result []string
for _, k := range keys {
val, err := client.Get(root + k)
if err != nil {
return result, err
}
result = append(result, val)
}
return result, nil
}
// valueForPath returns the canonical value for a user-defined path and value
func valueForPath(path string, v string) (string, error) {
// check if path is part of fileKeys
for _, p := range fileKeys {
if path == p {
// read value from filesystem
bytes, err := ioutil.ReadFile(utils.ResolvePath(v))
if err != nil {
return "", err
}
// see if we should return base64 encoded value
for _, pp := range b64Keys {
if path == pp {
return base64.StdEncoding.EncodeToString(bytes), nil
}
}
return string(bytes), nil
}
}
return v, nil
}