mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 15:58:37 +00:00
contrib: add git-sync container
This commit is contained in:
parent
9b6aec5e22
commit
0003d5d983
4
contrib/git-sync/Dockerfile
Normal file
4
contrib/git-sync/Dockerfile
Normal file
@ -0,0 +1,4 @@
|
||||
FROM golang:1.4-onbuild
|
||||
VOLUME ["/git"]
|
||||
CMD ["-dest", "/git"]
|
||||
ENTRYPOINT ["/go/bin/git-sync"]
|
16
contrib/git-sync/README.md
Normal file
16
contrib/git-sync/README.md
Normal file
@ -0,0 +1,16 @@
|
||||
# git-sync
|
||||
|
||||
git-sync is a command that periodically sync a git repository to a local directory.
|
||||
|
||||
It can be used to source a container volume with the content of a git repo.
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
# build the container
|
||||
docker build -t git-sync .
|
||||
# run the git-sync container
|
||||
docker run -d -e INTERVAL=1s -e REPO=https://github.com/GoogleCloudPlatform/kubernetes -e BRANCH=gh-pages -v /git-data:/usr/share/nginx/html git-sync
|
||||
# run a nginx container to serve sync'ed content
|
||||
docker run -d -p 8080:80 -v /git-data:/var/www nginx
|
||||
```
|
72
contrib/git-sync/main.go
Normal file
72
contrib/git-sync/main.go
Normal file
@ -0,0 +1,72 @@
|
||||
package main // import "github.com/GoogleCloudPlatform/kubernetes/git-sync"
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var interval = flag.String("interval", env("INTERVAL", "60s"), "git pull interval")
|
||||
var repo = flag.String("repo", env("REPO", ""), "git repo url")
|
||||
var branch = flag.String("branch", env("BRANCH", "master"), "git branch")
|
||||
var hook = flag.String("hook", env("HOOK", "/"), "web hook path")
|
||||
var dest = flag.String("dest", env("DEST", ""), "destination path")
|
||||
|
||||
func env(key, def string) string {
|
||||
if env := os.Getenv(key); env != "" {
|
||||
return env
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
const usage = "usage: REPO= DEST= [INTERVAL= BRANCH= HOOK=] git-sync -repo GIT_REPO_URL -dest PATH [-interval -branch -hook]"
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
if *repo == "" || *dest == "" {
|
||||
flag.Usage()
|
||||
log.Fatal(usage)
|
||||
}
|
||||
pullInterval, err := time.ParseDuration(*interval)
|
||||
if err != nil {
|
||||
log.Fatalf("error parsing time duration %q: %v", *interval, err)
|
||||
}
|
||||
if _, err := exec.LookPath("git"); err != nil {
|
||||
log.Fatalf("required git executable not found: %v", err)
|
||||
}
|
||||
go func() {
|
||||
for _ = range time.Tick(pullInterval) {
|
||||
gitSync()
|
||||
}
|
||||
}()
|
||||
http.HandleFunc(*hook, func(w http.ResponseWriter, r *http.Request) {
|
||||
gitSync()
|
||||
})
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
func gitSync() {
|
||||
if _, err := os.Stat(path.Join(*dest, ".git")); os.IsNotExist(err) {
|
||||
cmd := exec.Command("git", "clone", "-b", *branch, *repo, *dest)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Printf("command %q : %v", strings.Join(cmd.Args, " "), err)
|
||||
return
|
||||
}
|
||||
log.Println(string(output))
|
||||
return
|
||||
}
|
||||
cmd := exec.Command("git", "pull", "origin", *branch)
|
||||
cmd.Dir = *dest
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Printf("command %q : %v", strings.Join(cmd.Args, " "), err)
|
||||
return
|
||||
}
|
||||
log.Println(string(output))
|
||||
}
|
Loading…
Reference in New Issue
Block a user