This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init
executable file
·322 lines (272 loc) · 9.88 KB
/
init
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/bin/bash
SCRIPT_USE_COLOURS=true
# Colors
if [ "$SCRIPT_USE_COLOURS" = true ]; then
ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"31;01m"
COL_GREEN=$ESC_SEQ"32;01m"
COL_YELLOW=$ESC_SEQ"33;01m"
COL_BLUE=$ESC_SEQ"34;01m"
COL_MAGENTA=$ESC_SEQ"35;01m"
COL_CYAN=$ESC_SEQ"36;01m"
fi
##
# Read config file.
#
# Use first argument as path of config file or fall back to "config.cfg".
CONFIG_FILE=${1:-config.cfg}
if [ ! -f "$CONFIG_FILE" ]; then
echo -e "${COL_RED}Error: Config file \"$CONFIG_FILE\" not found.${COL_RESET}"
echo "Tip: Copy \"sample.cfg\" to your own \"appname.cfg\", edit it, then run \"./init appname.cfg\"."
exit 1
fi
source $CONFIG_FILE
##
# Check some config values.
#
if [ -z "$local_app_path" ]; then
echo -e "${COL_RED}Error: Config value \"local_app_path\" is not set.${COL_RESET}"
exit 1
fi
if [ -z "$app_production_conf" ]; then
echo -e "${COL_RED}Error: Config value \"app_production_conf\" is not set.${COL_RESET}"
exit 1
elif [ ! -f "$local_app_path/conf/$app_production_conf" ]; then
echo -e "$COL_YELLOW Warning: Play application's config file \"$local_app_path/conf/$app_production_conf\" not found. Make sure it exists before you package and deploy the app or check config value \"app_production_conf\" now and start script again. Continuing anyway.${COL_RESET}"
fi
if [ -z "$app_zip_name" ]; then
echo -e "${COL_RED}Error: Config value app_zip_name is not set.${COL_RESET}"
exit 1
fi
if [ ! -d "$local_app_path" ]; then
echo -e "${COL_RED}Error: App path \"$local_app_path\" not found.${COL_RESET}"
exit 1
fi
if [ -z "$local_aws_profile" ]; then
echo -e "${COL_RED}Error: Config value local_aws_profile is not set.${COL_RESET}"
exit 1
fi
if [ -z "$s3_deployment_url" ]; then
echo -e "${COL_RED}Error: Config value s3_deployment_url is not set.${COL_RESET}"
exit 1
fi
if [ ! -f "templates/package-app.template.sh" ]; then
echo -e "${COL_RED}Error: Template templates/package-app.template.sh not found. Run this script from its own directory!${COL_RESET}"
exit 1
fi
if [ -z "$local_app_credentials_file" ]; then
echo -e "$COL_YELLOW Warning: Config value local_app_credentials_file is not set. Continuing anyway.${COL_RESET}"
elif [ ! -f "uploads/$local_app_credentials_file" ]; then
echo -e "${COL_RED}Error: Credentials file \"uploads/$local_app_credentials_file\" not found.${COL_RESET}"
exit 1
fi
if [ ! -z "$local_newrelic_agent_zip" ]; then
if [ ! -f "$local_newrelic_agent_zip" ]; then
echo -e "${COL_RED}Error: New Relic Java Agent \"$local_newrelic_agent_zip\" not found.${COL_RESET}"
exit 1
fi
fi
if [ ! -z "$local_newrelic_agent_yml" ]; then
if [ ! -f "$local_newrelic_agent_yml" ]; then
echo -e "${COL_RED}Error: New Relic Java Agent config \"$local_newrelic_agent_yml\" not found.${COL_RESET}"
exit 1
fi
fi
if [ ! -z "$url_newrelic_monitor" ]; then
if [ -z "$local_newrelic_monitor_cfg" ]; then
echo -e "${COL_RED}Error: Config value \"local_newrelic_monitor_cfg\" is required with \"url_newrelic_monitor\".${COL_RESET}"
exit 1
else
if [ ! -f "$local_newrelic_monitor_cfg" ]; then
echo -e "${COL_RED}Error: New Relic Server Monitor config \"$local_newrelic_monitor_cfg\" not found.${COL_RESET}"
exit 1
fi
fi
fi
if [ ! -z ${newrelic_app_id} ]; then
if [ -z ${newrelic_api_key} ]; then
echo -e "${COL_RED}Error: Config value \"newrelic_api_key\" is required with \"newrelic_app_id\".${COL_RESET}"
exit 1
fi
fi
if [ ! -z ${newrelic_api_key} ]; then
if [ -z ${newrelic_app_id} ]; then
echo -e "${COL_RED}Error: Config value \"newrelic_app_id\" is required with \"newrelic_api_key\".${COL_RESET}"
exit 1
fi
fi
##
# Check if temporary directory can be used
#
if [ -d "temp" ]; then
rm temp/*
#echo -e "${COL_RED}Error: Temporary directory \"temp\" must not exist already beacause it will be created and used by this script.${COL_RESET}"
#exit 1
elif [ -f "temp" ]; then
echo -e "${COL_RED}Error: A file named \"temp\" must not exist beacause it will be created as a directory and used by this script.${COL_RESET}"
exit 1
else
mkdir temp
fi
if [ ! -d "temp" ]; then
echo -e "${COL_RED}Error: Temporary directory \"temp\" could not be created. Please check write permissions of this directory.${COL_RESET}"
exit 1
fi
##
# Generate script used to create and upload ZIP package of the app.
#
SCRIPT_PACKAGE="$local_app_path/package-app"
echo "#!/bin/bash" > $SCRIPT_PACKAGE
echo "app_zip_name=\"$app_zip_name\"" >> $SCRIPT_PACKAGE
echo "local_aws_profile=\"$local_aws_profile\"" >> $SCRIPT_PACKAGE
echo "s3_deployment_url=\"$s3_deployment_url\"" >> $SCRIPT_PACKAGE
cat templates/package-app.template.sh >> $SCRIPT_PACKAGE
chmod 0755 $SCRIPT_PACKAGE
echo -e "${COL_GREEN}Success: Script \"$SCRIPT_PACKAGE\" created.${COL_RESET}"
##
# Generate EC2 prepare script
#
SCRIPT_PREPARE="temp/ec2-prepare"
echo "#!/bin/bash" > $SCRIPT_PREPARE
echo "s3_deployment_url=\"${s3_deployment_url}\"" >> $SCRIPT_PREPARE
if [ -z ${local_newrelic_agent_zip} ]; then
echo "install_newrelic_agent=false" >> $SCRIPT_PREPARE
else
echo "install_newrelic_agent=true" >> $SCRIPT_PREPARE
fi
echo "url_newrelic_monitor=\"${url_newrelic_monitor}\"" >> $SCRIPT_PREPARE
cat templates/ec2-prepare.template.sh >> $SCRIPT_PREPARE
echo -e "${COL_GREEN}Success: Script \"$SCRIPT_PREPARE\" created.${COL_RESET}"
##
# Generate EC2 app update script
#
SCRIPT_DEPUPD="temp/ec2-appupdate"
echo "#!/bin/bash" > $SCRIPT_DEPUPD
echo "app_zip_name=\"${app_zip_name}\"" >> $SCRIPT_DEPUPD
echo "s3_deployment_url=\"${s3_deployment_url}\"" >> $SCRIPT_DEPUPD
echo "credentials_file=\"${local_app_credentials_file}\"" >> $SCRIPT_DEPUPD
cat templates/ec2-appupdate.template.sh >> $SCRIPT_DEPUPD
echo -e "${COL_GREEN}Success: Script \"$SCRIPT_DEPUPD\" created.${COL_RESET}"
##
# Generate EC2 app deployment script
#
SCRIPT_DEPLOY="temp/ec2-deploy"
echo "#!/bin/bash" > $SCRIPT_DEPLOY
echo "app_zip_name=\"${app_zip_name}\"" >> $SCRIPT_DEPLOY
echo "app_production_conf=\"${app_production_conf}\"" >> $SCRIPT_DEPLOY
echo "credentials_file=\"${local_app_credentials_file}\"" >> $SCRIPT_DEPLOY
if [ ! -z ${newrelic_app_id} ]; then
echo "newrelic_app_id=\"${newrelic_app_id}\"" >> $SCRIPT_DEPLOY
echo "newrelic_api_key=\"${newrelic_api_key}\"" >> $SCRIPT_DEPLOY
fi
cat templates/ec2-deploy.template.sh >> $SCRIPT_DEPLOY
echo -e "${COL_GREEN}Success: Script \"$SCRIPT_DEPLOY\" created.${COL_RESET}"
##
# Generate user data file for EC2 launch configuration
#
SCRIPT_USERDATA="temp/ec2-userdata"
echo "#!/bin/bash" > $SCRIPT_USERDATA
echo "yum update -y" >> $SCRIPT_USERDATA
echo "cd /root/" >> $SCRIPT_USERDATA
echo "aws s3 cp s3://spiegeleye-deployment/ec2-prepare ." >> $SCRIPT_USERDATA
echo "chmod 0700 ec2-prepare" >> $SCRIPT_USERDATA
echo "./ec2-prepare" >> $SCRIPT_USERDATA
echo -e "${COL_GREEN}Success: Script \"$SCRIPT_USERDATA\" created.${COL_RESET}"
##
# Print some information
#
echo
echo "The following script should be used to build and upload a standalone version of your app:"
echo " - $SCRIPT_PACKAGE"
echo
echo "The following scripts should be made available to your EC2 app server instances by storing them on S3 location \"$s3_deployment_url\":"
echo " - $SCRIPT_PREPARE"
echo " - $SCRIPT_DEPUPD"
echo " - $SCRIPT_DEPLOY"
echo
echo "The following script should be used as \"user data\" script to launch your EC2 app server instances:"
echo " - $SCRIPT_USERDATA"
echo
##
# Upload scripts to S3
#
while true; do
read -p "Do you wish to upload scripts and credentials to S3 now? " yn
case $yn in
[Yy]*)
echo "Uploading \"$SCRIPT_PREPARE\"..."
aws --profile $local_aws_profile s3 cp "$SCRIPT_PREPARE" $s3_deployment_url && \
echo -e " ${COL_GREEN}OK${COL_RESET}"
echo "Uploading \"$SCRIPT_DEPUPD\"..."
aws --profile $local_aws_profile s3 cp "$SCRIPT_DEPUPD" $s3_deployment_url && \
echo -e " ${COL_GREEN}OK${COL_RESET}"
echo "Uploading \"$SCRIPT_DEPLOY\"..."
aws --profile $local_aws_profile s3 cp "$SCRIPT_DEPLOY" $s3_deployment_url && \
echo -e " ${COL_GREEN}OK${COL_RESET}"
if [ ! -z "$local_app_credentials_file" ]; then
echo "Uploading \"uploads/$local_app_credentials_file\"..."
aws --profile $local_aws_profile s3 cp "uploads/$local_app_credentials_file" $s3_deployment_url && \
echo -e " ${COL_GREEN}OK${COL_RESET}"
fi
break;;
[Nn]*) break;;
*) echo "Please answer y or n.";;
esac
done
##
# Upload New Relic Java Agent to S3 (optional)
#
if [ ! -z ${local_newrelic_agent_zip} ]; then
while true; do
read -p "Do you wish to upload the New Relic Java Agent to S3 now? " yn
case $yn in
[Yy]*)
echo "Uploading \"${local_newrelic_agent_zip}\"..."
TEMP_FILE="temp/newrelic-java.zip"
cp "${local_newrelic_agent_zip}" "${TEMP_FILE}"
aws --profile $local_aws_profile s3 cp "${TEMP_FILE}" $s3_deployment_url && \
echo -e " ${COL_GREEN}OK${COL_RESET}"
rm ${TEMP_FILE}
if [ ! -z ${local_newrelic_agent_yml} ]; then
echo "Uploading \"${local_newrelic_agent_yml}\"..."
TEMP_FILE="temp/newrelic.yml"
cp "${local_newrelic_agent_yml}" "${TEMP_FILE}"
aws --profile $local_aws_profile s3 cp "${TEMP_FILE}" $s3_deployment_url && \
echo -e " ${COL_GREEN}OK${COL_RESET}"
rm ${TEMP_FILE}
fi
break;;
[Nn]*) break;;
*) echo "Please answer y or n.";;
esac
done
fi
##
# Upload New Relic Server Monitor config file
#
if [ ! -z ${local_newrelic_monitor_cfg} ]; then
while true; do
read -p "Do you wish to upload the New Relic Server Monitor config file to S3 now? " yn
case $yn in
[Yy]*)
echo "Uploading \"${local_newrelic_monitor_cfg}\"..."
TEMP_FILE="temp/nrsysmond.cfg"
cp "${local_newrelic_monitor_cfg}" "${TEMP_FILE}"
aws --profile $local_aws_profile s3 cp "${TEMP_FILE}" $s3_deployment_url && \
echo -e " ${COL_GREEN}OK${COL_RESET}"
rm ${TEMP_FILE}
break;;
[Nn]*) break;;
*) echo "Please answer y or n.";;
esac
done
fi
##
# Remove temp directory
#
echo
echo "Done."
echo "Your next step is to build and upload the latest version of your app by running the script \"$SCRIPT_PACKAGE\"."
echo
# rm -r temp