Skip to content

Commit

Permalink
Merge pull request #119 from andywaltlova/feat/pass-loglevel-to-script
Browse files Browse the repository at this point in the history
Pass log level to executed script for more granular logging possibility
  • Loading branch information
r0x0d authored Apr 12, 2024
2 parents 4407b0e + b96326d commit 3eab527
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions rhc-worker-script.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ env:
FOO: "some-string-value"
BAR: "other-string-value"
# ...

# Script log level
script_log_level: "info"
7 changes: 5 additions & 2 deletions src/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func setEnvVariablesForCommand(cmd *exec.Cmd, variables map[string]string) {
prefixedKey := getEnvVarName(key)
envVarSetString := fmt.Sprintf("%s=%s", prefixedKey, value)
cmd.Env = append(cmd.Env, envVarSetString)
log.Infoln("Successfully set env variable ", prefixedKey)
log.Infoln("Successfully set env variable", prefixedKey)
}
}

Expand Down Expand Up @@ -126,8 +126,11 @@ func processSignedScript(incomingContent []byte) string {
cmd := exec.Command(yamlContent.Vars.Interpreter, scriptFileName) //nolint:gosec
cmd.Env = os.Environ()

variables := yamlContent.Vars.ContentVars
variables["LOG_LEVEL"] = *config.ScriptLogLevel

// Set env vars from yaml envelope
setEnvVariablesForCommand(cmd, yamlContent.Vars.ContentVars)
setEnvVariablesForCommand(cmd, variables)

// Set env vars from config
if config.Env != nil {
Expand Down
2 changes: 2 additions & 0 deletions src/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ func TestProcessSignedScript(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
scriptLogLevel := "info"
shouldVerifyYaml := tc.verifyYAML
temporaryWorkerDirectory := t.TempDir()
envMap := map[string]string{"NAME": "Test"}
config = &Config{
VerifyYAML: &shouldVerifyYaml,
TemporaryWorkerDirectory: &temporaryWorkerDirectory,
Env: &envMap,
ScriptLogLevel: &scriptLogLevel,
}

defer os.RemoveAll(temporaryWorkerDirectory)
Expand Down
2 changes: 2 additions & 0 deletions src/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,14 @@ func TestProcessData(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
scriptLogLevel := "info"
shouldVerifyYaml := false
temporaryWorkerDirectory := t.TempDir()
config = &Config{
VerifyYAML: &shouldVerifyYaml,
TemporaryWorkerDirectory: &temporaryWorkerDirectory,
Env: &map[string]string{"NAME": "Test"},
ScriptLogLevel: &scriptLogLevel,
}

returnURL := "bar"
Expand Down
11 changes: 11 additions & 0 deletions src/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type Config struct {
VerifyYAML *bool `yaml:"verify_yaml,omitempty"`
TemporaryWorkerDirectory *string `yaml:"temporary_worker_directory,omitempty"`
Env *map[string]string `yaml:"env,omitempty"`
ScriptLogLevel *string `yaml:"script_log_level"`
}

// Set default values for the Config struct
Expand Down Expand Up @@ -127,6 +128,16 @@ func setDefaultValues(config *Config) {
log.Infof("config 'env' value is empty default value (%s) will be used", defaultEnvMap)
config.Env = &defaultEnvMap
}

if config.ScriptLogLevel == nil {
defaultLogLevel := "info"
yggdLogLevel, ok := os.LookupEnv("YGG_LOG_LEVEL")
if ok {
config.ScriptLogLevel = &yggdLogLevel
} else {
config.ScriptLogLevel = &defaultLogLevel
}
}
}

// Load yaml config, if file doesn't exist or is invalid yaml then empty Config is returned
Expand Down
8 changes: 4 additions & 4 deletions src/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ func TestSetDefaultValues(t *testing.T) {
}{
{
name: "test default values",
args: args{config: &Config{nil, nil, nil, nil}},
want: args{config: &Config{strPtr("rhc-worker-script"), boolPtr(true), strPtr("/var/lib/rhc-worker-script"), mapStrPtr(map[string]string{})}},
args: args{config: &Config{nil, nil, nil, nil, nil}},
want: args{config: &Config{strPtr("rhc-worker-script"), boolPtr(true), strPtr("/var/lib/rhc-worker-script"), mapStrPtr(map[string]string{}), strPtr("INFO")}},
},
{
name: "test non default values",
args: args{config: &Config{strPtr("rhc-worker-script"), boolPtr(true), strPtr("/var/lib/rhc-worker-script"), mapStrPtr(map[string]string{"ENV_VAR1": "value1"})}},
want: args{config: &Config{strPtr("rhc-worker-script"), boolPtr(true), strPtr("/var/lib/rhc-worker-script"), mapStrPtr(map[string]string{"ENV_VAR1": "value1"})}},
args: args{config: &Config{strPtr("rhc-worker-script"), boolPtr(true), strPtr("/var/lib/rhc-worker-script"), mapStrPtr(map[string]string{"ENV_VAR1": "value1"}), strPtr("INFO")}},
want: args{config: &Config{strPtr("rhc-worker-script"), boolPtr(true), strPtr("/var/lib/rhc-worker-script"), mapStrPtr(map[string]string{"ENV_VAR1": "value1"}), strPtr("INFO")}},
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 3eab527

Please sign in to comment.