sourcegraph/cmd/worker/shared/config.go
Stephen Gutekanst be4f4409a3
Sourcegraph App (single-binary branch) (#46547)
* internal: add service and singleprogram packages
* sg.config.yaml: add single-binary build targets
* internal/env: add a function for clearing environ cache
* internal/{workerutil,metrics}: add a hack to allow running 2 executors in the same process
* internal/conf: add single-program deploy type
* internal/singleprogram: clarify security
* cmd/sourcegraph-oss: add initial single-binary main (will not build yet)
* enterprise/cmd/sourcegraph: initial enterprise single-binary
* Add multi-platform builds for single-program
* single-binary: correctly build JS artifacts into binary
* license_finder licenses add github.com/xi2/xz "Public domain"
* internal/service/svcmain: correctly initialize logger for DeprecatedSingleServiceMain
* worker: refactor to new service pattern
* cmd/github-proxy: refactor to use new service pattern
* symbols: refactor to use new service pattern
* gitserver: refactor to user new service pattern
* searcher: refactor to use new service pattern
* gitserver: refactor to use new service pattern
* repo-updater: refactor to use new service pattern
* frontend: refactor to use new service pattern
* executor: refactor to use new service pattern
* internal/symbols: use new LoadConfig pattern
* precise-code-intel-worker: refactor to use new service pattern
* internal/symbols: load config for tests
* cmd/repo-updater: remove LoadConfig approach
* cmd/symbols: workaround env var conflict with searcher
* executor: internal: add workaround to allow running 2 instances in same process
* executors: add EXECUTOR_QUEUE_DISABLE_ACCESS_TOKEN for single-binary and dev deployments only
* single-binary: use EXECUTOR_QUEUE_DISABLE_ACCESS_TOKEN
* extsvc/github: fix default value for single-program deploy type
* single-binary: stop relying on a local ctags image
* single-binary: use unix sockets for postgres
* release App snapshots in CI when pushed to app/release-snapshot branch
* internal/service/svcmain: update TODO comment
* executor: correct DEPLOY_TYPE check
* dev/check: allow single-binary to import dbconn
* executor: remove accidental reliance on dbconn package
* executor: improve error logging when running commands (#46546)
* executor: improve error logging when running commands
* executor: do not attempt std config validation running e.g. install cmd
* executor: do not pull in the conf package / frontend reliance
* ci: executors: correct site config for passwordless auth
* server: fix bug where github-proxy would try to be a conf server
* CI: executors: fix integration test passwordless auth
* executors: allow passwordless auth in sourcegraph/server for testing
* repo-updater: fix enterprise init (caused regression in repository syncing)

Signed-off-by: Stephen Gutekanst <stephen@sourcegraph.com>
Co-authored-by: Peter Guy <peter.guy@sourcegraph.com>
Co-authored-by: Quinn Slack <quinn@slack.org>
2023-01-19 17:35:39 -07:00

92 lines
2.3 KiB
Go

package shared
import (
"strings"
"github.com/sourcegraph/sourcegraph/cmd/worker/job"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
// Config is the configuration that controls what jobs will be initialized
// and monitored. By default, all jobs are enabled. Individual jobs can be
// explicit allowed or blocked from running on a particular instance.
type Config struct {
env.BaseConfig
names []string
Jobs map[string]job.Job
JobAllowlist []string
JobBlocklist []string
}
var config = &Config{}
// Load reads from the environment and stores the transformed data on the config
// object for later retrieval.
func (c *Config) Load() {
c.JobAllowlist = safeSplit(c.Get(
"WORKER_JOB_ALLOWLIST",
"all",
`A comma-seprated list of names of jobs that should be enabled. The value "all" (the default) enables all jobs.`,
), ",")
c.JobBlocklist = safeSplit(c.Get(
"WORKER_JOB_BLOCKLIST",
"",
"A comma-seprated list of names of jobs that should not be enabled. Values in this list take precedence over the allowlist.",
), ",")
}
// Validate returns an error indicating if there was an invalid environment read
// during Load. The environment is invalid when a supplied job name is not recognized
// by the set of names registered to the worker (at compile time).
//
// This method assumes that the name field has been set externally.
func (c *Config) Validate() error {
allowlist := map[string]struct{}{}
for _, name := range c.names {
allowlist[name] = struct{}{}
}
for _, name := range c.JobAllowlist {
if _, ok := allowlist[name]; !ok && name != "all" {
return errors.Errorf("unknown job %q", name)
}
}
for _, name := range c.JobBlocklist {
if _, ok := allowlist[name]; !ok {
return errors.Errorf("unknown job %q", name)
}
}
return nil
}
// shouldRunJob returns true if the given job should be run.
func shouldRunJob(name string) bool {
for _, candidate := range config.JobBlocklist {
if name == candidate {
return false
}
}
for _, candidate := range config.JobAllowlist {
if candidate == "all" || name == candidate {
return true
}
}
return false
}
// safeSplit is strings.Split but returns nil (not a []string{""}) on empty input.
func safeSplit(text, sep string) []string {
if text == "" {
return nil
}
return strings.Split(text, sep)
}