|
1 | 1 | package commands |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "github.com/knadh/koanf/maps" |
| 5 | + xfs "github.com/saitho/golang-extended-fs/v2" |
| 6 | + logger "github.com/sirupsen/logrus" |
| 7 | + "github.com/spf13/cast" |
4 | 8 | "github.com/spf13/viper" |
| 9 | + "gopkg.in/yaml.v3" |
| 10 | + |
| 11 | + "github.com/getstackhead/stackhead/config" |
| 12 | + "github.com/getstackhead/stackhead/project" |
| 13 | + "github.com/getstackhead/stackhead/system" |
5 | 14 |
|
6 | 15 | container_docker "github.com/getstackhead/stackhead/modules/container/docker" |
7 | 16 | dns_cloudflare "github.com/getstackhead/stackhead/modules/dns/cloudflare" |
8 | 17 | plugin_portainer "github.com/getstackhead/stackhead/modules/plugin/portainer" |
9 | 18 | proxy_caddy "github.com/getstackhead/stackhead/modules/proxy/caddy" |
10 | 19 | proxy_nginx "github.com/getstackhead/stackhead/modules/proxy/nginx" |
11 | | - "github.com/getstackhead/stackhead/project" |
12 | | - "github.com/getstackhead/stackhead/system" |
13 | 20 | ) |
14 | 21 |
|
| 22 | +func EnforceSimpleValueOption(name string, value map[string]interface{}) { |
| 23 | + stringMap := viper.GetStringMap(name) |
| 24 | + for mapKey, mapValue := range value { |
| 25 | + stringMap[mapKey] = mapValue |
| 26 | + logger.Warnf("Enforcing setting \"%s.%s=%v\" via remote server configuration", name, mapKey, mapValue) |
| 27 | + } |
| 28 | + viper.Set(name, stringMap) |
| 29 | +} |
| 30 | + |
| 31 | +func EnforceNestedValueOption(name string, value map[string]interface{}) { |
| 32 | + modulesConfigMap := viper.GetStringMap(name) |
| 33 | + for moduleName, moduleConfig := range cast.ToStringMap(value) { |
| 34 | + remoteSettings := cast.ToStringMap(moduleConfig) |
| 35 | + logMessage := "Enforcing module settings for \"" + moduleName + "\" via remote server configuration" |
| 36 | + // todo: uncomment when we can sanitize secrets in CLI output |
| 37 | + //for k, v := range remoteSettings { |
| 38 | + // logMessage += fmt.Sprintf("\n"+k+"=%v", v) |
| 39 | + //} |
| 40 | + logger.Warn(logMessage) |
| 41 | + newMap := cast.ToStringMap(modulesConfigMap[moduleName]) |
| 42 | + maps.Merge(remoteSettings, newMap) |
| 43 | + modulesConfigMap[moduleName] = newMap |
| 44 | + } |
| 45 | + viper.Set(name, modulesConfigMap) |
| 46 | +} |
| 47 | + |
15 | 48 | func PrepareContext(host string, action string, projectDefinition *project.Project) { |
16 | 49 | system.InitializeContext(host, action, projectDefinition) |
17 | 50 |
|
| 51 | + // Check remote config on server |
| 52 | + hasFile, _ := xfs.HasFile("ssh://" + config.GetServerConfigFilePath()) |
| 53 | + if hasFile { |
| 54 | + fileContent, err := xfs.ReadFile("ssh://" + config.GetServerConfigFilePath()) |
| 55 | + if err != nil { |
| 56 | + panic("Found remote config file but was unable to read it: " + err.Error()) |
| 57 | + } |
| 58 | + var c map[string]interface{} |
| 59 | + if err = yaml.Unmarshal([]byte(fileContent), &c); err != nil { |
| 60 | + panic("Found remote config file but was unable to parse it: " + err.Error()) |
| 61 | + } |
| 62 | + |
| 63 | + // Enforce remote configurations on viper |
| 64 | + EnforceSimpleValueOption("modules", cast.ToStringMap(c["modules"])) |
| 65 | + EnforceNestedValueOption("modules_config", cast.ToStringMap(c["modules_config"])) |
| 66 | + EnforceSimpleValueOption("terraform", cast.ToStringMap(c["terraform"])) |
| 67 | + } |
| 68 | + |
18 | 69 | // set proxy |
19 | 70 | switch viper.GetStringMapString("modules")["proxy"] { |
20 | 71 | case "nginx": |
|
0 commit comments