Class: Chef::Knife::AzureServerCreate

Inherits:
Chef::Knife show all
Includes:
AzureBase
Defined in:
lib/chef/knife/azure_server_create.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AzureBase

#connection, included, #locate_config_value, #msg_pair

Instance Attribute Details

#initial_sleep_delayObject

Returns the value of attribute initial_sleep_delay.



38
39
40
# File 'lib/chef/knife/azure_server_create.rb', line 38

def initial_sleep_delay
  @initial_sleep_delay
end

Instance Method Details

#bootstrap_for_node(server, fqdn, port) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/chef/knife/azure_server_create.rb', line 261

def bootstrap_for_node(server,fqdn,port)
  bootstrap = Chef::Knife::Bootstrap.new
  bootstrap.name_args = [fqdn]
  bootstrap.config[:run_list] = config[:run_list]
  bootstrap.config[:ssh_user] = locate_config_value(:ssh_user)
  bootstrap.config[:ssh_password] = locate_config_value(:ssh_password)
  bootstrap.config[:ssh_port] = port
  bootstrap.config[:identity_file] = locate_config_value(:identity_file)
  bootstrap.config[:chef_node_name] = locate_config_value(:chef_node_name) || server.name
  bootstrap.config[:prerelease] = locate_config_value(:prerelease)
  bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
  bootstrap.config[:distro] = locate_config_value(:distro)
  bootstrap.config[:use_sudo] = true unless locate_config_value(:ssh_user) == 'root'
  bootstrap.config[:template_file] = config[:template_file]
  bootstrap.config[:environment] = locate_config_value(:environment)
  # may be needed for vpc_mode
  bootstrap.config[:no_host_key_verify] = config[:no_host_key_verify]
  bootstrap
end

#create_server_defObject



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/chef/knife/azure_server_create.rb', line 296

def create_server_def
  server_def = {
    :hosted_service_name => locate_config_value(:hosted_service_name), 
    :storage_account => locate_config_value(:storage_account),
    :role_name => locate_config_value(:role_name), 
    :host_name => locate_config_value(:host_name), 
    :ssh_user => locate_config_value(:ssh_user),
    :ssh_password => locate_config_value(:ssh_password), 
    :service_location => locate_config_value(:service_location), 
    :os_disk_name => locate_config_value(:os_disk_name), 
    :source_image => locate_config_value(:source_image), 
    :role_size => locate_config_value(:role_size),
    :tcp_endpoints => locate_config_value(:tcp_endpoints),
    :udp_endpoints => locate_config_value(:udp_endpoints)
  }
  server_def
end

#parameter_testObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/chef/knife/azure_server_create.rb', line 186

def parameter_test
  details = Array.new
  details << ui.color('name', :bold, :blue)
  details << ui.color('Chef::Config', :bold, :blue)
  details << ui.color('config', :bold, :blue)
  details << ui.color('winner is', :bold, :blue)
  [
    :azure_subscription_id, 
    :azure_mgmt_cert, 
    :azure_host_name,
    :role_name, 
    :host_name, 
    :ssh_user, 
    :ssh_password, 
    :service_location, 
    :source_image, 
    :role_size
  ].each do |key|
    key = key.to_sym
    details << key.to_s
    details << Chef::Config[:knife][key].to_s
    details << config[key].to_s
    details << locate_config_value(key)
  end 
  puts ui.list(details, :columns_across, 4)
end

#random_string(len = 10) ⇒ Object



154
155
156
# File 'lib/chef/knife/azure_server_create.rb', line 154

def random_string(len=10)
  (0...len).map{65.+(rand(25)).chr}.join
end

#runObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/chef/knife/azure_server_create.rb', line 212

def run
  $stdout.sync = true
  storage = nil

  Chef::Log.info("validating...")
  validate!

  Chef::Log.info("creating...")

  if not locate_config_value(:hosted_service_name)
    config[:hosted_service_name] = [strip_non_ascii(locate_config_value(:role_name)), random_string].join
  end

  #If Storage Account is not specified, check if the geographic location has one to re-use 
  if not locate_config_value(:storage_account)
    storage_accts = connection.storageaccounts.all
    storage = storage_accts.find { |storage_acct| storage_acct.location.to_s == locate_config_value(:service_location) }
    if not storage
      config[:storage_account] = [strip_non_ascii(locate_config_value(:role_name)), random_string].join.downcase
    else
      config[:storage_account] = storage.name.to_s
    end
  end
  server = connection.deploys.create(create_server_def)

  puts("\n")

  unless server && server.sshipaddress && server.sshport
    Chef::Log.fatal("server not created")
    exit 1
  end

  fqdn = server.sshipaddress
  port = server.sshport

  print "\n#{ui.color("Waiting for sshd on #{fqdn}:#{port}", :magenta)}"

  print(".") until tcp_test_ssh(fqdn,port) {
    sleep @initial_sleep_delay ||= 10
    puts("done")
  }

  sleep 15

  bootstrap_for_node(server,fqdn,port).run

  puts "\n"
end

#strip_non_ascii(string) ⇒ Object



150
151
152
# File 'lib/chef/knife/azure_server_create.rb', line 150

def strip_non_ascii(string)
  string.gsub(/[^0-9a-z ]/i, '')
end

#tcp_test_ssh(fqdn, sshport) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/chef/knife/azure_server_create.rb', line 158

def tcp_test_ssh(fqdn, sshport)
  tcp_socket = TCPSocket.new(fqdn, sshport)
  readable = IO.select([tcp_socket], nil, nil, 5)
  if readable
    Chef::Log.debug("sshd accepting connections on #{fqdn}, banner is #{tcp_socket.gets}")
    yield
    true
  else
    false
  end
rescue SocketError
  sleep 2
  false
rescue Errno::ETIMEDOUT
  false
rescue Errno::EPERM
  false
rescue Errno::ECONNREFUSED
  sleep 2
  false
  # This happens on EC2 quite often
rescue Errno::EHOSTUNREACH
  sleep 2
  false
ensure
  tcp_socket && tcp_socket.close
end

#validate!Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/chef/knife/azure_server_create.rb', line 281

def validate!
  super([
        :azure_subscription_id, 
        :azure_mgmt_cert, 
        :azure_host_name,
        :role_name, 
        :host_name, 
        :ssh_user, 
        :ssh_password, 
        :service_location, 
        :source_image, 
        :role_size
  ])
end