Module: Chef::Knife::AzureBase

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object

:nodoc: Would prefer to do this in a rational way, but can’t be done b/c of Mixlib::CLI’s design :(



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chef/knife/azure_base.rb', line 30

def self.included(includer)
  includer.class_eval do

    deps do
      require 'readline'
      require 'chef/json_compat'
    end

    option :azure_subscription_id,
      :short => "-S ID",
      :long => "--azure-subscription-id ID",
      :description => "Your Azure subscription ID",
      :proc => Proc.new { |key| Chef::Config[:knife][:azure_subscription_id] = key }

    option :azure_mgmt_cert,
      :short => "-p FILENAME",
      :long => "--azure-mgmt-cert FILENAME",
      :description => "Your Azure PEM file name",
      :proc => Proc.new { |key| Chef::Config[:knife][:azure_mgmt_cert] = key }

    option :azure_api_host_name,
      :short => "-H HOSTNAME",
      :long => "--azure-api-host-name HOSTNAME",
      :description => "Your Azure host name",
      :proc => Proc.new { |key| Chef::Config[:knife][:azure_api_host_name] = key }

    option :verify_ssl_cert,
      :long => "--verify-ssl-cert",
      :description => "Verify SSL Certificates for communication over HTTPS",
      :boolean => true,
      :default => false

    option :azure_publish_settings_file,
      :long => "--azure-publish-settings-file FILENAME",
      :description => "Your Azure Publish Settings File",
      :proc => Proc.new { |key| Chef::Config[:knife][:azure_publish_settings_file] = key }
  end
end

Instance Method Details

#connectionObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/chef/knife/azure_base.rb', line 79

def connection
  @connection ||= begin
                    connection = Azure::Connection.new(
                      :azure_subscription_id => locate_config_value(:azure_subscription_id),
                      :azure_mgmt_cert => locate_config_value(:azure_mgmt_cert),
                      :azure_api_host_name => locate_config_value(:azure_api_host_name),
                      :verify_ssl_cert => locate_config_value(:verify_ssl_cert)
                    )
                  end
end

#find_file(name) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/chef/knife/azure_base.rb', line 165

def find_file(name)
  config_dir = Chef::Knife.chef_config_dir
  if File.exist? name
    file = name
  elsif config_dir && File.exist?(File.join(config_dir, name))
    file = File.join(config_dir, name)
  elsif File.exist?(File.join(ENV['HOME'], '.chef', name))
    file = File.join(ENV['HOME'], '.chef', name)
  else
    ui.error('Unable to find file - ' + name)
    exit 1
  end
  file
end

#is_image_windows?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
# File 'lib/chef/knife/azure_base.rb', line 69

def is_image_windows?
  images = connection.images
  target_image = images.all.select { |i| i.name == locate_config_value(:azure_source_image) }
  unless target_image[0].nil?
    return target_image[0].os == 'Windows'
  else
    ui.error("Invalid image. Use the command \"knife azure image list\" to verify the image name")
    exit 1
  end
end

#locate_config_value(key) ⇒ Object



90
91
92
93
# File 'lib/chef/knife/azure_base.rb', line 90

def locate_config_value(key)
  key = key.to_sym
  config[key] || Chef::Config[:knife][key]
end

#msg_pair(label, value, color = :cyan) ⇒ Object



95
96
97
98
99
# File 'lib/chef/knife/azure_base.rb', line 95

def msg_pair(label, value, color=:cyan)
  if value && !value.to_s.empty?
    puts "#{ui.color(label, color)}: #{value}"
  end
end

#msg_server_summary(server) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/chef/knife/azure_base.rb', line 101

def msg_server_summary(server)
  puts "\n"
  msg_pair('DNS Name', server.hostedservicename + ".cloudapp.net")
  msg_pair('VM Name', server.name)
  msg_pair('Size', server.size)
  msg_pair('Azure Source Image', locate_config_value(:azure_source_image))
  msg_pair('Azure Service Location', locate_config_value(:azure_service_location))
  msg_pair('Public Ip Address', server.publicipaddress)
  msg_pair('Private Ip Address', server.ipaddress)
  msg_pair('SSH Port', server.sshport) unless server.sshport.nil?
  msg_pair('WinRM Port', server.winrmport) unless server.winrmport.nil?
  msg_pair('TCP Ports', server.tcpports) unless server.tcpports.nil? or server.tcpports.empty?
  msg_pair('UDP Ports', server.udpports) unless server.udpports.nil? or server.udpports.empty?
  msg_pair('Environment', locate_config_value(:environment) || '_default')
  msg_pair('Runlist', locate_config_value(:run_list)) unless locate_config_value(:run_list).empty?
  puts "\n"
end

#parse_publish_settings_file(filename) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/chef/knife/azure_base.rb', line 138

def parse_publish_settings_file(filename)
  require 'nokogiri'
  require 'base64'
  require 'openssl'
  require 'uri'
  begin
    doc = Nokogiri::XML(File.open(find_file(filename)))
    profile = doc.at_css("PublishProfile")
    subscription = profile.at_css("Subscription")
    #check given PublishSettings XML file format.Currently PublishSettings file have two different XML format
    if profile.attribute("SchemaVersion").nil?
      management_cert = OpenSSL::PKCS12.new(Base64.decode64(profile.attribute("ManagementCertificate").value))
      Chef::Config[:knife][:azure_api_host_name] = URI(profile.attribute("Url").value).host
    elsif profile.attribute("SchemaVersion").value == "2.0"
      management_cert = OpenSSL::PKCS12.new(Base64.decode64(subscription.attribute("ManagementCertificate").value))
      Chef::Config[:knife][:azure_api_host_name] = URI(subscription.attribute("ServiceManagementUrl").value).host
    else
      ui.error("Publish settings file Schema not supported - " + filename)
    end
    Chef::Config[:knife][:azure_mgmt_cert] = management_cert.certificate.to_pem + management_cert.key.to_pem
    Chef::Config[:knife][:azure_subscription_id] = doc.at_css("Subscription").attribute("Id").value
  rescue
    ui.error("Incorrect publish settings file - " + filename)
    exit 1
  end
end

#validate!(keys = [:azure_subscription_id, :azure_mgmt_cert, :azure_api_host_name]) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/chef/knife/azure_base.rb', line 119

def validate!(keys=[:azure_subscription_id, :azure_mgmt_cert, :azure_api_host_name])
  errors = []
  if(locate_config_value(:azure_mgmt_cert) != nil)
    config[:azure_mgmt_cert] = File.read find_file(locate_config_value(:azure_mgmt_cert))
  end
  if(locate_config_value(:azure_publish_settings_file) != nil)
    parse_publish_settings_file(locate_config_value(:azure_publish_settings_file))
  end
  keys.each do |k|
    pretty_key = k.to_s.gsub(/_/, ' ').gsub(/\w+/){ |w| (w =~ /(ssh)|(aws)/i) ? w.upcase  : w.capitalize }
    if locate_config_value(k).nil?
      errors << "You did not provide a valid '#{pretty_key}' value. Please set knife[:#{k}] in your knife.rb or pass as an option."
    end
  end
  if errors.each{|e| ui.error(e)}.any?
    exit 1
  end
end