Skip to content

Commit

Permalink
updated to use new Azure CLI 'az' and respective commands
Browse files Browse the repository at this point in the history
  • Loading branch information
SharePointOscar committed Jan 27, 2018
1 parent 76b2ce8 commit 0868c21
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions contrib/azure-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,47 @@ 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"
echo " for use in your packer templates."
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)
Expand All @@ -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"
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -234,8 +247,7 @@ retryable() {
setup() {
requirements

azure config mode arm
azure login
#az login

askSubscription
askName
Expand Down

0 comments on commit 0868c21

Please sign in to comment.