From afeaf5009ba94ecc23b05445a5f4db26dabeea22 Mon Sep 17 00:00:00 2001
From: pierreozoux <pierre@ozoux.net>
Date: Wed, 4 Mar 2015 11:56:39 +0000
Subject: [PATCH] Adds script to create vultr instances for tests purposes

---
 configs/backup.config    |  11 ++++
 configs/server.config    | 105 +++++++++++++++++++++++++++++++++++++++
 scripts/create_vultr.sh  |  49 ++++++++++++++++++
 scripts/destroy_vultr.sh |   7 +++
 scripts/install.sh       |  51 +++++++++++++++++++
 scripts/start.sh         |  30 +++++++++++
 scripts/stop.sh          |  16 ++++++
 7 files changed, 269 insertions(+)
 create mode 100644 configs/backup.config
 create mode 100644 configs/server.config
 create mode 100755 scripts/create_vultr.sh
 create mode 100755 scripts/destroy_vultr.sh
 create mode 100755 scripts/install.sh
 create mode 100755 scripts/start.sh
 create mode 100755 scripts/stop.sh

diff --git a/configs/backup.config b/configs/backup.config
new file mode 100644
index 0000000..b0c0c86
--- /dev/null
+++ b/configs/backup.config
@@ -0,0 +1,11 @@
+#cloud-config
+
+hostname: backup.test
+coreos:
+  update:
+    reboot-strategy: best-effort  
+users:
+  - name: backup
+    ssh-authorized-keys:
+      - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9rLjfQVci9g/ex+ZYGH0n1cSyp65oKeAgcPQtWGJ6V3KbzzePlR+ROSwA8XtSks9geQdq8kuWvO07cWrgvoOg1OYRfRhjQj0mY0uIKXSEAcGC2ndZoKN0CBOsPhYhZCxeMIem9ixmOFPSRhYgfg6+JZDn5/EZR/s2Et04yksHRmwxxuc7Pj6d0bBvUnkWz6IEZ7G8qhNikOAXskxEJKnm5grpPf7bQk6nztNbHwxuCHC3FrcTlu3a3yEMpC1+bmkCicVOQGtl7aVEQiGJMc7jITv52YC6Gcg+Oz65hLG9JJF3w42uFj01di6ghJAHwaqOBP2fGpKhoi1+FwQnmnf root@server.test
+
diff --git a/configs/server.config b/configs/server.config
new file mode 100644
index 0000000..1db14b5
--- /dev/null
+++ b/configs/server.config
@@ -0,0 +1,105 @@
+#cloud-config
+
+hostname: server.test
+coreos:
+  update:
+    reboot-strategy: best-effort
+  etcd:
+    addr: 172.17.42.1:4001
+    bind-addr: 172.17.42.1:4001
+    peer-addr: 172.17.42.1:7001
+    peer-bind-addr: 172.17.42.1:7001
+  units:
+    - name: etcd.service
+      command: start
+write_files:
+  - path: /etc/environment
+    permissions: 0600
+    owner: root
+    content: |
+      ENCRYPT_KEY="30082747"
+      BACKUP_DESTINATION="backup@##BACKUP_IP##"
+  - path: /etc/hosts
+    permissions: 0644
+    owner: root
+    content: |
+      127.0.0.1 localhost
+      255.255.255.255 broadcasthost
+      ::1 localhost
+      127.0.0.1 static.test
+      127.0.0.1 wordpress.test
+      127.0.0.1 known.test
+      127.0.0.1 piwik.test
+      127.0.0.1 owncloud.test
+      ##BACKUP_IP## backup.test
+  - path: /root/key.pub
+    permissions: 0644
+    owner: root
+    content: |
+      -----BEGIN PGP PUBLIC KEY BLOCK-----
+      Version: GnuPG v1
+      
+      mQENBFS2lIEBCAC+kiok9swkG+ufytTbsT4jUrrPiRLqCt8ATlkCsCeensSE0c5q
+      H9bbsUhEEA6JUhsQ4P/p+5KVB9du4odvsBeB5bW4dm79f+YuZMNiOXW7rzQ8AXjK
+      PJLSYtwa7P5xPTN4OU8IPg06FMEyrWpdfviMv+f5ekJrC6hyTfDBTtfcr2/u4Hu8
+      PSC3AP76FO1QwRzCBHzzCWwoMe/WnUc1F36C3S1//yJn6IZvgQppc4Z2NXkFTUFm
+      +xK63vSzH5b3Foqd0JgFZ0O/40V3Y/g2lhA3dFLKFlZU3BpgOGQbXck7pUuQpmMR
+      k7+pz6b0CRtnk2kB7ViRzY65x74f+JY44q7pABEBAAG0FVRlc3QgQmFja3VwIDxy
+      b290QHQxPokBOAQTAQIAIgUCVLaUgQIbAwYLCQgHAwIGFQgCCQoLBBYCAwECHgEC
+      F4AACgkQN2JsMjAIJ0efrggAkf7uh7fM+6uKlamJRBxN0GQdo8ORNpflND62GLQp
+      TdvGB2sJdVrAspwcEiccaUsEcCiFGyJlNzCuNCjj7OGtBme3+NCCerIR+hFukO5l
+      tVttcvIQTf+9FPXT5B0TvempI7Of3+WEwmhG7OBOt6VlikKxQ3gFcRCAhznPJyu0
+      SUU6h0swEYeojNJTbrsoUDGQt3VFbN+0Iv6SoY9gYM18ak8yxxUJBTyiVofFZBDV
+      Rx1iGQ8reaHUvkjfwmdSSDpRUk7S7Z4ciAWOt7ZvENJI9LERmdEHm3JYfIM1ENB2
+      CfwLziIzpQ2DcwxZJw3W+tF6ip78+PDftBoy5MqTTKKzibkBDQRUtpSBAQgApp/S
+      R3pEn3fLQeOG5w5DI6nUZFOfMjiAL5azvkcDiBdtJp7KafQnjGOuJBH0u+Rb9t32
+      BkvuKNNbumLn0H8a8HDqwEI5vVYil4YCbPtmFIrmBjWwaReEH/hYWz/vZcbdSA9w
+      V5nNToE8SoESJh9GyAfS3UMTCQj6HX/i8ldHMqVgoY7gwOQHSLuZlRYT/b5Clbfy
+      9iKen0sjxhtnW+sYwXfY3iwda7m8LMFkUhdzjthhhInrtv2KhNTvJS94PLmx6F9v
+      PSq87Wozg/mN+nCYCSA2NuXMZS5G9Wxl2n7qHVLNIwRL17qJ2uG+nLEIi/gG9nf7
+      bA+qItGljIKBwpYC/wARAQABiQEfBBgBAgAJBQJUtpSBAhsMAAoJEDdibDIwCCdH
+      NqMIAJP+iow4wkvmiphHCKbCMFM9k82w9qJ8cE3uCDlWDrjnjf9vJFId6NwhLnl/
+      TIb/MeKHPP178Kn05ZAI0nFokRGhzyx1OFZOy5HfoDjo8Y6uiAZWq0jRDqYPWkDr
+      rRnApbcjKTdirrhYL4b3v7MloaKU4qrpYiYGIhdd2+pZTkvY7AKojVw1Xi5CiIj6
+      puXiXENEetu0HhyGMiH9TjAQgt81R0VIj+X+CTZQa/3e9NWXf0O5T7zqadvIGzEC
+      YsLlEkuop3TqPdIEk0uJVmtbfNOaHMUIl4/ZziYZmVDUhXyfPfU5JM12yMTRTe7t
+      OCeGO1ft3KS3Dm4KSgDcDK43vi8=
+      =2QSP
+      -----END PGP PUBLIC KEY BLOCK-----
+  - path: /root/.ssh/id_rsa
+    permissions: 0600
+    owner: root
+    content: |
+      -----BEGIN RSA PRIVATE KEY-----
+      MIIEpAIBAAKCAQEAwPay430FXIvYP3sfmWBh9J9XEsqeuaCngIHD0LVhieldym88
+      3j5UfkTksAPF7UpLPYHkHavJLlrztO3Fq4L6DoNTmEX0YY0I9JmNLiCl0hAHBgtp
+      3WaCjdAgTrD4WIWQsXjCHpvYsZjhT0kYWIH4OviWQ5+fxGUf7NhLdOMpLB0ZsMcb
+      nOz4+ndGwb1J5Fs+iBGexvKoTYpDgF7JMRCSp5uYK6T3+20JOp87TWx8Mbghwtxa
+      3E5bt2t8hDKQtfm5pAonFTkBrZe2lREIhiTHO4yE7+dmAuhnIPjs+uYSxvSSRd8O
+      NrhY9NXYuoISQB8GqjgT9nxqSoaItfhcEJ5p3wIDAQABAoIBAHMwUWV/cj3mtTZf
+      92MFqDPxEneDQPEquBL8Y7opLIq5JmX7tWgt6R5fXey8s+CM4xFagqQ7siLb5HNz
+      JPbXfxI+zx4tVE8Pc/Mf3qlUj+gv7SFsmsuxr+mb/k07U2T0hoWKwsam3dpWmtlL
+      lx7upxH85yaDGUqR21b1X0XtfNx5kMj9584JQ9Bgs4vXd/iCCrF9SsCMIk0pdg/8
+      LDvhHJr4sLFJWVj54HW4k+Bf+xbERp+adHsR6FIlT6OcJD9vzLrTQT8KTgyHppM9
+      ZTqdThp+qYw4GKV0fDtlwC3G80q60aYfLdzf7m8SBsuJWgRyjm7YPpjxK3xJjpEH
+      qM/7c3ECgYEA7HXAUYR+ZZpWj3acGYOMfbAPu8d26L0TD8qs6LbLUb4uSOlYuQQu
+      mn/F8sRiszuWEqAAA3RJlRSUK6HhyI30Tbe/K3p/QnBxjNfU5reolRvjSvD2Txpt
+      q1xHNHCezZb8JuZgyYn0CfKHyxs2OjQDmQoa+jHHQ2QkCtinmqfLya0CgYEA0OjL
+      clHMrJIhgHDx+KpzGGaRKh1/8Zn+pa1iBFYdXEN/h36/ZYuAYgmxV/jX+nc3hrce
+      iUj1xNEN+51Ozn5KyHImSSGlJ1Ra0af0h0+0jtXN+a3lB8uWozenItMuH2S5dEDM
+      VXqDWRRjc6dHTjoDda2Tdy1yT/9ci2E7zcd+izsCgYAZTojksI9uhFQbSpBuUpvo
+      n70vVauuQP4XRJO43EdqWjZN61tpgb8THfWrg8UkdY4cNC+MLxfo8qjg+pRViIvK
+      /Y1iM9wG2k3syvSoNKPAQZnT3XIfCS8PlfKw9BSq4CvgZeuZ5j/OCLzryd/F3uc7
+      q88zU7yFw+PvnTxi2u5+iQKBgQCeFDm2ZK1ivCT7ovuGdbwn75zS6IWjFSr6xSsR
+      KvSGl1DN/5vmw9u+06TNc7BCh6mNl81Uhs/qQWK6TbuPR0NhT/cmiX4GN6+NS/Ed
+      3rM7DhARaYOQFX8EPJ2NHY2eFcK7dDjqSY1Qf7wNjLvJNt7dUh3vJYWFgS7PDSGK
+      wfVAdwKBgQCsaRcaopsbjknffjPefhx6DinLrdWZ+HN4hKft29gXUyiWxyKHl7Bw
+      L6nmdssgy786p/zR4uAy7Q9NEm5vhbfsStGOEfNb1gVgmcvCkLmRJ/ptIyg3t6pq
+      QBCRI/nRcO5FhEEqIXcn7tccBLVxJVp41XKqvONpYqq+oBNuy2po9w==
+      -----END RSA PRIVATE KEY-----
+  - path: /root/.ssh/id_rsa.pub
+    permissions: 0600
+    owner: root
+    content: |
+      ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA9rLjfQVci9g/ex+ZYGH0n1cSyp65oKeAgcPQtWGJ6V3KbzzePlR+ROSwA8XtSks9geQdq8kuWvO07cWrgvoOg1OYRfRhjQj0mY0uIKXSEAcGC2ndZoKN0CBOsPhYhZCxeMIem9ixmOFPSRhYgfg6+JZDn5/EZR/s2Et04yksHRmwxxuc7Pj6d0bBvUnkWz6IEZ7G8qhNikOAXskxEJKnm5grpPf7bQk6nztNbHwxuCHC3FrcTlu3a3yEMpC1+bmkCicVOQGtl7aVEQiGJMc7jITv52YC6Gcg+Oz65hLG9JJF3w42uFj01di6ghJAHwaqOBP2fGpKhoi1+FwQnmnf root@server.test
+
diff --git a/scripts/create_vultr.sh b/scripts/create_vultr.sh
new file mode 100755
index 0000000..45b9c77
--- /dev/null
+++ b/scripts/create_vultr.sh
@@ -0,0 +1,49 @@
+#!/bin/bash
+
+function valid_ip()
+{
+  local  ip=$1
+  local  stat=1
+  if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
+    OIFS=$IFS
+    IFS='.'
+    ip=($ip)
+    IFS=$OIFS
+    [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
+      && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
+    stat=$?
+  fi
+  return $stat
+}
+
+LABEL=$1
+
+SSHKEYID=`curl -s https://api.vultr.com/v1/sshkey/list\?api_key\=$VULTR_API_KEY | cut -d\" -f2`
+
+SUBID=`curl -s -d "DCID=24&VPSPLANID=29&OSID=179&label=$LABEL&SSHKEYID=$SSHKEYID" https://api.vultr.com/v1/server/create\?api_key\=$VULTR_API_KEY | cut -d\" -f4`
+
+while :
+do
+  IP=`curl -s https://api.vultr.com/v1/server/list_ipv4\?api_key\=$VULTR_API_KEY\&SUBID\=$SUBID | cut -d\" -f6`
+  if valid_ip $IP; then
+    break
+  else
+    echo "waiting to get an IP..."
+    sleep 5
+  fi
+done
+
+sudo -- sh -c "echo $IP $LABEL \#$SUBID >> /etc/hosts"
+
+while :
+do
+  ssh -o "StrictHostKeyChecking no" -o "BatchMode yes" root@$LABEL exit
+  if [ $? == 0 ];
+  then
+    break
+  else
+    echo "waiting to be able to ssh..."
+    sleep 5
+  fi
+done
+
diff --git a/scripts/destroy_vultr.sh b/scripts/destroy_vultr.sh
new file mode 100755
index 0000000..3ca17c7
--- /dev/null
+++ b/scripts/destroy_vultr.sh
@@ -0,0 +1,7 @@
+#!/bin/bash -eux
+
+LABEL=$1
+SUBID=`cat /etc/hosts | grep $LABEL | cut -d# -f2`
+sudo sed -i "/$LABEL/ d" /etc/hosts
+curl -d SUBID=$SUBID https://api.vultr.com/v1/server/destroy\?api_key\=$VULTR_API_KEY
+
diff --git a/scripts/install.sh b/scripts/install.sh
new file mode 100755
index 0000000..a30d6e8
--- /dev/null
+++ b/scripts/install.sh
@@ -0,0 +1,51 @@
+#!/bin/bash -eux
+
+/usr/bin/coreos-cloudinit --from-file=/var/lib/coreos-install/user_data
+
+docker pull pierreozoux/rsyslog
+docker pull pierreozoux/haproxy
+docker pull pierreozoux/confd
+docker pull pierreozoux/postfix
+#docker pull pierreozoux/dovecot
+docker pull pierreozoux/nginx
+docker pull pierreozoux/mysql
+docker pull pierreozoux/wordpress
+docker pull pierreozoux/known
+docker pull pierreozoux/piwik
+docker pull pierreozoux/owncloud
+docker pull pierreozoux/duplicity
+docker pull ibuildthecloud/systemd-docker
+
+# Create Directory structure
+mkdir -p /data/domains
+mkdir -p /data/runtime/haproxy/approved-certs
+git clone https://github.com/pierreozoux/IndiePaaS.git /data/indiehosters
+
+# Install unit-files
+cp /data/indiehosters/unit-files/* /etc/systemd/system && systemctl daemon-reload
+
+# Configure and start HAproxy
+cp /data/indiehosters/tests/unsecure-certs/indiehosters.dev.pem /data/runtime/haproxy/approved-certs/default.pem
+
+systemctl enable rsyslog
+systemctl start  rsyslog
+systemctl enable postfix
+systemctl start  postfix
+systemctl enable confd
+systemctl start  confd
+systemctl enable haproxy.path
+systemctl start  haproxy.path
+
+source /etc/environment
+# Put the backup server in known_hosts files using RSA algo
+# https://github.com/paramiko/paramiko/issues/243
+ssh -o "StrictHostKeyChecking no" -o "BatchMode yes" -o "HostKeyAlgorithms=ssh-rsa" $BACKUP_DESTINATION exit
+
+# Import backup encryption key
+gpg --import /root/key.pub
+TRUSTVAR=`gpg --fingerprint root | grep Key|cut -d= -f2|sed 's/ //g'`
+TRUST_VALUE=':6:'
+echo $TRUSTVAR$TRUST_VALUE | gpg --import-ownertrust
+
+docker run --rm -v /opt/bin:/target jpetazzo/nsenter
+
diff --git a/scripts/start.sh b/scripts/start.sh
new file mode 100755
index 0000000..2cc5540
--- /dev/null
+++ b/scripts/start.sh
@@ -0,0 +1,30 @@
+#!/bin/bash -eux
+
+ROOT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/..
+
+# Create backup for tests
+
+$ROOT_DIR/scripts/create_vultr.sh backup.test
+
+scp $ROOT_DIR/configs/backup.config root@backup.test:/var/lib/coreos-install/user_data
+ssh root@backup.test /usr/bin/coreos-cloudinit --from-file=/var/lib/coreos-install/user_data
+BACKUP_IP=`cat /etc/hosts | grep backup.test | cut -d" " -f1`
+
+# Create server for tests
+
+$ROOT_DIR/scripts/create_vultr.sh server.test
+cat $ROOT_DIR/configs/server.config | sed s/##BACKUP_IP##/$BACKUP_IP/g > /tmp/server.config
+scp /tmp/server.config root@server.test:/var/lib/coreos-install/user_data
+scp $ROOT_DIR/scripts/install.sh root@server.test:/tmp/install.sh
+ssh root@server.test /tmp/install.sh
+IP=`cat /etc/hosts | grep server.test | cut -d" " -f1`
+
+# Adds ip to /etc/hosts file
+
+echo "We'll now modify your /etc/hosts to add the test application name"
+applications=( `cat $ROOT_DIR/SUPPORTED_APPLICATIONS` )
+for application in "${applications[@]}"
+do
+  sudo -- sh -c "echo $IP $application.test  >> /etc/hosts"
+done
+
diff --git a/scripts/stop.sh b/scripts/stop.sh
new file mode 100755
index 0000000..11fc628
--- /dev/null
+++ b/scripts/stop.sh
@@ -0,0 +1,16 @@
+#!/bin/bash -eux
+
+ROOT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/..
+
+$ROOT_DIR/scripts/destroy_vultr.sh backup.test
+ssh-keygen -f ~/.ssh/known_hosts -R backup.test
+$ROOT_DIR/scripts/destroy_vultr.sh server.test
+ssh-keygen -f ~/.ssh/known_hosts -R server.test
+
+applications=( `cat $ROOT_DIR/SUPPORTED_APPLICATIONS` )
+
+for application in "${applications[@]}"
+do
+  sudo sed -i "/$application.test/ d" /etc/hosts
+done
+
-- 
GitLab