/
mockcloud-deploy
executable file
·180 lines (161 loc) · 5.95 KB
/
mockcloud-deploy
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
#!/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) 2019, Joyent, Inc.
#
#
# Call this from a test/dev TritonDC headnode GZ to deploy a single mockcloud
# VM.
#
# Usage (prompts for parameters):
# bash -c "$(curl -ksSL https://raw.githubusercontent.com/joyent/triton-mockcloud/master/tools/mockcloud-deploy)"
#
# or:
# curl -ksSL -O https://raw.githubusercontent.com/joyent/triton-mockcloud/master/tools/mockcloud-deploy
# chmod +x ./mockcloud-deploy
# ./mockcloud-deploy [-y] [-i IMAGE] DEPLOY-SERVER NUM-MOCK-SERVERS
#
if [[ -n "$TRACE" ]]; then
export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -o xtrace
fi
set -o errexit
set -o pipefail
# ---- support functions
function fatal
{
echo "$0: fatal error: $*"
exit 1
}
# ---- mainline
[[ $(zonename) == "global" ]] \
|| fatal "This script must run from a TritonDC headnode global zone: zonename=$(zonename)"
[[ $(sysinfo | /usr/bin/json "Boot Parameters.headnode") == "true" ]] \
|| fatal "This script must run from a TritonDC headnode global zone: sysinfo says this is not a headnode"
mockcloudImageChannel=dev
packageName=sample-4G
skipConfirmations=no
mockcloudImage=
deployServer=
numMockServers=
if [[ $# -gt 0 ]]; then
mockcloudImage=latest
while getopts "hi:y" opt
do
case "$opt" in
h)
fatal "TODO: usage"
exit 0
;;
i)
mockcloudImage=$OPTARG
;;
y)
skipConfirmations=yes
;;
*)
fatal "TODO: usage"
exit 1
;;
esac
done
shift $(( $OPTIND - 1 ))
deployServer=$1
[[ -n "$deployServer" ]] || fatal "missing DEPLOY-SERVER argument"
numMockServers=$2
[[ -n "$numMockServers" ]] || fatal "missing NUM-MOCK-SERVERS argument"
else
echo "Please enter some data for your mockcloud VM."
echo -n " - mockcloud image (UUID, 'latest' [the default], or 'current'): "
read mockcloudImage
[[ -z "$mockcloudImage" ]] && mockcloudImage=latest
echo -n " - deploy server (UUID or hostname, defaults to current server): "
read deployServer
[[ -z "$deployServer" ]] && deployServer=current
echo -n " - number of mock servers (default 5): "
read numMockServers
[[ -z "$numMockServers" ]] && numMockServers=5
fi
# Validate and gather VM data.
if [[ "$deployServer" == "current" ]]; then
deployServerUuid=$(sysinfo | json UUID)
else
deployServerUuid=$(sdc-cnapi /servers/$deployServer | json -H uuid)
if [[ -z "$deployServerUuid" ]]; then
deployServerUuid=$(sdc-cnapi /servers?hostname=$deployServer | json -H 0.uuid)
fi
fi
[[ -n "$deployServerUuid" ]] || fatal "could not find server '$deployServer'"
deployServerHostname=$(sdc-cnapi /servers/$deployServerUuid | json -H hostname)
if [[ -z "$(echo "$numMockServers" | grep -E '^[0-9]+$' || true)" ]]; then
fatal "number of mock servers value is not an integer: '$numMockServers'"
fi
if [[ "$mockcloudImage" == "current" ]]; then
mockcloudImage=$(sdc-imgadm list name=mockcloud --latest -H -o uuid)
[[ -n "$mockcloudImage" ]] \
|| fatal "there is no 'current' mockcloud image installed in this " \
"DC's IMGAPI, use 'latest' to import from updates.joyent.com"
mockcloudImageVersion=$(sdc-imgadm get $mockcloudImage | json version)
fi
if [[ "$mockcloudImage" == "latest" ]]; then
mockcloudImage=$(updates-imgadm -C $mockcloudImageChannel list -H -o uuid --latest name=mockcloud)
fi
if [[ -z "$mockcloudImageVersion" ]]; then
mockcloudImageVersion=$(updates-imgadm -C '*' get $mockcloudImage | json version)
fi
ufdsAdminUuid=$(bash /lib/sdc/config.sh -json | json ufds_admin_uuid)
latestAliasN=$(sdc-vmapi "/vms?state=active&owner_uuid=$ufdsAdminUuid&alias=mockcloud" | json -Ha alias | cut -c10- | sort -n | tail -1)
[[ -n $latestAliasN ]] || latestAliasN="-1"
alias=mockcloud$(( $latestAliasN + 1 ))
# Summary, assumptions, and confirmation.
echo ""
echo "This script will deploy a mockcloud VM. Currently it assumes:"
echo " 1. the '$packageName' package exists (typically installed via"
echo " 'sdcadm post-setup dev-sample-data'), and"
echo " 2. the IMGAPI service has an external NIC so it can import the"
echo " mockcloud image (run 'sdcadm post-setup common-external-nics')."
echo ""
echo "Mockcloud VM data:"
echo " - package: $packageName"
echo " - image: $mockcloudImage (mockcloud@$mockcloudImageVersion)"
echo " - server: $deployServerUuid ($deployServerHostname)"
echo " - VM alias: $alias"
echo " - num mock servers: $numMockServers"
if [[ "$skipConfirmations" != "yes" ]]; then
echo ""
echo -n "Press return to continue / Ctrl+C to abort."
read
fi
echo ""
# Import the image if necessary.
if ! sdc-imgadm get $mockcloudImage >/dev/null 2>&1; then
echo "Importing mockcloud image (assumes IMGAPI external nic)"
sdc-imgadm import $mockcloudImage -S https://updates.joyent.com?channel=$mockcloudImageChannel
fi
# TODO: Want a '--wait' option on 'sdc-vmadm create'.
sdc-vmadm create <<EOP
{
"alias": "$alias",
"brand": "joyent-minimal",
"owner_uuid": "$(bash /lib/sdc/config.sh -json | json ufds_admin_uuid)",
"billing_id": "$(sdc-papi /packages?name=$packageName | json -H 0.uuid)",
"networks": [
{"name": "admin"}
],
"server_uuid": "$deployServerUuid",
"image_uuid": "$mockcloudImage",
"delegate_dataset": true,
"customer_metadata": {
"user-script": "/opt/smartdc/boot/setup.sh",
"ufdsAdmin": "$(sdc-sapi /applications?name=sdc | json -H 0.metadata.ufds_admin_uuid)",
"dnsDomain": "$(sdc-sapi /applications?name=sdc | json -H 0.metadata.dns_domain)",
"mockcloudNumServers": $numMockServers
},
"tags": {
"smartdc_role": "mockcloud"
}
}
EOP