From d8c82736ad10d09e2e32863740cc02915939b119 Mon Sep 17 00:00:00 2001 From: pierreozoux Date: Tue, 4 Nov 2014 19:16:15 +0000 Subject: [PATCH] Splits nginx into static and static-git --- README.md | 4 +-- deploy/add-placeholder-site.sh | 4 +-- doc/architecture-overview.md | 6 ++-- importers/nginx.sh | 11 ------ scripts/activate-user.sh | 4 +-- scripts/setup.sh | 4 +-- unit-files/nginx-gitpuller@.path | 9 ----- unit-files/nginx-importer@.service | 17 ---------- ...ery@.service => static-discovery@.service} | 4 +-- unit-files/static-git-discovery@.service | 34 +++++++++++++++++++ unit-files/static-git-importer@.service | 19 +++++++++++ ...er@.service => static-git-puller@.service} | 4 +-- ...puller@.timer => static-git-puller@.timer} | 2 +- unit-files/static-git@.service | 28 +++++++++++++++ unit-files/static-importer@.service | 18 ++++++++++ .../{nginx@.service => static@.service} | 3 +- 16 files changed, 116 insertions(+), 55 deletions(-) delete mode 100755 importers/nginx.sh delete mode 100644 unit-files/nginx-gitpuller@.path delete mode 100644 unit-files/nginx-importer@.service rename unit-files/{nginx-discovery@.service => static-discovery@.service} (93%) create mode 100644 unit-files/static-git-discovery@.service create mode 100644 unit-files/static-git-importer@.service rename unit-files/{nginx-gitpuller@.service => static-git-puller@.service} (52%) rename unit-files/{nginx-gitpuller@.timer => static-git-puller@.timer} (82%) create mode 100644 unit-files/static-git@.service create mode 100644 unit-files/static-importer@.service rename unit-files/{nginx@.service => static@.service} (87%) diff --git a/README.md b/README.md index 943850c..506b829 100644 --- a/README.md +++ b/README.md @@ -23,11 +23,11 @@ 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: +### If you want to add another static-git instance apart from indiehosters.dev: ```bash vagrant ssh -sudo sh /data/indiehosters/scripts/activate-user.sh example.dev nginx https://github.com/indiehosters/website-static.git +sudo sh /data/indiehosters/scripts/activate-user.sh example.dev static-git https://github.com/indiehosters/website.git ``` Check https://example.dev in your bowser! diff --git a/deploy/add-placeholder-site.sh b/deploy/add-placeholder-site.sh index 8aa0d5b..8b5a671 100755 --- a/deploy/add-placeholder-site.sh +++ b/deploy/add-placeholder-site.sh @@ -15,6 +15,6 @@ fi echo "Adding $DOMAIN to $SERVER with cert from $PEMFILE" echo "Remote user is $USER" -ssh $USER@$SERVER sudo mkdir -p /data/domains/$DOMAIN/nginx/data +ssh $USER@$SERVER sudo mkdir -p /data/domains/$DOMAIN/static scp $PEMFILE $USER@$SERVER:/data/runtime/haproxy/approved-certs/$DOMAIN.pem -ssh $USER@$SERVER sudo sh /data/indiehosters/scripts/activate-user.sh $DOMAIN nginx +ssh $USER@$SERVER sudo sh /data/indiehosters/scripts/activate-user.sh $DOMAIN static diff --git a/doc/architecture-overview.md b/doc/architecture-overview.md index 47a0b37..e220c31 100644 --- a/doc/architecture-overview.md +++ b/doc/architecture-overview.md @@ -12,12 +12,12 @@ There is currently no similar side-kick for updating `/data/runtime/postfix/`, s The `scripts/setup.sh` takes care of setting up etcd, enabling and starting the haproxy and postfix service (as well as one haproxy backend, to serve the default site), and the haproxy-confd side-kick to listen for changes in the backends configuration in etcd, so that new backends are automatically added to the haproxy config as soon as their IP address is written into etcd. -## HAProxy backends: nginx, wordpress +## HAProxy backends: static, static-git, wordpress -A per user process is a haproxy backend for a specific domain name. At the time of writing we have two applications available: nginx and wordpress. +A per user process is a haproxy backend for a specific domain name. At the time of writing we have three applications available: static, static-git and wordpress. You will notice there are also some other units in the `unit-files/` folder of this repo, like the gitpuller and mysql ones. Whenever you start a wordpress unit, it requires a mysql service. -Whenever you start an nginx unit, it wants a gitpuller unit. In all three cases, an -importer unit and a -discovery unit are required. +Whenever you start a static-git unit, it wants a static-git-puller unit. In all three cases, an -importer unit and a -discovery unit are required. This works through a [`Requires=` directive](https://github.com/indiehosters/indiehosters/blob/0.1.0/unit-files/nginx@.service#L6-L7) which systemd interprets, so that if you start one service, its dependencies are also started (you can see that in `systemctl list-units`). diff --git a/importers/nginx.sh b/importers/nginx.sh deleted file mode 100755 index 3a3ff8f..0000000 --- a/importers/nginx.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -eux - -if [ ! -e "/data/domains/$DOMAIN/nginx/data/www-content/index.html" ]; then - if [ -e "/data/domains/$DOMAIN/nginx/data/GITURL" ]; then - git clone `cat /data/domains/$DOMAIN/nginx/data/GITURL` /data/domains/$DOMAIN/nginx/data/www-content - cd /data/domains/$DOMAIN/nginx/data/www-content && git checkout master - else - mkdir -p /data/domains/$DOMAIN/nginx/data/www-content - echo Hello $DOMAIN > /data/domains/$DOMAIN/nginx/data/www-content/index.html - fi -fi diff --git a/scripts/activate-user.sh b/scripts/activate-user.sh index 7cb4ce2..3d48355 100755 --- a/scripts/activate-user.sh +++ b/scripts/activate-user.sh @@ -7,10 +7,10 @@ else echo "Usage: sh /data/indiehosters/scripts/activate-user.sh domain image [gitrepo]" exit 1 fi -mkdir -p /data/domains/$DOMAIN/$IMAGE/data +mkdir -p /data/domains/$DOMAIN/$IMAGE if [ $# -ge 3 ]; then GITREPO=$3 - echo $GITREPO > /data/domains/$DOMAIN/$IMAGE/data/GITURL + echo $GITREPO > /data/domains/$DOMAIN/$IMAGE/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 5436859..7a944d7 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -25,8 +25,8 @@ cp /data/indiehosters/unit-files/* /etc/systemd/system systemctl daemon-reload # Activate default domain -sh /data/indiehosters/scripts/activate-user.sh $HOSTNAME nginx -etcdctl set /services/default '{"app":"nginx", "hostname":"'$HOSTNAME'"}' +sh /data/indiehosters/scripts/activate-user.sh $HOSTNAME static +etcdctl set /services/default '{"app":"static", "hostname":"'$HOSTNAME'"}' # Configure and start HAproxy mkdir -p /data/runtime/haproxy/approved-certs diff --git a/unit-files/nginx-gitpuller@.path b/unit-files/nginx-gitpuller@.path deleted file mode 100644 index 0431878..0000000 --- a/unit-files/nginx-gitpuller@.path +++ /dev/null @@ -1,9 +0,0 @@ -[Unit] -Description=Start the timer if there is a GITURL file - -# Dependency binding -BindsTo=nginx@%i.service - -[Path] -PathExists=/data/domains/%i/nginx/data/GITURL -Unit=nginx-gitpuller@%i.timer diff --git a/unit-files/nginx-importer@.service b/unit-files/nginx-importer@.service deleted file mode 100644 index 4dd4ce0..0000000 --- a/unit-files/nginx-importer@.service +++ /dev/null @@ -1,17 +0,0 @@ -[Unit] -Description=nginx importer - -# Dependency ordering -Before=nginx@%i.service - -# Dependency binding -BindsTo=mysql@%i.service - -[Service] -Type=oneshot -RemainAfterExit=yes -Environment=DOMAIN=%i -ExecStart=/data/indiehosters/importers/nginx.sh - -[Install] -WantedBy=nginx@%i.service diff --git a/unit-files/nginx-discovery@.service b/unit-files/static-discovery@.service similarity index 93% rename from unit-files/nginx-discovery@.service rename to unit-files/static-discovery@.service index 5832900..94de75a 100644 --- a/unit-files/nginx-discovery@.service +++ b/unit-files/static-discovery@.service @@ -6,10 +6,10 @@ Requires=etcd.service # Dependency ordering After=etcd.service -After=nginx@%i.service +After=static@%i.service # Dependency binding -BindsTo=nginx@%i.service +BindsTo=static@%i.service [Service] # Start diff --git a/unit-files/static-git-discovery@.service b/unit-files/static-git-discovery@.service new file mode 100644 index 0000000..466a0b9 --- /dev/null +++ b/unit-files/static-git-discovery@.service @@ -0,0 +1,34 @@ +[Unit] +Description=%p for %i etcd registration + +# Requirements +Requires=etcd.service + +# Dependency ordering +After=etcd.service +After=static-git@%i.service + +# Dependency binding +BindsTo=static-git@%i.service + +[Service] +# Start +## Test whether service is accessible and then register useful information +ExecStart=/bin/bash -euxc ' \ + 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 -euxc ' \ + app=`echo %p | cut -d"-" -f1`; \ + /usr/bin/etcdctl rm /services/$app/%i diff --git a/unit-files/static-git-importer@.service b/unit-files/static-git-importer@.service new file mode 100644 index 0000000..b289e77 --- /dev/null +++ b/unit-files/static-git-importer@.service @@ -0,0 +1,19 @@ +[Unit] +Description=static importer + +# Dependency ordering +Before=static-git@%i.service + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/bin/bash -euxc ' \ + runtime_folder=/data/runtime/domains/%i/static-git; \ + mkdir -p $runtime_folder; \ + git_folder=$runtime_folder/www-content; \ + if [ ! -d $git_folder/.git ]; then \ + git clone `cat /data/domains/%i/static-git/GITURL` $git_folder; \ + fi;' + +[Install] +WantedBy=static-git@%i.service diff --git a/unit-files/nginx-gitpuller@.service b/unit-files/static-git-puller@.service similarity index 52% rename from unit-files/nginx-gitpuller@.service rename to unit-files/static-git-puller@.service index 17fcba2..4f49258 100644 --- a/unit-files/nginx-gitpuller@.service +++ b/unit-files/static-git-puller@.service @@ -4,6 +4,6 @@ Description=git puller [Service] Type=oneshot ExecStart=/bin/bash -euxc ' \ - app=`echo %p | cut -d"-" -f1`; \ - cd /data/domains/%i/$app/data/www-content; \ + app=`echo %p | cut -d"-" -f1,2`; \ + cd /data/runtime/domains/%i/$app/www-content; \ git pull' diff --git a/unit-files/nginx-gitpuller@.timer b/unit-files/static-git-puller@.timer similarity index 82% rename from unit-files/nginx-gitpuller@.timer rename to unit-files/static-git-puller@.timer index 493f351..94e6fce 100644 --- a/unit-files/nginx-gitpuller@.timer +++ b/unit-files/static-git-puller@.timer @@ -2,7 +2,7 @@ Description=Run git puller service every 10 minutes # Dependency binding -BindsTo=nginx-gitpuller@%i.path +BindsTo=static-git@%i.service [Timer] OnUnitActiveSec=10min diff --git a/unit-files/static-git@.service b/unit-files/static-git@.service new file mode 100644 index 0000000..b845cae --- /dev/null +++ b/unit-files/static-git@.service @@ -0,0 +1,28 @@ +[Unit] +Description=%p-%i + +# Requirements +Requires=docker.service +Requires=%p-importer@%i.service +Requires=%p-discovery@%i.service +Requires=%p-puller@%i.timer +Requires=backup@%i.timer + +# Dependency ordering +After=docker.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 \ + -v /data/runtime/domains/%i/%p/www-content:/app \ + indiehosters/nginx +ExecReload=/usr/bin/docker restart %p-%i +ExecStop=/usr/bin/docker stop %p-%i + +[Install] +WantedBy=multi-user.target diff --git a/unit-files/static-importer@.service b/unit-files/static-importer@.service new file mode 100644 index 0000000..052abc7 --- /dev/null +++ b/unit-files/static-importer@.service @@ -0,0 +1,18 @@ +[Unit] +Description=static importer + +# Dependency ordering +Before=static@%i.service + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/bin/bash -euxc ' \ + folder=/data/domains/%i/static/www-content; \ + mkdir -p $folder; \ + if [ ! -f $folder/index.html ]; then \ + echo Hello %i > $folder/index.html; \ + fi;' + +[Install] +WantedBy=static@%i.service diff --git a/unit-files/nginx@.service b/unit-files/static@.service similarity index 87% rename from unit-files/nginx@.service rename to unit-files/static@.service index c4a2797..c8fd436 100644 --- a/unit-files/nginx@.service +++ b/unit-files/static@.service @@ -5,7 +5,6 @@ Description=%p-%i Requires=docker.service Requires=%p-importer@%i.service Requires=%p-discovery@%i.service -Requires=%p-gitpuller@%i.path Requires=backup@%i.timer # Dependency ordering @@ -19,7 +18,7 @@ ExecStartPre=-/usr/bin/docker kill %p-%i ExecStartPre=-/usr/bin/docker rm %p-%i ExecStart=/usr/bin/docker run \ --name %p-%i \ - -v /data/domains/%i/%p/data/www-content:/app \ + -v /data/domains/%i/%p/www-content:/app \ indiehosters/nginx ExecReload=/usr/bin/docker restart %p-%i ExecStop=/usr/bin/docker stop %p-%i -- GitLab