Skip to content

Commit

Permalink
chore: extra debug logging around env var replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
iPromKnight committed Jan 6, 2025
1 parent 2fee900 commit 99e382b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
20 changes: 15 additions & 5 deletions src/internal/mount_manager/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os/exec"
"rclone-manager/internal/config"
"rclone-manager/internal/constants"
"rclone-manager/internal/environment"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -42,7 +43,7 @@ func createMountCommand(instance *MountedEndpoint, logger zerolog.Logger) *exec.
fsArg := fmt.Sprintf("%s%s:", constants.Fs, instance.BackendName)
mountPointArg := fmt.Sprintf("%s%s", constants.MountPoint, instance.MountPoint)

payloads, err := constructPayloadsFromCombinedEnvVars(currentRCDEnv, instance.EnvVars, logger)
payloads, err := constructPayloadsFromCombinedEnvVars(currentRCDEnv, instance.Environment, logger)
if err != nil {
logger.Error().AnErr(constants.LogError, err).Str(constants.LogBackend, instance.BackendName).
Msg("Failed to construct mount payloads")
Expand All @@ -57,6 +58,8 @@ func createMountCommand(instance *MountedEndpoint, logger zerolog.Logger) *exec.
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}

cmd.Env = environment.PrepareEnvironment(instance.Environment)

return cmd
}

Expand All @@ -74,12 +77,17 @@ func constructPayloadsFromCombinedEnvVars(options map[string]interface{}, envVar
mountTags := extractConfigTags(&mountlib.Options{}, logger)
vfsTags := extractConfigTags(&vfscommon.Options{}, logger)

logger.Debug().Interface("envVars", envVars).Msg("Augmenting options with environment variables")

vfsEnvs := envVars
mountEnvs := envVars

// Update options using extracted config tags
updateOptionsWithEnv(vfsOptions, envVars, "vfs", vfsTags, logger)
updateOptionsWithEnv(mountOptions, envVars, "mount", mountTags, logger)
updateOptionsWithEnv(vfsOptions, vfsEnvs, "vfs", vfsTags, logger)
logger.Debug().Interface("vfsOptions", vfsOptions).Msg("Updated VFS Options")

updateOptionsWithEnv(mountOptions, mountEnvs, "mount", mountTags, logger)
logger.Debug().Interface("mountOptions", mountOptions).Msg("Updated Mount Options")
logger.Debug().Interface("vfsOptions", vfsOptions).Msg("Updated VFS Options")

vfsPayloadJson, err := json.Marshal(vfsOptions)
if err != nil {
Expand Down Expand Up @@ -116,6 +124,8 @@ func extractConfigTags(opt interface{}, logger zerolog.Logger) map[string]string
}

func updateOptionsWithEnv(options map[string]interface{}, envVars map[string]string, section string, configTags map[string]string, logger zerolog.Logger) {
logger.Debug().Str("section", section).Interface("options", options).Msg("Updating options from environment variables")

for key, value := range envVars {
if strings.HasPrefix(key, fmt.Sprintf("RCLONE_%s_", strings.ToUpper(section))) {
cleanKey := strings.TrimPrefix(key, fmt.Sprintf("RCLONE_%s_", strings.ToUpper(section)))
Expand Down Expand Up @@ -224,7 +234,7 @@ func setupMountsFromConfig(conf *config.Config, logger zerolog.Logger) {
instance := &MountedEndpoint{
BackendName: mount.BackendName,
MountPoint: mount.MountPoint,
EnvVars: mount.Environment,
Environment: mount.Environment,
}
if existing, ok := getMountedEndpoint(mount.BackendName); ok {
if existing.MountPoint != instance.MountPoint {
Expand Down
4 changes: 2 additions & 2 deletions src/internal/mount_manager/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
type MountedEndpoint struct {
BackendName string
MountPoint string
EnvVars map[string]string
Environment map[string]string
}

func SetRCDEnv(env map[string]interface{}) {
Expand All @@ -37,7 +37,7 @@ func InitializeMounts(conf *config.Config, logger zerolog.Logger, processLock *s
instance := &MountedEndpoint{
BackendName: mount.BackendName,
MountPoint: mount.MountPoint,
EnvVars: mount.Environment,
Environment: mount.Environment,
}
StartMountWithRetries(instance, logger)
}
Expand Down
10 changes: 5 additions & 5 deletions src/internal/serve_manager/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func createServeCommand(instance *ServeProcess) *exec.Cmd {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

cmd.Env = environment.PrepareEnvironment(instance.EnvVars)
cmd.Env = environment.PrepareEnvironment(instance.Environment)

return cmd
}
Expand All @@ -42,10 +42,10 @@ func untrackServe(instance *ServeProcess) {
func setupServesFromConfig(conf *config.Config, logger zerolog.Logger) {
for _, serve := range conf.Serves {
instance := &ServeProcess{
Backend: serve.BackendName,
Protocol: serve.Protocol,
Addr: serve.Addr,
EnvVars: serve.Environment,
Backend: serve.BackendName,
Protocol: serve.Protocol,
Addr: serve.Addr,
Environment: serve.Environment,
}
if existing, ok := getServeInstance(serve.BackendName); ok {
if existing.Protocol != serve.Protocol || existing.Addr != serve.Addr {
Expand Down
8 changes: 4 additions & 4 deletions src/internal/serve_manager/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func MonitorServeProcesses(logger zerolog.Logger) {
Msgf("Process (PID: %d) died. Restarting...", serveProcess.PID)

newServe := &ServeProcess{
Backend: serveProcess.Backend,
Protocol: serveProcess.Protocol,
Addr: serveProcess.Addr,
EnvVars: serveProcess.EnvVars,
Backend: serveProcess.Backend,
Protocol: serveProcess.Protocol,
Addr: serveProcess.Addr,
Environment: serveProcess.Environment,
}

newProcess := StartServeWithRetries(newServe, logger)
Expand Down
10 changes: 5 additions & 5 deletions src/internal/serve_manager/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type ServeProcess struct {
Addr string
StartedAt time.Time
GracePeriod time.Duration
EnvVars map[string]string
Environment map[string]string
}

var (
Expand All @@ -41,10 +41,10 @@ func InitializeServeEndpoints(conf *config.Config, logger zerolog.Logger, proces
logger.Info().Msg("Initializing all serve endpoints")
for _, serve := range conf.Serves {
instance := &ServeProcess{
Backend: serve.BackendName,
Protocol: serve.Protocol,
Addr: serve.Addr,
EnvVars: serve.Environment,
Backend: serve.BackendName,
Protocol: serve.Protocol,
Addr: serve.Addr,
Environment: serve.Environment,
}
StartServeWithRetries(instance, logger)
}
Expand Down

0 comments on commit 99e382b

Please sign in to comment.