-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsetup_manta_zone.sh
executable file
·243 lines (197 loc) · 6.31 KB
/
setup_manta_zone.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
#!/usr/bin/bash
#
# 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 (c) 2014, Joyent, Inc.
#
#
# setup_manta_zone.sh: bootstrap a manta deployment zone
#
# BASHSTYLED
export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -o xtrace
PATH=/opt/smartdc/bin:$PATH
ZONE_ALIAS=manta0
function fatal {
echo "$(basename $0): fatal error: $*" >&2
exit 1
}
function add_external_nic {
local zone_uuid=$1
local external_net_uuid
external_net_uuid=$(sdc-napi /networks?nic_tag=external |
json -Ha uuid)
local tmpfile=/tmp/update_nics.$$.json
local num_nics
num_nics=$(sdc-vmapi /vms/${zone_uuid} | json -H nics.length);
if [[ ${num_nics} == 2 ]]; then
return # External NIC already present
fi
echo "Adding external NIC to ${zone_uuid}"
echo "
{
\"networks\": [
{
\"uuid\": \"${external_net_uuid}\",
\"primary\": true
}
]
}" > ${tmpfile}
sdc-vmapi /vms/${zone_uuid}?action=add_nics -X POST \
-d @${tmpfile}
[[ $? -eq 0 ]] || fatal "failed to add external NIC"
# The add_nics job takes about 20 seconds.
sleep 30
rm -f ${tmpffile}
}
function import_manta_image {
local manifest
manifest=$(ls -r1 /usbkey/datasets/manta-d*imgmanifest | head -n 1)
local file
file=$(ls -r1 /usbkey/datasets/manta-d*gz | head -n 1)
local uuid
uuid=$(json -f ${manifest} uuid)
echo $(basename ${manifest}) > /usbkey/zones/manta/dataset
# If image already exists, don't import again.
sdc-imgadm get ${uuid} >/dev/null
if [[ $? -eq 0 ]]; then
return
fi
sdc-imgadm import -m ${manifest} -f ${file}
[[ $? -eq 0 ]] || fatal "failed to import image"
}
function deploy_manta_zone {
local service_uuid server_uuid
service_uuid=$(sdc-sapi /services?name=manta | json -Ha uuid)
server_uuid=$(sysinfo | json UUID)
if [[ -z "$server_uuid" ]]; then
fatal "could not find appropriate server_uuid"
fi
echo "
{
\"service_uuid\": \"${service_uuid}\",
\"params\": {
\"alias\": \"${ZONE_ALIAS}\",
\"server_uuid\": \"${server_uuid}\"
}
}" | sapiadm provision
[[ $? -eq 0 ]] || fatal "failed to provision manta zone"
}
function enable_firewall {
local zone_uuid=$1
vmadm update ${zone_uuid} firewall_enabled=true
[[ $? -eq 0 ]] || fatal "failed to enable firewall for the manta zone"
}
# Wait for /opt/smartdc/manta-deployment/etc/config.json to be written out
# by config-agent.
function wait_for_config_agent {
local CONFIG_PATH=/opt/smartdc/manta-deployment/etc/config.json
local MANTA_ZONE
MANTA_ZONE=$(vmadm lookup -1 alias=${ZONE_ALIAS})
echo "Wait up to a minute for config-agent to write '$CONFIG_PATH'."
local ZONE_CONFIG_PATH=/zones/$MANTA_ZONE/root$CONFIG_PATH
for i in {1..30}; do
if [[ -f "$ZONE_CONFIG_PATH" ]]; then
break
fi
sleep 2
done
if [[ ! -f "$ZONE_CONFIG_PATH" ]]; then
fatal "Timeout waiting for '$ZONE_CONFIG_PATH' to be written."
else
echo "'$CONFIG_PATH' created in manta zone."
fi
}
function wait_for_manta_zone {
local zone_uuid=$1
local state="unknown"
for i in {1..60}; do
state=$(vmadm lookup -j alias alias=${ZONE_ALIAS} | json -ga zone_state)
if [[ "running" == "$state" ]]; then
break
fi
sleep 1
done
if [[ "$state" != "running" ]]; then
fatal "manta zone isn't running after reboot"
else
echo "manta zone running"
fi
}
# Copy manta tools into the GZ from the manta zone
function copy_manta_tools {
local zone_uuid=$1
local target
if [[ -n ${zone_uuid} ]]; then
from_dir=/zones/${zone_uuid}/root/opt/smartdc/manta-deployment
to_dir=/opt/smartdc/bin
# remove any tools from a previous setup
rm -f ${to_dir}/manta-login
rm -f ${to_dir}/manta-adm
rm -f ${to_dir}/manta-oneach
mkdir -p /opt/smartdc/manta-deployment/log
# While manta-login is a bash script and we could link it directly,
# we are using a little wrapper to avoid permission issues on the GZ.
cat <<EOF > ${to_dir}/manta-login
#!/bin/bash
exec ${from_dir}/bin/manta-login "\$@"
EOF
chmod +x ${to_dir}/manta-login
#
# manta-adm and manta-oneach are node programs, so we must write little
# wrappers that call the real version using the node delivered in the
# manta zone.
#
cat <<-EOF > ${to_dir}/manta-adm
#!/bin/bash
exec ${from_dir}/build/node/bin/node ${from_dir}/bin/manta-adm "\$@"
EOF
chmod +x ${to_dir}/manta-adm
cat <<-EOF > ${to_dir}/manta-oneach
#!/bin/bash
exec ${from_dir}/build/node/bin/node ${from_dir}/bin/manta-oneach "\$@"
EOF
chmod +x ${to_dir}/manta-oneach
#
# Install a symlink in the parallel "man" tree for each program that
# doesn't already have one.
#
for manpage in ${from_dir}/man/man1/*; do
#
# If we're looking at a zone version that does not have manual
# pages here, we'll get a bogus entry for "*"
#
if [[ ! -e $manpage ]]; then
continue;
fi
target="${to_dir}/../man/man1/$(basename "$manpage")"
if [[ -e $target ]]; then
echo "skipping $manpage ($target already exists)"
continue;
fi
echo "creating symlink \"$target\" for \"$manpage\""
ln -s "$manpage" "$target"
done
fi
}
# Mainline
manta_uuid=$(vmadm lookup -1 alias=${ZONE_ALIAS})
if [[ -n ${manta_uuid} ]]; then
echo "Manta zone already present."
copy_manta_tools ${manta_uuid}
exit
fi
imgapi_uuid=$(vmadm lookup alias=imgapi0)
add_external_nic ${imgapi_uuid}
enable_firewall ${imgapi_uuid}
import_manta_image
deploy_manta_zone
wait_for_config_agent
manta_zone_uuid=$(vmadm lookup -1 alias=${ZONE_ALIAS})
add_external_nic ${manta_zone_uuid}
wait_for_manta_zone ${manta_zone_uuid}
enable_firewall ${manta_zone_uuid}
copy_manta_tools ${manta_zone_uuid}