aboutsummaryrefslogtreecommitdiff
path: root/docker/webpull
diff options
context:
space:
mode:
Diffstat (limited to 'docker/webpull')
-rw-r--r--docker/webpull/.gitignore1
-rw-r--r--docker/webpull/Dockerfile.pug10
-rw-r--r--docker/webpull/README.md10
-rw-r--r--docker/webpull/main.go100
4 files changed, 121 insertions, 0 deletions
diff --git a/docker/webpull/.gitignore b/docker/webpull/.gitignore
new file mode 100644
index 0000000..ba2906d
--- /dev/null
+++ b/docker/webpull/.gitignore
@@ -0,0 +1 @@
+main
diff --git a/docker/webpull/Dockerfile.pug b/docker/webpull/Dockerfile.pug
new file mode 100644
index 0000000..9600f35
--- /dev/null
+++ b/docker/webpull/Dockerfile.pug
@@ -0,0 +1,10 @@
+FROM node:13.8-buster
+
+RUN apt-get update && \
+ apt-get install -y git && \
+ npm install -g pug-cli jstransformer-markdown-it
+
+COPY ./main /srv/httpd
+WORKDIR /srv
+CMD ["/srv/httpd"]
+
diff --git a/docker/webpull/README.md b/docker/webpull/README.md
new file mode 100644
index 0000000..351d300
--- /dev/null
+++ b/docker/webpull/README.md
@@ -0,0 +1,10 @@
+# webpull
+
+Webpull allows you to update your live website without deploying a new docker container but by simply calling an URL
+
+## Pug version
+
+```
+go build ./main.go
+sudo docker build -f ./Dockerfile.pug -t superboum/amd64_webpull_pug:v1
+```
diff --git a/docker/webpull/main.go b/docker/webpull/main.go
new file mode 100644
index 0000000..46c90b9
--- /dev/null
+++ b/docker/webpull/main.go
@@ -0,0 +1,100 @@
+package main
+
+import (
+ "fmt"
+ "errors"
+ "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) error {
+ 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
+ }
+ }
+
+ 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 errors.New("Missing environment variable WEBPULL_REPO")
+ }
+ fmt.Fprintf(w, "git remote is not yet set...\n")
+ err := myexec(w, "git", "remote", "add", "origin", repo)
+ if err != nil {
+ return err
+ }
+ }
+
+ err = myexec(w, "git", "pull", "origin", "master")
+ if err != nil {
+ fmt.Fprintf(w, "Failed to pull...\n")
+ return err
+ }
+
+ _, err = os.Stat("./.webpull")
+ if err != nil {
+ fmt.Fprintf(w, "You must create an executable file named '.webpull' at the root of your repository.\nIf you have nothing to run, just create an empty bash script...\n")
+ return err
+ }
+
+ err = myexec(w, "./.webpull")
+ if err != nil {
+ fmt.Fprintf(w, "An error occured during script execution\n")
+ return err
+ }
+
+ fmt.Fprintf(w, "Success.\n")
+ return nil
+}
+
+func main() {
+ token, exists := os.LookupEnv("WEBPULL_TOKEN")
+ if !exists {
+ log.Fatal("Environment variable 'WEBPULL_TOKEN' must be defined")
+ }
+
+ if update(os.Stdout) != nil {
+ log.Fatal("Initial 'update' failed")
+ }
+
+ fs := http.FileServer(http.Dir("./static"))
+ http.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) {
+ keys, ok := r.URL.Query()["token"]
+ if !ok || len(keys[0]) < 1 {
+ http.Error(w, "Missing 'token' query parameter", 401)
+ return
+ }
+
+ if keys[0] != token {
+ http.Error(w, "Wrong token", 401)
+ return
+ }
+
+ update(w)
+ })
+ http.Handle("/", fs)
+
+ log.Fatal(http.ListenAndServe(":8080", nil))
+}