This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
/
agent_ctl.sh.erb
139 lines (105 loc) · 3.51 KB
/
agent_ctl.sh.erb
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
#!/bin/bash -exu
SCRIPT_NAME=$(basename $0)
LOG_DIR=/var/vcap/sys/log/consul_agent
RUN_DIR=/var/vcap/sys/run/consul_agent
DATA_DIR=/var/vcap/store/consul_agent
CONF_DIR=/var/vcap/jobs/consul_agent/config
CERT_DIR=$CONF_DIR/certs
PKG=/var/vcap/packages/consul
JOB_DIR=/var/vcap/jobs/consul_agent
PIDFILE=$RUN_DIR/consul_agent.pid
CONFAB_PACKAGE=/var/vcap/packages/confab
mkdir -p "${LOG_DIR}"
chown -R vcap:vcap "${LOG_DIR}"
exec > >(tee -a >(logger -p user.info -t vcap.${SCRIPT_NAME}.stdout) | awk -W interactive '{lineWithDate="echo [`date +\"%Y-%m-%d %H:%M:%S%z\"`] \"" $0 "\""; system(lineWithDate) }' >>$LOG_DIR/${SCRIPT_NAME}.log)
exec 2> >(tee -a >(logger -p user.error -t vcap.${SCRIPT_NAME}.stderr) | awk -W interactive '{lineWithDate="echo [`date +\"%Y-%m-%d %H:%M:%S%z\"`] \"" $0 "\""; system(lineWithDate) }' >>$LOG_DIR/${SCRIPT_NAME}.err.log)
function setup_resolvconf() {
local resolvconf_file
resolvconf_file=/etc/resolvconf/resolv.conf.d/head
if ! grep -qE '127.0.0.1\b' "${resolvconf_file}"; then
if [[ "$(stat -c "%s" "${resolvconf_file}")" = "0" ]]; then
echo 'nameserver 127.0.0.1' > "${resolvconf_file}"
else
sed -i -e '1i nameserver 127.0.0.1' "${resolvconf_file}"
fi
fi
resolvconf -u
}
function create_directories_and_chown_to_vcap() {
mkdir -p "${RUN_DIR}"
chown -R vcap:vcap "${RUN_DIR}"
mkdir -p "${DATA_DIR}"
chown -R vcap:vcap "${DATA_DIR}"
mkdir -p "${CONF_DIR}"
chown -R vcap:vcap "${CONF_DIR}"
chown vcap:vcap ${CERT_DIR}/*.{crt,key}
chmod 640 ${CERT_DIR}/*.{crt,key}
}
function set_virtual_memory() {
# "Consul uses a significant amount of virtual memory, since LMDB uses
# mmap() underneath. It uses about 700MB of a 32bit system and 40GB on a
# 64bit system."
#
# this mainly applies to bosh-lite
ulimit -v unlimited
ulimit -n 4096
}
function start_confab() {
set -exu
local confab_package
confab_package=$1
local job_dir
job_dir=$2
local log_dir
log_dir=$3
exec > >(tee -a ${log_dir}/consul_agent.stdout.log | logger -p user.info -t vcap.consul-agent)
exec 2> >(tee -a ${log_dir}/consul_agent.stderr.log | logger -p user.error -t vcap.consul-agent)
local nameservers
nameservers=("$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}' | grep -Ev '127.0.0.1\b')")
local recursors
recursors=""
for nameserver in ${nameservers[@]}; do
recursors="${recursors} -recursor=${nameserver}"
done
"${confab_package}/bin/confab" \
start \
${recursors} \
--config-file "${job_dir}/confab.json"
}
export -f start_confab
function stop_confab() {
set -exu
local confab_package
confab_package=$1
local job_dir
job_dir=$2
local log_dir
log_dir=$3
exec > >(tee -a ${log_dir}/consul_agent.stdout.log | logger -p user.info -t vcap.consul-agent)
exec 2> >(tee -a ${log_dir}/consul_agent.stderr.log | logger -p user.error -t vcap.consul-agent)
"${confab_package}/bin/confab" \
stop \
--config-file "${job_dir}/confab.json"
}
export -f stop_confab
function setup() {
create_directories_and_chown_to_vcap
set_virtual_memory
setup_resolvconf
setcap cap_net_bind_service=+ep ${PKG}/bin/consul
}
function main() {
case ${1} in
start)
setup
su vcap -c "start_confab ${CONFAB_PACKAGE} ${JOB_DIR} ${LOG_DIR}" &> /dev/null
;;
stop)
su vcap -c "stop_confab ${CONFAB_PACKAGE} ${JOB_DIR} ${LOG_DIR}" &> /dev/null
;;
*)
echo "Usage: ${0} {start|stop}"
;;
esac
}
main $@