From 84bb761f8d5b012da6091dd828a326c005445757 Mon Sep 17 00:00:00 2001
From: Michiel de Jong <michiel@unhosted.org>
Date: Wed, 29 Oct 2014 15:32:18 +0000
Subject: [PATCH] merge wordpress-subdir, close #19

---
 importers/nginx.sh                            |  5 +--
 importers/wordpress-subdir.sh                 | 18 ++++++++++
 importers/wordpress.sh                        |  3 +-
 scripts/activate-user.sh                      |  2 +-
 scripts/setup.sh                              | 12 +++++--
 unit-files/gitpuller@.path                    |  2 ++
 ...-gitpuller@.service => gitpuller@.service} |  3 --
 ...ginx-gitpuller@.timer => gitpuller@.timer} |  0
 unit-files/nginx-gitpuller@.path              |  2 --
 unit-files/nginx@.service                     |  1 +
 .../wordpress-subdir-discovery@.service       | 35 +++++++++++++++++++
 unit-files/wordpress-subdir-importer@.service | 15 ++++++++
 unit-files/wordpress-subdir@.service          | 34 ++++++++++++++++++
 13 files changed, 121 insertions(+), 11 deletions(-)
 create mode 100755 importers/wordpress-subdir.sh
 create mode 100644 unit-files/gitpuller@.path
 rename unit-files/{nginx-gitpuller@.service => gitpuller@.service} (76%)
 rename unit-files/{nginx-gitpuller@.timer => gitpuller@.timer} (100%)
 delete mode 100644 unit-files/nginx-gitpuller@.path
 create mode 100644 unit-files/wordpress-subdir-discovery@.service
 create mode 100644 unit-files/wordpress-subdir-importer@.service
 create mode 100644 unit-files/wordpress-subdir@.service

diff --git a/importers/nginx.sh b/importers/nginx.sh
index 06c7953..008c3fc 100755
--- a/importers/nginx.sh
+++ b/importers/nginx.sh
@@ -1,8 +1,9 @@
 #!/bin/bash
 
 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
+  if [ -e "/data/per-user/$USER/nginx/data/GITURL" ]; then
+    git clone `cat /data/per-user/$USER/nginx/data/GITURL` /data/per-user/$USER/nginx/data/www-content
+    cd /data/per-user/$USER/nginx/data/www-content && git checkout master
   else
     mkdir -p /data/per-user/$USER/nginx/data/www-content
     echo Hello $USER > /data/per-user/$USER/nginx/data/www-content/index.html
diff --git a/importers/wordpress-subdir.sh b/importers/wordpress-subdir.sh
new file mode 100755
index 0000000..977a311
--- /dev/null
+++ b/importers/wordpress-subdir.sh
@@ -0,0 +1,18 @@
+#!/bin/bash
+
+if [ ! -d "/data/per-user/$USER/wordpress-subdir/data" ]; then
+  cd /data/per-user/$USER/
+  tar xvzf /data/indiehosters/blueprints/wordpress.tgz
+fi
+
+cat /data/per-user/$USER/mysql/.env | sed s/MYSQL_PASS/DB_PASS/ > /data/per-user/$USER/wordpress-subdir/.env
+
+if [ ! -e "/data/per-user/$USER/wordpress-subdir/data/www-content/index.html" ]; then
+  if [ -e "/data/per-user/$USER/wordpress-subdir/data/GITURL" ]; then
+    git clone `cat /data/per-user/$USER/wordpress-subdir/data/GITURL` /data/per-user/$USER/wordpress-subdir/data/www-content
+    cd /data/per-user/$USER/wordpress-subdir/data/www-content && git checkout master
+  else
+    mkdir -p /data/per-user/$USER/wordpress-subdir/data/www-content
+    echo Hello $USER > /data/per-user/$USER/wordpress-subdir/data/www-content/index.html
+  fi
+fi
diff --git a/importers/wordpress.sh b/importers/wordpress.sh
index 7e1f056..3af43a0 100755
--- a/importers/wordpress.sh
+++ b/importers/wordpress.sh
@@ -3,5 +3,6 @@
 if [ ! -d "/data/per-user/$USER/wordpress/data" ]; then
   cd /data/per-user/$USER/
   tar xvzf /data/indiehosters/blueprints/wordpress.tgz
-  cat /data/per-user/$USER/mysql/.env | sed s/MYSQL_PASS/DB_PASS/ > /data/per-user/$USER/wordpress/.env
 fi
+
+cat /data/per-user/$USER/mysql/.env | sed s/MYSQL_PASS/DB_PASS/ > /data/per-user/$USER/wordpress/.env
diff --git a/scripts/activate-user.sh b/scripts/activate-user.sh
index 0e2bd85..c4ce8dd 100755
--- a/scripts/activate-user.sh
+++ b/scripts/activate-user.sh
@@ -10,7 +10,7 @@ fi
 mkdir -p /data/per-user/$DOMAIN/$IMAGE/data
 if [ $# -ge 3 ]; then
   GITREPO=$3
-  echo $GITREPO > /data/per-user/$DOMAIN/$IMAGE/data/git-url.txt
+  echo $GITREPO > /data/per-user/$DOMAIN/$IMAGE/data/GITURL
 fi
 
 # Start service for new site (and create the user). This will also enable the git puller.
diff --git a/scripts/setup.sh b/scripts/setup.sh
index edc6ddd..5a61542 100755
--- a/scripts/setup.sh
+++ b/scripts/setup.sh
@@ -1,5 +1,12 @@
 #!/bin/bash -eux
 
+if [ $# -ge 1 ]; then
+  HOSTNAME=$1
+else
+  echo "Usage: sh /data/indiehosters/scripts/setup.sh k1.you.indiehosters.net"
+  exit 1
+fi
+
 # Install cloud-config
 if [ -f /tmp/vagrantfile-user-data ]; then
   mv /tmp/vagrantfile-user-data /var/lib/coreos-vagrant/vagrantfile-user-data
@@ -12,14 +19,15 @@ docker pull indiehosters/postfix-forwarder
 docker pull indiehosters/nginx
 docker pull indiehosters/mysql
 docker pull indiehosters/wordpress
+docker pull indiehosters/wordpress-subdir
 
 # Install unit-files
 cp /data/indiehosters/unit-files/* /etc/systemd/system
 systemctl daemon-reload
 
 # Activate default domain
-sh /data/indiehosters/scripts/activate-user.sh $1 nginx
-etcdctl set /services/default '{"app":"nginx", "hostname":"'$1'"}'
+sh /data/indiehosters/scripts/activate-user.sh $HOSTNAME nginx
+etcdctl set /services/default '{"app":"nginx", "hostname":"'$HOSTNAME'"}'
 
 # Configure and start HAproxy
 mkdir -p /data/server-wide/haproxy/approved-certs
diff --git a/unit-files/gitpuller@.path b/unit-files/gitpuller@.path
new file mode 100644
index 0000000..3bba895
--- /dev/null
+++ b/unit-files/gitpuller@.path
@@ -0,0 +1,2 @@
+[Path]
+PathExists=/data/per-user/%i/nginx/data/GITURL
diff --git a/unit-files/nginx-gitpuller@.service b/unit-files/gitpuller@.service
similarity index 76%
rename from unit-files/nginx-gitpuller@.service
rename to unit-files/gitpuller@.service
index 277cb1f..f85414f 100644
--- a/unit-files/nginx-gitpuller@.service
+++ b/unit-files/gitpuller@.service
@@ -1,9 +1,6 @@
 [Unit]
 Description=nginx git puller
 
-# Dependency ordering
-Before=nginx@%i.service
-
 [Service]
 ExecStart=cd /data/per-user/%i/nginx/data/www-content && git pull
 
diff --git a/unit-files/nginx-gitpuller@.timer b/unit-files/gitpuller@.timer
similarity index 100%
rename from unit-files/nginx-gitpuller@.timer
rename to unit-files/gitpuller@.timer
diff --git a/unit-files/nginx-gitpuller@.path b/unit-files/nginx-gitpuller@.path
deleted file mode 100644
index 564b2bc..0000000
--- a/unit-files/nginx-gitpuller@.path
+++ /dev/null
@@ -1,2 +0,0 @@
-[Path]
-PathExists=/data/per-user/%i/nginx/data/git-url.txt
diff --git a/unit-files/nginx@.service b/unit-files/nginx@.service
index d8830ce..9b6c733 100644
--- a/unit-files/nginx@.service
+++ b/unit-files/nginx@.service
@@ -5,6 +5,7 @@ Description=%p-%i
 Requires=docker.service
 Requires=%p-importer@%i.service
 Requires=%p-discovery@%i.service
+Wants=gitpuller@%i.service
 
 # Dependency ordering
 After=docker.service
diff --git a/unit-files/wordpress-subdir-discovery@.service b/unit-files/wordpress-subdir-discovery@.service
new file mode 100644
index 0000000..099c7b3
--- /dev/null
+++ b/unit-files/wordpress-subdir-discovery@.service
@@ -0,0 +1,35 @@
+[Unit]
+Description=%p for %i etcd registration
+
+# Requirements
+Requires=etcd.service
+Requires=wordpress-subdir@%i.service
+
+# Dependency ordering
+After=etcd.service
+After=wordpress-subdir@%i.service
+
+# Dependency binding
+BindsTo=wordpress-subdir@%i.service
+
+[Service]
+# Start
+## Test whether service is accessible and then register useful information
+ExecStart=/bin/bash -c ' \
+  sleep 3; \
+  while true; do \
+    app=`echo %p | cut -d"-" -f1,2`; \
+    ip=`docker inspect --format \'{{.NetworkSettings.IPAddress}}\' $app-%i`; \
+    curl -f $ip; \
+    if [ $? -eq 0 ]; then \
+      etcdctl set /services/$app/%i \'{"ip":"\'$ip\'", "port":"80"}\' --ttl 60; \
+    else \
+      etcdctl rm /services/$app/%i; \
+    fi; \
+    sleep 50; \
+  done'
+
+# Stop
+ExecStop=/bin/bash -ceux ' \
+  app=`echo %p | cut -d"-" -f1`; \
+  /usr/bin/etcdctl rm /services/$app/%i
diff --git a/unit-files/wordpress-subdir-importer@.service b/unit-files/wordpress-subdir-importer@.service
new file mode 100644
index 0000000..2ab17f4
--- /dev/null
+++ b/unit-files/wordpress-subdir-importer@.service
@@ -0,0 +1,15 @@
+[Unit]
+Description=wordpress-subdir importer
+
+# Dependency ordering
+After=mysql-importer@%i.service
+Before=wordpress-subdir@%i.service
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+Environment=USER=%i
+ExecStart=/data/indiehosters/importers/wordpress-subdir.sh
+
+[Install]
+WantedBy=wordpress-subdir@%i.service
diff --git a/unit-files/wordpress-subdir@.service b/unit-files/wordpress-subdir@.service
new file mode 100644
index 0000000..201d7e1
--- /dev/null
+++ b/unit-files/wordpress-subdir@.service
@@ -0,0 +1,34 @@
+[Unit]
+Description=%p-%i
+
+# Requirements
+Requires=docker.service
+Requires=etcd.service
+Requires=mysql@%i.service
+Requires=%p-importer@%i.service
+Requires=%p-discovery@%i.service
+Wants=gitpuller@%i.service
+
+# Dependency ordering
+After=docker.service
+After=mysql@%i.service
+After=%p-importer@%i.service
+Before=%p-discovery@%i.service
+
+[Service]
+Restart=always
+ExecStartPre=-/usr/bin/docker kill %p-%i
+ExecStartPre=-/usr/bin/docker rm %p-%i
+ExecStart=/usr/bin/docker run \
+  --name %p-%i \
+  --link mysql-%i:db \
+  -v /data/per-user/%i/%p/data/wp-content:/wordpress/wp-content \
+  -v /data/per-user/%i/%p/data/.htaccess:/wordpress/.htaccess \
+  -v /data/per-user/%i/%p/data/www-content:/app \
+  --env-file /data/per-user/%i/%p/.env \
+  indiehosters/wordpress-subdir
+ExecReload=/usr/bin/docker restart %p-%i
+ExecStop=/usr/bin/docker stop %p-%i
+
+[Install]
+WantedBy=multi-user.target
-- 
GitLab