-
Notifications
You must be signed in to change notification settings - Fork 0
/
php.sh
executable file
·200 lines (162 loc) · 7.79 KB
/
php.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash -ex
#include common functions
MY_PATH=`dirname $(readlink -f "$0")`
source "${MY_PATH}/_utils.sh"
function usage {
echo "Usage:"
echo " $0 -r <packageUrl> -t <releaseDir> [-u <downloadUsername>] [-p <downloadPassword>] [-a <awsCliProfile>]"
echo " -r Package url (http, S3 or local file)"
echo " -t Target release dir normaly a folder where the last path is 'releases' "
echo " -u Download username"
echo " -p Download password"
echo " -a aws cli profile (defaults to 'default')"
echo " -s PATH to the folder where shared assets are stored, YAD_SHARED_FOLDERS need to be set, PATH/YAD_SHARED_FOLDERS[i] linked to target/current/YAD_SHARED_FOLDERS[i]"
echo ""
echo "Optional you can set following Variables: YAD_INSTALL_SCRIPT, YAD_POSTINSTALL_SCRIPT, YAD_POSTDEPLOYMENT_SCRIPT, YAD_ALLOW_REINSTALL"
exit $1
}
if [ -z "${YAD_INSTALL_SCRIPT}" ]; then
echo "Using default YAD_INSTALL_SCRIPT"
YAD_INSTALL_SCRIPT="setup/install.sh"
fi
if [ -z "${AWSCLIPROFILE}" ] ; then
AWSCLIPROFILE=''
fi
EXTRA=0
while getopts 'r:t:u:p:a:' OPTION ; do
case "${OPTION}" in
r) YAD_PACKAGE="${OPTARG}";;
t) YAD_RELEASE_FOLDER="${OPTARG}";;
u) YAD_PACKAGE_USERNAME="${OPTARG}";;
p) YAD_PACKAGE_PASSWORD="${OPTARG}";;
s) YAD_SHARED_FOLDER_BASE="${OPTARG}";;
a) AWSCLIPROFILE="${OPTARG}";;
\?) echo; usage 1;;
esac
done
# Check if releases folder exists
if [ ! -d "${YAD_RELEASE_FOLDER}" ] ; then echo "Releases dir ${YAD_RELEASE_FOLDER} not found"; exit 1; fi
if [ -f "${YAD_RELEASE_FOLDER}/INSTALLING.lock" ] ; then echo "Install already in progress"; exit 1; fi
# Check if shared folder base exists if set
if [ ! -z ${YAD_SHARED_FOLDER_BASE+x} ] && [ ! -d ${YAD_SHARED_FOLDER_BASE} ]; then echo "Shared dir ${YAD_SHARED_FOLDER_BASE} not found"; exit 1; fi
if [ ! -z ${YAD_SHARED_FOLDER_BASE+x} ] && [ -z ${YAD_SHARED_FOLDERS+x} ]; then echo "If you want to symlink shared folders, than "; exit 1; fi
# 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}"
if [ -f "${YAD_RELEASE_FOLDER}/INSTALLING.lock" ]; then rm ${YAD_RELEASE_FOLDER}/INSTALLING.lock; fi
}
trap cleanup EXIT
# Call download function
download $YAD_PACKAGE $TMPDIR 'package.tar.gz'
PACKAGE_BASENAME=`basename $YAD_PACKAGE`
PACKAGE_NAME=${PACKAGE_BASENAME%.*.*}
# Unpack the package
mkdir "${TMPDIR}/package" || { echo "Error while creating temporary package folder" ; exit 1; }
echo "Extracting base package"
tar xzf "${TMPDIR}/package.tar.gz" -C "${TMPDIR}/package" ||
unzip "${TMPDIR}/package.tar.gz" -d "${TMPDIR}/package" ||
{ echo "Error while extracting base package" ; exit 1; }
# Check if package contained subfolder "package"
UNPACKED_FOLDER="${TMPDIR}/package"
if [ -d "${TMPDIR}/package/$PACKAGE_NAME" ] ; then
UNPACKED_FOLDER="${TMPDIR}/package/$PACKAGE_NAME"
fi
# Get buildnumber
if [ -f "${UNPACKED_FOLDER}/version.txt" ] ; then
RELEASENAME=`cat ${UNPACKED_FOLDER}/version.txt`
elif [ -f "${UNPACKED_FOLDER}/build.txt" ] ; then
RELEASENAME=`cat ${UNPACKED_FOLDER}/build.txt`
else
echo "Could not find ${UNPACKED_FOLDER}/version.txt or build.txt! Fallback to timestamp";
RELEASENAME=$(date +%s)
fi
# Check if package contained subfolder with "/$RELEASENAME/"
if [ -d "${TMPDIR}/package/$RELEASENAME" ] ; then
UNPACKED_FOLDER="${TMPDIR}/package/$RELEASENAME"
fi
if [ -z "${RELEASENAME}" ] ; then echo "Error detecting a RELEASENAME!"; exit 1; fi
# check if current release already exist
FINAL_RELEASEFOLDER="${YAD_RELEASE_FOLDER}/${RELEASENAME}"
if [ -d "${FINAL_RELEASEFOLDER}" ]
then
if [ -z ${YAD_ALLOW_REINSTALL+x} ]
then
echo "Release folder ${FINAL_RELEASEFOLDER} already exists";
exit 1;
else
tmpReleaseFolder=$FINAL_RELEASEFOLDER
counter=0;
while [ -d $tmpReleaseFolder ]
do
counter=$((counter + 1))
tmpReleaseFolder="${FINAL_RELEASEFOLDER}_${counter}"
done;
FINAL_RELEASEFOLDER=$tmpReleaseFolder
fi
fi
export FINAL_RELEASEFOLDER
# Add Lock-File
touch ${YAD_RELEASE_FOLDER}/INSTALLING.lock
if [ -x "${YAD_PREINSTALL_SCRIPT}" ] ; then
echo "Executing \"${YAD_PREINSTALL_SCRIPT}\" as pre-install script."
${YAD_PREINSTALL_SCRIPT} -r "${FINAL_RELEASEFOLDER}" || { echo "ERROR!!!! The pre-install script failed!"; exit 1; }
fi
# Move unpacked folder to target path:
mv "${UNPACKED_FOLDER}" "${FINAL_RELEASEFOLDER}" || { echo "Error while moving package ${UNPACKED_FOLDER} folder to ${FINAL_RELEASEFOLDER}" ; exit 1; }
if [ "${YAD_ADD_EXTRA_PACKAGE+x}" ]; then
EXTRAPACKAGE=${YAD_PACKAGE/.tar.gz/.extra.tar.gz}
download $EXTRAPACKAGE $TMPDIR 'package.extra.tar.gz'
echo "Extracting extra package"
tar xzf "${TMPDIR}/package.extra.tar.gz" -C "${FINAL_RELEASEFOLDER}" ||
unzip "${TMPDIR}/package.extra.tar.gz" -d "${FINAL_RELEASEFOLDER}" ||
{ echo "Error while extracting extra package" ; exit 1; }
fi
# Shared folders feature
if [[ ! -z ${YAD_SHARED_FOLDER_BASE+x} ]] && [[ -d ${YAD_SHARED_FOLDER_BASE} ]]
then
for path in ${YAD_SHARED_FOLDERS};
do
if [ -d "${FINAL_RELEASEFOLDER}/${path}" ]; then rm -rf ${FINAL_RELEASEFOLDER}/${path}; fi
ln -s ${YAD_SHARED_FOLDER_BASE}/${path} ${FINAL_RELEASEFOLDER}/${path}
done
fi
# Install the package
if [ ! -f "${FINAL_RELEASEFOLDER}/${YAD_INSTALL_SCRIPT}" ] ; then echo "Could not find installer ${FINAL_RELEASEFOLDER}/${YAD_INSTALL_SCRIPT} - you may want to define another installer with the Variable YAD_INSTALL_SCRIPT" ; exit 1; fi
${FINAL_RELEASEFOLDER}/${YAD_INSTALL_SCRIPT} || { echo "Installing package failed"; exit 1; }
echo
echo "Updating release symlinks"
echo "-------------------------"
cd ${YAD_RELEASE_FOLDER}
echo "Setting next symlink (${YAD_RELEASE_FOLDER}/next) to this release (${RELEASENAME})"
ln -sf "${FINAL_RELEASEFOLDER}" "next" || { echo "Error while symlinking the 'next' folder"; exit 1; }
# If you want to manually check before switching the other symlinks, this would be a good point to stop (maybe add another parameter to this script)
#if [ -n "${CURRENT_BUILD}" ] ; then
# echo "Setting previous (${RELEASES}/previous) to current (${CURRENT_BUILD})"
# ln -sfn "${CURRENT_BUILD}" "${RELEASES}/previous"
#fi
if [[ -h "${YAD_RELEASE_FOLDER}/current" ]] ; then
echo "Setting previous to previous"
ln -sfn "`readlink --canonicalize ${YAD_RELEASE_FOLDER}/current`" "previous"
fi
echo "Settings latest (${YAD_RELEASE_FOLDER}/latest) to release folder (${RELEASENAME})"
ln -sfn "${FINAL_RELEASEFOLDER}" "latest" || { echo "Error while symlinking 'latest' to release folder" ; exit 1; }
if [ -x "${YAD_POSTINSTALL_SCRIPT}" ] ; then
echo "Executing \"${YAD_POSTINSTALL_SCRIPT}\" as postinstall script."
${YAD_POSTINSTALL_SCRIPT} -r "${FINAL_RELEASEFOLDER}" || { echo "ERROR!!!! The postinstall script failed and denied switching the current symlink! This is pretty serious..."; exit 1; }
fi
echo "Settings current (${YAD_RELEASE_FOLDER}/current) to 'latest'"
ln -sfn "latest" "current" || { echo "Error while symlinking 'current' to 'latest'" ; exit 1; }
echo "--> THIS PACKAGE IS LIVE NOW! <--"
# Remove Lock-File
rm ${YAD_RELEASE_FOLDER}/INSTALLING.lock
echo "Deleting next symlink (${YAD_RELEASE_FOLDER}/next)"
unlink "${YAD_RELEASE_FOLDER}/next"
# clean up old releases
YAD_KEEP=${YAD_RELEASES_TO_KEEP:-5}
ls -1t "${YAD_RELEASE_FOLDER}" | egrep -v "current|latest|next|previous|$(basename $FINAL_RELEASEFOLDER)" | sort -r | tail -n +$(($YAD_KEEP+1)) | xargs rm -rf
if [ -x "${YAD_POSTDEPLOYMENT_SCRIPT}" ] ; then
echo "Executing \"${YAD_POSTDEPLOYMENT_SCRIPT}\" as post-deployment script."
${YAD_POSTDEPLOYMENT_SCRIPT} -r "${FINAL_RELEASEFOLDER}" || { echo "ERROR!!!! The post-deployment script failed!"; exit 1; }
fi