Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- set -euo pipefail
- IFS=$'nt'
- # -e: immediately exit if any command has a non-zero exit status
- # -o: prevents errors in a pipeline from being masked
- # IFS new value is less likely to cause confusing bugs when looping arrays or arguments (e.g. $@)
- usage() { echo "Usage: $0 -i <subscriptionId> -g <resourceGroupName> -n <deploymentName> -l <resourceGroupLocation>" 1>&2; exit 1; }
- declare subscriptionId=""
- declare resourceGroupName=""
- declare deploymentName=""
- declare resourceGroupLocation=""
- declare kvResourceGroupName=""
- # Initialize parameters specified from command line
- while getopts ":i:g:n:l:" arg; do
- case "${arg}" in
- i)
- subscriptionId=${OPTARG}
- ;;
- g)
- resourceGroupName=${OPTARG}
- ;;
- n)
- deploymentName=${OPTARG}
- ;;
- l)
- resourceGroupLocation=${OPTARG}
- ;;
- esac
- done
- shift $((OPTIND-1))
- #Prompt for parameters is some required parameters are missing
- if [[ -z "$subscriptionId" ]]; then
- echo "Subscription Id:"
- read subscriptionId
- [[ "${subscriptionId:?}" ]]
- fi
- if [[ -z "$resourceGroupName" ]]; then
- echo "ResourceGroupName:"
- read resourceGroupName
- [[ "${resourceGroupName:?}" ]]
- fi
- if [[ -z "$deploymentName" ]]; then
- echo "DeploymentName:"
- read deploymentName
- fi
- if [[ -z "$resourceGroupLocation" ]]; then
- echo "Enter a location below to create a new resource group else skip this"
- echo "ResourceGroupLocation:"
- read resourceGroupLocation
- fi
- templateURI="https://raw.githubusercontent.com/CalCof/azure-quickstart-templates/master/glassfish-on-suse/"
- templateFile="${templateURI}azuredeploy.json"
- if [ ! -f "$templateFile" ]; then
- echo "$templateFile not found"
- exit 1
- fi
- #parameter file path
- parametersFile="azuredeploy.parameters.json"
- if [ ! -f "$parametersFile" ]; then
- echo "$parametersFile not found"
- exit 1
- fi
- if [ -z "$subscriptionId" ] || [ -z "$resourceGroupName" ] || [ -z "$deploymentName" ]; then
- echo "Either one of subscriptionId, resourceGroupName, deploymentName is empty"
- usage
- fi
- #login to azure using your credentials
- az account show 1> /dev/null
- if [ $? != 0 ];
- then
- az login
- fi
- #set the default subscription id
- az account set --subscription $subscriptionId
- set +e
- #Check for existing RG
- az group show $resourceGroupName 1> /dev/null
- if [ $? != 0 ]; then
- echo "Resource group with name" $resourceGroupName "could not be found. Creating new resource group.."
- set -e
- (
- set -x
- az group create --name $resourceGroupName --location $resourceGroupLocation 1> /dev/null
- )
- else
- echo "Using existing resource group..."
- fi
- #Start deployment
- echo "Starting deployment..."
- (
- set -x
- az group deployment create --name $deploymentName --resource-group $resourceGroupName --template-uri $templateFile --parameters $parametersFile
- )
- if [ $? == 0 ];
- then
- echo "Template has been successfully deployed"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement