about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-12-20T13·16+0000
committerVincent Ambo <tazjin@google.com>2019-12-20T13·16+0000
commit7078bc7a4a2134bb12002144f660f4dbdcbf838d (patch)
tree176e55fa92261f437c02bbae20e4a0093461c6c3
parent43ceaa17e35eac0101a3628143f6b58b67c9067a (diff)
feat(services/sync-gcsr): Add git synchronisation helper r/211
Adds a tiny program that keeps a checkout of a remote git repository
in sync with a folder on the local filesystem.

This is going to be used to mirror the GCSR repository onto a local
disk for cgit serving.
-rw-r--r--services/sync-gcsr/default.nix10
-rw-r--r--services/sync-gcsr/main.go80
2 files changed, 90 insertions, 0 deletions
diff --git a/services/sync-gcsr/default.nix b/services/sync-gcsr/default.nix
new file mode 100644
index 0000000000..114ff221be
--- /dev/null
+++ b/services/sync-gcsr/default.nix
@@ -0,0 +1,10 @@
+{ pkgs, ... }:
+
+pkgs.buildGo.program {
+  name = "sync-gcsr";
+  srcs = [ ./main.go ];
+
+  deps = with pkgs.third_party; map (p: p.gopkg) [
+    gopkgs."gopkg.in".src-d.go-git
+  ];
+}
diff --git a/services/sync-gcsr/main.go b/services/sync-gcsr/main.go
new file mode 100644
index 0000000000..0b295a2c81
--- /dev/null
+++ b/services/sync-gcsr/main.go
@@ -0,0 +1,80 @@
+// Copyright 2019 Google LLC.
+// SPDX-License-Identifier: Apache-2.0
+//
+// sync-gcsr implements a small utility that periodically mirrors a
+// remote Google Cloud Source Repository to a local file path.
+package main
+
+import (
+	"fmt"
+	"log"
+	"os"
+	"time"
+
+	git "gopkg.in/src-d/go-git.v4"
+	"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
+)
+
+func EnvOr(key, def string) string {
+	v := os.Getenv(key)
+	if v == "" {
+		return def
+	}
+
+	return v
+}
+
+func main() {
+	var dest = EnvOr("SYNC_DEST", "/git/depot")
+	var project = EnvOr("SYNC_PROJECT", "tazjins-infrastructure")
+	var repo = EnvOr("SYNC_REPO", "depot")
+	var user = os.Getenv("SYNC_USER")
+	var pass = os.Getenv("SYNC_PASS")
+
+	log.Printf("Syncing repository '%s/%s' to destination '%s'", project, repo, dest)
+
+	var cloneOpts = git.CloneOptions{
+		URL: fmt.Sprintf("https://source.developers.google.com/p/%s/r/%s", project, repo),
+	}
+
+	if user != "" && pass != "" {
+		cloneOpts.Auth = &http.BasicAuth{
+			Username: user,
+			Password: pass,
+		}
+		log.Println("Enabling basic authentication as user", user)
+	}
+
+	action := "clone"
+	handle, err := git.PlainClone(dest, true, &cloneOpts)
+
+	if err == git.ErrRepositoryAlreadyExists {
+		log.Println("Repository has already been cloned!")
+		handle, err = git.PlainOpen(dest)
+		action = "open"
+	}
+
+	if err != nil {
+		log.Fatalln("Failed to %s repository:", action, err)
+	} else {
+		log.Println("Initiating update loop")
+	}
+
+	fetchOpts := git.FetchOptions{
+		Auth: cloneOpts.Auth,
+	}
+
+	for {
+		time.Sleep(30 * time.Second) //  TODO(tazjin): Config option for fetch interval?
+		err = handle.Fetch(&fetchOpts)
+
+		if err == git.NoErrAlreadyUpToDate {
+			// no-op ...
+			err = nil
+		} else if err != nil {
+			log.Fatalf("Failed to fetch updated repository: %s", err)
+		} else {
+			log.Println("Fetched new updates from remote repository")
+		}
+	}
+}