Skip to content

Commit a6b7b35

Browse files
authored
Merge pull request #3737 from ActiveState/CP-1106
Remote installer respects and forwards config-set
2 parents 58e94d3 + 7513962 commit a6b7b35

File tree

3 files changed

+88
-59
lines changed

3 files changed

+88
-59
lines changed

cmd/state-installer/cmd.go

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ func main() {
7272
exitCode = 1
7373
}
7474

75-
if err := events.WaitForEvents(5*time.Second, rollbar.Wait, an.Wait, logging.Close); err != nil {
75+
ev := []func(){rollbar.Wait, logging.Close}
76+
if an != nil {
77+
ev = append(ev, an.Wait)
78+
}
79+
if err := events.WaitForEvents(5*time.Second, ev...); err != nil {
7680
logging.Warning("state-installer failed to wait for events: %v", err)
7781
}
7882

@@ -98,6 +102,22 @@ func main() {
98102
multilog.Critical("Could not set up configuration handler: " + errs.JoinMessage(err))
99103
fmt.Fprintln(os.Stderr, err.Error())
100104
exitCode = 1
105+
return
106+
}
107+
108+
// Set config as early as possible to ensure we respect the values
109+
configArgs := []string{}
110+
for i, arg := range os.Args[1:] {
111+
if arg == "--config-set" && i+1 < len(os.Args[1:]) {
112+
configArgs = append(configArgs, os.Args[1:][i+1])
113+
}
114+
}
115+
116+
if err := cfg.ApplyArgs(configArgs); err != nil {
117+
multilog.Critical("Could not apply config: " + errs.JoinMessage(err))
118+
fmt.Fprintln(os.Stderr, err.Error())
119+
exitCode = 1
120+
return
101121
}
102122

103123
rollbar.SetConfig(cfg)
@@ -142,22 +162,10 @@ func main() {
142162
break
143163
}
144164

145-
// Parse command line arguments manually to extract config settings before analytics
146-
params := newParams()
147-
148-
for i, arg := range processedArgs[1:] {
149-
if arg == "--config-set" && i+1 < len(processedArgs[1:]) {
150-
params.configSettings = append(params.configSettings, processedArgs[1:][i+1])
151-
}
152-
}
153-
154-
if err := applyConfigSettings(cfg, params.configSettings); err != nil {
155-
logging.Warning("Could not apply config settings before analytics: %s", errs.JoinMessage(err))
156-
}
157-
158165
an = sync.New(anaConst.SrcStateInstaller, cfg, nil, out)
159166
an.Event(anaConst.CatInstallerFunnel, "start")
160167

168+
params := newParams()
161169
cmd := captain.NewCommand(
162170
"state-installer",
163171
"",
@@ -522,44 +530,3 @@ func assertCompatibility() error {
522530

523531
return nil
524532
}
525-
526-
func applyConfigSettings(cfg *config.Instance, configSettings []string) error {
527-
for _, setting := range configSettings {
528-
setting = strings.TrimSpace(setting)
529-
if setting == "" {
530-
continue // Skip empty settings
531-
}
532-
if err := applyConfigSetting(cfg, setting); err != nil {
533-
return errs.Wrap(err, "Failed to apply config setting: %s", setting)
534-
}
535-
}
536-
return nil
537-
}
538-
539-
func applyConfigSetting(cfg *config.Instance, setting string) error {
540-
var key, valueStr string
541-
542-
if strings.Contains(setting, "=") {
543-
parts := strings.SplitN(setting, "=", 2)
544-
if len(parts) == 2 {
545-
key = strings.TrimSpace(parts[0])
546-
valueStr = strings.TrimSpace(parts[1])
547-
}
548-
}
549-
550-
if key == "" || valueStr == "" {
551-
return locale.NewInputError("err_config_invalid_format", "Config setting must be in 'key=value' format: {{.V0}}", setting)
552-
}
553-
554-
// Store the raw string value without type validation since config options
555-
// are not yet registered in the installer context
556-
err := cfg.Set(key, valueStr)
557-
if err != nil {
558-
// Log the error but don't fail the installation for config issues
559-
logging.Warning("Could not set config value %s=%s: %s", key, valueStr, errs.JoinMessage(err))
560-
return locale.WrapError(err, "err_config_set", "Could not set value {{.V0}} for key {{.V1}}", valueStr, key)
561-
}
562-
563-
logging.Debug("Config setting applied: %s=%s", key, valueStr)
564-
return nil
565-
}

cmd/state-remote-installer/main.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Params struct {
3737
force bool
3838
version string
3939
nonInteractive bool
40+
configSettings []string
4041
}
4142

4243
func newParams() *Params {
@@ -58,13 +59,18 @@ func main() {
5859
exitCode = 1
5960
}
6061

61-
if err := cfg.Close(); err != nil {
62-
logging.Error("Failed to close config: %w", err)
62+
ev := []func(){rollbar.Wait, logging.Close}
63+
if an != nil {
64+
ev = append(ev, an.Wait)
6365
}
64-
65-
if err := events.WaitForEvents(5*time.Second, rollbar.Wait, an.Wait, logging.Close); err != nil {
66+
if err := events.WaitForEvents(5*time.Second, ev...); err != nil {
6667
logging.Warning("state-remote-installer failed to wait for events: %v", err)
6768
}
69+
70+
if cfg != nil {
71+
events.Close("config", cfg.Close)
72+
}
73+
6874
os.Exit(exitCode)
6975
}()
7076

@@ -84,6 +90,18 @@ func main() {
8490
exitCode = 1
8591
}
8692

93+
// Set config as early as possible to ensure we respect the values
94+
configArgs := []string{}
95+
for i, arg := range os.Args[1:] {
96+
if arg == "--config-set" && i+1 < len(os.Args[1:]) {
97+
configArgs = append(configArgs, os.Args[1:][i+1])
98+
}
99+
}
100+
101+
if err := cfg.ApplyArgs(configArgs); err != nil {
102+
logging.Warning("Could not apply config settings before analytics: %s", errs.JoinMessage(err))
103+
}
104+
87105
rollbar.SetConfig(cfg)
88106

89107
// Set up output handler
@@ -154,6 +172,11 @@ func main() {
154172
Hidden: true,
155173
Value: &params.nonInteractive,
156174
},
175+
{
176+
Name: "config-set",
177+
Description: "Set config values in 'key=value' format, can be specified multiple times",
178+
Value: &params.configSettings,
179+
},
157180
},
158181
[]*captain.Argument{},
159182
func(ccmd *captain.Command, args []string) error {
@@ -224,6 +247,11 @@ func execute(out output.Outputer, prompt prompt.Prompter, cfg *config.Instance,
224247
if params.force {
225248
args = append(args, "--force") // forward to installer
226249
}
250+
if len(params.configSettings) > 0 {
251+
for _, setting := range params.configSettings {
252+
args = append(args, "--config-set", setting)
253+
}
254+
}
227255
env := []string{
228256
constants.InstallerNoSubshell + "=true",
229257
}

internal/config/instance.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"os"
77
"path/filepath"
8+
"strings"
89
"sync"
910
"time"
1011

@@ -258,3 +259,36 @@ func (i *Instance) GetStringMap(key string) map[string]interface{} {
258259
func (i *Instance) ConfigPath() string {
259260
return i.appDataDir
260261
}
262+
263+
// ApplyArgs applies command line arguments to the config instance
264+
// These take the format of 'key=value'
265+
func (i *Instance) ApplyArgs(args []string) error {
266+
for _, setting := range args {
267+
setting = strings.TrimSpace(setting)
268+
if setting == "" {
269+
continue // Skip empty settings
270+
}
271+
var key, valueStr string
272+
273+
if strings.Contains(setting, "=") {
274+
parts := strings.SplitN(setting, "=", 2)
275+
if len(parts) == 2 {
276+
key = strings.TrimSpace(parts[0])
277+
valueStr = strings.TrimSpace(parts[1])
278+
}
279+
}
280+
281+
if key == "" || valueStr == "" {
282+
return errs.New("Config setting must be in 'key=value' format: %s", setting)
283+
}
284+
285+
// Store the raw string value without type validation since config options
286+
// are not yet registered in the installer context
287+
err := i.Set(key, valueStr)
288+
if err != nil {
289+
// Log the error but don't fail the installation for config issues
290+
return errs.Wrap(err, "Could not set value %s for key %s", valueStr, key)
291+
}
292+
}
293+
return nil
294+
}

0 commit comments

Comments
 (0)