-
Notifications
You must be signed in to change notification settings - Fork 19
/
opsman
executable file
·259 lines (217 loc) · 6.3 KB
/
opsman
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
#!/bin/bash
set -eu
set -o pipefail
if ! command -v uaac >/dev/null 2>&1; then
shopt -s expand_aliases
alias uaac='BUNDLE_GEMFILE=/home/tempest-web/tempest/web/vendor/uaac/Gemfile bundle exec uaac'
fi
OPSMAN_HOST=${OPSMAN_HOST:-localhost}
usage_and_exit() {
cat <<EOF
Usage: opsman <command> [options]
Examples:
opsman login
opsman unlock
opsman upload cf-1.8.5-build.4.pivotal
opsman upload bosh-stemcell-3363.24-vsphere-esxi-ubuntu-trusty-go_agent.tgz
opsman get-vm-types
opsman delete-vm-types
opsman set-vm-type --name mytype --cpu 2 --ram 1024 --disk 10240
EOF
exit 1
}
error_and_exit() {
echo "$1" && exit 1
}
jq_exists() {
command -v jq >/dev/null 2>&1
}
unlock_opsman() {
local PASSPHRASE
read -r -s -p "Decryption passphrase: " PASSPHRASE
echo # extra linefeed
local STATUS_CODE=$(curl "https://$OPSMAN_HOST/api/v0/unlock" -k \
-s -o /dev/null -w "%{http_code}" \
-X PUT \
-H "Content-Type: application/json" \
-d "{\"passphrase\": \"$PASSPHRASE\"}")
if [ "403" = "$STATUS_CODE" ]; then
echo "Decryption passphrase is incorrect" >&2
exit 1
fi
[ "200" = "$STATUS_CODE" ]
}
login_to_uaac() {
local OPSMAN_USER=
read -r -p "Ops Manager User: " OPSMAN_USER
local OPSMAN_PASS=
read -r -s -p "Ops Manager Pass: " OPSMAN_PASS
uaac target "https://$OPSMAN_HOST/uaa" --skip-ssl-validation
uaac token owner get opsman "$OPSMAN_USER" -p "$OPSMAN_PASS" -s ''
printf 'User \e[33m%s\e[0m logged in successfully.\n' "$OPSMAN_USER"
}
is_valid_access_token() {
local UAA_ACCESS_TOKEN=${1:-}
[ -n "$UAA_ACCESS_TOKEN" ] || return 1
local STATUS_CODE=$(curl https://$OPSMAN_HOST/uaa/check_token -k -L -G \
-s -o /dev/null -w "%{http_code}" \
-u "opsman:" \
-d token_type=bearer \
-d token="$UAA_ACCESS_TOKEN")
[ "200" = "$STATUS_CODE" ]
}
is_stemcell() {
local filename=${1:?filename null or not set}
[[ "$filename" == *stemcell*.tgz ]]
}
upload_file() {
local LOCAL_FILE_NAME=${1:-}
if [ -z "$LOCAL_FILE_NAME" ]; then
read -r -p "Local file name: " LOCAL_FILE_NAME
fi
if [ ! -f "$LOCAL_FILE_NAME" ]; then
error_and_exit "Invalid file: $LOCAL_FILE_NAME"
fi
local UAA_ACCESS_TOKEN=$(uaac context | grep access_token | awk '{ print $2 }')
if ! is_valid_access_token "$UAA_ACCESS_TOKEN"; then
login_to_uaac
UAA_ACCESS_TOKEN=$(uaac context | grep access_token | awk '{ print $2 }')
fi
if is_stemcell "$LOCAL_FILE_NAME"; then
printf 'Uploading stemcell: \e[33m%s\e[0m\n' "$LOCAL_FILE_NAME"
curl "https://$OPSMAN_HOST/api/v0/stemcells" \
-k -# -o /dev/null \
-X POST \
-H "Authorization: Bearer $UAA_ACCESS_TOKEN" \
-F "stemcell[file]=@$LOCAL_FILE_NAME"
else
printf 'Uploading tile: \e[33m%s\e[0m\n' "$LOCAL_FILE_NAME"
curl "https://$OPSMAN_HOST/api/v0/available_products" \
-k -# -o /dev/null \
-X POST \
-H "Authorization: Bearer $UAA_ACCESS_TOKEN" \
-F "product[file]=@$LOCAL_FILE_NAME"
fi
}
upload_files() {
local filenames=${1:?filenames null or not set}
for filename in $filenames; do
upload_file "$filename"
done
}
get_vm_types() {
local UAA_ACCESS_TOKEN=$(uaac context | grep access_token | awk '{ print $2 }')
if ! is_valid_access_token "$UAA_ACCESS_TOKEN"; then
login_to_uaac
UAA_ACCESS_TOKEN=$(uaac context | grep access_token | awk '{ print $2 }')
fi
curl "https://$OPSMAN_HOST/api/v0/vm_types" \
-k -s \
-H "Authorization: Bearer $UAA_ACCESS_TOKEN" 2>/dev/null
}
delete_vm_types() {
local UAA_ACCESS_TOKEN=$(uaac context | grep access_token | awk '{ print $2 }')
if ! is_valid_access_token "$UAA_ACCESS_TOKEN"; then
login_to_uaac
UAA_ACCESS_TOKEN=$(uaac context | grep access_token | awk '{ print $2 }')
fi
curl "https://$OPSMAN_HOST/api/v0/vm_types" \
-k -s \
-X DELETE \
-H "Authorization: Bearer $UAA_ACCESS_TOKEN" 2>/dev/null
}
has_vm_type() {
local name=$1
local vm_types=$2
echo "$vm_types" | jq -e --arg name "$name" 'select (.vm_types[].name == $name)' >/dev/null
}
set_vm_type() {
if ! jq_exists; then
error_and_exit "jq command not found. Please install jq to support set-vm-type functionality (https://stedolan.github.io/jq/download/)"
fi
local args name cpu ram disk
while (( "$#" )); do
case "$1" in
-n|--name)
name=$2 && shift 2
;;
-c|--cpu)
cpu=$2 && shift 2
;;
-r|--ram)
ram=$2 && shift 2
;;
-d|--disk)
disk=$2 && shift 2
;;
--) # end argument parsing
shift && break
;;
-*|--*) # unsupported flags
echo "Error; Unsupported flag $1" >&2 && exit 1
;;
*) # preserve positional arguments
args="$args $1" && shift
;;
esac
done
local UAA_ACCESS_TOKEN=$(uaac context | grep access_token | awk '{ print $2 }')
if ! is_valid_access_token "$UAA_ACCESS_TOKEN"; then
login_to_uaac
UAA_ACCESS_TOKEN=$(uaac context | grep access_token | awk '{ print $2 }')
fi
local old_types=$(get_vm_types)
local new_types
#TODO: figure out a single "insert or update" jq statement
if has_vm_type "$name" "$old_types"; then
new_types=$(echo $old_types | jq \
--arg name "$name" \
--argjson cpu "$cpu" \
--argjson ram "$ram" \
--argjson disk "$disk" \
'(.vm_types[] | select(.name == $name)) += {
name: $name,
cpu: $cpu,
ram: $ram,
ephemeral_disk: $disk
}')
else
new_types=$(echo $old_types | jq \
--arg name "$name" \
--argjson cpu "$cpu" \
--argjson ram "$ram" \
--argjson disk "$disk" \
'.vm_types += [{
name: $name,
cpu: $cpu,
ram: $ram,
ephemeral_disk: $disk
}]')
fi
curl "https://$OPSMAN_HOST/api/v0/vm_types" \
-k -X PUT \
-H "Authorization: Bearer $UAA_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d "$new_types"
}
if [ $# -lt 1 ]; then
usage_and_exit
fi
CMD=${1:-}
shift
ARGS=$@
if [ "login" = "$CMD" ]; then
login_to_uaac
elif [ "unlock" = "$CMD" ]; then
unlock_opsman
elif [ "upload" = "$CMD" ] && [ -n "$ARGS" ]; then
upload_files "$ARGS"
elif [ "get-vm-types" = "$CMD" ]; then
get_vm_types
elif [ "delete-vm-types" = "$CMD" ]; then
delete_vm_types
elif [ "set-vm-type" = "$CMD" ] && [ -n "$ARGS" ]; then
set_vm_type $ARGS
else
usage_and_exit
fi