Class: Chef::Knife::BmcsServerCreate
Overview
Server Create Command: Launch an instance and bootstrap it.
Instance Method Summary
collapse
included
Methods included from BmcsHelper
#bmcs_config, #check_can_access_instance, #compartment_id, #compute_client, #confirm, #display_list, #display_server_info, #error_and_exit, #identity_client, #network_client, #show_value, #validate_required_params, #warn_if_page_is_truncated
Instance Method Details
#bootstrap(name) ⇒ Object
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/chef/knife/bmcs_server_create.rb', line 173
def bootstrap(name)
bootstrap = Chef::Knife::Bootstrap.new
bootstrap.name_args = [name]
bootstrap.config[:chef_node_name] = config[:chef_node_name]
bootstrap.config[:ssh_user] = config[:ssh_user]
bootstrap.config[:ssh_password] = config[:ssh_password]
bootstrap.config[:identity_file] = config[:identity_file]
bootstrap.config[:use_sudo] = true
bootstrap.config[:ssh_gateway] = config[:ssh_user] + '@' + name
bootstrap.config[:run_list] = config[:run_list]
bootstrap.config[:yes] = true if config[:yes]
bootstrap.run
end
|
#can_ssh(hostname, ssh_port) ⇒ Object
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
# File 'lib/chef/knife/bmcs_server_create.rb', line 229
def can_ssh(hostname, ssh_port)
socket = TCPSocket.new(hostname, ssh_port)
readable = IO.select([socket], nil, nil, 5)
if readable
content = socket.gets
return true unless content.nil? || content.empty?
else
false
end
rescue SocketError, IOError, Errno::ETIMEDOUT, Errno::EPERM, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::ECONNRESET, Errno::ENOTCONN
false
ensure
socket && socket.close
end
|
#end_progress_indicator ⇒ Object
315
316
317
|
# File 'lib/chef/knife/bmcs_server_create.rb', line 315
def end_progress_indicator
print ui.color("done\n", :magenta)
end
|
#get_file_content(file_name_param) ⇒ Object
319
320
321
322
323
324
325
|
# File 'lib/chef/knife/bmcs_server_create.rb', line 319
def get_file_content(file_name_param)
file_name = config[file_name_param]
return if file_name.nil?
file_name = File.expand_path(file_name)
File.open(file_name, 'r').read
end
|
#get_vnic(instance_id, compartment) ⇒ Object
Return the first VNIC found (which should be the only VNIC).
272
273
274
275
276
277
278
|
# File 'lib/chef/knife/bmcs_server_create.rb', line 272
def get_vnic(instance_id, compartment)
compute_client.list_vnic_attachments(compartment, instance_id: instance_id).each do |response|
response.data.each do |vnic_attachment|
return network_client.get_vnic(vnic_attachment.vnic_id).data
end
end
end
|
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
# File 'lib/chef/knife/bmcs_server_create.rb', line 280
def merge_metadata
metadata = config[:metadata]
if metadata
begin
metadata = JSON.parse(metadata)
rescue JSON::ParserError
error_and_exit('Metadata value must be in JSON format. Example: \'{"key1":"value1", "key2":"value2"}\'')
end
else
metadata = {}
end
ssh_authorized_keys = get_file_content(:ssh_authorized_keys_file)
user_data = get_file_content(:user_data_file)
user_data = Base64.strict_encode64(user_data) if user_data
if ssh_authorized_keys
error_and_exit('Cannot specify ssh-authorized-keys as part of both --ssh-authorized-keys-file and --metadata.') if metadata.key? 'ssh_authorized_keys'
metadata['ssh_authorized_keys'] = ssh_authorized_keys
end
if user_data
error_and_exit('Cannot specify CloudInit user-data as part of both --user-data-file and --metadata.') if metadata.key? 'user_data'
metadata['user_data'] = user_data
end
metadata
end
|
#run ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
|
# File 'lib/chef/knife/bmcs_server_create.rb', line 116
def run
$stdout.sync = true
validate_required_params(%i[availability_domain image_id shape subnet_id identity_file ssh_authorized_keys_file], config)
validate_wait_options
metadata = merge_metadata
error_and_exit 'SSH authorized keys must be specified.' unless metadata['ssh_authorized_keys']
request = OracleBMC::Core::Models::LaunchInstanceDetails.new
request.availability_domain = config[:availability_domain]
request.compartment_id = compartment_id
request.display_name = config[:display_name]
request.hostname_label = config[:hostname_label]
request.image_id = config[:image_id]
request.metadata = metadata
request.shape = config[:shape]
request.subnet_id = config[:subnet_id]
response = compute_client.launch_instance(request)
instance = response.data
ui.msg "Launched instance '#{instance.display_name}' [#{instance.id}]"
show_value('Display Name', instance.display_name)
show_value('Instance ID', instance.id)
show_value('Availability Domain', instance.availability_domain)
show_value('Compartment ID', instance.compartment_id)
show_value('Region', instance.region)
show_value('Image ID', instance.image_id)
show_value('Shape', instance.shape)
instance = wait_for_instance_running(instance.id)
ui.msg "Instance '#{instance.display_name}' is now running."
vnic = get_vnic(instance.id, instance.compartment_id)
show_value('Public IP Address', vnic.public_ip)
show_value('Private IP Address', vnic.private_ip)
unless wait_for_ssh(vnic.public_ip, SSH_PORT, WAIT_FOR_SSH_INTERVAL_SECONDS, config[:wait_for_ssh_max])
error_and_exit 'Timed out while waiting for SSH access.'
end
wait_to_stabilize
config[:chef_node_name] = instance.display_name unless config[:chef_node_name]
ui.msg "Bootstrapping with node name '#{config[:chef_node_name]}'."
bootstrap(vnic.public_ip)
ui.msg "Created and bootstrapped node '#{config[:chef_node_name]}'."
ui.msg "\n"
display_server_info(config, instance, [vnic])
end
|
#show_progress ⇒ Object
310
311
312
313
|
# File 'lib/chef/knife/bmcs_server_create.rb', line 310
def show_progress
print ui.color('.', :magenta)
$stdout.flush
end
|
#validate_wait_option(p, default) ⇒ Object
190
191
192
193
194
195
196
|
# File 'lib/chef/knife/bmcs_server_create.rb', line 190
def validate_wait_option(p, default)
arg_name = "--#{p.to_s.tr('_', '-')}"
config[p] = config[p].to_s.empty? ? default : Integer(config[p])
error_and_exit "#{arg_name} must be 0 or greater" if config[p] < 0
rescue
error_and_exit "#{arg_name} must be numeric"
end
|
#validate_wait_options ⇒ Object
#wait_for_instance_running(instance_id) ⇒ Object
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'lib/chef/knife/bmcs_server_create.rb', line 246
def wait_for_instance_running(instance_id)
print ui.color('Waiting for instance to reach running state...', :magenta)
begin
response = compute_client.get_instance(instance_id).wait_until(:lifecycle_state,
OracleBMC::Core::Models::Instance::LIFECYCLE_STATE_RUNNING,
max_interval_seconds: 3) do |poll_response|
if poll_response.data.lifecycle_state == OracleBMC::Core::Models::Instance::LIFECYCLE_STATE_TERMINATED ||
poll_response.data.lifecycle_state == OracleBMC::Core::Models::Instance::LIFECYCLE_STATE_TERMINATING
throw :stop_succeed
end
show_progress
end
ensure
end_progress_indicator
end
if response.data.lifecycle_state != OracleBMC::Core::Models::Instance::LIFECYCLE_STATE_RUNNING
error_and_exit 'Instance failed to provision.'
end
response.data
end
|
#wait_for_ssh(hostname, ssh_port, interval_seconds, max_time_seconds) ⇒ Object
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
# File 'lib/chef/knife/bmcs_server_create.rb', line 210
def wait_for_ssh(hostname, ssh_port, interval_seconds, max_time_seconds)
print ui.color('Waiting for ssh access...', :magenta)
end_time = Time.now + max_time_seconds
begin
while Time.now < end_time
return true if can_ssh(hostname, ssh_port)
show_progress
sleep interval_seconds
end
ensure
end_progress_indicator
end
false
end
|
#wait_to_stabilize ⇒ Object
203
204
205
206
207
208
|
# File 'lib/chef/knife/bmcs_server_create.rb', line 203
def wait_to_stabilize
Kernel.sleep(config[:wait_to_stabilize])
end
|