Class: Chef::Knife::Ec2ServerCreate

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/ec2_server_create.rb

Constant Summary

Constants inherited from Chef::Knife

DEFAULT_SUBCOMMAND_FILES

Instance Attribute Summary collapse

Attributes inherited from Chef::Knife

#name_args

Instance Method Summary collapse

Methods inherited from Chef::Knife

#ask_question, #bulk_delete, category, #configure_chef, #confirm, #create_object, #delete_object, #edit_data, #edit_object, #file_exists_and_is_readable?, #format_for_display, #format_list_for_display, guess_category, inherited, #initialize, list_commands, load_commands, #load_from_file, #msg, msg, #output, #parse_options, #pretty_print, #rest, run, #show_usage, snake_case_name, #stdin, #stdout, subcommand_category, subcommand_class_from, subcommands, subcommands_by_category, unnamed?

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Attribute Details

#initial_sleep_delayObject

Returns the value of attribute initial_sleep_delay.



29
30
31
# File 'lib/chef/knife/ec2_server_create.rb', line 29

def initial_sleep_delay
  @initial_sleep_delay
end

Instance Method Details

#bootstrap_for_node(server) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/chef/knife/ec2_server_create.rb', line 200

def bootstrap_for_node(server)
  bootstrap = Chef::Knife::Bootstrap.new
  bootstrap.name_args = [server.dns_name]
  bootstrap.config[:run_list] = @name_args
  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[:distro] = config[:distro]
  bootstrap.config[:use_sudo] = true
  bootstrap.config[:template_file] = config[:template_file]
  bootstrap
end

#hObject



114
115
116
# File 'lib/chef/knife/ec2_server_create.rb', line 114

def h
  @highline ||= HighLine.new
end

#runObject



137
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
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
# File 'lib/chef/knife/ec2_server_create.rb', line 137

def run 
  require 'fog'
  require 'highline'
  require 'net/ssh/multi'
  require 'readline'

  $stdout.sync = true

  connection = Fog::AWS::EC2.new(
    :aws_access_key_id => Chef::Config[:knife][:aws_access_key_id],
    :aws_secret_access_key => Chef::Config[:knife][:aws_secret_access_key],
    :region => config[:region]
  )

  server = connection.servers.create(
    :image_id => config[:image],
    :groups => config[:security_groups],
    :flavor_id => config[:flavor],
    :key_name => Chef::Config[:knife][:aws_ssh_key_id],
    :availability_zone => config[:availability_zone]
  )

  puts "#{h.color("Instance ID", :cyan)}: #{server.id}"
  puts "#{h.color("Flavor", :cyan)}: #{server.flavor_id}"
  puts "#{h.color("Image", :cyan)}: #{server.image_id}"
  puts "#{h.color("Availability Zone", :cyan)}: #{server.availability_zone}"
  puts "#{h.color("Security Groups", :cyan)}: #{server.groups.join(", ")}"
  puts "#{h.color("SSH Key", :cyan)}: #{server.key_name}"
     
  print "\n#{h.color("Waiting for server", :magenta)}"

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

  puts("\n")


  puts "#{h.color("Public DNS Name", :cyan)}: #{server.dns_name}"
  puts "#{h.color("Public IP Address", :cyan)}: #{server.ip_address}"
  puts "#{h.color("Private DNS Name", :cyan)}: #{server.private_dns_name}"
  puts "#{h.color("Private IP Address", :cyan)}: #{server.private_ip_address}"

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

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


  bootstrap_for_node(server).run

  puts "\n"
  puts "#{h.color("Instance ID", :cyan)}: #{server.id}"
  puts "#{h.color("Flavor", :cyan)}: #{server.flavor_id}"
  puts "#{h.color("Image", :cyan)}: #{server.image_id}"
  puts "#{h.color("Availability Zone", :cyan)}: #{server.availability_zone}"
  puts "#{h.color("Security Groups", :cyan)}: #{server.groups.join(", ")}"
  puts "#{h.color("SSH Key", :cyan)}: #{server.key_name}"
  puts "#{h.color("Public DNS Name", :cyan)}: #{server.dns_name}"
  puts "#{h.color("Public IP Address", :cyan)}: #{server.ip_address}"
  puts "#{h.color("Private DNS Name", :cyan)}: #{server.private_dns_name}"
  puts "#{h.color("Private IP Address", :cyan)}: #{server.private_ip_address}"
  puts "#{h.color("Run List", :cyan)}: #{@name_args.join(', ')}"
end

#tcp_test_ssh(hostname) ⇒ Object



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

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::ECONNREFUSED
  sleep 2
  false
ensure
  tcp_socket && tcp_socket.close
end