diff options
author | Quentin Dufour <quentin@deuxfleurs.fr> | 2020-03-01 20:21:20 +0100 |
---|---|---|
committer | Quentin Dufour <quentin@deuxfleurs.fr> | 2020-03-01 20:21:20 +0100 |
commit | e652a838945b70c95652460c8a303f077eae068b (patch) | |
tree | 610d2d0fb843cc25d2df3914dc0ab685de7c4ce9 /docker/webpull | |
parent | 52e6ba3c0036160ce9c419dd0188624cc55fa2b1 (diff) | |
download | infrastructure-e652a838945b70c95652460c8a303f077eae068b.tar.gz infrastructure-e652a838945b70c95652460c8a303f077eae068b.zip |
[website] move website outside repo and WIP static http server with rebuild option
Diffstat (limited to 'docker/webpull')
-rw-r--r-- | docker/webpull/README.md | 3 | ||||
-rw-r--r-- | docker/webpull/main.go | 80 |
2 files changed, 83 insertions, 0 deletions
diff --git a/docker/webpull/README.md b/docker/webpull/README.md new file mode 100644 index 0000000..386a7ef --- /dev/null +++ b/docker/webpull/README.md @@ -0,0 +1,3 @@ +# webpull + +Webpull allows you to update your live website without deploying a new docker container but by simply calling an URL diff --git a/docker/webpull/main.go b/docker/webpull/main.go new file mode 100644 index 0000000..2161256 --- /dev/null +++ b/docker/webpull/main.go @@ -0,0 +1,80 @@ +package main + +import ( + "fmt" + "io" + "os/exec" + "os" + "log" + "net/http" + "strings" +) + +func myexec(w io.Writer, main string, params ...string) error { + cmd := exec.Command(main, params...) + cmd.Stdout = w + cmd.Stderr = w + err := cmd.Run() + if err != nil { + fmt.Fprintf(w, "Failed to run: %s %s\n", main, strings.Join(params, " ")) + } + return err +} + +func update(w io.Writer) { + fmt.Fprintf(w, "Start update...\n") + _, err := os.Stat("./.git") + if err != nil { + fmt.Fprintf(w, ".git folder does not exist, creating it...\n") + err := myexec(w, "git", "init") + if err != nil { + return + } + } + + err = myexec(w, "git", "remote", "get-url", "origin") + if err != nil { + repo, exists := os.LookupEnv("WEBPULL_REPO") + if !exists { + fmt.Fprintf(w, "You must define WEBPULL_REPO env variable...\n") + return + } + fmt.Fprintf(w, "git remote is not yet set...\n") + err := myexec(w, "git", "remote", "add", "origin", repo) + if err != nil { + return + } + } + + err = myexec(w, "git", "pull", "origin", "master") + if err != nil { + fmt.Fprintf(w, "Failed to pull...\n") + return + } + + _, err = os.Stat("./.webpool") + if err != nil { + fmt.Fprintf(w, "You must create an executable file named '.webpool' at the root of your repository.\nIf you have nothing to run, just create an empty bash script...\n") + return + } + + err = myexec(w, "./.webpool") + if err != nil { + fmt.Fprintf(w, "An error occured during script execution\n") + return + } + + fmt.Fprintf(w, "Success.\n") +} + +func main() { + + update(os.Stdout) + fs := http.FileServer(http.Dir("./static")) + http.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) { + update(w) + }) + http.Handle("/", fs) + + log.Fatal(http.ListenAndServe(":8080", nil)) +} |