diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..997ca2f846554a247d3cc3f653e17dd1d5a15ffe --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vagrant \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..70356c4e28620eef6f87cc022dd3feb369a68b96 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +## Prerequisites to work on this project: +- [vagrant](http://www.vagrantup.com/) +- [virtualbox](https://www.virtualbox.org/) +- optional: [vagrant-hostsupdater](https://github.com/cogitatio/vagrant-hostsupdater) + +## Get started: + +```bash +vagrant up +``` + +Wait the provisionning to finish (~40mins), and go to your browser: http://coreos.dev + +### If you want to start another wordpress: +```bash +vagrant ssh +sudo systemctl start wordpress@myuser.service +``` +Update haproxy configuration in ``/data/server-wide/haproxy/haproxy.cfg`. +Check in your bowser! diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 0000000000000000000000000000000000000000..0f315a471b5b634ae5a146a37d50f9388effbca2 --- /dev/null +++ b/Vagrantfile @@ -0,0 +1,40 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +# Vagrantfile API/syntax version. Don't touch unless you know what you're doing! +VAGRANTFILE_API_VERSION = "2" + +Vagrant.require_version ">= 1.5.0" + +# Size of the CoreOS cluster created by Vagrant +$num_instances=1 + +# Official CoreOS channel from which updates should be downloaded +$update_channel='stable' + +# Setting for VirtualBox VMs +$vb_memory = 1024 +$vb_cpus = 1 + +BASE_IP_ADDR = ENV['BASE_IP_ADDR'] || "192.168.65" + +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| + config.vm.box = "coreos-%s" % $update_channel + config.vm.box_version = ">= 308.0.1" + config.vm.box_url = "http://%s.release.core-os.net/amd64-usr/current/coreos_production_vagrant.json" % $update_channel + + (1..$num_instances).each do |i| + config.vm.define "core-#{i}" do |core| + config.vm.provider :virtualbox do |vb| + vb.memory = $vb_memory + vb.cpus = $vb_cpus + end + + core.vm.hostname = "coreos.dev" + 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" + end + end +end diff --git a/blueprints/wordpress.tgz b/blueprints/wordpress.tgz new file mode 100644 index 0000000000000000000000000000000000000000..b0eb7859be05e9a743c2a2879ea4d3840d05f6b3 Binary files /dev/null and b/blueprints/wordpress.tgz differ diff --git a/config/user-data b/config/user-data new file mode 100644 index 0000000000000000000000000000000000000000..2a32c3a16242fa3904e09223ce7101a310139d48 --- /dev/null +++ b/config/user-data @@ -0,0 +1,5 @@ +#cloud-config + +coreos: + update: + reboot-strategy: best-effort diff --git a/importers/mysql.sh b/importers/mysql.sh new file mode 100755 index 0000000000000000000000000000000000000000..632bc10f8cb5590b4512c8d12ce905db1cedab56 --- /dev/null +++ b/importers/mysql.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +if [ ! -d "/data/per-user/$USER/mysql/data" ]; then + mkdir -p /data/per-user/$USER/mysql/data + echo MYSQL_PASS=`echo $RANDOM ${date} | md5sum | base64 | cut -c-10` > /data/per-user/$USER/mysql/.env +fi diff --git a/importers/wordpress.sh b/importers/wordpress.sh new file mode 100755 index 0000000000000000000000000000000000000000..6e38abb2fc178453c26fcb4b3ac175cbaed01df4 --- /dev/null +++ b/importers/wordpress.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +if [ ! -d "/data/per-user/$USER/wordpress/data" ]; then + cd /data/per-user/$USER/ + tar xvzf /data/infrastructure/blueprints/wordpress.tgz + cat /data/per-user/$USER/mysql/.env | sed s/MYSQL_PASS/DB_PASS/ > /data/per-user/$USER/wordpress/.env +fi diff --git a/scripts/vagrant.sh b/scripts/vagrant.sh new file mode 100644 index 0000000000000000000000000000000000000000..fc3cd8f3a081533303755afb63f2df845282133a --- /dev/null +++ b/scripts/vagrant.sh @@ -0,0 +1,24 @@ +#!/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.cfg b/templates/haproxy.cfg new file mode 100644 index 0000000000000000000000000000000000000000..87c02c3e41087c8e14ec829e18f220a82282b7e3 --- /dev/null +++ b/templates/haproxy.cfg @@ -0,0 +1,28 @@ +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/haproxy.service b/unit-files/haproxy.service new file mode 100644 index 0000000000000000000000000000000000000000..de7e400e80a1ba51b1fbd56b51abf64d582ba7d9 --- /dev/null +++ b/unit-files/haproxy.service @@ -0,0 +1,20 @@ +[Unit] +Description=%p +After=docker.service +Requires=docker.service + +[Service] +Restart=always +TimeoutStartSec=0 +ExecStartPre=-/usr/bin/docker kill %p +ExecStartPre=-/usr/bin/docker rm %p +ExecStart=/usr/bin/docker run\ +--name %p\ +-p 80:80\ +-v /data/server-wide/%p:/haproxy-override\ +dockerfile/haproxy +ExecReload=/usr/bin/docker restart %p +ExecStop=/usr/bin/docker stop %p + +[Install] +WantedBy=multi-user.target diff --git a/unit-files/mysql-importer@.service b/unit-files/mysql-importer@.service new file mode 100644 index 0000000000000000000000000000000000000000..f05608da5b69c6c04ce9233271abe561842272e6 --- /dev/null +++ b/unit-files/mysql-importer@.service @@ -0,0 +1,12 @@ +[Unit] +Description=MySQL importer +Before=mysql@%i.service + +[Service] +Type=oneshot +RemainAfterExit=yes +Environment=USER=%i +ExecStart=/data/infrastructure/importers/mysql.sh + +[Install] +WantedBy=mysql@%i.service diff --git a/unit-files/mysql@.service b/unit-files/mysql@.service new file mode 100644 index 0000000000000000000000000000000000000000..cdbaeb5567be672dc6bbca378a1673451f599512 --- /dev/null +++ b/unit-files/mysql@.service @@ -0,0 +1,22 @@ +[Unit] +Description=%p-%i +After=docker.service +After=%p-importer@%i.service +Requires=docker.service +Requires=%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:/var/lib/mysql\ +--env-file /data/per-user/%i/%p/.env\ +tutum/mysql +ExecReload=/usr/bin/docker restart %p-%i +ExecStop=/usr/bin/docker stop %p-%i + +[Install] +WantedBy=multi-user.target diff --git a/unit-files/wordpress-importer@.service b/unit-files/wordpress-importer@.service new file mode 100644 index 0000000000000000000000000000000000000000..c1a4771fc7c712b297a46672358ff4476d0a3c96 --- /dev/null +++ b/unit-files/wordpress-importer@.service @@ -0,0 +1,13 @@ +[Unit] +Description=WordPress importer +After=mysql-importer@%i.service +Before=wordpress@%i.service + +[Service] +Type=oneshot +RemainAfterExit=yes +Environment=USER=%i +ExecStart=/data/infrastructure/importers/wordpress.sh + +[Install] +WantedBy=wordpress@%i.service diff --git a/unit-files/wordpress@.service b/unit-files/wordpress@.service new file mode 100644 index 0000000000000000000000000000000000000000..ddbae3639e57023b9bc485c266709258d6524049 --- /dev/null +++ b/unit-files/wordpress@.service @@ -0,0 +1,26 @@ +[Unit] +Description=%p-%i +After=docker.service +Requires=docker.service +Requires=mysql@%i.service +After=mysql@%i.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\ +--link mysql-%i:db\ +-v /data/per-user/%i/%p/data/wp-content:/app/wp-content\ +-v /data/per-user/%i/%p/data/.htaccess:/app/.htaccess\ +--env-file /data/per-user/%i/%p/.env\ +tutum/wordpress-stackable +ExecReload=/usr/bin/docker restart %p-%i +ExecStop=/usr/bin/docker stop %p-%i + +[Install] +WantedBy=multi-user.target