forked from nginxinc/docker-nginx-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
93 lines (72 loc) · 2.85 KB
/
entrypoint.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
#!/bin/sh
#
# This script launches nginx and the NGINX Controller Agent.
#
# If several instances use the same imagename, the metrics will
# be aggregated into a single object in Controller. Otherwise NGINX Controller
# will create separate objects for monitoring (an object per instance).
# Variables
agent_conf_file="/etc/controller-agent/agent.conf"
agent_log_file="/var/log/nginx-controller/agent.log"
nginx_status_conf="/etc/nginx/conf.d/stub_status.conf"
api_key=""
controller_hostname=""
controller_url=""
location=""
# Launch nginx
echo "starting nginx ..."
nginx -g "daemon off;" &
nginx_pid=$!
test -n "${ENV_API_KEY}" && \
api_key=${ENV_API_KEY}
# if controller_hostname is defined in the env vars, use it
test -n "${ENV_CONTROLLER_HOSTNAME}" && \
controller_hostname=${ENV_CONTROLLER_HOSTNAME}
# if controller_hostname is not defined in the env vars, fail back to hostname
test -z "${controller_hostname}" && \
controller_hostname=$(hostname -f)
test -n "${ENV_CONTROLLER_URL}" && \
controller_url=${ENV_CONTROLLER_URL}
test -n "${ENV_LOCATION}" && \
location=${ENV_LOCATION}
if [ -n "${api_key}" -o -n "${controller_hostname}" -o -n "${controller_url}" -o -n "${location}" ]; 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 "${controller_hostname}" && \
echo " ---> using hostname = ${controller_hostname}" && \
sh -c "sed -i.old -e 's/instance_name.*$/instance_name = $controller_hostname/' \
${agent_conf_file}"
test -n "${controller_url}" && \
echo " ---> using controller = ${controller_url}" && \
sh -c "sed -i.old -e 's@api_url.*@api_url = $controller_url@' \
${agent_conf_file}"
test -n "${location}" && \
echo " ---> using location = ${location}" && \
sh -c "sed -i.old -e 's/location_name.*$/location_name = $location/' \
${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 "starting controller-agent ..."
service controller-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."