diff --git a/README.md b/README.md index 943850c03284d0e56a8db25a4b55587aa5aaa3a0..506b82945552d5c40f63cd82edfeb07ebc9df121 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 8aa0d5bd6316c67680a1bfb69a149dab594c1f6c..8b5a671bf55e435c421b880a9e274341f862ca73 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 47a0b379661bcbac388b669afba224da9a828487..e220c31c846ed97f6ae8e108e7359e844799f96b 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 3a3ff8f8695c98b80048f2d0d87bccc08f161c1d..0000000000000000000000000000000000000000 --- 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 7cb4ce24924d96bc74ea7597343626df456c98a4..3d483555e2dede08abb4f5561463ca92460e9348 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 5436859993f8d3e4cf98c4d50bcac6fd409465e8..7a944d7809bc140f2b260b012b98a10e6cb5615e 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 043187877d7afeda5d080a650bd075515f427e61..0000000000000000000000000000000000000000 --- 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 4dd4ce0e137aebcff9c203cbe95f6b227717eedc..0000000000000000000000000000000000000000 --- 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 58329009e12724f4132046d353a372f43001b921..94de75af84f68774500e9473ba9db641741e3fe2 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 0000000000000000000000000000000000000000..466a0b96982c812fa432ff845d6d76acbc9566bd --- /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 0000000000000000000000000000000000000000..b289e77404abcc313a484ac6ee11f37b69939796 --- /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 17fcba27e28d83b15b2c159a9103181cc9572505..4f492580b14ba3b05291e15c6dd92afa855594f1 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 493f351730056e67103b148bbbb7aa58a1fdf2f5..94e6fce8ff1639b892bce4af4c2380ad3fa5af9c 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 0000000000000000000000000000000000000000..b845cae47eebc1638bd189f371bd17c601f514bf --- /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 0000000000000000000000000000000000000000..052abc7e7c28e3ebdbc1944db5d46f4b8a452fe6 --- /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 c4a27973c97cfdb57cef82aa603b14c8f6ec9649..c8fd4361fe5911d5e3f9b4745482172edbf269ec 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