Class: Chef::Knife::BrightboxServerCreate

Inherits:
Chef::Knife
  • Object
show all
Includes:
BrightboxBase
Defined in:
lib/chef/knife/brightbox_server_create.rb

Instance Method Summary collapse

Methods included from BrightboxBase

#connection, included, #locate_config_value

Instance Method Details

#bootstrap_for_node(server) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/chef/knife/brightbox_server_create.rb', line 227

def bootstrap_for_node(server)
  bootstrap = Chef::Knife::Bootstrap.new
  bootstrap.name_args = [server.cloud_ips.first['public_ip']]
  bootstrap.config[:run_list] = config[:run_list]
  bootstrap.config[:ssh_user] = config[:ssh_user] || "root"
  bootstrap.config[:ssh_port] = config[:ssh_port]
  bootstrap.config[:identity_file] = config[:identity_file]
  bootstrap.config[:chef_node_name] = config[:chef_node_name] || server.name || server.id
  bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
  bootstrap.config[:distro] = locate_config_value(:distro)
  # bootstrap will run as root...sudo (by default) also messes up Ohai on CentOS boxes
  bootstrap.config[:use_sudo] = true unless config[:ssh_user] == 'root'
  bootstrap.config[:template_file] = locate_config_value(:template_file)
  bootstrap.config[:environment] = config[:environment]
  bootstrap.config[:first_boot_attributes] = config[:first_boot_attributes]
  bootstrap.config[:no_host_key_verify] = config[:no_host_key_verify]
  bootstrap
end

#runObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/chef/knife/brightbox_server_create.rb', line 164

def run
  $stdout.sync = true

  unless config[:image]
    ui.error("You have not provided a valid image value.  Please note the short option for this value recently changed from '-i' to '-I'.")
    exit 1
  end

  print "#{ui.color("Creating server... ", :magenta)}"
  server = connection.servers.create(
    :name => config[:server_name] || config[:chef_node_name],
    :image_id => config[:image],
    :zone_id => zone_id,
    :flavor_id => config[:flavor]
  )
  puts " done \n"

  puts "#{ui.color("Instance ID", :cyan)}: #{server.id}"
  puts "#{ui.color("Name", :cyan)}: #{server.name}"
  puts "#{ui.color("Flavor", :cyan)}: #{server.flavor_id}"
  puts "#{ui.color("Image", :cyan)}: #{server.image.name}"
  puts "#{ui.color("Zone", :cyan)}: #{server.zone['handle']}"

  print "\n#{ui.color("Waiting server", :magenta)}"

  # wait for it to be ready to do stuff
  server.wait_for { print "."; service.servers.get(server.id).ready? }

  puts("\n")

  print "#{ui.color("Creating cloud ip ", :magenta)}"
  ip = connection.create_cloud_ip
  cip = connection.cloud_ips.get ip['id']
  destination_id = server.interfaces.last['id']
  cip.map destination_id
  server.wait_for { print "."; service.cloud_ips.get(ip['id']).mapped? }
  puts " done\n"

  server = connection.servers.get(server.id)
  puts "#{ui.color("Public IP Address", :cyan)}: #{server.public_ip_address}"
  puts "#{ui.color("Private IP Address", :cyan)}: #{server.private_ip_address}"

  config[:server_groups].each do |server_group|
    connection.add_servers_server_group(server_group,
                                        :servers => [{ :server => server.id }])
  end

  print "\n#{ui.color("Bootstrapping server ", :magenta)}"
  print "\n#{ui.color("Waiting for sshd ", :magenta)}"
  print(".") until tcp_test_ssh(server.public_ip_address) { sleep @initial_sleep_delay ||= 10; puts(" done") }
  bootstrap_for_node(server).run

  puts "\n"
  puts "#{ui.color("Instance ID", :cyan)}: #{server.id}"
  puts "#{ui.color("Name", :cyan)}: #{server.name}"
  puts "#{ui.color("Flavor", :cyan)}: #{server.flavor_id}"
  puts "#{ui.color("Image", :cyan)}: #{server.image.name}"
  puts "#{ui.color("Public IP Address", :cyan)}: #{server.public_ip_address}"
  puts "#{ui.color("Private IP Address", :cyan)}: #{server.private_ip_address}"
  puts "#{ui.color("Environment", :cyan)}: #{config[:environment] || '_default'}"
  puts "#{ui.color("Run List", :cyan)}: #{config[:run_list].join(', ')}"
end

#tcp_test_ssh(hostname) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/chef/knife/brightbox_server_create.rb', line 145

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