about summary refs log tree commit diff
path: root/ops/besadii/main.go
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-11-29T15·22+0300
committerVincent Ambo <mail@tazj.in>2021-12-02T10·10+0300
commit168114df525f7f9b5fe5377e0a66e213b779a723 (patch)
treed54aeb281d7e0aef8ca2580c2d9d875af0436c42 /ops/besadii/main.go
parentc53d6d34538298657c7b1a6abebeb542c86cc0b7 (diff)
refactor(ops/besadii): Move Sourcegraph config to a file r/3133
Initial step towards moving besadii away from hardcoded values and
onto config files. This is required because I want to reuse besadii
outside of the TVL context.

Change-Id: Id4fa7a49c5d4f876a02b202f04a421ab5ba0dcc4
Diffstat (limited to 'ops/besadii/main.go')
-rw-r--r--ops/besadii/main.go76
1 files changed, 56 insertions, 20 deletions
diff --git a/ops/besadii/main.go b/ops/besadii/main.go
index 5ffe1beeda..3d694800a4 100644
--- a/ops/besadii/main.go
+++ b/ops/besadii/main.go
@@ -8,7 +8,7 @@
 //
 // Gerrit (ref-updated) hook:
 // - Trigger Buildkite CI builds
-// - Trigger SourceGraph (cs.tvl.fyi) repository index updates
+// - Trigger SourceGraph repository index updates
 //
 // Buildkite (post-command) hook:
 // - Submit CL verification status back to Gerrit
@@ -32,6 +32,12 @@ import (
 // Regular expression to extract change ID out of a URL
 var changeIdRegexp = regexp.MustCompile(`^.*/(\d+)$`)
 
+// besadii configuration file structure
+type config struct {
+	SourcegraphUrl   string `json:"sourcegraphUrl"`
+	SourcegraphToken string `json:"sourcegraphToken"`
+}
+
 // buildTrigger represents the information passed to besadii when it
 // is invoked as a Gerrit hook.
 //
@@ -79,6 +85,31 @@ type reviewInput struct {
 	Tag                            string         `json:"tag"`
 }
 
+func loadConfig() (*config, error) {
+	configPath := os.Getenv("BESADII_CONFIG")
+	if configPath == "" {
+		configPath = "/etc/besadii/config.json"
+	}
+
+	configJson, err := ioutil.ReadFile(configPath)
+	if err != nil {
+		return nil, fmt.Errorf("failed to load besadii config: %w", err)
+	}
+
+	var cfg config
+	err = json.Unmarshal(configJson, &cfg)
+	if err != nil {
+		return nil, fmt.Errorf("failed to unmarshal besadii config: %w", err)
+	}
+
+	// Rudimentary config validation logic
+	if cfg.SourcegraphUrl != "" && cfg.SourcegraphToken == "" {
+		return nil, fmt.Errorf("'SourcegraphToken' must be set if 'SourcegraphUrl' is set")
+	}
+
+	return &cfg, nil
+}
+
 // updateGerrit posts a comment on a Gerrit CL to indicate the current build status.
 func updateGerrit(review reviewInput, changeId, patchset string) {
 	body, _ := json.Marshal(review)
@@ -189,19 +220,24 @@ func triggerBuild(log *syslog.Writer, token string, trigger *buildTrigger) error
 	return nil
 }
 
-// Trigger a Sourcegraph repository index update on cs.tvl.fyi.
+// Trigger a Sourcegraph repository index update.
 //
 // https://docs.sourcegraph.com/admin/repo/webhooks
-func triggerIndexUpdate(token string) error {
-	req, err := http.NewRequest("POST", "https://cs.tvl.fyi/.api/repos/depot/-/refresh", nil)
+func triggerIndexUpdate(cfg *config, log *syslog.Writer) error {
+	req, err := http.NewRequest("POST", cfg.SourcegraphUrl, nil)
 	if err != nil {
 		return err
 	}
 
-	req.Header.Add("Authorization", "token "+token)
+	req.Header.Add("Authorization", "token "+cfg.SourcegraphToken)
 
 	_, err = http.DefaultClient.Do(req)
-	return err
+	if err != nil {
+		return fmt.Errorf("failed to trigger Sourcegraph index update: %w", err)
+	}
+
+	log.Info("triggered sourcegraph index update")
+	return nil
 }
 
 // Gerrit passes more flags than we want, but Rob Pike decided[0] in
@@ -291,7 +327,7 @@ func buildTriggerFromChangeMerged() *buildTrigger {
 	return &trigger
 }
 
-func gerritHookMain(log *syslog.Writer, trigger *buildTrigger) {
+func gerritHookMain(cfg *config, log *syslog.Writer, trigger *buildTrigger) {
 	if trigger == nil {
 		// The hook was not for something we care about.
 		os.Exit(0)
@@ -304,24 +340,18 @@ func gerritHookMain(log *syslog.Writer, trigger *buildTrigger) {
 	}
 	buildkiteToken := strings.TrimSpace(string(buildkiteTokenBytes))
 
-	sourcegraphTokenBytes, err := ioutil.ReadFile("/etc/secrets/sourcegraph-token")
-	if err != nil {
-		log.Alert(fmt.Sprintf("sourcegraph token could not be read: %s", err))
-		os.Exit(1)
-	}
-	sourcegraphToken := strings.TrimSpace(string(sourcegraphTokenBytes))
-
 	err = triggerBuild(log, buildkiteToken, trigger)
 
 	if err != nil {
 		log.Err(fmt.Sprintf("failed to trigger Buildkite build: %s", err))
 	}
 
-	err = triggerIndexUpdate(sourcegraphToken)
-	if err != nil {
-		log.Err(fmt.Sprintf("failed to trigger sourcegraph index update: %s", err))
+	if cfg.SourcegraphUrl != "" && trigger.ref == "refs/heads/canon" {
+		err = triggerIndexUpdate(cfg, log)
+		if err != nil {
+			log.Err(fmt.Sprintf("failed to trigger sourcegraph index update: %s", err))
+		}
 	}
-	log.Info("triggered sourcegraph index update")
 }
 
 func gerritPassword() string {
@@ -390,6 +420,12 @@ func main() {
 	log.Info(fmt.Sprintf("besadii called with arguments: %v", os.Args))
 
 	bin := path.Base(os.Args[0])
+	cfg, err := loadConfig()
+
+	if err != nil {
+		log.Crit(fmt.Sprintf("besadii configuration error: %v", err))
+		os.Exit(4)
+	}
 
 	if bin == "patchset-created" {
 		trigger, err := buildTriggerFromPatchsetCreated()
@@ -397,10 +433,10 @@ func main() {
 			log.Crit("failed to parse 'patchset-created' invocation from args")
 			os.Exit(1)
 		}
-		gerritHookMain(log, trigger)
+		gerritHookMain(cfg, log, trigger)
 	} else if bin == "change-merged" {
 		trigger := buildTriggerFromChangeMerged()
-		gerritHookMain(log, trigger)
+		gerritHookMain(cfg, log, trigger)
 	} else if bin == "post-command" {
 		postCommandMain()
 	} else {