MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / serverMainPersistentConfig

Function serverMainPersistentConfig

rest/main.go:70–166  ·  view source on GitHub ↗

serverMainPersistentConfig runs the Sync Gateway server with persistent config.

(ctx context.Context, fs *flag.FlagSet, flagStartupConfig *StartupConfig, sigChan chan os.Signal)

Source from the content-addressed store, hash-verified

68
69// serverMainPersistentConfig runs the Sync Gateway server with persistent config.
70func serverMainPersistentConfig(ctx context.Context, fs *flag.FlagSet, flagStartupConfig *StartupConfig, sigChan chan os.Signal) (disablePersistentConfigFallback bool, err error) {
71
72 sc := DefaultStartupConfig(defaultLogFilePath)
73 base.TracefCtx(ctx, base.KeyAll, "default config: %#v", sc)
74
75 configPath := fs.Args()
76 if len(configPath) > 1 {
77 return false, fmt.Errorf("%d startup configs defined. Must be at most one startup config: %v", len(configPath), configPath)
78 }
79
80 var fileStartupConfig *StartupConfig
81 var legacyDbUsers map[string]map[string]*auth.PrincipalConfig // [db][user]PrincipleConfig
82 var legacyDbRoles map[string]map[string]*auth.PrincipalConfig // [db][roles]PrincipleConfig
83 if len(configPath) == 1 {
84 fileStartupConfig, err = LoadStartupConfigFromPath(ctx, configPath[0])
85 if pkgerrors.Cause(err) == base.ErrUnknownField {
86 // If we have an unknown field error processing config its possible that the config is a 2.x config
87 // requiring automatic upgrade. We should attempt to perform this upgrade
88
89 base.InfofCtx(ctx, base.KeyAll, "Found unknown fields in startup config. Attempting to read as legacy config.")
90
91 var upgradeError error
92 fileStartupConfig, disablePersistentConfigFallback, legacyDbUsers, legacyDbRoles, upgradeError = automaticConfigUpgrade(ctx, configPath[0])
93 if upgradeError != nil {
94
95 // We need to validate if the error was again, an unknown field error. If this is the case its possible
96 // the config is actually a 3.x config but with a genuine unknown field, therefore we should return the
97 // original error from LoadStartupConfigFromPath.
98 if pkgerrors.Cause(upgradeError) == base.ErrUnknownField {
99 base.WarnfCtx(ctx, "Automatic upgrade attempt failed, %s not recognized as legacy config format: %v", base.MD(configPath[0]), upgradeError)
100 base.WarnfCtx(ctx, "Provided config %s not recognized as bootstrap config format: %v", base.MD(configPath[0]), err)
101 return false, fmt.Errorf("unknown config fields supplied. Unable to continue")
102 }
103
104 return false, upgradeError
105 }
106
107 if disablePersistentConfigFallback {
108 return true, nil
109 }
110
111 } else if err != nil {
112 return false, fmt.Errorf("Couldn't open config file: %w", err)
113 }
114 if fileStartupConfig != nil {
115 redactedConfig, err := sc.Redacted()
116 if err != nil {
117 return false, err
118 }
119 base.TracefCtx(ctx, base.KeyAll, "got config from file: %#v", redactedConfig)
120 err = sc.Merge(fileStartupConfig)
121 if err != nil {
122 return false, err
123 }
124 }
125 }
126
127 // merge flagStartupConfig on top of fileStartupConfig, because flags take precedence over config files.

Callers 1

serverMainFunction · 0.85

Calls 15

TracefCtxFunction · 0.92
InfofCtxFunction · 0.92
WarnfCtxFunction · 0.92
MDFunction · 0.92
DefaultStartupConfigFunction · 0.85
automaticConfigUpgradeFunction · 0.85
getInitialStartupConfigFunction · 0.85
SetupServerContextFunction · 0.85
StartServerFunction · 0.85
ArgsMethod · 0.80
ErrorfMethod · 0.80

Tested by

no test coverage detected