|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/deis/deis/pkg/prettyprint" |
| 12 | + |
| 13 | + "github.com/deis/deis/client-go/controller/api" |
| 14 | + "github.com/deis/deis/client-go/controller/models/config" |
| 15 | +) |
| 16 | + |
| 17 | +// ConfigList lists an app's config. |
| 18 | +func ConfigList(appID string, oneLine bool) error { |
| 19 | + c, appID, err := load(appID) |
| 20 | + |
| 21 | + if err != nil { |
| 22 | + return err |
| 23 | + } |
| 24 | + |
| 25 | + config, err := config.List(c, appID) |
| 26 | + |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + |
| 31 | + if oneLine { |
| 32 | + for key, value := range config.Values { |
| 33 | + fmt.Printf("%s=%s ", key, value) |
| 34 | + } |
| 35 | + fmt.Println() |
| 36 | + } else { |
| 37 | + fmt.Printf("=== %s Config\n", appID) |
| 38 | + |
| 39 | + configMap := make(map[string]string) |
| 40 | + |
| 41 | + // config.Values is type interface, so it needs to be converted to a string |
| 42 | + for key, value := range config.Values { |
| 43 | + configMap[key] = value.(string) |
| 44 | + } |
| 45 | + |
| 46 | + fmt.Print(prettyprint.PrettyTabs(configMap, 5)) |
| 47 | + } |
| 48 | + |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +// ConfigSet sets an app's config variables. |
| 53 | +func ConfigSet(appID string, configVars []string) error { |
| 54 | + c, appID, err := load(appID) |
| 55 | + |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + configMap := parseConfig(configVars) |
| 61 | + |
| 62 | + value, ok := configMap["SSH_KEY"] |
| 63 | + |
| 64 | + if ok { |
| 65 | + sshKey := value.(string) |
| 66 | + |
| 67 | + if _, err := os.Stat(value.(string)); err == nil { |
| 68 | + contents, err := ioutil.ReadFile(value.(string)) |
| 69 | + |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + sshKey = string(contents) |
| 75 | + } |
| 76 | + |
| 77 | + sshRegex := regexp.MustCompile("^-.+ .SA PRIVATE KEY-*") |
| 78 | + |
| 79 | + if !sshRegex.MatchString(sshKey) { |
| 80 | + return fmt.Errorf("Could not parse SSH private key:\n %s", sshKey) |
| 81 | + } |
| 82 | + |
| 83 | + configMap["SSH_KEY"] = base64.StdEncoding.EncodeToString([]byte(sshKey)) |
| 84 | + } |
| 85 | + |
| 86 | + fmt.Print("Creating config... ") |
| 87 | + |
| 88 | + quit := progress() |
| 89 | + configObj := api.Config{Values: configMap} |
| 90 | + _, err = config.Set(c, appID, configObj) |
| 91 | + |
| 92 | + quit <- true |
| 93 | + <-quit |
| 94 | + |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + fmt.Print("done\n\n") |
| 100 | + |
| 101 | + return ConfigList(appID, false) |
| 102 | +} |
| 103 | + |
| 104 | +// ConfigUnset removes a config variable from an app. |
| 105 | +func ConfigUnset(appID string, configVars []string) error { |
| 106 | + c, appID, err := load(appID) |
| 107 | + |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + fmt.Print("Removing config... ") |
| 113 | + |
| 114 | + quit := progress() |
| 115 | + |
| 116 | + configObj := api.Config{} |
| 117 | + |
| 118 | + valuesMap := make(map[string]interface{}) |
| 119 | + |
| 120 | + for _, configVar := range configVars { |
| 121 | + valuesMap[configVar] = nil |
| 122 | + } |
| 123 | + |
| 124 | + configObj.Values = valuesMap |
| 125 | + |
| 126 | + _, err = config.Set(c, appID, configObj) |
| 127 | + |
| 128 | + quit <- true |
| 129 | + <-quit |
| 130 | + |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + |
| 135 | + fmt.Print("done\n\n") |
| 136 | + |
| 137 | + return ConfigList(appID, false) |
| 138 | +} |
| 139 | + |
| 140 | +// ConfigPull pulls an app's config to a file. |
| 141 | +func ConfigPull(appID string, interactive bool, overwrite bool) error { |
| 142 | + filename := ".env" |
| 143 | + |
| 144 | + if !overwrite { |
| 145 | + if _, err := os.Stat(filename); err == nil { |
| 146 | + return fmt.Errorf("%s already exists, pass -o to overwrite", filename) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + c, appID, err := load(appID) |
| 151 | + |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + |
| 156 | + configVars, err := config.List(c, appID) |
| 157 | + |
| 158 | + if interactive { |
| 159 | + contents, err := ioutil.ReadFile(filename) |
| 160 | + |
| 161 | + if err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + localConfigVars := strings.Split(string(contents), "\n") |
| 165 | + |
| 166 | + configMap := parseConfig(localConfigVars[:len(localConfigVars)-1]) |
| 167 | + |
| 168 | + for key, value := range configVars.Values { |
| 169 | + localValue, ok := configMap[key] |
| 170 | + |
| 171 | + if ok { |
| 172 | + if value != localValue { |
| 173 | + var confirm string |
| 174 | + fmt.Printf("%s: overwrite %s with %s? (y/N) ", key, localValue, value) |
| 175 | + |
| 176 | + fmt.Scanln(&confirm) |
| 177 | + |
| 178 | + if strings.ToLower(confirm) == "y" { |
| 179 | + configMap[key] = value |
| 180 | + } |
| 181 | + } |
| 182 | + } else { |
| 183 | + configMap[key] = value |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + return ioutil.WriteFile(filename, []byte(formatConfig(configMap)), 0755) |
| 188 | + } |
| 189 | + |
| 190 | + return ioutil.WriteFile(filename, []byte(formatConfig(configVars.Values)), 0755) |
| 191 | +} |
| 192 | + |
| 193 | +// ConfigPush pushes an app's config from a file. |
| 194 | +func ConfigPush(appID string, fileName string) error { |
| 195 | + contents, err := ioutil.ReadFile(fileName) |
| 196 | + |
| 197 | + if err != nil { |
| 198 | + return err |
| 199 | + } |
| 200 | + |
| 201 | + config := strings.Split(string(contents), "\n") |
| 202 | + return ConfigSet(appID, config[:len(config)-1]) |
| 203 | +} |
| 204 | + |
| 205 | +func parseConfig(configVars []string) map[string]interface{} { |
| 206 | + configMap := make(map[string]interface{}) |
| 207 | + |
| 208 | + regex := regexp.MustCompile("^([A-z_]+[A-z0-9_]*)=(.+)$") |
| 209 | + for _, config := range configVars { |
| 210 | + if regex.MatchString(config) { |
| 211 | + captures := regex.FindStringSubmatch(config) |
| 212 | + configMap[captures[1]] = captures[2] |
| 213 | + } else { |
| 214 | + fmt.Printf("'%s' does not match the pattern 'key=var', ex: MODE=test\n", config) |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + return configMap |
| 219 | +} |
| 220 | + |
| 221 | +func formatConfig(configVars map[string]interface{}) string { |
| 222 | + var formattedConfig string |
| 223 | + |
| 224 | + for key, value := range configVars { |
| 225 | + formattedConfig += fmt.Sprintf("%s=%s\n", key, value) |
| 226 | + } |
| 227 | + |
| 228 | + return formattedConfig |
| 229 | +} |
0 commit comments