-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeploy.sh
executable file
·140 lines (124 loc) · 3.23 KB
/
deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash
usage()
{
cat << EOF
usage: $0 [OPTIONS] <TARGET> [[<repo>@]<branch>]
OPTIONS (all optional):
-h Show this message
-p Run provisioning steps to initialize a new server.
-s Delete all existing data, recreate and reseed the database. USE AT YOUR OWN RISK
-d Install the development self-signed SSL certificate
TARGET:
The target VM, or "vagrant" or "local".
REPO:
The git repository to use as the source for the deployment, defaults to https://github.com/welldone/strato.git
BRANCH:
The git branch to use as the source for the deployment, defaults to master
EOF
}
SSH_CMD=""
GITREPO="https://github.com/welldone/strato.git"
GITBRANCH="master"
CLEAN=
PROVISION=
APP_CONTEXT="PRODUCTION"
while getopts "hspd" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
s)
CLEAN=true
;;
p)
PROVISION=true
;;
d)
APP_CONTEXT="DEVELOPMENT"
;;
?)
usage
exit 1
;;
esac
done
shift $(($OPTIND - 1))
if [ -z "$1" ]; then usage
exit 1
fi
TARGET="$1"
if [ -n "$2" ]; then
GIT_ARG="$2"
GIT_DELIM=`echo $GIT_ARG | sed -n "s/@.*//p" | wc -c`
if [ $GIT_DELIM -ne 0 ]; then
GITREPO=${GIT_ARG:0:(GIT_DELIM-1)}
fi
rem=(GIT_DELIM-${#GIT_ARG})
GITBRANCH=${GIT_ARG:rem}
fi
echo "REPO: $GITREPO"
echo "BRANCH: $GITBRANCH"
if [ $TARGET == "vagrant" ]; then
SSH_CMD="vagrant ssh -c"
elif [ $TARGET == "local" ]; then
SSH_CMD="bash -c"
else
SSH_CMD="ssh $TARGET"
echo "Installing Git..."
$SSH_CMD "sudo apt-get update && sudo apt-get install -y git" 2>>deploy.log 1>>deploy.log
if [ $? -ne 0 ]; then
echo "Failed!"
exit 1
fi
echo "Downloading source..."
$SSH_CMD "sudo mkdir -p /welldone_tmp
sudo chmod a+rw /welldone_tmp
cd /welldone_tmp
git clone $GITREPO
cd strato
git checkout $GITBRANCH
git pull
cd app
npm update" 2>>deploy.log 1>>deploy.log
if [ $? -ne 0 ]; then
echo "Failed!"
exit 1
fi
echo "Backing up existing files..."
$SSH_CMD "sudo mkdir -p /welldone
rm -rf /welldone_backup
sudo mv -f /welldone /welldone_backup" 2>>deploy.log 1>>deploy.log
if [ $? -ne 0 ]; then
echo "Failed!"
exit 1
fi
echo "Hotswapping..."
$SSH_CMD "sudo mv -f /welldone_tmp/strato /welldone
sudo rm -rf /welldone_tmp /welldone_backup" 2>>deploy.log 1>>deploy.log
if [ $? -ne 0 ]; then
echo "Failed! Restoring..."
$SSH_CMD "sudo mv -f /welldone_backup /welldone" 2>>deploy.log 1>>deploy.log
exit 1
fi
fi
if [ -n "$PROVISION" ]; then
echo "Provisioning server"
$SSH_CMD "sudo bash -c 'export APP_CONTEXT=$APP_CONTEXT; /welldone/config/provision.sh'" 2>>deploy.log 1>>deploy.log
fi
echo "Restarting the process(es)..."
$SSH_CMD "set -e
cd /welldone/app
sudo service nginx restart
sudo bash ./control/stop.sh
chmod +x ./control/initdb.sh
sudo -u postgres bash -c './control/initdb.sh $CLEAN'
chmod +x ./control/start.sh
sudo -u application bash -c 'export DATABASE_URL=tcp://dbadmin:GikmnmJKDOB3@localhost:5432/welldone; ./control/start.sh'" 2>>deploy.log 1>>deploy.log
if [ $? -ne 0 ]; then
echo "Failed! Recovering..."
$SSH_CMD "sudo mv -f /welldone_backup /welldone" 2>>deploy.log 1>>deploy.log
exit 1
fi
echo "SUCCESS!"