forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit-variables.sh
executable file
·65 lines (54 loc) · 2.29 KB
/
init-variables.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
#!/bin/bash -e
CDIR=$(dirname "${BASH_SOURCE}")
SETTINGS_JSON="${SETTINGS_JSON:-./packer/settings.json}"
SP_JSON="${SP_JSON:-./packer/sp.json}"
SUBSCRIPTION_ID="${SUBSCRIPTION_ID:-`az account show -o json --query="id" | tr -d '"'`}"
STORAGE_ACCOUNT_NAME="aksimages$(date +%s)"
echo "Subscription ID: ${SUBSCRIPTION_ID}"
echo "Service Principal Path: ${SP_JSON}"
if [ -a "${SP_JSON}" ]; then
echo "Existing credentials file found."
exit 0
elif [ -z "${CLIENT_ID}" ]; then
echo "Service principal not found! Generating one @ ${SP_JSON}"
az ad sp create-for-rbac -n aks-images-packer$(date +%s) -o json > ${SP_JSON}
CLIENT_ID=`cat ${SP_JSON} | jq -r .appId`
CLIENT_SECRET=`cat ${SP_JSON} | jq -r .password`
TENANT_ID=`cat ${SP_JSON} | jq -r .tenant`
fi
avail=$(az storage account check-name -n ${STORAGE_ACCOUNT_NAME} -o json | jq -r .nameAvailable)
if $avail ; then
echo "creating new storage account ${STORAGE_ACCOUNT_NAME}"
az storage account create -n $STORAGE_ACCOUNT_NAME -g $AZURE_RESOURCE_GROUP_NAME --sku "Standard_RAGRS"
echo "creating new container system"
key=$(az storage account keys list -n $STORAGE_ACCOUNT_NAME -g $AZURE_RESOURCE_GROUP_NAME | jq -r '.[0].value')
az storage container create --name system --public-access container --account-key=$key --account-name=$STORAGE_ACCOUNT_NAME
else
echo "storage account ${STORAGE_ACCOUNT_NAME} already exists."
fi
if [ -z "${CLIENT_ID}" ]; then
echo "CLIENT_ID was not set! Something happened when generating the service principal or when trying to read the sp file!"
exit 1
fi
if [ -z "${CLIENT_SECRET}" ]; then
echo "CLIENT_SECRET was not set! Something happened when generating the service principal or when trying to read the sp file!"
exit 1
fi
if [ -z "${TENANT_ID}" ]; then
echo "TENANT_ID was not set! Something happened when generating the service principal or when trying to read the sp file!"
exit 1
fi
echo "storage name: ${STORAGE_ACCOUNT_NAME}"
cat <<EOF > packer/settings.json
{
"subscription_id": "${SUBSCRIPTION_ID}",
"client_id": "${CLIENT_ID}",
"client_secret": "${CLIENT_SECRET}",
"tenant_id": "${TENANT_ID}",
"resource_group_name": "${AZURE_RESOURCE_GROUP_NAME}",
"location": "${AZURE_LOCATION}",
"storage_account_name": "${STORAGE_ACCOUNT_NAME}",
"vm_size": "${AZURE_VM_SIZE}"
}
EOF
cat packer/settings.json