Class: Kitchen::Driver::Ec2
- Inherits:
-
Base
- Object
- Base
- Kitchen::Driver::Ec2
- Defined in:
- lib/kitchen/driver/ec2.rb
Overview
Amazon EC2 driver for Test Kitchen.
Constant Summary collapse
- INTERFACE_TYPES =
Ordered mapping from config name to Fog name. Ordered by preference when looking up hostname.
{ "dns" => "public_dns_name", "public" => "public_ip_address", "private" => "private_ip_address", "private_dns" => "private_dns_name" }
Class Method Summary collapse
- .validation_error(driver, old_key, new_key) ⇒ Object
- .validation_warn(driver, old_key, new_key) ⇒ Object
Instance Method Summary collapse
-
#actual_platform ⇒ Object
The actual platform is the platform detected from the image.
-
#create(state) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
-
#create_ec2_json(state) ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/LineLength.
- #create_spot_request ⇒ Object
- #default_ami ⇒ Object
- #default_instance_type ⇒ Object
- #default_windows_user_data ⇒ Object
- #desired_platform ⇒ Object
- #destroy(state) ⇒ Object
- #ec2 ⇒ Object
-
#fetch_windows_admin_password(server, state) ⇒ Object
rubocop:disable Lint/UnusedBlockArgument.
-
#hostname(server, interface_type = nil) ⇒ Object
Lookup hostname of provided server.
- #image ⇒ Object
- #image_info(image) ⇒ Object
-
#initialize(*args, &block) ⇒ Ec2
constructor
A new instance of Ec2.
- #instance_generator ⇒ Object
-
#show_chosen_image ⇒ Object
rubocop:enable Metrics/MethodLength, Metrics/LineLength.
-
#submit_server ⇒ Object
Fog AWS helper for creating the instance.
-
#submit_spot(state) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#sudo_command ⇒ Object
Returns the sudo command to use or empty string if sudo is not configured.
- #tag_server(server) ⇒ Object
- #update_username(state) ⇒ Object
-
#wait_until_ready(server, state) ⇒ Object
Normally we could use
server.wait_until_runningbut we actually need to check more than just the instance state. -
#wait_with_destroy(server, state, status_msg, &block) ⇒ Object
Poll a block, waiting for it to return true.
Constructor Details
#initialize(*args, &block) ⇒ Ec2
Returns a new instance of Ec2.
86 87 88 89 90 91 |
# File 'lib/kitchen/driver/ec2.rb', line 86 def initialize(*args, &block) super # AWS Ruby SDK loading isn't thread safe, so as soon as we know we're # going to use EC2, autoload it. Seems to have been fixed in Ruby 2.3+ ::Aws.eager_autoload! unless RUBY_VERSION.to_f >= 2.3 end |
Class Method Details
.validation_error(driver, old_key, new_key) ⇒ Object
98 99 100 101 |
# File 'lib/kitchen/driver/ec2.rb', line 98 def self.validation_error(driver, old_key, new_key) raise "ERROR: The driver[#{driver.class.name}] config key `#{old_key}` " \ "has been removed, please use `#{new_key}`" end |
.validation_warn(driver, old_key, new_key) ⇒ Object
93 94 95 96 |
# File 'lib/kitchen/driver/ec2.rb', line 93 def self.validation_warn(driver, old_key, new_key) driver.warn "WARN: The driver[#{driver.class.name}] config key `#{old_key}` " \ "is deprecated, please use `#{new_key}`" end |
Instance Method Details
#actual_platform ⇒ Object
The actual platform is the platform detected from the image
274 275 276 |
# File 'lib/kitchen/driver/ec2.rb', line 274 def actual_platform @actual_platform ||= Aws::StandardPlatform.from_image(self, image) if image end |
#create(state) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
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 |
# File 'lib/kitchen/driver/ec2.rb', line 168 def create(state) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength return if state[:server_id] update_username(state) info(Kitchen::Util.outdent!(" If you are not using an account that qualifies under the AWS\n free-tier, you may be charged to run these suites. The charge\n should be minimal, but neither Test Kitchen nor its maintainers\n are responsible for your incurred costs.\n END\n\n if config[:spot_price]\n # Spot instance when a price is set\n server = submit_spot(state)\n else\n # On-demand instance\n server = submit_server\n end\n info(\"Instance <\#{server.id}> requested.\")\n server.wait_until_exists do |w|\n w.before_attempt do |attempts|\n info(\"Polling AWS for existence, attempt \#{attempts}...\")\n end\n end\n\n # See https://github.com/aws/aws-sdk-ruby/issues/859\n # Tagging can fail with a NotFound error even though we waited until the server exists\n # Waiting can also fail, so we have to also retry on that. If it means we re-tag the\n # instance, so be it.\n Retryable.retryable(\n :tries => 10,\n :sleep => lambda { |n| [2**n, 30].min },\n :on => ::Aws::EC2::Errors::InvalidInstanceIDNotFound\n ) do |r, _|\n info(\"Attempting to tag the instance, \#{r} retries\")\n tag_server(server)\n\n state[:server_id] = server.id\n info(\"EC2 instance <\#{state[:server_id]}> created.\")\n wait_until_ready(server, state)\n end\n\n if windows_os? &&\n instance.transport[:username] =~ /administrator/i &&\n instance.transport[:password].nil?\n # If we're logging into the administrator user and a password isn't\n # supplied, try to fetch it from the AWS instance\n fetch_windows_admin_password(server, state)\n end\n\n info(\"EC2 instance <\#{state[:server_id]}> ready.\")\n instance.transport.connection(state).wait_until_ready\n create_ec2_json(state)\n debug(\"ec2:create '\#{state[:hostname]}'\")\nend\n")) |
#create_ec2_json(state) ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/LineLength
485 486 487 488 489 490 491 492 493 |
# File 'lib/kitchen/driver/ec2.rb', line 485 def create_ec2_json(state) if windows_os? cmd = "New-Item -Force C:\\chef\\ohai\\hints\\ec2.json -ItemType File" else debug "Using sudo_command='#{sudo_command}' for ohai hints" cmd = "#{sudo_command} mkdir -p /etc/chef/ohai/hints; #{sudo_command} touch /etc/chef/ohai/hints/ec2.json" end instance.transport.connection(state).execute(cmd) end |
#create_spot_request ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/kitchen/driver/ec2.rb', line 358 def create_spot_request request_data = { :spot_price => config[:spot_price].to_s, :launch_specification => instance_generator.ec2_instance_data } if config[:block_duration_minutes] request_data[:block_duration_minutes] = config[:block_duration_minutes] end response = ec2.client.request_spot_instances(request_data) response[:spot_instance_requests][0][:spot_instance_request_id] end |
#default_ami ⇒ Object
289 290 291 292 293 294 295 296 |
# File 'lib/kitchen/driver/ec2.rb', line 289 def default_ami @default_ami ||= begin search_platform = desired_platform || Aws::StandardPlatform.from_platform_string(self, "ubuntu") image_search = config[:image_search] || search_platform.image_search search_platform.find_image(image_search) end end |
#default_instance_type ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/kitchen/driver/ec2.rb', line 259 def default_instance_type @instance_type ||= begin # We default to the free tier (t2.micro for hvm, t1.micro for paravirtual) if image && image.virtualization_type == "hvm" info("instance_type not specified. Using free tier t2.micro instance ...") "t2.micro" else info("instance_type not specified. Using free tier t1.micro instance since" \ " image is paravirtual (pick an hvm image to use the superior t2.micro!) ...") "t1.micro" end end end |
#default_windows_user_data ⇒ Object
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/kitchen/driver/ec2.rb', line 495 def default_windows_user_data # Preparing custom static admin user if we defined something other than Administrator custom_admin_script = "" if !(instance.transport[:username] =~ /administrator/i) && instance.transport[:password] custom_admin_script = Kitchen::Util.outdent!(" \"Disabling Complex Passwords\" >> $logfile\n $seccfg = [IO.Path]::GetTempFileName()\n & secedit.exe /export /cfg $seccfg >> $logfile\n (Get-Content $seccfg) | Foreach-Object {$_ -replace \"PasswordComplexity\\\\s*=\\\\s*1\", \"PasswordComplexity = 0\"} | Set-Content $seccfg\n & secedit.exe /configure /db $env:windir\\\\security\\\\new.sdb /cfg $seccfg /areas SECURITYPOLICY >> $logfile\n & cp $seccfg \"c:\\\\\"\n & del $seccfg\n $username=\"\#{instance.transport[:username]}\"\n $password=\"\#{instance.transport[:password]}\"\n \"Creating static user: $username\" >> $logfile\n & net.exe user /y /add $username $password >> $logfile\n \"Adding $username to Administrators\" >> $logfile\n & net.exe localgroup Administrators /add $username >> $logfile\n EOH\n end\n\n # Returning the fully constructed PowerShell script to user_data\n Kitchen::Util.outdent!(<<-EOH)\n <powershell>\n $logfile=\"C:\\\\Program Files\\\\Amazon\\\\Ec2ConfigService\\\\Logs\\\\kitchen-ec2.log\"\n # Allow script execution\n Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force\n #PS Remoting and & winrm.cmd basic config\n Enable-PSRemoting -Force -SkipNetworkProfileCheck\n & winrm.cmd set winrm/config '@{MaxTimeoutms=\"1800000\"}' >> $logfile\n & winrm.cmd set winrm/config/winrs '@{MaxMemoryPerShellMB=\"1024\"}' >> $logfile\n & winrm.cmd set winrm/config/winrs '@{MaxShellsPerUser=\"50\"}' >> $logfile\n #Server settings - support username/password login\n & winrm.cmd set winrm/config/service/auth '@{Basic=\"true\"}' >> $logfile\n & winrm.cmd set winrm/config/service '@{AllowUnencrypted=\"true\"}' >> $logfile\n & winrm.cmd set winrm/config/winrs '@{MaxMemoryPerShellMB=\"1024\"}' >> $logfile\n #Firewall Config\n & netsh advfirewall firewall set rule name=\"Windows Remote Management (HTTP-In)\" profile=public protocol=tcp localport=5985 remoteip=localsubnet new remoteip=any >> $logfile\n \#{custom_admin_script}\n </powershell>\n EOH\nend\n") |
#desired_platform ⇒ Object
278 279 280 281 282 283 284 285 286 287 |
# File 'lib/kitchen/driver/ec2.rb', line 278 def desired_platform @desired_platform ||= begin platform = Aws::StandardPlatform.from_platform_string(self, instance.platform.name) if platform debug("platform name #{instance.platform.name} appears to be a standard platform." \ " Searching for #{platform} ...") end platform end end |
#destroy(state) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/kitchen/driver/ec2.rb', line 224 def destroy(state) return if state[:server_id].nil? server = ec2.get_instance(state[:server_id]) unless server.nil? instance.transport.connection(state).close server.terminate end if state[:spot_request_id] debug("Deleting spot request <#{state[:server_id]}>") ec2.client.cancel_spot_instance_requests( :spot_instance_request_ids => [state[:spot_request_id]] ) state.delete(:spot_request_id) end info("EC2 instance <#{state[:server_id]}> destroyed.") state.delete(:server_id) state.delete(:hostname) end |
#ec2 ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/kitchen/driver/ec2.rb', line 308 def ec2 @ec2 ||= Aws::Client.new( config[:region], config[:shared_credentials_profile], config[:aws_access_key_id], config[:aws_secret_access_key], config[:aws_session_token], config[:http_proxy], config[:retry_limit] ) end |
#fetch_windows_admin_password(server, state) ⇒ Object
rubocop:disable Lint/UnusedBlockArgument
429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/kitchen/driver/ec2.rb', line 429 def fetch_windows_admin_password(server, state) wait_with_destroy(server, state, "to fetch windows admin password") do |aws_instance| enc = server.client.get_password_data( :instance_id => state[:server_id] ).password_data # Password data is blank until password is available !enc.nil? && !enc.empty? end pass = server.decrypt_windows_password(File.(instance.transport[:ssh_key])) state[:password] = pass info("Retrieved Windows password for instance <#{state[:server_id]}>.") end |
#hostname(server, interface_type = nil) ⇒ Object
Lookup hostname of provided server. If interface_type is provided use that interface to lookup hostname. Otherwise, try ordered list of options.
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'lib/kitchen/driver/ec2.rb', line 460 def hostname(server, interface_type = nil) if interface_type interface_type = INTERFACE_TYPES.fetch(interface_type) do raise Kitchen::UserError, "Invalid interface [#{interface_type}]" end server.send(interface_type) else potential_hostname = nil INTERFACE_TYPES.values.each do |type| potential_hostname ||= server.send(type) # AWS returns an empty string if the dns name isn't populated yet potential_hostname = nil if potential_hostname == "" end potential_hostname end end |
#image ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/kitchen/driver/ec2.rb', line 244 def image return @image if defined?(@image) if config[:image_id] @image = ec2.resource.image(config[:image_id]) show_chosen_image else raise "Neither image_id nor an image_search specified for instance #{instance.name}!" \ " Please specify one or the other." end @image end |
#image_info(image) ⇒ Object
551 552 553 554 555 556 557 558 559 560 |
# File 'lib/kitchen/driver/ec2.rb', line 551 def image_info(image) root_device = image.block_device_mappings. find { |b| b.device_name == image.root_device_name } volume_type = " #{root_device.ebs.volume_type}" if root_device && root_device.ebs " Architecture: #{image.architecture}," \ " Virtualization: #{image.virtualization_type}," \ " Storage: #{image.root_device_type}#{volume_type}," \ " Created: #{image.creation_date}" end |
#instance_generator ⇒ Object
320 321 322 |
# File 'lib/kitchen/driver/ec2.rb', line 320 def instance_generator @instance_generator ||= Aws::InstanceGenerator.new(config, ec2, instance.logger) end |
#show_chosen_image ⇒ Object
rubocop:enable Metrics/MethodLength, Metrics/LineLength
539 540 541 542 543 544 545 546 547 548 549 |
# File 'lib/kitchen/driver/ec2.rb', line 539 def show_chosen_image # Print some debug stuff debug("Image for #{instance.name}: #{image.name}. #{image_info(image)}") if actual_platform info("Detected platform: #{actual_platform.name} version #{actual_platform.version}" \ " on #{actual_platform.architecture}. Instance Type: #{config[:instance_type]}." \ " Default username: #{actual_platform.username} (default).") else debug("No platform detected for #{image.name}.") end end |
#submit_server ⇒ Object
Fog AWS helper for creating the instance
325 326 327 328 329 330 331 332 333 334 |
# File 'lib/kitchen/driver/ec2.rb', line 325 def submit_server instance_data = instance_generator.ec2_instance_data debug("Creating EC2 instance in region #{config[:region]} with properties:") instance_data.each do |key, value| debug("- #{key} = #{value.inspect}") end instance_data[:min_count] = 1 instance_data[:max_count] = 1 ec2.create_instance(instance_data) end |
#submit_spot(state) ⇒ Object
rubocop:disable Metrics/AbcSize
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/kitchen/driver/ec2.rb', line 336 def submit_spot(state) # rubocop:disable Metrics/AbcSize debug("Creating EC2 Spot Instance..") spot_request_id = create_spot_request # deleting the instance cancels the request, but deleting the request # does not affect the instance state[:spot_request_id] = spot_request_id ec2.client.wait_until( :spot_instance_request_fulfilled, :spot_instance_request_ids => [spot_request_id] ) do |w| w.max_attempts = config[:retryable_tries] w.delay = config[:retryable_sleep] w.before_attempt do |attempts| c = attempts * config[:retryable_sleep] t = config[:retryable_tries] * config[:retryable_sleep] info "Waited #{c}/#{t}s for spot request <#{spot_request_id}> to become fulfilled." end end ec2.get_instance_from_spot_request(spot_request_id) end |
#sudo_command ⇒ Object
Returns the sudo command to use or empty string if sudo is not configured
480 481 482 |
# File 'lib/kitchen/driver/ec2.rb', line 480 def sudo_command instance.provisioner[:sudo] ? instance.provisioner[:sudo_command].to_s : "" end |
#tag_server(server) ⇒ Object
371 372 373 374 375 376 377 378 379 |
# File 'lib/kitchen/driver/ec2.rb', line 371 def tag_server(server) if config[:tags] = [] config[:tags].each do |k, v| << { :key => k, :value => v } end server.(:tags => ) end end |
#update_username(state) ⇒ Object
298 299 300 301 302 303 304 305 306 |
# File 'lib/kitchen/driver/ec2.rb', line 298 def update_username(state) # TODO: if the user explicitly specified the transport's default username, # do NOT overwrite it! if instance.transport[:username] == instance.transport.class.defaults[:username] debug("No SSH username specified: using default username #{actual_platform.username} " \ " for image #{config[:image_id]}, which we detected as #{actual_platform}.") state[:username] = actual_platform.username end end |
#wait_until_ready(server, state) ⇒ Object
Normally we could use server.wait_until_running but we actually need
to check more than just the instance state
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/kitchen/driver/ec2.rb', line 383 def wait_until_ready(server, state) wait_with_destroy(server, state, "to become ready") do |aws_instance| hostname = hostname(aws_instance, config[:interface]) # We aggressively store the hostname so if the process fails here # we still have it, even if it will change later state[:hostname] = hostname # Euca instances often report ready before they have an IP ready = aws_instance.exists? && aws_instance.state.name == "running" && hostname != "0.0.0.0" if ready && windows_os? output = server.console_output.output unless output.nil? output = Base64.decode64(output) debug "Console output: --- \n#{output}" end ready = !!(output =~ /Windows is Ready to use/) end ready end end |
#wait_with_destroy(server, state, status_msg, &block) ⇒ Object
Poll a block, waiting for it to return true. If it does not succeed within the configured time we destroy the instance to save people money
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/kitchen/driver/ec2.rb', line 407 def wait_with_destroy(server, state, status_msg, &block) wait_log = proc do |attempts| c = attempts * config[:retryable_sleep] t = config[:retryable_tries] * config[:retryable_sleep] info "Waited #{c}/#{t}s for instance <#{state[:server_id]}> #{status_msg}." end begin server.wait_until( :max_attempts => config[:retryable_tries], :delay => config[:retryable_sleep], :before_attempt => wait_log, &block ) rescue ::Aws::Waiters::Errors::WaiterFailed error("Ran out of time waiting for the server with id [#{state[:server_id]}]" \ " #{status_msg}, attempting to destroy it") destroy(state) raise end end |