diff --git a/contrib/azure-setup.sh b/contrib/azure-setup.sh index b5f6de220..105cea88c 100755 --- a/contrib/azure-setup.sh +++ b/contrib/azure-setup.sh @@ -17,7 +17,7 @@ create_sleep=10 showhelp() { echo "azure-setup" echo "" - echo " azure-setup helps you generate packer credentials for Azure" + 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" @@ -49,13 +49,14 @@ showhelp() { requirements() { found=0 - azureversion=$(azure -v) + azureversion=$(az -v) if [ $? -eq 0 ]; then found=$((found + 1)) echo "Found azure-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 "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) @@ -73,19 +74,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 +120,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 +140,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 +151,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("'$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 echo "Error creating application: $meta_name @ http://$meta_name" return 1 @@ -167,9 +179,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 +194,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 +248,7 @@ retryable() { setup() { requirements - azure config mode arm - azure login + az login askSubscription askName