-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathazure-copy
executable file
·155 lines (130 loc) · 4.98 KB
/
azure-copy
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
#!/bin/bash
# Accepted format is "Region 1:storageaccount1,Region 2:storageaccount2"
: ${AZURE_STORAGE_ACCOUNTS?= required}
: ${AZURE_IMAGE_NAME?= required}
: ${ARM_STORAGE_ACCOUNT?= required}
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_login() {
if [[ "$ARM_CLIENT_ID" ]] && [[ "$ARM_CLIENT_SECRET" ]]; then
az login --username $ARM_CLIENT_ID --password $ARM_CLIENT_SECRET --service-principal --tenant $ARM_TENANT_ID
fi
}
_delete_azure_storage_account_list() {
rm "$STORAGE_ACCOUNT_LIST_FILE"
}
azure_storage_account_list() {
STORAGE_ACCOUNT_LIST_FILE=$(mktemp)
az storage account list --output json > "$STORAGE_ACCOUNT_LIST_FILE"
trap _delete_azure_storage_account_list EXIT
}
export_fns() {
eval "$(sed -n 's/^\([A-Za-z0-9\-_]*\)() {/export -f \1/p' $BASH_SOURCE)"
}
get_storage_accounts_to_copy() {
for loc in "$(echo "$AZURE_STORAGE_ACCOUNTS" | tr "," "\n")"; do
echo "$loc" | cut -d":" -f2
done
}
azure_copy_everywhere() {
local vhdPath=$(azure_latest_vhd_by_prefix $AZURE_IMAGE_NAME)
debug "vhdPath=$vhdPath"
local sourceBlob="$ARM_STORAGE_ACCOUNT/system/${vhdPath}"
debug "sourceBlob=$sourceBlob"
rm -f checks.yml
touch checks.yml
export_fns
for loc in $(get_storage_accounts_to_copy); do
local destBlob="$loc/images/${ARM_DESTINATION_IMAGE_PREFIX}$AZURE_IMAGE_NAME.vhd"
debug "[COPY] $sourceBlob ==> $destBlob"
env DEBUG=$DEBUG STORAGE_ACCOUNT_LIST_FILE="$STORAGE_ACCOUNT_LIST_FILE" bash -c "azure_blob_copy $sourceBlob $destBlob"
done
}
azure_blob_check() {
declare dest=${1:? required dest: account/container/blob}
read dest_account dest_container dest_blob <<< "$(echo $dest | sed 's:/: :'| sed 's:/: :')"
debug "$dest_account"
debug "$dest_container"
debug "$dest_blob"
local dest_key=$(_azure_get_account_key $dest_account) || exit 1
debug "$dest_key"
local result=$(az storage blob exists \
--container-name $dest_container \
--name $dest_blob \
--account-name $dest_account \
--account-key $dest_key \
--output json | jq .exists)
debug "$result"
}
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) || exit 1
local dest_key=$(_azure_get_account_key $dest_account) || exit 1
az storage blob copy start \
--source-account-name $source_account \
--source-account-key $source_key \
--source-blob $source_blob \
--source-container $source_container \
--destination-container $dest_container \
--destination-blob $dest_blob \
--account-name $dest_account \
--account-key $dest_key \
--destination-if-none-match "*" \
--debug \
--timeout "${REQUEST_TIMEOUT:-15}" \
--output json 1>&2
local checkCmd="az storage blob show --account-name $dest_account --account-key $dest_key --container-name $dest_container --name $dest_blob --output json | jq -r '.properties.copy | .progress, .status'"
debug "===> CHECK PROGRESS: $checkCmd"
echo "$dest_account: $checkCmd" >> checks.yml
}
_azure_get_account_group() {
declare storage=${1:? storage account}
local account_group=$(cat "$STORAGE_ACCOUNT_LIST_FILE" | jq '.[]|select(.name|startswith("'${storage}'"))|.resourceGroup' -r | head -1)
if [[ -z "$account_group" ]]; then
debug "Failed to get account group for storage account ${storage}"
exit 1
fi
echo $account_group
}
_azure_get_account_key() {
declare storage=${1:?required: storage account}
declare group=${2}
if [[ -z "$group" ]]; then
group=$(_azure_get_account_group ${storage}) || exit 1
fi
local account_key=$(az storage account keys list --resource-group $group --account-name $storage --output json | jq -r '.[0].value')
if [[ -z "$account_key" ]]; then
debug "Failed to get account key for storage account ${storage} in group ${group}"
exit 1
fi
echo $account_key
}
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) || exit 1
az storage blob list \
--account-name $ARM_STORAGE_ACCOUNT \
--account-key $key \
--container-name system \
--prefix Microsoft.Compute/Images/packer/${imageName}-osDisk \
--output json \
| jq '.[].name' -r
}
main() {
: ${DEBUG:=1}
azure_login
azure_storage_account_list
azure_copy_everywhere
}
[[ "$0" == "$BASH_SOURCE" ]] && main "$@"