-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.go
More file actions
127 lines (105 loc) · 2.47 KB
/
config.go
File metadata and controls
127 lines (105 loc) · 2.47 KB
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
package config
import (
"encoding/base64"
"fmt"
"io/ioutil"
"strings"
docopt "github.com/docopt/docopt-go"
)
// Config runs the config subcommand
func Config() error {
usage := `Deis Cluster Configuration
Usage:
deisctl config <target> get [<key>...] [options]
deisctl config <target> set <key=val>... [options]
Options:
--verbose print out the request bodies [default: false]
`
// parse command-line arguments
args, err := docopt.Parse(usage, nil, true, "", true)
if err != nil {
return err
}
err = setConfigFlags(args)
if err != nil {
return err
}
return doConfig(args)
}
// Flags for config package
var Flags struct {
}
func setConfigFlags(args map[string]interface{}) error {
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.Split(kv, "=")
if len(split) != 2 {
return result, fmt.Errorf("invalid argument: %v", kv)
}
k, v := split[0], split[1]
// prepare path and value
path := root + k
var val string
// special handling for sshKey
if path == "/deis/platform/sshPrivateKey" {
b64, err := readSSHPrivateKey(v)
if err != nil {
return result, err
}
val = b64
} else {
val = v
}
// 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
}
// readSSHPrivateKey reads the key file and returns a base64 encoded string
func readSSHPrivateKey(path string) (string, error) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(bytes), nil
}