Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #!/bin/bash
  2. set -euo pipefail
  3. IFS=$'nt'
  4.  
  5. # -e: immediately exit if any command has a non-zero exit status
  6. # -o: prevents errors in a pipeline from being masked
  7. # IFS new value is less likely to cause confusing bugs when looping arrays or arguments (e.g. $@)
  8.  
  9. usage() { echo "Usage: $0 -i <subscriptionId> -g <resourceGroupName> -n <deploymentName> -l <resourceGroupLocation>" 1>&2; exit 1; }
  10.  
  11. declare subscriptionId=""
  12. declare resourceGroupName=""
  13. declare deploymentName=""
  14. declare resourceGroupLocation=""
  15.  
  16. declare kvResourceGroupName=""
  17.  
  18. # Initialize parameters specified from command line
  19. while getopts ":i:g:n:l:" arg; do
  20. case "${arg}" in
  21. i)
  22. subscriptionId=${OPTARG}
  23. ;;
  24. g)
  25. resourceGroupName=${OPTARG}
  26. ;;
  27. n)
  28. deploymentName=${OPTARG}
  29. ;;
  30. l)
  31. resourceGroupLocation=${OPTARG}
  32. ;;
  33. esac
  34. done
  35. shift $((OPTIND-1))
  36.  
  37. #Prompt for parameters is some required parameters are missing
  38. if [[ -z "$subscriptionId" ]]; then
  39. echo "Subscription Id:"
  40. read subscriptionId
  41. [[ "${subscriptionId:?}" ]]
  42. fi
  43.  
  44. if [[ -z "$resourceGroupName" ]]; then
  45. echo "ResourceGroupName:"
  46. read resourceGroupName
  47. [[ "${resourceGroupName:?}" ]]
  48. fi
  49.  
  50. if [[ -z "$deploymentName" ]]; then
  51. echo "DeploymentName:"
  52. read deploymentName
  53. fi
  54.  
  55. if [[ -z "$resourceGroupLocation" ]]; then
  56. echo "Enter a location below to create a new resource group else skip this"
  57. echo "ResourceGroupLocation:"
  58. read resourceGroupLocation
  59. fi
  60.  
  61. templateURI="https://raw.githubusercontent.com/CalCof/azure-quickstart-templates/master/glassfish-on-suse/"
  62.  
  63. templateFile="${templateURI}azuredeploy.json"
  64.  
  65. if [ ! -f "$templateFile" ]; then
  66. echo "$templateFile not found"
  67. exit 1
  68. fi
  69.  
  70. #parameter file path
  71. parametersFile="azuredeploy.parameters.json"
  72.  
  73. if [ ! -f "$parametersFile" ]; then
  74. echo "$parametersFile not found"
  75. exit 1
  76. fi
  77.  
  78. if [ -z "$subscriptionId" ] || [ -z "$resourceGroupName" ] || [ -z "$deploymentName" ]; then
  79. echo "Either one of subscriptionId, resourceGroupName, deploymentName is empty"
  80. usage
  81. fi
  82.  
  83. #login to azure using your credentials
  84. az account show 1> /dev/null
  85.  
  86. if [ $? != 0 ];
  87. then
  88. az login
  89. fi
  90.  
  91. #set the default subscription id
  92. az account set --subscription $subscriptionId
  93.  
  94. set +e
  95.  
  96. #Check for existing RG
  97. az group show $resourceGroupName 1> /dev/null
  98.  
  99. if [ $? != 0 ]; then
  100. echo "Resource group with name" $resourceGroupName "could not be found. Creating new resource group.."
  101. set -e
  102. (
  103. set -x
  104. az group create --name $resourceGroupName --location $resourceGroupLocation 1> /dev/null
  105. )
  106. else
  107. echo "Using existing resource group..."
  108. fi
  109.  
  110. #Start deployment
  111. echo "Starting deployment..."
  112. (
  113. set -x
  114. az group deployment create --name $deploymentName --resource-group $resourceGroupName --template-uri $templateFile --parameters $parametersFile
  115. )
  116.  
  117. if [ $? == 0 ];
  118. then
  119. echo "Template has been successfully deployed"
  120. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement