-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.sh
More file actions
executable file
·304 lines (261 loc) · 5.61 KB
/
Copy pathbundle.sh
File metadata and controls
executable file
·304 lines (261 loc) · 5.61 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#! /usr/bin/env bash
set -e
PS4='+ ${BASH_SOURCE}:${LINENO}: '
shopt -s nullglob
assets=""
workers=()
installexecs=()
xtrace=false
libdir="$(realpath $(dirname $(readlink -f ${BASH_SOURCE[0]})))"
outdir=bundles
userconfigfile="production.yml"
publicdir=public
nginxgroup=www-data
log() {
printf '[bundle] %s\n' "$1"
}
while [[ $# -gt 0 ]]; do
case $1 in
-v)
xtrace=true
shift
;;
--pkg-name)
pkgname="$2"
shift
shift
;;
--namespace)
namespace="$2"
shift
shift
;;
--pkg-dist)
pkgdist="$2"
shift
shift
;;
--pkg-version)
pkgver="$2"
shift
shift
;;
--output-directory)
outdir="$2"
shift
shift
;;
--target-user)
targetuser="$2"
shift
shift
;;
--target-instance)
targetinstance="$2"
shift
shift
;;
--target-domain)
targetdomain="$2"
shift
shift
;;
--target-ssldomain)
targetssldomain="$2"
shift
shift
;;
--target-aliasdomains)
targetaliasdomains="$2"
shift
shift
;;
--assets-manifest)
assetsmanifest="$2"
shift
shift
;;
--configuration-file)
userconfigfile="$2"
shift
shift
;;
--admin-email)
adminemail="$2"
shift
shift
;;
--pyver)
pyver="$2"
shift
shift
;;
--nginx-configfile)
nginxconfigfile="$2"
shift
shift
;;
--nginx-group)
nginxgroup="$2"
shift
shift
;;
--public-directory)
publicdir="$2"
shift
shift
;;
--worker)
workers+=("$2")
shift
shift
;;
--install-exec)
installexecs+=("$2")
shift
shift
;;
--)
shift
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
assets="${assets} $1"
shift
;;
esac
done
if ${xtrace}; then
set -x
fi
# # TODO: delete me
# appcmd="foo"
# instance="reelio"
# for worker in "${workers[@]}"; do
# eval "unitcmd=\"${worker}\""
# IFS='=' read -r worker_name worker_cmd <<< "$unitcmd"
# worker_service="${instance}-${worker_name}.service"
# echo ${worker_service}
# done
# declare -p workers >> deleteme
# exit
# validation
if [ -z "${pkgname}" ]; then
echo "--pkg-name required" >&2
exit 1
fi
if [ -z "${namespace}" ]; then
namespace=${pkgname}
fi
if [ -z "${pkgver}" ]; then
echo "--pkg-version required" >&2
exit 1
fi
if [ -z "${pkgdist}" ]; then
echo "--pkg-dist required" >&2
exit 1
fi
if [ -z "${targetuser}" ]; then
echo "--target-user required" >&2
exit 1
fi
if [ -z "${targetinstance}" ]; then
echo "--target-instance required" >&2
exit 1
fi
if [ -z "${targetdomain}" ]; then
echo "--target-domain required" >&2
exit 1
fi
if [ -z "${targetssldomain}" ]; then
targetssldomain=${targetdomain}
fi
if [ ! -f "${userconfigfile}" ]; then
echo "cannot find ${userconfigfile}, please provide this file or specify \
another file with --configuration-file option." >&2
fi
if [ -z "${adminemail}" ]; then
echo "--admin-email required" >&2
exit 1
fi
if [ -z "${pyver}" ]; then
echo "--pyver required" >&2
exit 1
fi
if [ -z "${nginxconfigfile}" ]; then
nginxconfigfile=${targetdomain}
fi
# setup vars
bundlename=${pkgname}-bundle-${pkgver}-${targetdomain}
bundledir=${outdir}/${bundlename}
# create a temporary directory
log "Creating bundle directory: ${bundledir}"
mkdir -p ${bundledir}
# cleanup the pre-existing files
if [ -d ${bundledir} ]; then
log "Cleaning existing bundle contents: ${bundledir}"
rm -fr ${bundledir}/*
rm -fr ${bundledir}/.*
fi
# copy python distribution
log "Copying Python distribution to ${bundledir}"
cp ${pkgdist} ${bundledir}
# assets
if [ -n "${assets}" ]; then
log "Creating assets directory: ${bundledir}/assets"
mkdir -p ${bundledir}/assets
log "Copying assets to ${bundledir}/assets"
cp ${assets} ${bundledir}/assets
fi
if [ -n "${assetsmanifest}" ]; then
log "Copying assets manifest to ${bundledir}/assets-manifest.json"
cp ${assetsmanifest} ${bundledir}/assets-manifest.json
fi
# public files
if [ -d "${publicdir}" ] && [ -n "$(ls -A ${publicdir})" ]; then
log "Creating public directory: ${bundledir}/public"
mkdir -p ${bundledir}/public
log "Copying public files to ${bundledir}/public"
cp -r ${publicdir}/* ${bundledir}/public
fi
# install.sh
log "Copying installation scripts to ${bundledir}"
cp ${libdir}/target-install.sh ${bundledir}/install.sh
cp ${libdir}/target-uninstall.sh ${bundledir}/uninstall.sh
log "Making installation scripts executable"
chmod +x ${bundledir}/install.sh
chmod +x ${bundledir}/uninstall.sh
# user configuration file
log "Copying user configuration to ${bundledir}"
cp ${userconfigfile} ${bundledir}
# vars
log "Writing bundle configuration: ${bundledir}/.vars"
echo -n "\
pydist=$(basename ${pkgdist})
pypkg=${pkgname}
pynamespace=${namespace}
user=${targetuser}
instance=${targetinstance}
domain=${targetdomain}
ssldomain=${targetssldomain}
aliasdomains=${targetaliasdomains}
userconfigfile=${userconfigfile}
adminemail=${adminemail}
pyver=${pyver}
nginxconfigfile=${nginxconfigfile}
nginxgroup=${nginxgroup}
" > ${bundledir}/.vars
declare -p workers >> ${bundledir}/.vars
declare -p installexecs >> ${bundledir}/.vars
# bundle
outfile=${outdir}/${bundlename}.tar.gz
log "Creating bundle archive: ${outfile}"
tar -cv -C ${outdir} -f ${outfile} ${bundlename}
# cleanup
log "Removing temporary bundle directory: ${bundledir}"
rm -r ${outdir}/${bundlename}
# report
echo "bundle successfully generated: ${outfile}"