github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/contrib/azure-setup.sh (about) 1 #!/usr/bin/env bash 2 set -e 3 4 meta_name= 5 azure_client_id= # Derived from application after creation 6 azure_client_name= # Application name 7 azure_client_secret= # Application password 8 azure_group_name= 9 azure_storage_name= 10 azure_subscription_id= # Derived from the account after login 11 azure_tenant_id= # Derived from the account after login 12 13 showhelp() { 14 echo "azure-setup" 15 echo "" 16 echo " azure-setup helps you generate packer credentials for Azure" 17 echo "" 18 echo " The script creates a resource group, storage account, application" 19 echo " (client), service principal, and permissions and displays a snippet" 20 echo " for use in your packer templates." 21 echo "" 22 echo " For simplicity we make a lot of assumptions and choose reasonable" 23 echo " defaults. If you want more control over what happens, please use" 24 echo " the azure-cli directly." 25 echo "" 26 echo " Note that you must already have an Azure account, username," 27 echo " password, and subscription. You can create those here:" 28 echo "" 29 echo " - https://account.windowsazure.com/" 30 echo "" 31 echo "REQUIREMENTS" 32 echo "" 33 echo " - azure-cli" 34 echo " - jq" 35 echo "" 36 echo " Use the requirements command (below) for more info." 37 echo "" 38 echo "USAGE" 39 echo "" 40 echo " ./azure-setup.sh requirements" 41 echo " ./azure-setup.sh setup" 42 echo "" 43 } 44 45 requirements() { 46 found=0 47 48 azureversion=$(azure -v) 49 if [ $? -eq 0 ]; then 50 found=$((found + 1)) 51 echo "Found azure-cli version: $azureversion" 52 else 53 echo "azure-cli is missing. Please install azure-cli from" 54 echo "https://azure.microsoft.com/en-us/documentation/articles/xplat-cli-install/" 55 fi 56 57 jqversion=$(jq --version) 58 if [ $? -eq 0 ]; then 59 found=$((found + 1)) 60 echo "Found jq version: $jqversion" 61 else 62 echo "jq is missing. Please install jq from" 63 echo "https://stedolan.github.io/jq/" 64 fi 65 66 if [ $found -lt 2 ]; then 67 exit 1 68 fi 69 } 70 71 askSubscription() { 72 azure account list 73 echo "" 74 echo "Please enter the Id of the account you wish to use. If you do not see" 75 echo "a valid account in the list press Ctrl+C to abort and create one." 76 echo "If you leave this blank we will use the Current account." 77 echo -n "> " 78 read azure_subscription_id 79 if [ "$azure_subscription_id" != "" ]; then 80 azure account set $azure_subscription_id 81 else 82 azure_subscription_id=$(azure account show --json | jq -r .[].id) 83 fi 84 azure_tenant_id=$(azure account show --json | jq -r .[].tenantId) 85 echo "Using subscription_id: $azure_subscription_id" 86 echo "Using tenant_id: $azure_tenant_id" 87 } 88 89 askName() { 90 echo "" 91 echo "Choose a name for your resource group, storage account, and client" 92 echo "client. This is arbitrary, but it must not already be in use by" 93 echo "any of those resources. ALPHANUMERIC ONLY. Ex: mypackerbuild" 94 echo -n "> " 95 read meta_name 96 } 97 98 askSecret() { 99 echo "" 100 echo "Enter a secret for your application. We recommend generating one with" 101 echo "openssl rand -base64 24. If you leave this blank we will attempt to" 102 echo "generate one for you using openssl. THIS WILL BE SHOWN IN PLAINTEXT." 103 echo "Ex: mypackersecret8734" 104 echo -n "> " 105 read azure_client_secret 106 if [ "$azure_client_secret" = "" ]; then 107 azure_client_secret=$(openssl rand -base64 24) 108 if [ $? -ne 0 ]; then 109 echo "Error generating secret" 110 exit 1 111 fi 112 echo "Generated client_secret: $azure_client_secret" 113 fi 114 } 115 116 createResourceGroup() { 117 echo "==> Creating resource group" 118 azure group create -n $meta_name -l westus 119 if [ $? -eq 0 ]; then 120 azure_group_name=$meta_name 121 else 122 echo "Error creating resource group: $meta_name" 123 exit 1 124 fi 125 } 126 127 createStorageAccount() { 128 echo "==> Creating storage account" 129 azure storage account create -g $meta_name -l westus --type LRS $meta_name 130 if [ $? -eq 0 ]; then 131 azure_storage_name=$meta_name 132 else 133 echo "Error creating storage account: $meta_name" 134 exit 1 135 fi 136 } 137 138 createApplication() { 139 echo "==> Creating application" 140 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) 141 if [ $? -ne 0 ]; then 142 echo "Error creating application: $meta_name @ http://$meta_name" 143 exit 1 144 fi 145 } 146 147 createServicePrinciple() { 148 echo "==> Creating service principal" 149 azure ad sp create $azure_client_id 150 if [ $? -ne 0 ]; then 151 echo "Error creating service principal: $azure_client_id" 152 exit 1 153 fi 154 } 155 156 createPermissions() { 157 echo "==> Creating permissions" 158 azure role assignment create -o "Owner" --spn http://$meta_name -c /subscriptions/$azure_subscription_id 159 # We want to use this more conservative scope but it does not work with the 160 # current implementation which uses temporary resource groups 161 # azure role assignment create --spn http://$meta_name -g $azure_group_name -o "API Management Service Contributor" 162 if [ $? -ne 0 ]; then 163 echo "Error creating permissions for: http://$meta_name" 164 exit 1 165 fi 166 } 167 168 showConfigs() { 169 echo "" 170 echo "Use the following configuration for your packer template:" 171 echo "" 172 echo " \"client_id\": \"$azure_client_id\"," 173 echo " \"client_secret\": \"$azure_client_secret\"," 174 echo " \"resource_group_name\": \"$azure_group_name\"," 175 echo " \"storage_account\": \"$azure_storage_name\"," 176 echo " \"subscription_id\": \"$azure_subscription_id\"," 177 echo " \"tenant_id\": \"$azure_tenant_id\"," 178 echo "" 179 } 180 181 setup() { 182 requirements 183 184 azure config mode arm 185 azure login 186 187 askSubscription 188 askName 189 askSecret 190 191 # Some of the resources take a while to converge in the API. To make the 192 # script more reliable we'll add a sleep after we create each resource. 193 194 createResourceGroup 195 sleep 5 196 createStorageAccount 197 sleep 5 198 createApplication 199 sleep 5 200 createServicePrinciple 201 sleep 5 202 createPermissions 203 204 showConfigs 205 } 206 207 case "$1" in 208 requirements) 209 requirements 210 ;; 211 setup) 212 setup 213 ;; 214 *) 215 showhelp 216 ;; 217 esac