forked from TritonDataCenter/manta-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.sh
275 lines (242 loc) · 8.8 KB
/
util.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
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
# -*- mode: shell-script; fill-column: 80; -*-
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
#
# Copyright 2019 Joyent, Inc.
#
#
# scripts/common/util.sh: common routines for configuring a Manta zone. This
# script is typically included by submodule in each Manta component repo.
#
#
# The zone's metadata is downloaded from SAPI and saved in this file.
#
export METADATA=/var/tmp/metadata.json
export SAPI_URL=$(mdata-get SAPI_URL)
#
# fatal MESSAGE ...: dump MESSAGE to stderr and exit as a failure
#
function fatal {
echo "$(basename $0): fatal error: $*" >&2
exit 1
}
#
# warn MESSAGE ...: dump MESSAGE to stderr but do not exit
#
function warn {
echo "$(basename $0): WARNING: $*" >&2
}
#
# manta_add_logadm_entry PATTERN [LOGDIR [MATCH-MODE]]: creates an entry in
# /etc/logadm.conf for hourly log rotation of files matching PATTERN in LOGDIR.
# Logs are rotated into /var/log/manta and eventually uploaded back to Manta.
# See services.sh for details on how this works.
#
# If LOGDIR is not specified, it defaults to /var/svc/log.
#
# By default, we'll use a fuzzy match (all files matching $LOGDIR/*$PATTERN*)
# and concatenate all matching files. If MATCH-MODE is "exact", then we'll only
# match $LOGDIR/$PATTERN.
#
function manta_add_logadm_entry {
[[ $# -ge 1 ]] || fatal "add_logadm_entry requires at least 1 argument"
local logdir="/var/svc/log"
if [[ $# -ge 2 ]]; then
logdir="$2"
fi
local pattern="$logdir/*$1*.log"
if [[ $# -ge 3 ]] && [[ $3 == "exact" ]]; then
pattern="$logdir/$1.log"
fi
logadm -w $1 -C 48 -c -p 1h \
-t "/var/log/manta/upload/$1_\$nodename_%FT%H:00:00.log" \
"$pattern" || fatal "unable to create logadm entry"
}
#
# manta_add_logadm_entry2 PATTERN [LOGDIR [MATCH-MODE]]: creates an entry in
# /etc/logadm.conf for hourly log rotation of files matching PATTERN in LOGDIR.
# Logs are rotated into /var/log/manta and eventually uploaded back to Manta.
# See services.sh for details on how this works.
#
# If LOGDIR is not specified, it defaults to /var/svc/log.
#
# By default, we'll use a fuzzy match (all files matching $LOGDIR/*$PATTERN*)
# and concatenate all matching files. If MATCH-MODE is "exact", then we'll only
# match $LOGDIR/$PATTERN.
#
# This function differs from manta_add_logadm_entry in that more granular
# datetime information is used to name the rotated file and the colons are
# omitted from the datetime information in the filename.
#
function manta_add_logadm_entry2 {
[[ $# -ge 1 ]] || fatal "add_logadm_entry2 requires at least 1 argument"
local logdir="/var/svc/log"
if [[ $# -ge 2 ]]; then
logdir="$2"
fi
local pattern="$logdir/*$1*.log"
if [[ $# -ge 3 ]] && [[ $3 == "exact" ]]; then
pattern="$logdir/$1.log"
fi
logadm -w $1 -C 48 -c -p 1h \
-t "/var/log/manta/upload/$1_\$nodename_%Y%m%dT%H%M%S.log" \
"$pattern" || fatal "unable to create logadm entry"
}
#
# manta_ensure_moray MORAY_HOST: waits up to about 90 seconds for a moray shard
# to come online. It's a fatal error if this doesn't happen within the allotted
# timeout.
#
function manta_ensure_moray {
[[ $# -ge 1 ]] || fatal "manta_ensure_moray requires at least 1 argument"
local attempt=0
local now
local isok=0
while [[ $attempt -lt 90 ]]; do
now=$(sql -h $1 -p 2020 'select now();' | json now)
if [[ $? -eq 0 ]] && [[ -n "$now" ]]; then
isok=1
break
fi
let attempt=attempt+1
sleep 1
done
[[ $isok -eq 1 ]] || fatal "moray $1 is not up"
}
#
# manta_ensure_zk: waits up to about 60 seconds for the local Zookeeper cluster
# to come online. It's a fatal error if this doesn't happen within the allotted
# timeout.
#
function manta_ensure_zk {
local attempt=0
local isok=0
local zkok
local zonename=$(zonename)
local zk_ips=$(json -f ${METADATA} ZK_SERVERS | json -a host)
if [[ $? -ne 0 ]]; then
zk_ips=127.0.0.1
fi
while [[ $attempt -lt 60 ]]; do
for ip in $zk_ips; do
zkok=$(echo "ruok" | nc -w 1 $ip 2181)
if [[ $? -eq 0 ]] && [[ "$zkok" == "imok" ]]; then
isok=1
break
fi
done
if [[ $isok -eq 1 ]]; then
break
fi
let attempt=attempt+1
sleep 1
done
[[ $isok -eq 1 ]] || fatal "ZooKeeper is not running"
}
#
# manta_clear_dns_except_sdc: reconfigure DNS. If the external network is the
# primary network for this zone, external DNS servers will be first in the list
# of resolvers. As this zone is setting up, the config-agent can't resolve the
# SAPI hostname (e.g. "sapi.coal.joyent.us") and zone setup will fail.
#
# Here, remove all resolvers but the SDC resolver so setup can finish
# appropriately. The config-agent will rewrite the /etc/resolv.conf file with
# the proper resolvers later, so this just allows that agent to discover and
# download the appropriate zone configuration.
#
function manta_clear_dns_except_sdc {
if [[ -z $SAPI_URL ]]; then
fatal "SAPI_URL not set"
fi
local sapi_hostname=$(basename $SAPI_URL)
if [[ -z $sapi_hostname ]] || [[ $sapi_hostname != *sapi* ]]; then
fatal "$sapi_hostname isn't recognizable as sapi"
fi
local sdc_resolver=''
local resolvers=$(cat /etc/resolv.conf | grep nameserver | \
cut -d ' ' -f 2 | tr '\n' ' ')
for resolver in $resolvers; do
local sapi_ip;
sapi_ip=$(dig @$resolver $sapi_hostname +short)
if [[ $? != 0 ]]; then
echo "$resolver was unavailable to resolve $sapi_hostname"
continue
fi
if [[ -n "$sapi_ip" ]]; then
sdc_resolver="$resolver"
break
else
echo "$resolver did not resolve $sapi_hostname"
fi
done
if [[ -z "$sdc_resolver" ]]; then
fatal "No resolvers were able to resolve $sapi_hostname"
fi
cat /etc/resolv.conf | grep -v nameserver > /tmp/resolv.conf
echo "nameserver $sdc_resolver" >> /tmp/resolv.conf
mv /tmp/resolv.conf /etc/resolv.conf
}
#
# manta_update_dns: This used to configure /etc/resolv.conf, but this
# placeholder remains only to satisfy consumers still calling it.
#
function manta_update_dns {
return 0
}
#
# manta_update_env: updates the $HOME directory of the root user to have various
# useful tools in their PATH and several useful aliases configured. This
# requires the global varaible SVC_ROOT to be set (from which we acquire node).
#
function manta_update_env {
echo "Updating ~/.bashrc (and environment)"
local RC=/root/.bashrc
#
# First create the default skeleton entry. We rewrite this every time,
# or it keeps getting appended to on reboot.
#
echo "" > /root/.bashrc
echo 'if [ "$PS1" ]; then' >> $RC
echo ' shopt -s checkwinsize cdspell extglob histappend' >> $RC
echo " alias ll='ls -lF'" >> $RC
echo ' HISTCONTROL=ignoreboth' >> $RC
echo ' HISTIGNORE="[bf]g:exit:quit"' >> $RC
echo ' PS1="[\u@\h \w]\\$ "' >> $RC
echo ' if [ -n "$SSH_CLIENT" ]; then' >> $RC
echo -n " PROMPT_COMMAND=" >> $RC
echo -n "'echo -ne " >> $RC
echo -n '\033]0;${HOSTNAME%%\.*} \007" && history -a' >> $RC
echo "'" >> $RC
echo " fi" >> $RC
echo "fi" >> $RC
#
# Now write the stuff we care about, starting with $PATH.
#
echo "export PATH=$SVC_ROOT/build/node/bin:$SVC_ROOT/node_modules/.bin:/opt/smartdc/configurator/bin:/opt/local/bin:\$PATH" >> $RC
local hostname=`hostname | cut -c1-8`
local role=$(mdata-get sdc:tags.manta_role)
if [[ $? -ne 0 ]]; then
role="unknown"
fi
echo "export PS1=\"[\\u@$hostname ($role) \\w]$ \"" >> $RC
echo "alias bunyan='bunyan --color'" >> $RC
echo "alias less='less -R'" >> $RC
#
# The SSH key will already be there (written by the config-agent). Just
# update its permissions.
#
if [[ -f /root/.ssh/id_rsa ]]; then
chmod 600 /root/.ssh/id_rsa
fi
local manta_url=$(json -f ${METADATA} MANTA_URL)
[[ $? -eq 0 ]] || fatal "Unable to retrieve MANTA_URL from metadata"
echo "export MANTA_USER=poseidon" >> /root/.bashrc
echo "export MANTA_KEY_ID=\$(ssh-keygen -l -f /root/.ssh/id_rsa.pub | awk '{print \$2}')" >> /root/.bashrc
echo "export MANTA_URL=$manta_url" >> /root/.bashrc
local manta_tls_insecure=$(json -f ${METADATA} MANTA_TLS_INSECURE)
echo "export MANTA_TLS_INSECURE=$manta_tls_insecure" >> /root/.bashrc
echo "alias js2json='node -e '\''s=\"\"; process.stdin.resume(); process.stdin.on(\"data\",function(c){s+=c}); process.stdin.on(\"end\",function(){o=eval(\"(\"+s+\")\");console.log(JSON.stringify(o)); });'\'''" >> /root/.bashrc
}