Merge pull request #5822 from SharePointOscar/master

Updated to use latest Azure CLI
This commit is contained in:
SwampDragons 2018-02-02 11:55:35 -08:00 committed by GitHub
commit 736e17d198
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 18 deletions

View File

@ -17,7 +17,7 @@ create_sleep=10
showhelp() { showhelp() {
echo "azure-setup" echo "azure-setup"
echo "" echo ""
echo " azure-setup helps you generate packer credentials for Azure" echo " azure-setup helps you generate packer credentials for azure"
echo "" echo ""
echo " The script creates a resource group, storage account, application" echo " The script creates a resource group, storage account, application"
echo " (client), service principal, and permissions and displays a snippet" echo " (client), service principal, and permissions and displays a snippet"
@ -49,13 +49,14 @@ showhelp() {
requirements() { requirements() {
found=0 found=0
azureversion=$(azure -v) azureversion=$(az -v)
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
found=$((found + 1)) found=$((found + 1))
echo "Found azure-cli version: $azureversion" echo "Found azure-cli version: $azureversion"
else else
echo "azure-cli is missing. Please install azure-cli from" echo "azure-cli is missing. Please install azure-cli from"
echo "https://azure.microsoft.com/en-us/documentation/articles/xplat-cli-install/" echo "https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest"
echo "Alternatively, you can use the Cloud Shell https://docs.microsoft.com/en-us/azure/cloud-shell/overview right from the Azure Portal or even VS Code."
fi fi
jqversion=$(jq --version) jqversion=$(jq --version)
@ -73,19 +74,20 @@ requirements() {
} }
askSubscription() { askSubscription() {
azure account list az account list
echo "" echo ""
echo "Please enter the Id of the account you wish to use. If you do not see" echo "Please enter the Id of the account you wish to use. If you do not see"
echo "a valid account in the list press Ctrl+C to abort and create one." echo "a valid account in the list press Ctrl+C to abort and create one."
echo "If you leave this blank we will use the Current account." echo "If you leave this blank we will use the Current account."
echo -n "> " echo -n "> "
read azure_subscription_id read azure_subscription_id
if [ "$azure_subscription_id" != "" ]; then if [ "$azure_subscription_id" != "" ]; then
azure account set $azure_subscription_id az account set --subscription $azure_subscription_id
else else
azure_subscription_id=$(azure account show --json | jq -r .[].id) azure_subscription_id=$(az account list | jq -r .[].id)
fi fi
azure_tenant_id=$(azure account show --json | jq -r .[].tenantId) azure_tenant_id=$(az account list | jq -r '.[] | select(.tenantId) | .tenantId')
echo "Using subscription_id: $azure_subscription_id" echo "Using subscription_id: $azure_subscription_id"
echo "Using tenant_id: $azure_tenant_id" echo "Using tenant_id: $azure_tenant_id"
} }
@ -118,16 +120,16 @@ askSecret() {
} }
askLocation() { askLocation() {
azure location list az account list-locations
echo "" echo ""
echo "Choose which region your resource group and storage account will be created." echo "Choose which region your resource group and storage account will be created. example: westus"
echo -n "> " echo -n "> "
read location read location
} }
createResourceGroup() { createResourceGroup() {
echo "==> Creating resource group" echo "==> Creating resource group"
azure group create -n $meta_name -l $location az group create -n $meta_name -l $location
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
azure_group_name=$meta_name azure_group_name=$meta_name
else else
@ -138,7 +140,7 @@ createResourceGroup() {
createStorageAccount() { createStorageAccount() {
echo "==> Creating storage account" echo "==> Creating storage account"
azure storage account create -g $meta_name -l $location --sku-name LRS --kind Storage $meta_name az storage account create --name $meta_name --resource-group $meta_name --location $location --kind Storage
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
azure_storage_name=$meta_name azure_storage_name=$meta_name
else else
@ -149,7 +151,17 @@ createStorageAccount() {
createApplication() { createApplication() {
echo "==> Creating application" echo "==> Creating application"
azure_client_id=$(azure ad app create -n $meta_name -i http://$meta_name --home-page http://$meta_name -p $azure_client_secret --json | jq -r .appId) echo "==> Does application exist?"
azure_client_id=$(az ad app list | jq -r '.[] | select(.displayName | contains("'$meta_name'")) ')
if [ "$azure_client_id" != "" ]; then
echo "==> application already exist, grab appId"
azure_client_id=$(az ad app list | jq -r '.[] | select(.displayName | contains("'$meta_name'")) .appId')
else
echo "==> application does not exist"
azure_client_id=$(az ad app create --display-name $meta_name --identifier-uris http://$meta_name --homepage http://$meta_name --password $azure_client_secret | jq -r .appId)
fi
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Error creating application: $meta_name @ http://$meta_name" echo "Error creating application: $meta_name @ http://$meta_name"
return 1 return 1
@ -167,9 +179,11 @@ createServicePrincipal() {
fi fi
if [ "${newer_syntax}" = true ]; then if [ "${newer_syntax}" = true ]; then
azure_object_id=$(azure ad sp create -a $azure_client_id --json | jq -r .objectId) azure_object_id=$(az ad sp create --id $azure_client_id | jq -r .objectId)
echo $azure_object_id "was selected."
else else
azure_object_id=$(azure ad sp create $azure_client_id --json | jq -r .objectId) azure_object_id=$(az ad sp create --id $azure_client_id | jq -r .objectId)
echo $azure_object_id "was selected."
fi fi
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
@ -180,10 +194,10 @@ createServicePrincipal() {
createPermissions() { createPermissions() {
echo "==> Creating permissions" echo "==> Creating permissions"
azure role assignment create --objectId $azure_object_id -o "Owner" -c /subscriptions/$azure_subscription_id az role assignment create --assignee $azure_object_id --role "Owner" --scope /subscriptions/$azure_subscription_id
# We want to use this more conservative scope but it does not work with the # We want to use this more conservative scope but it does not work with the
# current implementation which uses temporary resource groups # current implementation which uses temporary resource groups
# azure role assignment create --spn http://$meta_name -g $azure_group_name -o "API Management Service Contributor" # az role assignment create --spn http://$meta_name -g $azure_group_name -o "API Management Service Contributor"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Error creating permissions for: http://$meta_name" echo "Error creating permissions for: http://$meta_name"
return 1 return 1
@ -234,8 +248,7 @@ retryable() {
setup() { setup() {
requirements requirements
azure config mode arm az login
azure login
askSubscription askSubscription
askName askName