2016-03-04 00:46:51 -05:00
#!/usr/bin/env bash
set -e
2016-03-04 04:22:20 -05:00
meta_name =
azure_client_id = # Derived from application after creation
azure_client_name = # Application name
azure_client_secret = # Application password
azure_group_name =
azure_storage_name =
azure_subscription_id = # Derived from the account after login
azure_tenant_id = # Derived from the account after login
2016-08-16 08:53:23 -04:00
location =
2016-08-18 11:01:23 -04:00
azure_object_id =
2016-11-08 03:00:15 -05:00
azureversion =
2017-04-03 20:14:27 -04:00
create_sleep = 10
2016-03-04 00:46:51 -05:00
showhelp( ) {
2018-04-15 17:45:17 -04:00
echo "azure-setup"
echo ""
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"
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 ""
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/"
echo ""
echo "REQUIREMENTS"
echo ""
echo " - azure-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 ""
2016-03-04 00:46:51 -05:00
}
requirements( ) {
2018-04-15 17:45:17 -04:00
found = 0
2017-04-03 20:14:27 -04:00
2018-04-15 17:45:17 -04:00
azureversion = $( az --version)
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://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
2017-04-03 20:14:27 -04:00
2018-04-15 17:45:17 -04:00
jqversion = $( jq --version)
if [ $? -eq 0 ] ; then
found = $(( found + 1 ))
echo " Found jq version: $jqversion "
else
echo "jq is missing. Please install jq from"
echo "https://stedolan.github.io/jq/"
fi
2018-04-13 00:02:55 -04:00
2018-04-15 17:45:17 -04:00
if [ $found -lt 2 ] ; then
exit 1
fi
2016-03-04 00:46:51 -05:00
}
2016-03-04 04:22:20 -05:00
askSubscription( ) {
2018-04-15 17:45:17 -04:00
az account list -otable
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
2018-01-27 15:49:41 -05:00
2018-04-15 17:45:17 -04:00
if [ " $azure_subscription_id " != "" ] ; then
az account set --subscription $azure_subscription_id
else
2018-04-15 18:01:31 -04:00
azure_subscription_id = $( az account list --output json | jq -r '.[] | select(.isDefault==true) | .id' )
2018-04-15 17:45:17 -04:00
fi
2018-04-15 18:01:31 -04:00
azure_tenant_id = $( az account list --output json | jq -r '.[] | select(.id=="' $azure_subscription_id '") | .tenantId' )
2018-04-15 17:45:17 -04:00
echo " Using subscription_id: $azure_subscription_id "
echo " Using tenant_id: $azure_tenant_id "
2016-03-04 00:46:51 -05:00
}
2016-03-04 04:22:20 -05:00
askName( ) {
2018-04-15 17:45:17 -04:00
echo ""
echo "Choose a name for your resource group, storage account and client"
echo "client. This is arbitrary, but it must not already be in use by"
echo "any of those resources. ALPHANUMERIC ONLY. Ex: mypackerbuild"
echo -n "> "
read meta_name
2016-03-04 04:22:20 -05:00
}
askSecret( ) {
2018-04-15 17:45:17 -04:00
echo ""
echo "Enter a secret for your application. We recommend generating one with"
echo "openssl rand -base64 24. If you leave this blank we will attempt to"
echo "generate one for you using openssl. THIS WILL BE SHOWN IN PLAINTEXT."
echo "Ex: mypackersecret8734"
echo -n "> "
read azure_client_secret
if [ " $azure_client_secret " = "" ] ; then
azure_client_secret = $( openssl rand -base64 24)
if [ $? -ne 0 ] ; then
echo "Error generating secret"
exit 1
fi
echo " Generated client_secret: $azure_client_secret "
2017-04-03 20:14:27 -04:00
fi
2016-03-04 04:22:20 -05:00
}
2016-08-16 08:53:23 -04:00
askLocation( ) {
2018-04-15 17:45:17 -04:00
az account list-locations -otable
echo ""
echo "Choose which region your resource group and storage account will be created. example: westus"
echo -n "> "
read location
2016-08-16 08:53:23 -04:00
}
2016-03-04 04:22:20 -05:00
createResourceGroup( ) {
2018-04-15 17:45:17 -04:00
echo "==> Creating resource group"
az group create -n $meta_name -l $location
if [ $? -eq 0 ] ; then
azure_group_name = $meta_name
else
echo " Error creating resource group: $meta_name "
return 1
fi
2016-03-04 04:22:20 -05:00
}
createStorageAccount( ) {
2018-04-15 17:45:17 -04:00
echo "==> Creating storage account"
az storage account create --name $meta_name --resource-group $meta_name --location $location --kind Storage --sku Standard_LRS
if [ $? -eq 0 ] ; then
azure_storage_name = $meta_name
else
echo " Error creating storage account: $meta_name "
return 1
fi
2016-03-04 04:22:20 -05:00
}
2016-11-08 02:14:15 -05:00
createApplication( ) {
2018-04-15 17:45:17 -04:00
echo "==> Creating application"
echo "==> Does application exist?"
2018-04-15 18:01:31 -04:00
azure_client_id = $( az ad app list --output json | jq -r '.[] | select(.displayName | contains("' $meta_name '")) ' )
2018-04-15 17:45:17 -04:00
if [ " $azure_client_id " != "" ] ; then
echo "==> application already exist, grab appId"
2018-08-14 23:23:50 -04:00
azure_client_id = $( az ad app list --output json | jq -r '.[] | select(.displayName | contains("' $meta_name '")) .appId' )
2018-04-15 17:45:17 -04:00
else
echo "==> application does not exist"
2018-04-15 18:01:31 -04:00
azure_client_id = $( az ad app create --display-name $meta_name --identifier-uris http://$meta_name --homepage http://$meta_name --password $azure_client_secret --output json | jq -r .appId)
2018-04-15 17:45:17 -04:00
fi
2018-04-13 00:02:55 -04:00
2018-04-15 17:45:17 -04:00
if [ $? -ne 0 ] ; then
echo " Error creating application: $meta_name @ http:// $meta_name "
return 1
fi
2016-11-08 02:14:15 -05:00
}
2016-11-06 19:15:25 -05:00
createServicePrincipal( ) {
2018-04-15 17:45:17 -04:00
echo "==> Creating service principal"
2018-04-15 18:01:31 -04:00
azure_object_id = $( az ad sp create --id $azure_client_id --output json | jq -r .objectId)
2018-04-15 17:45:17 -04:00
echo $azure_object_id "was selected."
2017-04-03 20:14:27 -04:00
2018-04-15 17:45:17 -04:00
if [ $? -ne 0 ] ; then
echo " Error creating service principal: $azure_client_id "
return 1
fi
2016-03-04 04:22:20 -05:00
}
createPermissions( ) {
2018-04-15 17:45:17 -04:00
echo "==> Creating permissions"
az role assignment create --assignee $azure_object_id --role "Owner" --scope /subscriptions/$azure_subscription_id
# If the user wants to use a more conservative scope, she can. She must
# configure the Azure builder to use build_resource_group_name. The
# easiest solution is subscription wide permission.
# 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
fi
2016-03-04 04:22:20 -05:00
}
showConfigs( ) {
2018-04-15 17:45:17 -04:00
echo ""
echo "Use the following configuration for your packer template:"
echo ""
echo "{"
echo " \"client_id\": \" $azure_client_id \", "
echo " \"client_secret\": \" $azure_client_secret \", "
echo " \"object_id\": \" $azure_object_id \", "
echo " \"subscription_id\": \" $azure_subscription_id \", "
echo " \"tenant_id\": \" $azure_tenant_id \", "
echo " \"resource_group_name\": \" $azure_group_name \", "
echo " \"storage_account\": \" $azure_storage_name \", "
echo "}"
echo ""
2017-04-03 20:14:27 -04:00
}
doSleep( ) {
2018-04-15 17:45:17 -04:00
local sleep_time = ${ PACKER_SLEEP_TIME - $create_sleep }
echo ""
echo " Sleeping for ${ sleep_time } seconds to wait for resources to be "
echo "created. If you get an error about a resource not existing, you can "
echo "try increasing the amount of time we wait after creating resources "
echo "by setting PACKER_SLEEP_TIME to something higher than the default."
echo ""
sleep $sleep_time
2016-03-04 04:22:20 -05:00
}
2017-04-03 20:14:27 -04:00
retryable( ) {
2018-04-15 17:45:17 -04:00
n = 0
until [ $n -ge $1 ]
do
$2 && return 0
echo " $2 failed. Retrying... "
n = $[ $n +1]
doSleep
done
echo " $2 failed after $1 tries. Exiting. "
exit 1
2017-04-03 20:14:27 -04:00
}
2016-03-04 04:22:20 -05:00
setup( ) {
2018-04-15 17:45:17 -04:00
requirements
2017-04-03 20:14:27 -04:00
2018-04-15 17:45:17 -04:00
az login
2017-04-03 20:14:27 -04:00
2018-04-15 17:45:17 -04:00
askSubscription
askName
askSecret
askLocation
2017-04-03 20:14:27 -04:00
2018-04-15 17:45:17 -04:00
# Some of the resources take a while to converge in the API. To make the
# script more reliable we'll add a sleep after we create each resource.
2017-04-03 20:14:27 -04:00
2018-04-15 17:45:17 -04:00
retryable 3 createResourceGroup
retryable 3 createStorageAccount
retryable 3 createApplication
retryable 3 createServicePrincipal
retryable 3 createPermissions
2017-04-03 20:14:27 -04:00
2018-04-15 17:45:17 -04:00
showConfigs
2016-03-04 00:46:51 -05:00
}
case " $1 " in
2018-04-15 17:45:17 -04:00
requirements)
requirements
; ;
setup)
setup
; ;
*)
showhelp
; ;
2016-03-04 00:46:51 -05:00
esac