From e64e034618d8ed0e30d30244a150ac5c7bc93b20 Mon Sep 17 00:00:00 2001 From: Michiel de Jong <michiel@unhosted.org> Date: Sun, 5 Oct 2014 10:54:36 +0100 Subject: [PATCH] Add multi-user and nginx support (closes #4 and #5) --- README.md | 7 +++---- Vagrantfile | 8 +++++--- importers/nginx.sh | 7 +++++++ scripts/adduser.sh | 16 ++++++++++++++++ scripts/setup.sh | 19 +++++++++++++++++++ scripts/vagrant.sh | 24 ------------------------ templates/haproxy-backend.part | 7 +++++++ templates/haproxy-frontend.part | 4 ++++ templates/haproxy-main.part | 19 +++++++++++++++++++ templates/haproxy.cfg | 28 ---------------------------- unit-files/nginx-importer@.service | 12 ++++++++++++ unit-files/nginx@.service | 22 ++++++++++++++++++++++ 12 files changed, 114 insertions(+), 59 deletions(-) create mode 100755 importers/nginx.sh create mode 100644 scripts/adduser.sh create mode 100644 scripts/setup.sh delete mode 100644 scripts/vagrant.sh create mode 100644 templates/haproxy-backend.part create mode 100644 templates/haproxy-frontend.part create mode 100644 templates/haproxy-main.part delete mode 100644 templates/haproxy.cfg create mode 100644 unit-files/nginx-importer@.service create mode 100644 unit-files/nginx@.service diff --git a/README.md b/README.md index 5318fe5..62f31fb 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,9 @@ vagrant up Wait for the provisioning to finish (~40mins), and go to your browser: http://coreos.dev -### If you want to start another wordpress: +### If you want to add another wordpress instance: ```bash vagrant ssh -sudo systemctl start wordpress@myuser.service +sudo sh /data/infrastructure/scripts/adduser.sh example.dev wordpress ``` -Update haproxy configuration in ``/data/server-wide/haproxy/haproxy.cfg`. -Check in your bowser! +Check http://example.dev in your bowser! diff --git a/Vagrantfile b/Vagrantfile index 0f315a4..0cf8728 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -17,6 +17,7 @@ $vb_memory = 1024 $vb_cpus = 1 BASE_IP_ADDR = ENV['BASE_IP_ADDR'] || "192.168.65" +HOSTNAME = ENV['HOSTNAME'] || "coreos.dev" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "coreos-%s" % $update_channel @@ -30,11 +31,12 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| vb.cpus = $vb_cpus end - core.vm.hostname = "coreos.dev" + core.vm.hostname = HOSTNAME core.vm.network :private_network, ip: "#{BASE_IP_ADDR}.#{i+1}" config.vm.synced_folder ".", "/data/infrastructure" - core.vm.provision :file, source: "./config/user-data", destination: "/tmp/vagrantfile-user-data" - core.vm.provision :shell, path: "./scripts/vagrant.sh" + core.vm.provision :file, source: "./config/user-data", destination: "/var/lib/coreos-vagrant/vagrantfile-user-data" + core.vm.provision :shell, path: "./scripts/setup.sh" + core.vm.provision :shell, path: "./scripts/adduser.sh", args: [HOSTNAME, "wordpress"] end end end diff --git a/importers/nginx.sh b/importers/nginx.sh new file mode 100755 index 0000000..2c0688c --- /dev/null +++ b/importers/nginx.sh @@ -0,0 +1,7 @@ +#!/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 + touch /data/per-user/$USER/nginx/.env +fi diff --git a/scripts/adduser.sh b/scripts/adduser.sh new file mode 100644 index 0000000..d0f1475 --- /dev/null +++ b/scripts/adduser.sh @@ -0,0 +1,16 @@ +#!/bin/bash -eux + +# Start service for new site (and create the user) +systemctl enable $2@$1.service +systemctl start $2@$1.service + +sleep 10 + +# Configure new site in HAproxy +IP=`docker inspect --format '{{.NetworkSettings.IPAddress}}' $2-$1` + +echo IP address of new container \'$2-$1\' is \'$IP\' +sed s/%HOSTNAME%/$1/g /data/infrastructure/templates/haproxy-frontend.part | sed s/%IP%/$IP/g >> /data/server-wide/haproxy/frontends.part +sed s/%HOSTNAME%/$1/g /data/infrastructure/templates/haproxy-backend.part | sed s/%IP%/$IP/g >> /data/server-wide/haproxy/backends.part +cat /data/server-wide/haproxy/haproxy-main.part /data/server-wide/haproxy/frontends.part /data/server-wide/haproxy/backends.part > /data/server-wide/haproxy/haproxy.cfg +systemctl reload haproxy.service diff --git a/scripts/setup.sh b/scripts/setup.sh new file mode 100644 index 0000000..ed26876 --- /dev/null +++ b/scripts/setup.sh @@ -0,0 +1,19 @@ +#!/bin/bash -eux + +# Install unit-files +cp /data/infrastructure/unit-files/* /etc/systemd/system +systemctl daemon-reload + +# Pull relevant docker images +docker pull tutum/mysql +docker pull tutum/wordpress-stackable +docker pull tutum/nginx + +# Configure and start HAproxy +docker pull dockerfile/haproxy +mkdir -p /data/server-wide/haproxy +cp /data/infrastructure/templates/haproxy-main.part /data/server-wide/haproxy/haproxy-main.part +rm /data/server-wide/haproxy/frontends.part +rm /data/server-wide/haproxy/backends.part +systemctl enable haproxy.service +systemctl start haproxy.service diff --git a/scripts/vagrant.sh b/scripts/vagrant.sh deleted file mode 100644 index fc3cd8f..0000000 --- a/scripts/vagrant.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash -eux - -# Install cloud-config file -mv /tmp/vagrantfile-user-data /var/lib/coreos-vagrant/ - -# Install unit-files -cp /data/infrastructure/unit-files/* /etc/systemd/system -systemctl daemon-reload - -# Pull relevant docker images -docker pull tutum/mysql -docker pull tutum/wordpress-stackable - -# Start wordpress service for user coreos (and create the user) -systemctl enable wordpress@coreos.service -systemctl start wordpress@coreos.service - -# Configure and start HAproxy -docker pull dockerfile/haproxy -mkdir -p /data/server-wide/haproxy -IP=`docker inspect --format {{.NetworkSettings.IPAddress}} wordpress-coreos` -sed s/%IP%/$IP/ /data/infrastructure/templates/haproxy.cfg > /data/server-wide/haproxy/haproxy.cfg -systemctl enable haproxy.service -systemctl start haproxy.service diff --git a/templates/haproxy-backend.part b/templates/haproxy-backend.part new file mode 100644 index 0000000..f0755d0 --- /dev/null +++ b/templates/haproxy-backend.part @@ -0,0 +1,7 @@ + +# %HOSTNAME%: +backend %HOSTNAME% + cookie SERVERID insert nocache indirect + option httpclose + option forwardfor + server Server %IP%:80 cookie Server diff --git a/templates/haproxy-frontend.part b/templates/haproxy-frontend.part new file mode 100644 index 0000000..2992b1b --- /dev/null +++ b/templates/haproxy-frontend.part @@ -0,0 +1,4 @@ + +# %HOSTNAME%: + acl is_%HOSTNAME% hdr_end(host) -i %HOSTNAME% + use_backend %HOSTNAME% if is_%HOSTNAME% diff --git a/templates/haproxy-main.part b/templates/haproxy-main.part new file mode 100644 index 0000000..b8de0ca --- /dev/null +++ b/templates/haproxy-main.part @@ -0,0 +1,19 @@ +global + log 127.0.0.1 local0 + log 127.0.0.1 local1 notice + maxconn 4096 + user haproxy + group haproxy + +defaults + log global + mode http + option httplog + option dontlognull + retries 3 + timeout connect 5000 + timeout client 50000 + timeout server 50000 + +frontend http-in + bind *:80 diff --git a/templates/haproxy.cfg b/templates/haproxy.cfg deleted file mode 100644 index 87c02c3..0000000 --- a/templates/haproxy.cfg +++ /dev/null @@ -1,28 +0,0 @@ -global - log 127.0.0.1 local0 - log 127.0.0.1 local1 notice - maxconn 4096 - user haproxy - group haproxy - -defaults - log global - mode http - option httplog - option dontlognull - retries 3 - contimeout 5000 - clitimeout 50000 - srvtimeout 50000 - -frontend http-in - bind *:80 - acl is_core hdr_end(host) -i coreos.dev - - use_backend core if is_core - -backend core - cookie SERVERID insert nocache indirect - option httpclose - option forwardfor - server Server %IP%:80 cookie Server diff --git a/unit-files/nginx-importer@.service b/unit-files/nginx-importer@.service new file mode 100644 index 0000000..dfb71b7 --- /dev/null +++ b/unit-files/nginx-importer@.service @@ -0,0 +1,12 @@ +[Unit] +Description=nginx importer +Before=nginx@%i.service + +[Service] +Type=oneshot +RemainAfterExit=yes +Environment=USER=%i +ExecStart=/data/infrastructure/importers/nginx.sh + +[Install] +WantedBy=nginx@%i.service diff --git a/unit-files/nginx@.service b/unit-files/nginx@.service new file mode 100644 index 0000000..5795ce8 --- /dev/null +++ b/unit-files/nginx@.service @@ -0,0 +1,22 @@ +[Unit] +Description=%p-%i +After=docker.service +Requires=docker.service +Requires=%p-importer@%i.service +After=%p-importer@%i.service + +[Service] +Restart=always +TimeoutStartSec=0 +ExecStartPre=-/usr/bin/docker kill %p-%i +ExecStartPre=-/usr/bin/docker rm %p-%i +ExecStart=/usr/bin/docker run\ +--name %p-%i\ +-v /data/per-user/%i/%p/data/www-content:/app\ +--env-file /data/per-user/%i/%p/.env\ +tutum/nginx +ExecReload=/usr/bin/docker restart %p-%i +ExecStop=/usr/bin/docker stop %p-%i + +[Install] +WantedBy=multi-user.target -- GitLab