Skip to content

Commit d3a13c2

Browse files
Chef server ctl external database support (#6643)
* working external support for chef-server Signed-off-by: Vivek Shankar <[email protected]> * changes to support external pg for chef-server-ctl Signed-off-by: Vivek Shankar <[email protected]> * removed logs Signed-off-by: Vivek Shankar <[email protected]> * added bldr changes Signed-off-by: Vivek Shankar <[email protected]>
1 parent e6095da commit d3a13c2

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

.bldr.toml

+5
Original file line numberDiff line numberDiff line change
@@ -891,8 +891,13 @@ paths = [
891891
plan_path = "components/automate-cs-nginx"
892892
paths = [
893893
"components/automate-cs-nginx/*",
894+
"api/config/platform/*",
895+
"api/config/shared/*",
894896
"lib/io/*",
897+
"lib/platform/config/*",
898+
"lib/proxy/*",
895899
"lib/secrets/*",
900+
"lib/stringutils/*",
896901
"lib/user/*"
897902
]
898903

components/automate-cs-nginx/cmd/chef-server-ctl/ctl.go

+38-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/sirupsen/logrus"
2121

22+
platform_config "github.com/chef/automate/lib/platform/config"
2223
"github.com/chef/automate/lib/secrets"
2324
)
2425

@@ -38,6 +39,11 @@ const (
3839
tlsCrt = "/hab/svc/automate-cs-nginx/config/service.crt"
3940
tlsKey = "/hab/svc/automate-cs-nginx/config/service.key"
4041
tlsCA = "/hab/svc/automate-cs-nginx/config/root_ca.crt"
42+
erchefSvcName = "automate-cs-oc-erchef"
43+
erchefDBName = "automate-cs-oc-erchef"
44+
bifrostSvcName = "automate-cs-oc-bifrost"
45+
bifrostDBName = "automate-cs-oc-bifrost"
46+
automateSvcPath = "/hab/svc/"
4147
)
4248

4349
// These paths are injected at BUILD time based on our dependencies to
@@ -163,6 +169,36 @@ func (c passthrough) Run(args []string) error {
163169
bifrostSuperuserID = string(bifrostSecData)
164170
}
165171

172+
svcPath := automateSvcPath
173+
// svc path can be overridden with SVC_PATH env variable in the format: "/hab/svc/"
174+
if path := os.Getenv("SVC_PATH"); path != "" {
175+
svcPath = path
176+
}
177+
178+
erchefDB, err := platform_config.PGURIFromEnvironmentWithParams(erchefDBName, erchefSvcName, svcPath+erchefSvcName, "")
179+
if err != nil {
180+
logrus.WithError(err).Error("could not create pg connection url for erchef")
181+
}
182+
if erchefDB == "" {
183+
erchefDB = erchefDBURI
184+
}
185+
186+
bifrostDB, err := platform_config.PGURIFromEnvironmentWithParams(bifrostDBName, bifrostSvcName, svcPath+bifrostSvcName, "")
187+
if err != nil {
188+
logrus.WithError(err).Error("could not create pg connection url for bifrost")
189+
}
190+
if bifrostDB == "" {
191+
bifrostDB = bifrostDBURI
192+
}
193+
194+
// incase user wants to overide the uri
195+
if erchefDBEnv := os.Getenv("CSC_ERCHEF_DB_URI"); erchefDBEnv != "" {
196+
erchefDB = erchefDBEnv
197+
}
198+
if bifrostDBEnv := os.Getenv("CSC_BIFROST_DB_URI"); bifrostDBEnv != "" {
199+
bifrostDB = bifrostDBEnv
200+
}
201+
166202
// chef-server-ctl has been modified to take all necessary
167203
// config via environment variables. All CSC_ variables are
168204
// chef-server-ctl specific configuration.
@@ -171,8 +207,8 @@ func (c passthrough) Run(args []string) error {
171207
fmt.Sprintf("CSC_LB_URL=%s", lbURL),
172208
fmt.Sprintf("CSC_BIFROST_SUPERUSER_ID=%s", bifrostSuperuserID),
173209
fmt.Sprintf("CSC_BIFROST_URL=%s", bifrostURL),
174-
fmt.Sprintf("CSC_BIFROST_DB_URI=%s", bifrostDBURI),
175-
fmt.Sprintf("CSC_ERCHEF_DB_URI=%s", erchefDBURI),
210+
fmt.Sprintf("CSC_BIFROST_DB_URI=%s", bifrostDB),
211+
fmt.Sprintf("CSC_ERCHEF_DB_URI=%s", erchefDB),
176212
fmt.Sprintf("CSC_ERCHEF_REINDEX_SCRIPT=%s", erchefReindexScript),
177213
fmt.Sprintf("CSC_KNIFE_CONFIG_FILE=%s", knifeConfigFile),
178214
fmt.Sprintf("CSC_TLS_KEY=%s", tlsKey),

lib/platform/config/config.go

+35
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,33 @@ func ConfigFromEnvironment() (*Config, error) {
5959
return &platformConfig, nil
6060
}
6161

62+
func ConfigFromParams(svcName, svcPath, svcDBUser string) (*Config, error) {
63+
64+
if svcName == "" || svcPath == "" {
65+
return nil, ErrNoPlatformEnvironment
66+
}
67+
platformConfigPath := path.Join(svcPath, "config", "_a2_platform.json")
68+
f, err := os.Open(platformConfigPath)
69+
if err != nil {
70+
return nil, errors.Wrapf(err, "Could not read %s", platformConfigPath)
71+
}
72+
defer f.Close() // nolint: errcheck
73+
74+
platformConfig := Config{
75+
Config: &platform.Config{},
76+
SvcDBUser: svcDBUser,
77+
}
78+
unmarshaler := jsonpb.Unmarshaler{
79+
AllowUnknownFields: true,
80+
}
81+
82+
if err := unmarshaler.Unmarshal(f, platformConfig.Config); err != nil {
83+
return nil, errors.Wrapf(err, "Could not decode %s", platformConfigPath)
84+
}
85+
86+
return &platformConfig, nil
87+
}
88+
6289
func PGURIFromEnvironment(database string) (string, error) {
6390
platformConfig, err := ConfigFromEnvironment()
6491
if err != nil {
@@ -67,6 +94,14 @@ func PGURIFromEnvironment(database string) (string, error) {
6794
return platformConfig.GetPGURI(database)
6895
}
6996

97+
func PGURIFromEnvironmentWithParams(database, svc, path, svcDBUser string) (string, error) {
98+
platformConfig, err := ConfigFromParams(svc, path, svcDBUser)
99+
if err != nil {
100+
return "", err
101+
}
102+
return platformConfig.GetPGURI(database)
103+
}
104+
70105
func (c *Config) IsExternalPG() bool {
71106
return c.GetPlatform().GetExternalPostgresql().GetEnable().GetValue()
72107
}

0 commit comments

Comments
 (0)