-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathazure-functions
executable file
·246 lines (203 loc) · 7.97 KB
/
azure-functions
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
#!/bin/bash
if [[ "$TRACE" ]]; then
: ${START_TIME:=$(date +%s)}
export START_TIME
export PS4='+ [TRACE $BASH_SOURCE:$LINENO][ellapsed: $(( $(date +%s) - $START_TIME ))] '
set -x
fi
debug() {
[[ "$DEBUG" ]] && echo "-----> $*" 1>&2
}
alias r="source $BASH_SOURCE"
azure_copy_everywhere() {
local imageName=${1:-"$(azure_latest_image_name_form_atlas)"}
debug "imageName=$imageName"
local vhdPath=$(azure_latest_vhd_by_prefix $imageName)
debug "vhdPath=$vhdPath"
local sourceBlob="sequenceiqnortheurope2/system/${vhdPath}"
debug "sourceBlob=$sourceBlob"
local locations=${LOCATIONS:="sequenceiqbrazilsouth2 sequenceiqcentralus2 sequenceiqeastasia2 sequenceiqeastus12 sequenceiqeastus22 sequenceiqjapaneast2 sequenceiqjapanwest2 sequenceiqnortheurope2 sequenceiqorthcentralus2 sequenceiqouthcentralus2 sequenceiqsoutheastasia2 sequenceiqwesteurope2 sequenceiqwestus2"}
#local locations="sequenceiqcentralus2 sequenceiqeastasia2 sequenceiqeastus12 sequenceiqeastus22 sequenceiqjapaneast2 sequenceiqjapanwest2 sequenceiqorthcentralus2 sequenceiqouthcentralus2 sequenceiqsoutheastasia2 sequenceiqwesteurope2"
rm -f checks.yml
for loc in $locations; do
local destBlob="$loc/images/${AZURE_DESTINATION_IMAGE_PREFIX}$imageName.vhd"
debug "[COPY] $sourceBlob ==> $destBlob"
azure_blob_copy \
$sourceBlob \
$destBlob | tee -a checks.yml
done
}
export_all_fns() {
eval "$(sed -n 's/^\([A-Za-z0-9\-_]*\)() {/export -f \1/p' $BASH_SOURCE)"
}
azure_delete_everywhere() {
declare blobPrefix=${1:? required: blob prefix in form of: container/prefix}
declare locations=${LOCATIONS:="sequenceiqbrazilsouth2 sequenceiqcentralus2 sequenceiqeastasia2 sequenceiqeastus12 sequenceiqeastus22 sequenceiqjapaneast2 sequenceiqjapanwest2 sequenceiqnortheurope2 sequenceiqorthcentralus2 sequenceiqouthcentralus2 sequenceiqsoutheastasia2 sequenceiqwesteurope2 sequenceiqwestus2"}
export_all_fns
export -f azure_delete_by_prefix _azure_get_account_key
for loc in $locations; do
local prefix="$loc/$blobPrefix"
echo "$prefix"
done \
| xargs -P20 -n1 -I@ env DEBUG=$DEBUG bash -c "azure_delete_by_prefix @"
}
azure_delete_by_prefix() {
declare prefix=${1:? required source: account/container/blob}
read account container blobPrefix <<< "$(echo $prefix | sed 's:/: :'| sed 's:/: :')"
debug "account=$account container=$container blobPrefix=$blobPrefix"
local key=$(_azure_get_account_key $account)
azure storage blob list \
-a $account \
-k $key \
--container $container \
--prefix $blobPrefix \
--json \
| jq .[].name -r \
| xargs -n 1 azure storage blob delete \
-a $account \
-k $key \
--container $container \
--blob
}
azure_capture() {
local group=cbstoreancsa
local vmName=cbdeployerVM
azure vm stop \
-vv --json \
--resource-group $group \
--name $vmName \
| tee -a azure-capture.log
azure vm generalize \
-vv --json \
--resource-group $group \
--name $vmName \
| tee -a azure-capture.log
azure vm capture \
-vv --json \
--resource-group $group \
--storage-account-container-name images \
--template-file-name cbd-template \
--name $vmName \
--vhd-name-prefix cbd-image \
| tee -a azure-capture.log
}
azure_blob_copy() {
declare source=${1:? required source: account/container/blob}
declare dest=${2:? required dest: account/container/blob}
read source_account source_container source_blob <<< "$(echo $source | sed 's:/: :'| sed 's:/: :')"
read dest_account dest_container dest_blob <<< "$(echo $dest | sed 's:/: :'| sed 's:/: :')"
local source_key=$(_azure_get_account_key $source_account)
local dest_key=$(_azure_get_account_key $dest_account)
azure storage blob copy start \
-a $source_account \
-k $source_key \
--source-container $source_container \
--source-blob $source_blob \
--dest-account-name $dest_account \
--dest-account-key $dest_key \
--dest-container $dest_container \
--dest-blob $dest_blob \
--json 1>&2
local checkCmd="azure storage blob copy show -a $dest_account -k $dest_key --container $dest_container --blob $dest_blob --json | jq .copyProgress -r"
debug "===> CHECK PROGRESS: $checkCmd"
echo "$dest_account: $checkCmd"
}
_azure_get_account_group() {
azure storage account list --json | jq '.[]|select(.name|startswith("'${1:? strogae account}'"))|.resourceGroup' -r
}
_azure_get_account_key() {
declare storage=${1:?required: storage account}
declare group=${2}
if [[ "$group" == "" ]]; then
#group=$(_azure_get_account_group ${storage%%.*})
group=$(_azure_get_account_group ${storage})
fi
azure storage account keys list -g $group $storage --json | jq .storageAccountKeys.key1 -r
}
azure_blob_list() {
declare storage=${1:?required: storage account}
declare containers=${2}
local group=$(_azure_get_account_group $storage)
debug "group: $group"
local key=$(_azure_get_account_key $storage $group)
debug "key: $key"
if [[ "$containers" == "" ]]; then
containers=$( azure storage container list -a $storage -k $key --json | jq -r '.[].name')
fi
debug "containers: $containers"
for cont in $containers; do
debug "container: $cont"
azure storage blob list -a $storage -k $key --container $cont --json | jq '.[].name' -r
done
}
azure_latest_vhd_today() {
local group=$(_azure_get_account_group $ARM_STORAGE_ACCOUNT)
debug "group: $group"
local key=$(_azure_get_account_key $storage $group)
debug "key: $key"
DATE_STR=$(date | sed "s/... \([^ ]*\) *\([0-9]*\) .*/\2 \1/")
debug "DATE_STR=$DATE_STR"
azure storage blob list \
-a $ARM_STORAGE_ACCOUNT \
-k $key \
--container system \
--prefix Microsoft.Compute/Images/packer \
--json \
| jq '.[]|[.name,.properties["last-modified"]]' -c \
| sed -n '/'"${DATE_STR}"'/ {s/^..//;s/",.*//;p}' \
| grep .vhd
}
azure_latest_vhd_by_prefix() {
declare imageName=${1:? required: imageName prefix like cloudbreak-2016-02-24}
local key=$(_azure_get_account_key $ARM_STORAGE_ACCOUNT)
azure storage blob list \
-a $ARM_STORAGE_ACCOUNT \
-k $key \
--container system \
--prefix Microsoft.Compute/Images/packer/${imageName}-osDisk \
--json \
| jq '.[].name' -r
}
azure_latest_image_name_form_atlas() {
curl -sL https://atlas.hashicorp.com/api/v1/artifacts/sequenceiq/cloudbreak/azure-arm.image/search \
|jq '.versions[0].metadata.image_name' -r
}
azure_delete_blob_by_date() {
azure storage blob list \
-a $ARM_STORAGE_ACCOUNT \
-k $KEY \
--container system\
--prefix Microsoft.Compute/Images/images/packer --json \
| jq '.[]|[.name,.properties["last-modified"]]' -c \
| sed -n '/23 Feb/ {s/^..//;s/",.*//;p}' \
| xargs -n 1 \
azure storage blob delete \
-a $ARM_STORAGE_ACCOUNT \
-k $KEY \
--container system
}
azure_storage_by_prefix() {
local json=$(
azure storage account list --json \
| jq '[.[]|select(.name|startswith("'${1:?required: storage account prefix}'"))|{name:.name,group:.resourceGroup,endpoint:.primaryEndpoints.blob}]'
)
if [[ "$2" == "-q" ]]; then
jq -r '.[].name' <<< "$json"
else
in2csv -f json <<< "$json" | csvlook
fi
}
azure_delete_group_by_prefix() {
local groups=$( azure group list --json \
| jq '.[]|select(.name|startswith("'"${1:?required: group prefix}"'")).name' -r
)
debug "You are about to delete: $groups"
debug "press ENTER to continue or CTRL-C to exit"
read idontcare
xargs -n1 -P20 -I@ bash -xc 'azure group delete @ -q' <<< "$groups"
}
main() {
: ${DEBUG:=1}
azure_copy_everywhere "$@"
}
[[ "$0" == "$BASH_SOURCE" ]] && main "$@" || true