Class: Chef::Provider::AzureResourceTemplate

Inherits:
Chef::Provisioning::AzureRM::AzureProvider show all
Defined in:
lib/chef/provider/azure_resource_template.rb

Instance Method Summary collapse

Methods inherited from Chef::Provisioning::AzureRM::AzureProvider

#action_handler, #compute_management_client, #network_management_client, #resource_management_client, #resource_manager_endpoint_url, #storage_management_client, #try_azure_operation

Instance Method Details

#chef_vm_extension(machine_name, location) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chef/provider/azure_resource_template.rb', line 56

def chef_vm_extension(machine_name, location)
  chef_server_url = Chef::Config[:chef_server_url]
  validation_client_name = Chef::Config[:validation_client_name]
  validation_key_content = ::File.read(Chef::Config[:validation_key])
  chef_environment = new_resource.chef_extension[:environment].empty? ? '_default' : new_resource.chef_extension[:environment]
  machine_name = "\'#{machine_name}\'" unless machine_name[0] == '['
  <<-EOH
    {
      "type": "Microsoft.Compute/virtualMachines/extensions",
      "name": "[concat(#{machine_name.delete('[]')},'/', 'chefExtension')]",
      "apiVersion": "2015-05-01-preview",
      "location": "#{location}",
      "dependsOn": [
        "[concat('Microsoft.Compute/virtualMachines/', #{machine_name.delete('[]')})]"
      ],
      "properties": {
        "publisher": "Chef.Bootstrap.WindowsAzure",
        "type": "#{new_resource.chef_extension[:client_type]}",
        "typeHandlerVersion": "#{new_resource.chef_extension[:version]}",
        "settings": {
          "bootstrap_options": {
            "chef_node_name" : "[concat(#{machine_name.delete('[]')},'.','#{new_resource.resource_group}')]",
            "chef_server_url" : "#{chef_server_url}",
            "validation_client_name" : "#{validation_client_name}",
            "environment" : "#{chef_environment}"
          },
          "runlist": "#{new_resource.chef_extension[:runlist]}"
        },
        "protectedSettings": {
            "validation_key": "#{validation_key_content.gsub("\n", '\\n')}"
        }
      }
    }
  EOH
end

#deploymentObject



25
26
27
28
29
30
31
32
# File 'lib/chef/provider/azure_resource_template.rb', line 25

def deployment
  deployment = Azure::ARM::Resources::Models::Deployment.new
  deployment.properties = Azure::ARM::Resources::Models::DeploymentProperties.new
  deployment.properties.template = template
  deployment.properties.mode = Azure::ARM::Resources::Models::DeploymentMode::Incremental
  deployment.properties.parameters = parameters_in_values_format
  deployment
end

#deployment_stateObject



120
121
122
123
124
# File 'lib/chef/provider/azure_resource_template.rb', line 120

def deployment_state
  deployments = resource_management_client.deployments.get(new_resource.resource_group, new_resource.name)
  Chef::Log.debug("deployments result: #{deployments.inspect}")
  deployments.properties.provisioning_state
end

#follow_deployment_until_end_stateObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/chef/provider/azure_resource_template.rb', line 92

def follow_deployment_until_end_state
  end_provisioning_states = 'Canceled,Failed,Deleted,Succeeded'
  end_provisioning_state_reached = false
  until end_provisioning_state_reached
    list_outstanding_deployment_operations
    sleep 5
    deployment_provisioning_state = deployment_state
    end_provisioning_state_reached = end_provisioning_states.split(',').include?(deployment_provisioning_state)
  end
  action_handler.report_progress "Resource Template deployment reached end state of '#{deployment_provisioning_state}'."
end

#list_outstanding_deployment_operationsObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/provider/azure_resource_template.rb', line 104

def list_outstanding_deployment_operations
  end_operation_states = 'Failed,Succeeded'
  deployment_operations = resource_management_client.deployment_operations.list(new_resource.resource_group, new_resource.name)
  deployment_operations.each do |val|
    resource_provisioning_state = val.properties.provisioning_state
    unless val.properties.target_resource.nil?
      resource_name = val.properties.target_resource.resource_name
      resource_type = val.properties.target_resource.resource_type
    end
    end_operation_state_reached = end_operation_states.split(',').include?(resource_provisioning_state)
    unless end_operation_state_reached
      action_handler.report_progress "Resource #{resource_type} '#{resource_name}' provisioning status is #{resource_provisioning_state}\n"
    end
  end
end

#parameters_in_values_formatObject



49
50
51
52
53
54
# File 'lib/chef/provider/azure_resource_template.rb', line 49

def parameters_in_values_format
  parameters = new_resource.parameters.map do |key, value|
    { key.to_sym => { 'value' => value } }
  end
  parameters.reduce(:merge!)
end

#templateObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chef/provider/azure_resource_template.rb', line 34

def template
  template_src_file = new_resource.template_source
  Chef::Log.error "Cannot find file: #{template_src_file}" unless ::File.file?(template_src_file)
  template = JSON.parse(::IO.read(template_src_file))
  if new_resource.chef_extension
    machines = template['resources'].select { |h| h['type'] == 'Microsoft.Compute/virtualMachines' }
    machines.each do |machine|
      action_handler.report_progress "adding a Chef VM Extension with name: #{machine['name']} and location: #{machine['location']} "
      extension = chef_vm_extension(machine['name'], machine['location'])
      template['resources'] << JSON.parse(extension)
    end
  end
  template
end

#whyrun_supported?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/chef/provider/azure_resource_template.rb', line 8

def whyrun_supported?
  true
end