SHARE
TWEET

Untitled

a guest Mar 20th, 2017 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'azure_mgmt_resources'
  2.  
  3. class Deployer
  4.  
  5.   # Initialize the deployer class with subscription, resource group and resource group location. The class will raise an
  6.   # ArgumentError if there are empty values for Tenant Id, Client Id or Client Secret environment variables.
  7.   #
  8.   # @param [String] subscription_id the subscription to deploy the template
  9.   # @param [String] resource_group the resource group to create or update and then deploy the template
  10.   # @param [String] resource_group_location the location of the resource group
  11.   def initialize(subscription_id, resource_group, resource_group_location)
  12.     raise ArgumentError.new("Missing template file 'template.json' in current directory.") unless File.exist?('template.json')
  13.     raise ArgumentError.new("Missing parameters file 'parameters.json' in current directory.") unless File.exist?('parameters.json')
  14.     @resource_group = resource_group
  15.     @subscription_id = subscription_id
  16.     @resource_group_location = resource_group_location
  17.     provider = MsRestAzure::ApplicationTokenProvider.new(
  18.         ENV['AZURE_TENANT_ID'],
  19.         ENV['AZURE_CLIENT_ID'],
  20.         ENV['AZURE_CLIENT_SECRET'])
  21.     credentials = MsRest::TokenCredentials.new(provider)
  22.     @client = Azure::ARM::Resources::ResourceManagementClient.new(credentials)
  23.     @client.subscription_id = @subscription_id
  24.   end
  25.  
  26.   # Deploy the template to a resource group
  27.   def deploy
  28.     # ensure the resource group is created
  29.     params = Azure::ARM::Resources::Models::ResourceGroup.new.tap do |rg|
  30.       rg.location = @resource_group_location
  31.     end
  32.     @client.resource_groups.create_or_update(@resource_group, params).value!
  33.  
  34.     # build the deployment from a json file template from parameters
  35.     template = File.read(File.expand_path(File.join(__dir__, 'template.json')))
  36.     deployment = Azure::ARM::Resources::Models::Deployment.new
  37.     deployment.properties = Azure::ARM::Resources::Models::DeploymentProperties.new
  38.     deployment.properties.template = JSON.parse(template)
  39.     deployment.properties.mode = Azure::ARM::Resources::Models::DeploymentMode::Incremental
  40.  
  41.     # build the deployment template parameters from Hash to {key: {value: value}} format
  42.     deploy_params = File.read(File.expand_path(File.join(__dir__, 'parameters.json')))
  43.     deployment.properties.parameters = JSON.parse(deploy_params)["parameters"]
  44.  
  45.     # put the deployment to the resource group
  46.     @client.deployments.create_or_update(@resource_group, 'azure-sample', deployment)
  47.   end
  48. end
  49.  
  50. # Get user inputs and execute the script
  51. if(ARGV.empty?)
  52.   puts "Please specify subscriptionId resourceGroupName resourceGroupLocation as command line arguments"
  53.   exit
  54. end
  55.  
  56. subscription_id = ARGV[0]             # Azure Subscription Id
  57. resource_group = ARGV[1]              # The resource group for deployment
  58. resource_group_location = ARGV[2]     # The resource group location
  59.  
  60. msg = "\nInitializing the Deployer class with subscription id: #{subscription_id}, resource group: #{resource_group}"
  61. msg += "\nand resource group location: #{resource_group_location}...\n\n"
  62. puts msg
  63.  
  64. # Initialize the deployer class
  65. deployer = Deployer.new(subscription_id, resource_group, resource_group_location)
  66.  
  67. puts "Beginning the deployment... \n\n"
  68. # Deploy the template
  69. deployment = deployer.deploy
  70.  
  71. puts "Done deploying!!"
RAW Paste Data
Top