-
Notifications
You must be signed in to change notification settings - Fork 22
/
deploy.sh
executable file
·177 lines (143 loc) · 7.68 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/bash
# Magento Deployment Script
# Author: Fabrizio Branca
function usage {
echo ""
echo "Usage:"
echo ""
echo "$0 -r <packageUrl> -t <targetDir> -e <environment> [-u <downloadUsername>] [-p <downloadPassword>] [-a <awsCliProfile>] [-d] [-c]"
echo ""
echo " -r Package url (http, S3 or local file)"
echo " -t Target dir"
echo " -d Also download and install .extra.tar.gz package"
echo ""
echo " [http(s)]"
echo " -u Download username (only used for packages via http(s))"
echo " -p Download password (only used for packages via http(s))"
echo ""
echo " [aws cli (default)]"
echo " -a aws cli profile, defaults to 'default' (only used for aws cli, not s3cmd)"
echo ""
echo " [s3cmd]"
echo " -c Use s3cmd instead of aws cli to download the package"
echo ""
echo ""
echo "Examples:"
echo " Install extra package on devbox (note the -d option):"
echo " B=42; $0 -d -e devbox -r s3://mybucket/jobs/acme_build/$B/acme.tar.gz -t /var/www/acme/devbox/ -a awscliprofile"
echo " Install production package:"
echo " B=42; $0 -e production -r s3://mybucket/jobs/acme_build/$B/acme.tar.gz -t /var/www/acme/production/ -a awscliprofile"
echo " Install via http:"
echo " B=42; $0 -e production -r http://jenkins/jobs/acme_build/$B/acme.tar.gz -u username -p password -t /var/www/acme/production/"
echo " Install via local file:"
echo " B=42; $0 -e production -r /path/to/$B/acme.tar.gz -t /var/www/acme/integration/"
echo " Use s3cmd instead of aws cli (note the -c option). s3cmd must already be configured at this point."
echo " B=42; $0 -e devbox -c s3://mybucket/jobs/acme_build/$B/acme.tar.gz -t /var/www/acme/production/"
echo ""
exit $1
}
AWSCLIPROFILE=''
EXTRA=0
USES3CMD=0
while getopts 'r:t:u:p:e:a:dc' OPTION ; do
case "${OPTION}" in
r) PACKAGEURL="${OPTARG}";;
t) ENVROOTDIR="${OPTARG%/}";;
u) USERNAME="${OPTARG}";;
p) PASSWORD="${OPTARG}";;
e) ENVIRONMENT="${OPTARG}";;
a) AWSCLIPROFILE="${OPTARG}";;
d) EXTRA=1;;
c) USES3CMD=1;;
\?) echo; usage 1;;
esac
done
if [ -z "${PACKAGEURL}" ]; then echo "ERROR: Please provide package url (e.g. -r s3://mybucket/demo.tar.gz)"; usage 1; fi
if [ -z "${ENVROOTDIR}" ]; then echo "ERROR: Please provide a target dircteory (e.g. -t /var/www/demo/)"; usage 1; fi
if [ -z "${ENVIRONMENT}" ]; then echo "ERROR: Please provide an environment code (e.g. -e staging)"; usage 1; fi
# Check if releases folder exists
RELEASES="${ENVROOTDIR}/releases"
RELEASENAME="build_$(date +%Y%m%d%H%M%S)"
RELEASEFOLDER="${RELEASES}/${RELEASENAME}"
if [ ! -d "${RELEASES}" ] ; then echo "Releases dir ${RELEASES} not found"; usage 1; fi
if [ -d "${RELEASEFOLDER}" ] ; then echo "Release folder ${RELEASEFOLDER} already exists"; exit 1; fi
# Check if the shared folder exists (but creating the symlinks is the installer script's responsibilty)
SHAREDFOLDER="${ENVROOTDIR}/shared"
SHAREDFOLDERS=( "var" "media" )
if [ ! -d "${SHAREDFOLDER}" ] ; then echo "Shared folder ${SHAREDFOLDER} not found"; exit 1; fi
for i in "${SHAREDFOLDERS[@]}" ; do if [ ! -d "${SHAREDFOLDER}/$i" ] ; then echo "Shared folder ${SHAREDFOLDER}/$i not found"; exit 1; fi; done
# Create tmp dir and make sure it's going to be deleted in any case
TMPDIR=`mktemp -d`
function cleanup {
echo "Removing temp dir ${TMPDIR}"
rm -rf "${TMPDIR}"
}
trap cleanup EXIT
EXTRAPACKAGEURL=${PACKAGEURL/.tar.gz/.extra.tar.gz}
########################################################################################################################
# Step 1: get the package via http, S3 or local file
########################################################################################################################
if [ -f "${PACKAGEURL}" ] ; then
cp "${PACKAGEURL}" "${TMPDIR}/package.tar.gz" || { echo "Error while copying base package" ; exit 1; }
if [ "${EXTRA}" == 1 ] ; then
cp "${EXTRAPACKAGEURL}" "${TMPDIR}/package.extra.tar.gz" || { echo "Error while copying extra package" ; exit 1; }
fi
elif [[ "${PACKAGEURL}" =~ ^https?:// ]] ; then
if [ ! -z "${USERNAME}" ] && [ ! -z "${PASSWORD}" ] ; then
CREDENTIALS="--user='${USERNAME}' --password='${PASSWORD}'"
fi
echo "Downloading package via http"
wget --auth-no-challenge "${CREDENTIALS}" "${PACKAGEURL}" -O "${TMPDIR}/package.tar.gz" || { echo "Error while downloading base package from http" ; exit 1; }
if [ "${EXTRA}" == 1 ] ; then
echo "Downloading extra package via http"
wget --auth-no-challenge "${CREDENTIALS}" "${EXTRAPACKAGEURL}" -O "${TMPDIR}/package.extra.tar.gz" || { echo "Error while downloading extra package from http" ; exit 1; }
fi
elif [[ "${PACKAGEURL}" =~ ^s3:// ]] ; then
echo -n "Downloading base package via S3"
PROFILEPARAM=""
if [ ! -z "${AWSCLIPROFILE}" ] ; then
PROFILEPARAM="--profile ${AWSCLIPROFILE}"
fi
if [ "${USES3CMD}" == 0 ] ; then
echo " (via aws cli)";
aws ${PROFILEPARAM} s3 cp "${PACKAGEURL}" "${TMPDIR}/package.tar.gz" || { echo "Error while downloading base package from S3" ; exit 1; }
else
echo " (via s3cmd)";
s3cmd get "${PACKAGEURL}" "${TMPDIR}/package.tar.gz" || { echo "Error while downloading base package from S3" ; exit 1; }
fi
if [ "${EXTRA}" == 1 ] ; then
echo -n "Downloading extra package via S3"
if [ "${USES3CMD}" == 0 ] ; then
echo " (via aws cli)";
aws ${PROFILEPARAM} s3 cp "${EXTRAPACKAGEURL}" "${TMPDIR}/package.extra.tar.gz" || { echo "Error while downloading extra package from S3" ; exit 1; }
else
echo " (via s3cmd)";
s3cmd get "${EXTRAPACKAGEURL}" "${TMPDIR}/package.extra.tar.gz" || { echo "Error while downloading extra package from S3" ; exit 1; }
fi
fi
fi
########################################################################################################################
# Step 2: extract files into release folder
########################################################################################################################
echo "Creating release folder"
mkdir "${RELEASEFOLDER}" || { echo "Error while creating release folder" ; exit 1; }
echo "Extracting base package"
tar xzf "${TMPDIR}/package.tar.gz" -C "${RELEASEFOLDER}" || { echo "Error while extracting base package" ; exit 1; }
if [ "${EXTRA}" == 1 ] ; then
echo "Extracting extra package on top of base package"
tar xzf "${TMPDIR}/package.extra.tar.gz" -C "${RELEASEFOLDER}" || { echo "Error while extracting extra package" ; exit 1; }
fi
########################################################################################################################
# Step 3: Trigger installation
########################################################################################################################
if [ ! -f "${RELEASEFOLDER}/tools/install.sh" ] ; then echo "Could not find installer" ; exit 1; fi
${RELEASEFOLDER}/tools/install.sh -e "${ENVIRONMENT}" || { echo "Installing package failed"; exit 1; }
########################################################################################################################
# Step 4: Update symlink
########################################################################################################################
echo
echo "Updating release symlink"
echo "------------------------"
echo "Settings current (${RELEASES}/current) to release folder (${RELEASENAME})"
ln -sfn "${RELEASENAME}" "${RELEASES}/current" || { echo "Error while symlinking 'current' to release folder" ; exit 1; }
echo "--> THIS PACKAGE IS LIVE NOW! <--"