From 03bfe08e1dd9faf48b06cb146bfa446575cde88a Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 20 Dec 2019 20:18:41 +0000 Subject: chore: Significantly restructure folder layout This moves the various projects from "type-based" folders (such as "services" or "tools") into more appropriate semantic folders (such as "nix", "ops" or "web"). Deprecated projects (nixcon-demo & gotest) which only existed for testing/demonstration purposes have been removed. (Note: *all* builds are broken with this commit) --- ops/sync-gcsr/default.nix | 10 ++++++ ops/sync-gcsr/main.go | 92 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 ops/sync-gcsr/default.nix create mode 100644 ops/sync-gcsr/main.go (limited to 'ops/sync-gcsr') diff --git a/ops/sync-gcsr/default.nix b/ops/sync-gcsr/default.nix new file mode 100644 index 0000000000..114ff221be --- /dev/null +++ b/ops/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/ops/sync-gcsr/main.go b/ops/sync-gcsr/main.go new file mode 100644 index 0000000000..daec76b346 --- /dev/null +++ b/ops/sync-gcsr/main.go @@ -0,0 +1,92 @@ +// 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 updateRepo(repo *git.Repository, tree *git.Worktree, opts *git.PullOptions) error { + err := tree.Pull(opts) + if err == git.NoErrAlreadyUpToDate { + // nothing to do ... + return nil + } else if err != nil { + return err + } + + log.Println("Updated local repository mirror") + return nil +} + +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, false, &cloneOpts) + + if err == git.ErrRepositoryAlreadyExists { + log.Println("Repository has already been cloned!") + handle, err = git.PlainOpen(dest) + action = "open" + } + + if err != nil { + log.Fatalf("Failed to %s repository: %s", action, err) + } else { + log.Println("Initiating update loop") + } + + tree, err := handle.Worktree() + if err != nil { + log.Fatalln("Failed to open repository worktree:", err) + } + + pullOpts := git.PullOptions{ + Auth: cloneOpts.Auth, + Force: true, + } + + for { + if err = updateRepo(handle, tree, &pullOpts); err != nil { + log.Fatalf("Failed to pull updated repository: %s", err) + } + time.Sleep(30 * time.Second) // TODO(tazjin): Config option for pull interval? + } +} -- cgit 1.4.1