Class: Chef::Knife::ClodoServerCreate

Inherits:
Chef::Knife show all
Includes:
ClodoBase
Defined in:
lib/chef/knife/clodo_server_create.rb

Instance Method Summary collapse

Methods included from ClodoBase

#connection, included, #locate_config_value

Instance Method Details

#bootstrap_for_node(server) ⇒ Object



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

def bootstrap_for_node(server)
  bootstrap = Chef::Knife::Bootstrap.new
  bootstrap.name_args = [server.public_ip_address]
  bootstrap.config[:run_list] = config[:run_list]
  bootstrap.config[:ssh_user] = config[:ssh_user] || "root"
  bootstrap.config[:ssh_password] = server.password
  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 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[:environment] = locate_config_value(:environment)
  bootstrap.config[:distro] = locate_config_value(:distro)
  bootstrap.config[:template_file] = locate_config_value(:template_file)
  bootstrap
end

#runObject



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/clodo_server_create.rb', line 141

def run
  $stdout.sync = true

  unless locate_config_value(: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

  options = {
    :vps_type       => locate_config_value(:server_type),
    :vps_memory     => locate_config_value(:server_memory),
    :vps_memory_max => locate_config_value(:server_memory_max),
    :vps_hdd        => locate_config_value(:server_disk),
    :vps_admin      => locate_config_value(:server_support_level),
    :vps_os         => locate_config_value(:image),
    :vps_autoupdate => locate_config_value(:autoupdate)
  }

  options[:vps_title] = config[:server_name] if config[:server_name]

  server = connection.servers.create(options)

  puts "#{ui.color("ID", :cyan)}: #{server.id}"
  puts "#{ui.color("Name", :cyan)}: #{server.name}"
  puts "#{ui.color("Image", :cyan)}: #{server.image}"
  puts "#{ui.color("IP", :cyan)}: #{server.public_ip_address}"
  puts "#{ui.color("root password", :red)}: #{server.password}"

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

  # wait for it to be ready to do stuff
  server.wait_for(600, 10) do print "."; ready? end

  puts("\n")

  puts "#{ui.color("Public IP Address", :cyan)}: #{server.public_ip_address}"
  puts "#{ui.color("Password", :cyan)}: #{server.password}"

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

  print(".") until tcp_test_ssh(server.public_ip_address) { sleep @initial_sleep_delay ||= config[:bootstrap_delay].to_i; true; }

	if File::exists? "#{ENV['HOME']}/.ssh/id_rsa.pub"
   server.public_key_path = "#{ENV['HOME']}/.ssh/id_rsa.pub" 
   server.setup({:password => server.password})
	end

  bootstrap_for_node(server).run if locate_config_value(:distro) || locate_config_value(:template_file)

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

#tcp_test_ssh(hostname) ⇒ Object



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

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
  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