daily pastebin goal
65%
SHARE
TWEET

Untitled

a guest Apr 27th, 2016 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. LDIR=`pwd`
  4. cd `dirname $0`
  5. SDIR=`pwd`
  6. cd "$LDIT"
  7.  
  8. if [ -n "$1" ] ; then
  9.     NAME="$1"
  10. else
  11.     read -p "Instance Name: " NAME
  12. fi
  13.  
  14. DST="$SDIR/$NAME"
  15.  
  16. if [ -d "$DST" ] ; then
  17.     source "$DST/config"
  18.     exec ssh -i $DST/id_rsa root@$IP -p $PORT
  19. fi
  20.  
  21. mkdir "$DST"
  22.  
  23. read -s -p "DigitalOcean token: " TOKEN
  24.  
  25. echo ""
  26.  
  27. echo "Check DigitalOcean account"
  28.  
  29. curl -sS -X GET \
  30.     -o "$DST/account.log" \
  31.     -H "Content-Type: application/json" \
  32.     -H "Authorization: Bearer $TOKEN" \
  33.     "https://api.digitalocean.com/v2/account"
  34.  
  35. ACCOUNT=`sed -e 's/^.*"status":"\([^"]*\)".*$/\1/' $DST/account.log`
  36.  
  37. if [ "$ACCOUNT" != "active" ] ; then
  38.     echo "Wrong DigitalOcean token"
  39.     exit 1
  40. fi
  41.  
  42. echo "Create new SSH key"
  43.  
  44. ssh-keygen -q -t rsa -b 4096 -C "support@cesbo.com" -f $DST/id_rsa -N ""
  45.  
  46. PUBKEY=`cat $DST/id_rsa.pub`
  47.  
  48. curl -sS -X POST \
  49.     -o "$DST/key.log" \
  50.     -H "Content-Type: application/json" \
  51.     -H "Authorization: Bearer $TOKEN" \
  52.     -d "{\"name\":\"$NAME\",\"public_key\":\"$PUBKEY\"}" \
  53.     "https://api.digitalocean.com/v2/account/keys"
  54.  
  55. KEYID=`sed -e 's/^.*"id":\([0-9]*\).*$/\1/' $DST/key.log`
  56.  
  57. echo "Create new droplet"
  58.  
  59. curl -sS -X POST \
  60.     -o "$DST/droplet.log" \
  61.     -H "Content-Type: application/json" \
  62.     -H "Authorization: Bearer $TOKEN" \
  63.     -d "{\"name\":\"$NAME\",\"region\":\"ams3\",\"size\":\"512mb\",\"image\":\"ubuntu-14-04-x64\",\"ssh_keys\":[\"$KEYID\"],\"backups\":false,\"ipv6\":true,\"user_data\":null,\"private_networking\":null}" \
  64.     "https://api.digitalocean.com/v2/droplets"
  65.  
  66. DROPLETID=`cat $DST/droplet.log | sed -e 's/^."droplet":{//' | sed -e 's/"[a-z_]*":{[^}]*}//g' | sed -e 's/^.*"id":\([0-9]*\).*$/\1/'`
  67.  
  68. while true ; do
  69.     curl -sS -X GET \
  70.         -o "$DST/status.log" \
  71.         -H "Content-Type: application/json" \
  72.         -H "Authorization: Bearer $TOKEN" \
  73.         "https://api.digitalocean.com/v2/droplets/$DROPLETID"
  74.     STATUS=`sed -e 's/^.*"status":"\([^"]*\)".*$/\1/' $DST/status.log`
  75.     if [ "$STATUS" == "active" ] ; then
  76.         break
  77.     fi
  78.     sleep 5
  79. done
  80.  
  81. echo "Droplet ready"
  82.  
  83. IP4=`cat $DST/status.log | sed -e 's/^.*"v4":\[\([^]]*\)\].*$/\1/'`
  84. IP=`echo "$IP4" | sed -e 's/^.*"ip_address":"\([^"]*\)".*$/\1/'`
  85. echo "IP=\"$IP\"" >$DST/config
  86.  
  87. ssh -o StrictHostKeychecking=no -i $DST/id_rsa root@$IP echo "OK" >/dev/null 2>&1
  88.  
  89. SSH="ssh -i $DST/id_rsa root@$IP"
  90.  
  91. echo "Update system"
  92. $SSH "apt-get -y update >>/var/log/install.log 2>&1"
  93. $SSH "apt-get -y upgrade >>/var/log/install.log 2>&1"
  94.  
  95. echo "Install packages"
  96.  
  97. $SSH "sh" <<EOF
  98. apt-get -y install curl nginx unzip >>/var/log/install.log 2>&1
  99. sed -i -e "s/# \(server_tokens\)/\1/g" /etc/nginx/nginx.conf
  100. echo "Install Docker" >>/var/log/install.log
  101. curl -fsSL https://get.docker.com/ | sh >>/var/log/install.log 2>&1
  102. mkdir -p /opt/cesbo/www /opt/cesbo/download
  103. EOF
  104.  
  105. echo "Configure system"
  106.  
  107. scp -q -i $DST/id_rsa -r $SDIR/host/* root@$IP:/
  108.  
  109. read -p "SSH Port: " SSH_PORT
  110.  
  111. $SSH -t dpkg-reconfigure tzdata
  112.  
  113. $SSH "sh" <<EOF
  114. sed -i -e "s/^\(Port\) .*$/\1 $SSH_PORT/g" /etc/ssh/sshd_config
  115. service ssh restart >/dev/null
  116. EOF
  117.  
  118. PORT="$SSH_PORT"
  119. echo "PORT=\"$PORT\"" >>$DST/config
  120. SSH="ssh -i $DST/id_rsa -p $PORT root@$IP"
  121.  
  122. LINK=""
  123.  
  124. read -r -p "Install Mail container? [y/N] " RESP
  125. if [ "$RESP" == "y" -o "$RESP" == "Y" ] ; then
  126.     LINK="$LINK --link \"mail:mail.local\""
  127.     read -p "Relay domain: " RELAY_DOMAIN
  128.     read -p "Google Mail login: " GMAIL_LOGIN
  129.     read -s -p "Google Mail password: " GMAIL_PASS
  130.     echo ""
  131.     $SSH "sh" <<EOF
  132. docker run --name mail -e RELAY_DOMAIN="$RELAY_DOMAIN" --hostname "mail.local" -d cesbo/exim-relay >>/var/log/install.log 2>&1
  133. echo "*.google.com:$GMAIL_LOGIN:$GMAIL_PASS" | docker exec -i mail sh -c "cat >/etc/exim4/passwd.client"
  134. EOF
  135. fi
  136.  
  137. read -r -p "Install MySQL container? [y/N] " RESP
  138. if [ "$RESP" == "y" -o "$RESP" == "Y" ] ; then
  139.     LINK="$LINK --link \"mysql:mysql.local\""
  140.     read -s -p "MySQL root password: " MYSQL_PASS
  141.     echo ""
  142.     OPTS="-e MYSQL_ROOT_PASSWORD=\"$MYSQL_PASS\""
  143.     read -r -p "Make MySQL DB? [y/N] " RESP
  144.     if [ "$RESP" == "y" -o "$RESP" == "Y" ] ; then
  145.         read -p "DB Name: " DB_NAME
  146.         read -p "DB User: " DB_USER
  147.         read -s -p "DB Password: " DB_PASS
  148.         echo ""
  149.         OPTS="$OPTS -e MYSQL_DATABASE=\"$DB_NAME\" -e MYSQL_USER=\"$DB_USER\" -e MYSQL_PASSWORD=\"$DB_PASS\""
  150.     fi
  151.     $SSH "sh" <<EOF
  152. docker run --name mysql $OPTS --hostname "mysql.local" -d mysql:5.7 >>/var/log/install.log 2>&1
  153. EOF
  154. fi
  155.  
  156. read -r -p "Install PHP-FPM container? [y/N] " RESP
  157. if [ "$RESP" == "y" -o "$RESP" == "Y" ] ; then
  158.     $SSH "sh" <<EOF
  159. docker run --name php --volume "/opt/cesbo/www:/opt/cesbo/www" --hostname "php.local" $LINK -d php:5.6-fpm >>/var/log/install.log 2>&1
  160. docker exec -i -t php apt-get -y update >>/var/log/install.log 2>&1
  161. docker exec -i -t php apt-get -y install php5-mysqlnd php5-gd php5-curl php5-mcrypt php5-json >>/var/log/install.log 2>&1
  162. EOF
  163. fi
  164.  
  165. echo "Done!"
  166.  
  167. read -r -p "Launch SSH? [y/N] " RESP
  168. if [ "$RESP" == "y" -o "$RESP" == "Y" ] ; then
  169.     exec ssh -i $DST/id_rsa -p $PORT root@$IP
  170. fi
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top