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, #storage_management_client, #try_azure_operation

Instance Method Details

#chef_vm_extension(machine_name, location) ⇒ Object



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
# File 'lib/chef/provider/azure_resource_template.rb', line 57

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])
  machine_name = "\'#{machine_name}\'" unless machine_name[0] == '['
  "    {\n      \"type\": \"Microsoft.Compute/virtualMachines/extensions\",\n      \"name\": \"[concat(\#{machine_name.delete('[]')},'/', 'chefExtension')]\",\n      \"apiVersion\": \"2015-05-01-preview\",\n      \"location\": \"\#{location}\",\n      \"dependsOn\": [\n        \"[concat('Microsoft.Compute/virtualMachines/', \#{machine_name.delete('[]')})]\"\n      ],\n      \"properties\": {\n        \"publisher\": \"Chef.Bootstrap.WindowsAzure\",\n        \"type\": \"\#{new_resource.chef_extension[:client_type]}\",\n        \"typeHandlerVersion\": \"\#{new_resource.chef_extension[:version]}\",\n        \"settings\": {\n          \"bootstrap_options\": {\n            \"chef_node_name\" : \"[concat(\#{machine_name.delete('[]')},'.','\#{new_resource.resource_group}')]\",\n            \"chef_server_url\" : \"\#{chef_server_url}\",\n            \"validation_client_name\" : \"\#{validation_client_name}\"\n          },\n          \"runlist\": \"\#{new_resource.chef_extension[:runlist]}\"\n        },\n        \"protectedSettings\": {\n              \"validation_key\": \"\#{validation_key_content.gsub(\"\\n\", '\\\\n')}\"\n        }\n      }\n    }\n  EOH\nend\n"

#deploymentObject



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

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



117
118
119
120
121
# File 'lib/chef/provider/azure_resource_template.rb', line 117

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

#follow_deployment_until_end_stateObject



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

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



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

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).value!
  deployment_operations.body.value.each do |val|
    resource_provisioning_state = val.properties.provisioning_state
    resource_name = val.properties.target_resource.resource_name
    resource_type = val.properties.target_resource.resource_type
    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



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

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

#templateObject



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

def template
  template_src_file = ::File.join(Chef::Config[:chef_repo_path], new_resource.template_source)
  fail "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