From 0868c21d13da62ddf32ac47342360f530f41793b Mon Sep 17 00:00:00 2001 From: SharePointOscar Date: Sat, 27 Jan 2018 12:49:41 -0800 Subject: [PATCH 1/5] updated to use new Azure CLI 'az' and respective commands --- contrib/azure-setup.sh | 66 +++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/contrib/azure-setup.sh b/contrib/azure-setup.sh index b5f6de220..4c4d6ee6b 100755 --- a/contrib/azure-setup.sh +++ b/contrib/azure-setup.sh @@ -15,9 +15,9 @@ azureversion= create_sleep=10 showhelp() { - echo "azure-setup" + echo "az-setup" echo "" - echo " azure-setup helps you generate packer credentials for Azure" + echo " az-setup helps you generate packer credentials for az" echo "" echo " The script creates a resource group, storage account, application" echo " (client), service principal, and permissions and displays a snippet" @@ -25,37 +25,37 @@ showhelp() { echo "" echo " For simplicity we make a lot of assumptions and choose reasonable" echo " defaults. If you want more control over what happens, please use" - echo " the azure-cli directly." + echo " the az-cli directly." echo "" - echo " Note that you must already have an Azure account, username," + echo " Note that you must already have an az account, username," echo " password, and subscription. You can create those here:" echo "" echo " - https://account.windowsazure.com/" echo "" echo "REQUIREMENTS" echo "" - echo " - azure-cli" + echo " - az-cli" echo " - jq" echo "" echo " Use the requirements command (below) for more info." echo "" echo "USAGE" echo "" - echo " ./azure-setup.sh requirements" - echo " ./azure-setup.sh setup" + echo " ./az-setup.sh requirements" + echo " ./az-setup.sh setup" echo "" } requirements() { found=0 - azureversion=$(azure -v) + azureversion=$(az -v) if [ $? -eq 0 ]; then found=$((found + 1)) - echo "Found azure-cli version: $azureversion" + echo "Found az-cli version: $azureversion" else - echo "azure-cli is missing. Please install azure-cli from" - echo "https://azure.microsoft.com/en-us/documentation/articles/xplat-cli-install/" + echo "az cli is missing. Please install az cli from" + echo "https://az.microsoft.com/en-us/documentation/articles/xplat-cli-install/" fi jqversion=$(jq --version) @@ -73,19 +73,20 @@ requirements() { } askSubscription() { - azure account list + az account list echo "" 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 "If you leave this blank we will use the Current account." echo -n "> " read azure_subscription_id + if [ "$azure_subscription_id" != "" ]; then - azure account set $azure_subscription_id + az account set --subscription $azure_subscription_id else - azure_subscription_id=$(azure account show --json | jq -r .[].id) + azure_subscription_id=$(az account list | jq -r .[].id) 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 tenant_id: $azure_tenant_id" } @@ -118,16 +119,16 @@ askSecret() { } askLocation() { - azure location list + az account list-locations 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 "> " read location } createResourceGroup() { echo "==> Creating resource group" - azure group create -n $meta_name -l $location + az group create -n $meta_name -l $location if [ $? -eq 0 ]; then azure_group_name=$meta_name else @@ -138,7 +139,7 @@ createResourceGroup() { createStorageAccount() { 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 azure_storage_name=$meta_name else @@ -149,7 +150,17 @@ createStorageAccount() { createApplication() { 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("spfarmpacker")) .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 echo "Error creating application: $meta_name @ http://$meta_name" return 1 @@ -158,7 +169,7 @@ createApplication() { createServicePrincipal() { echo "==> Creating service principal" - # Azure CLI 0.10.2 introduced a breaking change, where appId must be supplied with the -a switch + # az CLI 0.10.2 introduced a breaking change, where appId must be supplied with the -a switch # prior version accepted appId as the only parameter without a switch newer_syntax=false IFS='.' read -ra azureversionsemver <<< "$azureversion" @@ -167,9 +178,11 @@ createServicePrincipal() { fi 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 - 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 if [ $? -ne 0 ]; then @@ -180,10 +193,10 @@ createServicePrincipal() { createPermissions() { 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 # 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 echo "Error creating permissions for: http://$meta_name" return 1 @@ -234,8 +247,7 @@ retryable() { setup() { requirements - azure config mode arm - azure login + #az login askSubscription askName From 6770bb8091241e7ae54c43f74ff7fdc922ebb273 Mon Sep 17 00:00:00 2001 From: SharePointOscar Date: Sat, 27 Jan 2018 12:55:16 -0800 Subject: [PATCH 2/5] changed verbiage on cli name --- contrib/azure-setup.sh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/contrib/azure-setup.sh b/contrib/azure-setup.sh index 4c4d6ee6b..ec5f1b896 100755 --- a/contrib/azure-setup.sh +++ b/contrib/azure-setup.sh @@ -15,9 +15,9 @@ azureversion= create_sleep=10 showhelp() { - echo "az-setup" + echo "azure-setup" echo "" - echo " az-setup helps you generate packer credentials for az" + echo " azure-setup helps you generate packer credentials for azure" echo "" echo " The script creates a resource group, storage account, application" echo " (client), service principal, and permissions and displays a snippet" @@ -25,7 +25,7 @@ showhelp() { echo "" echo " For simplicity we make a lot of assumptions and choose reasonable" echo " defaults. If you want more control over what happens, please use" - echo " the az-cli directly." + echo " the azure cli directly." echo "" echo " Note that you must already have an az account, username," echo " password, and subscription. You can create those here:" @@ -34,15 +34,15 @@ showhelp() { echo "" echo "REQUIREMENTS" echo "" - echo " - az-cli" + echo " - azure-cli" echo " - jq" echo "" echo " Use the requirements command (below) for more info." echo "" echo "USAGE" echo "" - echo " ./az-setup.sh requirements" - echo " ./az-setup.sh setup" + echo " ./azure-setup.sh requirements" + echo " ./azure-setup.sh setup" echo "" } @@ -52,9 +52,9 @@ requirements() { azureversion=$(az -v) if [ $? -eq 0 ]; then found=$((found + 1)) - echo "Found az-cli version: $azureversion" + echo "Found azure-cli version: $azureversion" else - echo "az cli is missing. Please install az cli from" + echo "azure cli is missing. Please install azure cli from" echo "https://az.microsoft.com/en-us/documentation/articles/xplat-cli-install/" fi From 6327ab6f3d7fe0a335e41d501420c01febebc54c Mon Sep 17 00:00:00 2001 From: SharePointOscar Date: Sat, 27 Jan 2018 12:56:43 -0800 Subject: [PATCH 3/5] changed verbiage on cli name --- contrib/azure-setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/azure-setup.sh b/contrib/azure-setup.sh index ec5f1b896..7e6817e74 100755 --- a/contrib/azure-setup.sh +++ b/contrib/azure-setup.sh @@ -247,7 +247,7 @@ retryable() { setup() { requirements - #az login + az login askSubscription askName From 361a175fc9c8ff80016bae86c3a437554d1f797c Mon Sep 17 00:00:00 2001 From: Oscar Medina Date: Sun, 28 Jan 2018 07:49:58 -0800 Subject: [PATCH 4/5] Removed static value from code The parameter within the contains() should not be hard-coded, using $meta_name variable value. --- contrib/azure-setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/azure-setup.sh b/contrib/azure-setup.sh index 7e6817e74..f30da9693 100755 --- a/contrib/azure-setup.sh +++ b/contrib/azure-setup.sh @@ -155,7 +155,7 @@ createApplication() { if [ "$azure_client_id" != "" ]; then echo "==> application already exist, grab appId" - azure_client_id=$(az ad app list | jq -r '.[] | select(.displayName | contains("spfarmpacker")) .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) From 59ecaee2fe7db7db73baa4d5d345bb85498505c9 Mon Sep 17 00:00:00 2001 From: SharePointOscar Date: Wed, 31 Jan 2018 18:00:13 -0800 Subject: [PATCH 5/5] implemented changes as per feedback for Azure CLI 2.0 --- contrib/azure-setup.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/contrib/azure-setup.sh b/contrib/azure-setup.sh index 7e6817e74..75e770055 100755 --- a/contrib/azure-setup.sh +++ b/contrib/azure-setup.sh @@ -25,9 +25,9 @@ showhelp() { echo "" echo " For simplicity we make a lot of assumptions and choose reasonable" echo " defaults. If you want more control over what happens, please use" - echo " the azure cli directly." + echo " the azure-cli directly." echo "" - echo " Note that you must already have an az account, username," + echo " Note that you must already have an Azure account, username," echo " password, and subscription. You can create those here:" echo "" echo " - https://account.windowsazure.com/" @@ -54,8 +54,9 @@ requirements() { found=$((found + 1)) echo "Found azure-cli version: $azureversion" else - echo "azure cli is missing. Please install azure cli from" - echo "https://az.microsoft.com/en-us/documentation/articles/xplat-cli-install/" + echo "azure-cli is missing. Please install azure-cli from" + 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 jqversion=$(jq --version) @@ -169,7 +170,7 @@ createApplication() { createServicePrincipal() { echo "==> Creating service principal" - # az CLI 0.10.2 introduced a breaking change, where appId must be supplied with the -a switch + # Azure CLI 0.10.2 introduced a breaking change, where appId must be supplied with the -a switch # prior version accepted appId as the only parameter without a switch newer_syntax=false IFS='.' read -ra azureversionsemver <<< "$azureversion"