Class: Chef::Knife::EucaServerCreate

Inherits:
Chef::Knife show all
Includes:
EucaBase
Defined in:
lib/chef/knife/euca_server_create.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EucaBase

#connection, included, #locate_config_value, #public_ip

Instance Attribute Details

#initial_sleep_delayObject

Returns the value of attribute initial_sleep_delay


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

def initial_sleep_delay
  @initial_sleep_delay
end

Instance Method Details

#bootstrap_for_node(server) ⇒ Object


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/chef/knife/euca_server_create.rb', line 210

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

#runObject


151
152
153
154
155
156
157
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/chef/knife/euca_server_create.rb', line 151

def run
  $stdout.sync = true

  server_def = {
    :image_id => locate_config_value(:image),
    :groups => config[:security_groups],
    :flavor_id => locate_config_value(:flavor),
    :key_name => Chef::Config[:knife][:euca_ssh_key_id],
    :availability_zone => Chef::Config[:knife][:availability_zone]
  }

  server = connection.servers.create(server_def)

  puts "#{ui.color("Instance ID", :cyan)}: #{server.id}"
  puts "#{ui.color("Flavor", :cyan)}: #{server.flavor_id}"
  puts "#{ui.color("Image", :cyan)}: #{server.image_id}"
  puts "#{ui.color("Availability Zone", :cyan)}: #{server.availability_zone}"
  puts "#{ui.color("Security Groups", :cyan)}: #{server.groups.join(", ")}"
  puts "#{ui.color("SSH Key", :cyan)}: #{server.key_name}"

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

  # wait for it to be ready to do stuff
  server.wait_for { print "."; ready? }

  puts("\n")

  puts "#{ui.color("Public DNS Name", :cyan)}: #{server.dns_name}"
  puts "#{ui.color("Private DNS Name", :cyan)}: #{server.private_dns_name}"

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

  print(".") until tcp_test_ssh(server.dns_name) {
    sleep @initial_sleep_delay ||= 20
    puts("done")
  }

  until begin
      bootstrap_for_node(server).run
      true
    rescue Errno::ECONNREFUSED
      sleep 2
      false
    end
  end

  puts "\n"
  puts "#{ui.color("Instance ID", :cyan)}: #{server.id}"
  puts "#{ui.color("Flavor", :cyan)}: #{server.flavor_id}"
  puts "#{ui.color("Image", :cyan)}: #{server.image_id}"
  puts "#{ui.color("Availability Zone", :cyan)}: #{server.availability_zone}"
  puts "#{ui.color("Security Groups", :cyan)}: #{server.groups.join(", ")}"
  puts "#{ui.color("Public DNS Name", :cyan)}: #{server.dns_name}"
  puts "#{ui.color("Private DNS Name", :cyan)}: #{server.private_dns_name}"
  puts "#{ui.color("SSH Key", :cyan)}: #{server.key_name}"
  puts "#{ui.color("Environment", :cyan)}: #{config[:environment] || '_default'}"
  puts "#{ui.color("Run List", :cyan)}: #{config[:run_list].join(', ')}"
end

#tcp_test_ssh(hostname) ⇒ Object


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/chef/knife/euca_server_create.rb', line 127

def tcp_test_ssh(hostname)
  tcp_socket = TCPSocket.new(hostname, 22)
  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
  false
rescue Errno::EPERM
  false
rescue Errno::ECONNREFUSED
  sleep 2
  false
rescue Errno::EHOSTUNREACH
  sleep 2
  false
ensure
  tcp_socket && tcp_socket.close
end