From 8ef40ec59cfc814167df6bd2dd7f3c259e5f6b45 Mon Sep 17 00:00:00 2001
From: Michiel de Jong <michiel@unhosted.org>
Date: Tue, 28 Oct 2014 07:42:53 +0000
Subject: [PATCH] add nginx-gitpuller

---
 README.md                           |  3 ++-
 importers/nginx-updater.sh          |  3 +++
 importers/nginx.sh                  | 10 +++++++---
 scripts/activate-user.sh            | 24 +++++++++++++++---------
 unit-files/nginx-gitpuller@.path    |  2 ++
 unit-files/nginx-gitpuller@.service | 12 ++++++++++++
 unit-files/nginx-gitpuller@.timer   |  8 ++++++++
 7 files changed, 49 insertions(+), 13 deletions(-)
 create mode 100755 importers/nginx-updater.sh
 create mode 100644 unit-files/nginx-gitpuller@.path
 create mode 100644 unit-files/nginx-gitpuller@.service
 create mode 100644 unit-files/nginx-gitpuller@.timer

diff --git a/README.md b/README.md
index 2d70f5a..943850c 100644
--- a/README.md
+++ b/README.md
@@ -21,12 +21,13 @@ vagrant up
 ```
 
 Wait for the provisioning to finish (~5mins), and go to your browser: https://indiehosters.dev
+If the process fails, for instance due to network problems, you can retry by running `vagrant provision`.
 
 ### If you want to add another nginx instance apart from indiehosters.dev:
 
 ```bash
 vagrant ssh
-sh /data/indiehosters/scripts/activate-user.sh example.dev nginx
+sudo sh /data/indiehosters/scripts/activate-user.sh example.dev nginx https://github.com/indiehosters/website-static.git
 ```
 Check https://example.dev in your bowser!
 
diff --git a/importers/nginx-updater.sh b/importers/nginx-updater.sh
new file mode 100755
index 0000000..034a402
--- /dev/null
+++ b/importers/nginx-updater.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+cd /data/per-user/$USER/nginx/data/www-content && git pull
diff --git a/importers/nginx.sh b/importers/nginx.sh
index ac46b0e..06c7953 100755
--- a/importers/nginx.sh
+++ b/importers/nginx.sh
@@ -1,6 +1,10 @@
 #!/bin/bash
 
-if [ ! -d "/data/per-user/$USER/nginx/data" ]; then
-  mkdir -p /data/per-user/$USER/nginx/data/www-content
-  echo Hello $USER > /data/per-user/$USER/nginx/data/www-content/index.html
+if [ ! -e "/data/per-user/$USER/nginx/data/www-content/index.html" ]; then
+  if [ -e "/data/per-user/$USER/nginx/data/git-url.txt" ]; then
+    git clone `cat /data/per-user/$USER/nginx/data/git-url.txt` /data/per-user/$USER/nginx/data/www-content
+  else
+    mkdir -p /data/per-user/$USER/nginx/data/www-content
+    echo Hello $USER > /data/per-user/$USER/nginx/data/www-content/index.html
+  fi
 fi
diff --git a/scripts/activate-user.sh b/scripts/activate-user.sh
index 4ee58a2..87a99d8 100755
--- a/scripts/activate-user.sh
+++ b/scripts/activate-user.sh
@@ -1,12 +1,18 @@
 #!/bin/bash -eux
 
-# mkdir -p /data/per-user/$1/nginx/data
-# if [ -e /data/per-user/$1/nginx/data/www-content ]; then
-#   cd /data/per-user/$1/nginx/data/www-content; git pull --rebase
-# else
-#   git clone $3 /data/per-user/$1/nginx/data/www-content
-# fi
+if [ $# -ge 2 ]; then
+  DOMAIN=$1
+  IMAGE=$2
+else
+  echo "Usage: sh /data/indiehosters/scripts/activate-user domain image [gitrepo]"
+  exit 1
+fi
+mkdir -p /data/per-user/$DOMAIN/nginx/data
+if [ $# -ge 3 ]; then
+  GITREPO=$3
+  echo $GITREPO > /data/per-user/$DOMAIN/nginx/data/git-url.txt
+fi
 
-# Start service for new site (and create the user)
-systemctl enable $2@$1.service
-systemctl start  $2@$1.service
+# Start service for new site (and create the user). This will also enable the git puller.
+systemctl enable $IMAGE@$DOMAIN.service
+systemctl start  $IMAGE@$DOMAIN.service
diff --git a/unit-files/nginx-gitpuller@.path b/unit-files/nginx-gitpuller@.path
new file mode 100644
index 0000000..564b2bc
--- /dev/null
+++ b/unit-files/nginx-gitpuller@.path
@@ -0,0 +1,2 @@
+[Path]
+PathExists=/data/per-user/%i/nginx/data/git-url.txt
diff --git a/unit-files/nginx-gitpuller@.service b/unit-files/nginx-gitpuller@.service
new file mode 100644
index 0000000..a05ab83
--- /dev/null
+++ b/unit-files/nginx-gitpuller@.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=nginx git puller
+
+# Dependency ordering
+Before=nginx@%i.service
+
+[Service]
+Environment=USER=%i
+ExecStart=/data/indiehosters/importers/nginx-updater.sh
+
+[Install]
+WantedBy=nginx@%i.service
diff --git a/unit-files/nginx-gitpuller@.timer b/unit-files/nginx-gitpuller@.timer
new file mode 100644
index 0000000..78faa1e
--- /dev/null
+++ b/unit-files/nginx-gitpuller@.timer
@@ -0,0 +1,8 @@
+[Unit]
+Description=Run git puller every 10 minutes
+
+[Timer]
+OnUnitActiveSec=10min
+
+[Install]
+WantedBy=timers.target
-- 
GitLab