This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare_context.go
101 lines (88 loc) · 3.38 KB
/
prepare_context.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
package commands
import (
"github.com/knadh/koanf/maps"
xfs "github.com/saitho/golang-extended-fs/v2"
logger "github.com/sirupsen/logrus"
"github.com/spf13/cast"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
"github.com/getstackhead/stackhead/config"
"github.com/getstackhead/stackhead/project"
"github.com/getstackhead/stackhead/system"
container_docker "github.com/getstackhead/stackhead/modules/container/docker"
dns_cloudflare "github.com/getstackhead/stackhead/modules/dns/cloudflare"
plugin_portainer "github.com/getstackhead/stackhead/modules/plugin/portainer"
proxy_caddy "github.com/getstackhead/stackhead/modules/proxy/caddy"
proxy_nginx "github.com/getstackhead/stackhead/modules/proxy/nginx"
)
func EnforceSimpleValueOption(name string, value map[string]interface{}) {
stringMap := viper.GetStringMap(name)
for mapKey, mapValue := range value {
stringMap[mapKey] = mapValue
logger.Warnf("Enforcing setting \"%s.%s=%v\" via remote server configuration", name, mapKey, mapValue)
}
viper.Set(name, stringMap)
}
func EnforceNestedValueOption(name string, value map[string]interface{}) {
modulesConfigMap := viper.GetStringMap(name)
for moduleName, moduleConfig := range cast.ToStringMap(value) {
remoteSettings := cast.ToStringMap(moduleConfig)
logMessage := "Enforcing module settings for \"" + moduleName + "\" via remote server configuration"
// todo: uncomment when we can sanitize secrets in CLI output
//for k, v := range remoteSettings {
// logMessage += fmt.Sprintf("\n"+k+"=%v", v)
//}
logger.Warn(logMessage)
newMap := cast.ToStringMap(modulesConfigMap[moduleName])
maps.Merge(remoteSettings, newMap)
modulesConfigMap[moduleName] = newMap
}
viper.Set(name, modulesConfigMap)
}
func PrepareContext(host string, action string, projectDefinition *project.Project) {
system.InitializeContext(host, action, projectDefinition)
// Check remote config on server
hasFile, _ := xfs.HasFile("ssh://" + config.GetServerConfigFilePath())
if hasFile {
fileContent, err := xfs.ReadFile("ssh://" + config.GetServerConfigFilePath())
if err != nil {
panic("Found remote config file but was unable to read it: " + err.Error())
}
var c map[string]interface{}
if err = yaml.Unmarshal([]byte(fileContent), &c); err != nil {
panic("Found remote config file but was unable to parse it: " + err.Error())
}
// Enforce remote configurations on viper
EnforceSimpleValueOption("modules", cast.ToStringMap(c["modules"]))
EnforceNestedValueOption("modules_config", cast.ToStringMap(c["modules_config"]))
}
// set proxy
switch viper.GetStringMapString("modules")["proxy"] {
case "nginx":
system.ContextSetProxyModule(proxy_nginx.Module{})
default: // use Caddy as default
system.ContextSetProxyModule(proxy_caddy.Module{})
}
// set container
switch viper.GetStringMapString("modules")["container"] {
default: // use Docker as default
system.ContextSetContainerModule(container_docker.Module{})
}
// set DNS
dnsNames := viper.GetStringMapStringSlice("modules")["dns"]
for _, dnsName := range dnsNames {
switch dnsName {
case "cloudflare":
system.ContextAddDnsModule(dns_cloudflare.Module{})
}
}
// set plugin
pluginNames := viper.GetStringMapStringSlice("modules")["plugins"]
for _, pluginName := range pluginNames {
switch pluginName {
case "portainer":
system.ContextAddPluginModule(plugin_portainer.Module{})
}
}
// todo: validate modules
}