-
Notifications
You must be signed in to change notification settings - Fork 2
/
start.sh
82 lines (65 loc) · 2.41 KB
/
start.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
#!/bin/sh
#
# This script launches nginx and the NGINX Amplify Agent.
#
# Unless already baked in the image, a real API_KEY is required for the
# NGINX Amplify Agent to be able to connect to the backend.
#
# If AMPLIFY_IMAGENAME is set, the script will use it to generate
# the 'imagename' to put in the /etc/amplify-agent/agent.conf
#
# If several instances use the same imagename, the metrics will
# be aggregated into a single object in Amplify. Otherwise NGINX Amplify
# will create separate objects for monitoring (an object per instance).
#
# Variables
agent_conf_file="/etc/amplify-agent/agent.conf"
agent_log_file="/var/log/amplify-agent/agent.log"
nginx_status_conf="/etc/nginx/conf.d/stub_status.conf"
api_key=""
amplify_imagename=""
# Launch nginx
echo "starting nginx ..."
nginx -g "daemon off;" &
nginx_pid=$!
test -n "${API_KEY}" && \
api_key=${API_KEY}
test -n "${AMPLIFY_IMAGENAME}" && \
amplify_imagename=${AMPLIFY_IMAGENAME}
if [ -n "${api_key}" -o -n "${amplify_imagename}" ]; then
echo "updating ${agent_conf_file} ..."
if [ ! -f "${agent_conf_file}" ]; then
test -f "${agent_conf_file}.default" && \
cp -p "${agent_conf_file}.default" "${agent_conf_file}" || \
{ echo "no ${agent_conf_file}.default found! exiting."; exit 1; }
fi
test -n "${api_key}" && \
echo " ---> using api_key = ${api_key}" && \
sh -c "sed -i.old -e 's/api_key.*$/api_key = $api_key/' \
${agent_conf_file}"
test -n "${amplify_imagename}" && \
echo " ---> using imagename = ${amplify_imagename}" && \
sh -c "sed -i.old -e 's/imagename.*$/imagename = $amplify_imagename/' \
${agent_conf_file}"
test -f "${agent_conf_file}" && \
chmod 644 ${agent_conf_file} && \
chown nginx ${agent_conf_file} > /dev/null 2>&1
test -f "${nginx_status_conf}" && \
chmod 644 ${nginx_status_conf} && \
chown nginx ${nginx_status_conf} > /dev/null 2>&1
fi
if ! grep '^api_key.*=[ ]*[[:alnum:]].*' ${agent_conf_file} > /dev/null 2>&1; then
echo "no api_key found in ${agent_conf_file}! exiting."
fi
echo
echo "starting filebeat ..."
filebeat modules enable nginx
filebeat -E cloud.id="${ES_CLOUD_ID}" -E cloud.auth="${ES_USER}:${ES_PASSWORD}" &
echo "starting amplify-agent ..."
service amplify-agent start > /dev/null 2>&1 < /dev/null
if [ $? != 0 ]; then
echo "couldn't start the agent, please check ${agent_log_file}"
exit 1
fi
wait ${nginx_pid}
echo "nginx master process has stopped, exiting."