commit 37c128d35eb6a7dc5ffa63af699f2dc3e102158e Author: Jack Engqvist Johansson Date: Fri Apr 20 21:57:06 2012 +0200 first commit diff --git a/README b/README new file mode 100644 index 0000000..a4541fc --- /dev/null +++ b/README @@ -0,0 +1,27 @@ +DEPLOY.SH - README +================== +Deploy your code, files, configurations etc. to multiple servers via ssh. +Perfect for the cloud. Written as a shell script (standard bourne shell) and +runs on every unix server. No need for remote install. Just run the +deploy.sh script on the local machine. + +USAGE +===== + ./deploy begin Run begin script + ./deploy copy Copy files to /tmp + ./deploy replace Replace the old files + ./deploy finish Run the finish script + + To deploy run: + ./deploy install + + To update already existing deployment run: + ./deploy update + +INSTALLATION +============ +Coming soon... + +GOOD TO KNOW +============ +Coming soon... diff --git a/deploy.conf b/deploy.conf new file mode 100644 index 0000000..ce3f6b2 --- /dev/null +++ b/deploy.conf @@ -0,0 +1,30 @@ +# The deploy.sh server list. +# To get the most out of deploy.sh you can make it dynamic. +# Use a heartbeat software or similar to maintain it. + +# Define the user to login with: +# user name keyfile +# ------------------------------------------ +user ubuntu /home/ubuntu/default.pem + +# Add server groups like this: +# server group ip address +# ------------------------------------------ +server all 10.0.0.101 +server all 10.0.0.102 +#server all 10.0.0.104 +server all 10.0.0.105 +server all 10.0.0.106 +server all 10.0.0.108 +server all 10.0.0.109 +server all 10.0.0.120 +server all 10.0.0.150 + +# DB servers +server db 10.0.0.106 +server db 10.0.0.150 + +# Web servers +server web 10.0.0.105 +server web 10.0.0.120 + diff --git a/deploy.sample b/deploy.sample new file mode 100644 index 0000000..6b1bff8 --- /dev/null +++ b/deploy.sample @@ -0,0 +1,63 @@ +# The deployment file +# +# Add files ro be copied like this: +# file group file +# ------------------------------------------ +file all /usr/local/alphamail/syschk-run +file all /usr/local/alphamail/syschk-fs +file all /usr/local/alphamail/syschk-ifdown +file all /usr/local/alphamail/syschk-udev +file all /usr/local/am-log/error +file all /usr/local/am-log/warning + +# Begin script +# Runs before everything +BEGIN: +# Add commands here to run before the deployment +# ------------------------------------------ +echo $DEPLOY_GROUP +if [ -d '/usr/local/alphamail/' ]; then + echo 'Alphamail folder exists...' +else + echo 'Creating alphamail folder...' + sudo mkdir /usr/local/alphamail +fi +# ------------------------------------------ +exit + +# Finishing script +# Runs after everything +FINISH: +# Add commands here to finish the deployment +# ------------------------------------------ +echo $DEPLOY_GROUP +sudo chmod 755 /usr/local/alphamail/* +sudo chown root:root /usr/local/alphamail/* +sudo chmod 755 /usr/local/am-log/error +sudo chmod 755 /usr/local/am-log/warning +sudo chown root:root /usr/local/am-log/error +sudo chown root:root /usr/local/am-log/warning + Add to crontabs +if [ -d "/var/lib/redis/" ]; then + sudo crontab -u redis -l > /tmp/am-crontab + sudo echo "*/5 * * * * /bin/bash /usr/local/alphamail/syschk-run" >> /tmp/am-crontab + crontab -u redis am-crontab +else + sudo crontab -u root -l > /tmp/am-crontab + sudo echo "*/5 * * * * /bin/bash /usr/local/alphamail/syschk-run" >> /tmp/am-crontab + sudo crontab -u root /tmp/am-crontab +fi +sudo rm /tmp/am-crontab +# ----------------------------------------- +exit + +# Update script +# Runs when call --update +UPDATE: +# Add commands here to do the update +# ------------------------------------------ +echo "I'm update!" +echo "Server group: ${DEPLOY_GROUP}" +# ------------------------------------------ +exit + diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..f82d637 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,129 @@ +#!/bin/sh +# (C) 2012 Comfirm AB +# deploy.sh +# Easy deployment to multiple servers. +# Deploy code, files, settings and much more to multiple servers via ssh +# +# Be sure to edit the .deploy file and /etc/deploy.conf before running this script. +# --------------------------------------------- +# Usage: +# --------------------------------------------- +# ./deploy begin Run begin script +# ./deploy copy Copy files to /tmp +# ./deploy replace Replace the old files +# ./deploy finish Run the finish script +# +# To deploy run: +# ./deploy install +# +# To update already existing deployment run: +# ./deploy update +# --------------------------------------------- + +APP="$0" +COMMAND=$1 +DIR="$2" + +# clean up the location string +if [ "$DIR" = "" ]; then + DIR="$( cd "$( dirname "$0" )" && pwd )" +elif [ "$DIR" = "./" ]; then + DIR="$( cd "$( dirname "$0" )" && pwd )" +else + DIR=$(echo "$DIR" | sed -e 's/\/$//g') +fi + +# full commands +if [ "$COMMAND" = "--install" ]; then + echo "Initiating executing of full deploying sequence..." + $APP --begin + $APP --copy + $APP --replace + $APP --finish + echo "Deployment is done" + exit +elif [ "$COMMAND" = "--update" ]; then + echo "Initiating executing of update sequence..." + $APP --copy + $APP --replace + $APP --update-s2 + echo "Update is done" + exit +fi + +# get user info +USER=$(cat /etc/deploy.conf | sed -e '/^$/ d' -e '/^[#].*/ d' -e '/server/,$ d' -e 's/^user//g' -e 's/[ \t]/,/g') +USER_NAME=$(echo "$USER" | cut -d',' -f2) +USER_KEY=$(echo "$USER" | cut -d',' -f3) + +# get server list +SERVERS=$(cat /etc/deploy.conf | sed -e '/^$/ d' -e '/^[#].*/ d') + +# get file list +FILES=$(cat /$DIR/.deploy | sed -e '/^$/ d' -e '/^[#].*/ d' -e '/BEGIN\:/,$ d') + +# get begin script +SCRIPT_BEGIN=$(cat /$DIR/.deploy | sed -e '1,/BEGIN\:/ d' -e '/FINISH\:/,$ d') + +# get finish script +SCRIPT_FINISH=$(cat /$DIR/.deploy | sed -e '1,/FINISH\:/ d' -e '/UPDATE\:/,$ d') + +# get update script +SCRIPT_UPDATE=$(cat /$DIR/.deploy | sed -e '1,/UPDATE\:/ d') + +# let's do the job... +GROUP=no +GROUP_NAME=all +FILE_GROUP=no +FILE_GROUP_NAME=all +for server in $SERVERS +do + if [ "$server" = "server" ]; then + GROUP=yes + elif [ "$GROUP" = "yes" ]; then + GROUP_NAME=$server + GROUP=no + else + LF=$(echo '\n ') + if [ "$COMMAND" = "--begin" ]; then + echo "Running script at $server($GROUP_NAME)..." + SHEBANG="#!/bin/sh${LF}DEPLOY_GROUP=${GROUP_NAME}" + SCRIPT="${SHEBANG}${LF}${SCRIPT_BEGIN}" + echo "/bin/sh ${SCRIPT}" | ssh -i $USER_KEY $USER_NAME@$server + elif [ "$COMMAND" = "--finish" ]; then + SHEBANG="#!/bin/sh${LF}DEPLOY_GROUP=${GROUP_NAME}" + SCRIPT="${SHEBANG}${LF}${SCRIPT_FINISH}" + echo "/bin/sh ${SCRIPT}" | ssh -i $USER_KEY $USER_NAME@$server + elif [ "$COMMAND" = "--update-s2" ]; then + SHEBANG="#!/bin/sh${LF}DEPLOY_GROUP=${GROUP_NAME}" + SCRIPT="${SHEBANG}${LF}${SCRIPT_UPDATE}" + echo "/bin/sh ${SCRIPT}" | ssh -i $USER_KEY $USER_NAME@$server + else + for file in $FILES + do + if [ "$file" = "file" ]; then + FILE_GROUP=yes + elif [ "$FILE_GROUP" = "yes" ]; then + FILE_GROUP_NAME=$file + FILE_GROUP=no + elif [ "$FILE_GROUP_NAME" = "$GROUP_NAME" ]; then + filename=$(basename $file) + echo " * $filename" + if [ "$COMMAND" = "--copy" ]; then + # copy files + echo "Copying files to $server($GROUP_NAME)..." + scp -i $USER_KEY $file $USER_NAME@$server:/tmp/$filename >> /dev/null + elif [ "$COMMAND" = "--replace" ]; then + # move files + echo "Replacing files at $server($GROUP_NAME)..." + echo "sudo mv /tmp/$filename $file" | ssh -i $USER_KEY $USER_NAME@$server + else + echo "Wrong options" + fi + fi + done + fi + fi +done + +exit