Class | BoxGrinder::EBSPlugin |
In: |
lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb
lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb |
Parent: | BasePlugin |
ROOT_DEVICE_NAME | = | '/dev/sda1' |
POLL_FREQ | = | 1 |
TIMEOUT | = | 1000 |
EC2_HOSTNAME_LOOKUP_TIMEOUT | = | 10 |
ROOT_DEVICE_NAME | = | '/dev/sda1' |
POLL_FREQ | = | 1 |
TIMEOUT | = | 1000 |
EC2_HOSTNAME_LOOKUP_TIMEOUT | = | 10 |
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 235 235: def adjust_fstab(guestfs) 236: guestfs.sh("cat /etc/fstab | grep -v '/mnt' | grep -v '/data' | grep -v 'swap' > /etc/fstab.new") 237: guestfs.mv("/etc/fstab.new", "/etc/fstab") 238: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 235 235: def adjust_fstab(guestfs) 236: guestfs.sh("cat /etc/fstab | grep -v '/mnt' | grep -v '/data' | grep -v 'swap' > /etc/fstab.new") 237: guestfs.mv("/etc/fstab.new", "/etc/fstab") 238: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 72 72: def after_init 73: register_supported_os('fedora', ['13', '14', '15', '16']) 74: register_supported_os('rhel', ['6']) 75: register_supported_os('centos', ['5', '6']) 76: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 72 72: def after_init 73: register_supported_os('fedora', ['13', '14', '15', '16']) 74: register_supported_os('rhel', ['6']) 75: register_supported_os('centos', ['5', '6']) 76: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 183 183: def ami_by_name(name) 184: @ec2helper.ami_by_name(name, @plugin_config['account_number']) 185: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 183 183: def ami_by_name(name) 184: @ec2helper.ami_by_name(name, @plugin_config['account_number']) 185: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 244 244: def device_for_suffix(suffix) 245: return "/dev/sd#{suffix}" if File.exists?("/dev/sd#{suffix}") 246: return "/dev/xvd#{suffix}" if File.exists?("/dev/xvd#{suffix}") 247: nil 248: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 244 244: def device_for_suffix(suffix) 245: return "/dev/sd#{suffix}" if File.exists?("/dev/sd#{suffix}") 246: return "/dev/xvd#{suffix}" if File.exists?("/dev/xvd#{suffix}") 247: nil 248: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 219 219: def ebs_appliance_name 220: base_path = "#{@appliance_config.name}/#{@appliance_config.os.name}/#{@appliance_config.os.version}/#{@appliance_config.version}.#{@appliance_config.release}" 221: 222: return "#{base_path}/#{@appliance_config.hardware.arch}" unless @plugin_config['snapshot'] 223: 224: snapshot = 1 225: 226: while ami_by_name("#{base_path}-SNAPSHOT-#{snapshot}/#{@appliance_config.hardware.arch}") 227: snapshot += 1 228: end 229: # Reuse the last key (if there was one) 230: snapshot -=1 if snapshot > 1 and @plugin_config['overwrite'] 231: 232: "#{base_path}-SNAPSHOT-#{snapshot}/#{@appliance_config.hardware.arch}" 233: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 219 219: def ebs_appliance_name 220: base_path = "#{@appliance_config.name}/#{@appliance_config.os.name}/#{@appliance_config.os.version}/#{@appliance_config.version}.#{@appliance_config.release}" 221: 222: return "#{base_path}/#{@appliance_config.hardware.arch}" unless @plugin_config['snapshot'] 223: 224: snapshot = 1 225: 226: while ami_by_name("#{base_path}-SNAPSHOT-#{snapshot}/#{@appliance_config.hardware.arch}") 227: snapshot += 1 228: end 229: # Reuse the last key (if there was one) 230: snapshot -=1 if snapshot > 1 and @plugin_config['overwrite'] 231: 232: "#{base_path}-SNAPSHOT-#{snapshot}/#{@appliance_config.hardware.arch}" 233: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 78 78: def execute 79: ebs_appliance_description = "#{@appliance_config.summary} | Appliance version #{@appliance_config.version}.#{@appliance_config.release} | #{@appliance_config.hardware.arch} architecture" 80: 81: @log.debug "Checking if appliance is already registered..." 82: ami = ami_by_name(ebs_appliance_name) 83: 84: if ami and @plugin_config['overwrite'] 85: @log.info "Overwrite is enabled. Stomping existing assets." 86: stomp_ebs(ami) 87: elsif ami 88: @log.warn "EBS AMI '#{ami.name}' is already registered as '#{ami.id}' (region: #{@current_region})." 89: return 90: end 91: 92: @log.info "Creating new EBS volume..." 93: size = 0 94: @appliance_config.hardware.partitions.each_value { |partition| size += partition['size'] } 95: 96: # create_volume, ceiling to avoid non-Integer values as per https://issues.jboss.org/browse/BGBUILD-224 97: volume = @ec2.volumes.create(:size => size.ceil.to_i, :availability_zone => @plugin_config['availability_zone']) 98: 99: @log.debug "Volume #{volume.id} created." 100: @log.debug "Waiting for EBS volume #{volume.id} to be available..." 101: 102: # wait for volume to be created 103: @ec2helper.wait_for_volume_status(:available, volume) 104: 105: # get first free device to mount the volume 106: suffix = free_device_suffix 107: device_name = "/dev/sd#{suffix}" 108: @log.trace "Got free device suffix: '#{suffix}'" 109: 110: @log.trace "Reading current instance id..." 111: # get_current_instance 112: current_instance = @ec2.instances[@current_instance_id] 113: 114: @log.trace "Got: #{current_instance.id}" 115: @log.info "Attaching created volume..." 116: # attach the volume to current host 117: volume.attach_to(current_instance, device_name) 118: 119: @log.debug "Waiting for EBS volume to be attached..." 120: # wait for volume to be attached 121: @ec2helper.wait_for_volume_status(:in_use, volume) 122: 123: @log.debug "Waiting for the attached EBS volume to be discovered by the OS" 124: wait_for_volume_attachment(suffix) 125: 126: @log.info "Copying data to EBS volume..." 127: 128: @image_helper.customize([@previous_deliverables.disk, device_for_suffix(suffix)], :automount => false) do |guestfs, guestfs_helper| 129: @image_helper.sync_filesystem(guestfs, guestfs_helper) 130: 131: @log.debug "Adjusting /etc/fstab..." 132: adjust_fstab(guestfs) 133: end 134: 135: @log.debug "Detaching EBS volume..." 136: volume.attachments.map(&:delete) 137: 138: @log.debug "Waiting for EBS volume to become available..." 139: @ec2helper.wait_for_volume_status(:available, volume) 140: 141: @log.info "Creating snapshot from EBS volume..." 142: snapshot = @ec2.snapshots.create( 143: :volume => volume, 144: :description => ebs_appliance_description) 145: 146: @log.debug "Waiting for snapshot #{snapshot.id} to be completed..." 147: @ec2helper.wait_for_snapshot_status(:completed, snapshot) 148: 149: @log.debug "Deleting temporary EBS volume..." 150: volume.delete 151: 152: @log.info "Registering image..." 153: 154: optmap = { 155: :name => ebs_appliance_name, 156: :root_device_name => ROOT_DEVICE_NAME, 157: :block_device_mappings => { ROOT_DEVICE_NAME => { 158: :snapshot => snapshot, 159: :delete_on_termination => @plugin_config['delete_on_termination'] 160: }, 161: '/dev/sdb' => 'ephemeral0', 162: '/dev/sdc' => 'ephemeral1', 163: '/dev/sdd' => 'ephemeral2', 164: '/dev/sde' => 'ephemeral3'}, 165: :architecture => @appliance_config.hardware.base_arch, 166: :kernel_id => @plugin_config['kernel'] || @ec2_endpoints[@current_region][:kernel][@appliance_config.hardware.base_arch.intern][:aki], 167: :description => ebs_appliance_description 168: } 169: 170: optmap.merge!(:ramdisk_id => @plugin_config['ramdisk']) if @plugin_config['ramdisk'] 171: 172: 173: image = @ec2.images.create(optmap) 174: 175: @log.info "Waiting for the new EBS AMI to become available" 176: @ec2helper.wait_for_image_state(:available, image) 177: @log.info "EBS AMI '#{image.name}' registered: #{image.id} (region: #{@current_region})" 178: rescue Timeout::Error 179: @log.error "An operation timed out. Manual intervention may be necessary to complete the task." 180: raise 181: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 78 78: def execute 79: ebs_appliance_description = "#{@appliance_config.summary} | Appliance version #{@appliance_config.version}.#{@appliance_config.release} | #{@appliance_config.hardware.arch} architecture" 80: 81: @log.debug "Checking if appliance is already registered..." 82: ami = ami_by_name(ebs_appliance_name) 83: 84: if ami and @plugin_config['overwrite'] 85: @log.info "Overwrite is enabled. Stomping existing assets." 86: stomp_ebs(ami) 87: elsif ami 88: @log.warn "EBS AMI '#{ami.name}' is already registered as '#{ami.id}' (region: #{@current_region})." 89: return 90: end 91: 92: @log.info "Creating new EBS volume..." 93: size = 0 94: @appliance_config.hardware.partitions.each_value { |partition| size += partition['size'] } 95: 96: # create_volume, ceiling to avoid non-Integer values as per https://issues.jboss.org/browse/BGBUILD-224 97: volume = @ec2.volumes.create(:size => size.ceil.to_i, :availability_zone => @plugin_config['availability_zone']) 98: 99: @log.debug "Volume #{volume.id} created." 100: @log.debug "Waiting for EBS volume #{volume.id} to be available..." 101: 102: # wait for volume to be created 103: @ec2helper.wait_for_volume_status(:available, volume) 104: 105: # get first free device to mount the volume 106: suffix = free_device_suffix 107: device_name = "/dev/sd#{suffix}" 108: @log.trace "Got free device suffix: '#{suffix}'" 109: 110: @log.trace "Reading current instance id..." 111: # get_current_instance 112: current_instance = @ec2.instances[@current_instance_id] 113: 114: @log.trace "Got: #{current_instance.id}" 115: @log.info "Attaching created volume..." 116: # attach the volume to current host 117: volume.attach_to(current_instance, device_name) 118: 119: @log.debug "Waiting for EBS volume to be attached..." 120: # wait for volume to be attached 121: @ec2helper.wait_for_volume_status(:in_use, volume) 122: 123: @log.debug "Waiting for the attached EBS volume to be discovered by the OS" 124: wait_for_volume_attachment(suffix) 125: 126: @log.info "Copying data to EBS volume..." 127: 128: @image_helper.customize([@previous_deliverables.disk, device_for_suffix(suffix)], :automount => false) do |guestfs, guestfs_helper| 129: @image_helper.sync_filesystem(guestfs, guestfs_helper) 130: 131: @log.debug "Adjusting /etc/fstab..." 132: adjust_fstab(guestfs) 133: end 134: 135: @log.debug "Detaching EBS volume..." 136: volume.attachments.map(&:delete) 137: 138: @log.debug "Waiting for EBS volume to become available..." 139: @ec2helper.wait_for_volume_status(:available, volume) 140: 141: @log.info "Creating snapshot from EBS volume..." 142: snapshot = @ec2.snapshots.create( 143: :volume => volume, 144: :description => ebs_appliance_description) 145: 146: @log.debug "Waiting for snapshot #{snapshot.id} to be completed..." 147: @ec2helper.wait_for_snapshot_status(:completed, snapshot) 148: 149: @log.debug "Deleting temporary EBS volume..." 150: volume.delete 151: 152: @log.info "Registering image..." 153: 154: optmap = { 155: :name => ebs_appliance_name, 156: :root_device_name => ROOT_DEVICE_NAME, 157: :block_device_mappings => { ROOT_DEVICE_NAME => { 158: :snapshot => snapshot, 159: :delete_on_termination => @plugin_config['delete_on_termination'] 160: }, 161: '/dev/sdb' => 'ephemeral0', 162: '/dev/sdc' => 'ephemeral1', 163: '/dev/sdd' => 'ephemeral2', 164: '/dev/sde' => 'ephemeral3'}, 165: :architecture => @appliance_config.hardware.base_arch, 166: :kernel_id => @plugin_config['kernel'] || @ec2_endpoints[@current_region][:kernel][@appliance_config.hardware.base_arch.intern][:aki], 167: :description => ebs_appliance_description 168: } 169: 170: optmap.merge!(:ramdisk_id => @plugin_config['ramdisk']) if @plugin_config['ramdisk'] 171: 172: 173: image = @ec2.images.create(optmap) 174: 175: @log.info "Waiting for the new EBS AMI to become available" 176: @ec2helper.wait_for_image_state(:available, image) 177: @log.info "EBS AMI '#{image.name}' registered: #{image.id} (region: #{@current_region})" 178: rescue Timeout::Error 179: @log.error "An operation timed out. Manual intervention may be necessary to complete the task." 180: raise 181: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 250 250: def free_device_suffix 251: ("f".."p").each do |suffix| 252: return suffix unless File.exists?("/dev/sd#{suffix}") or File.exists?("/dev/xvd#{suffix}") 253: end 254: raise "Found too many attached devices. Cannot attach EBS volume." 255: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 250 250: def free_device_suffix 251: ("f".."p").each do |suffix| 252: return suffix unless File.exists?("/dev/sd#{suffix}") or File.exists?("/dev/xvd#{suffix}") 253: end 254: raise "Found too many attached devices. Cannot attach EBS volume." 255: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 194 194: def stomp_ebs(ami) 195: #Find any instances that are running, if they are not stopped then abort. 196: if live = @ec2helper.live_instances(ami) 197: if @plugin_config['terminate_instances'] 198: @log.info "Terminating the following instances: #{live.collect{|i| "#{i.id} (#{i.status})"}.join(", ")}." 199: terminate_instances(live) 200: else 201: raise "There are still instances of #{ami.id} running, you should terminate them after " << 202: "preserving any important data: #{live.collect{|i| "#{i.id} (#{i.status})"}.join(", ")}." 203: end 204: end 205: 206: @log.info("Finding the primary snapshot associated with #{ami.id}.") 207: primary_snapshot = @ec2helper.snapshot_by_id(ami.block_device_mappings[ami.root_device_name].snapshot_id) 208: 209: @log.info("De-registering the EBS AMI.") 210: ami.deregister 211: @ec2helper.wait_for_image_death(ami) 212: 213: if !@plugin_config['preserve_snapshots'] and primary_snapshot 214: @log.info("Deleting the primary snapshot.") 215: primary_snapshot.delete 216: end 217: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 194 194: def stomp_ebs(ami) 195: #Find any instances that are running, if they are not stopped then abort. 196: if live = @ec2helper.live_instances(ami) 197: if @plugin_config['terminate_instances'] 198: @log.info "Terminating the following instances: #{live.collect{|i| "#{i.id} (#{i.status})"}.join(", ")}." 199: terminate_instances(live) 200: else 201: raise "There are still instances of #{ami.id} running, you should terminate them after " << 202: "preserving any important data: #{live.collect{|i| "#{i.id} (#{i.status})"}.join(", ")}." 203: end 204: end 205: 206: @log.info("Finding the primary snapshot associated with #{ami.id}.") 207: primary_snapshot = @ec2helper.snapshot_by_id(ami.block_device_mappings[ami.root_device_name].snapshot_id) 208: 209: @log.info("De-registering the EBS AMI.") 210: ami.deregister 211: @ec2helper.wait_for_image_death(ami) 212: 213: if !@plugin_config['preserve_snapshots'] and primary_snapshot 214: @log.info("Deleting the primary snapshot.") 215: primary_snapshot.delete 216: end 217: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 187 187: def terminate_instances(instances) 188: instances.map(&:terminate) 189: instances.each do |i| 190: @ec2helper.wait_for_instance_death(i) 191: end 192: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 187 187: def terminate_instances(instances) 188: instances.map(&:terminate) 189: instances.each do |i| 190: @ec2helper.wait_for_instance_death(i) 191: end 192: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 257 257: def valid_platform? 258: begin 259: region = EC2Helper::availability_zone_to_region(EC2Helper::current_availability_zone) 260: return true if @ec2_endpoints.has_key? region 261: @log.warn "You may be using an ec2 region that BoxGrinder Build is not aware of: #{region}, BoxGrinder Build knows of: #{@ec2_endpoints.join(", ")}" 262: rescue Net::HTTPServerException => e 263: @log.warn "An error was returned when attempting to retrieve the ec2 hostname: #{e.to_s}" 264: rescue Timeout::Error => t 265: @log.warn "A timeout occurred while attempting to retrieve the ec2 hostname: #{t.to_s}" 266: end 267: false 268: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 257 257: def valid_platform? 258: begin 259: region = EC2Helper::availability_zone_to_region(EC2Helper::current_availability_zone) 260: return true if @ec2_endpoints.has_key? region 261: @log.warn "You may be using an ec2 region that BoxGrinder Build is not aware of: #{region}, BoxGrinder Build knows of: #{@ec2_endpoints.join(", ")}" 262: rescue Net::HTTPServerException => e 263: @log.warn "An error was returned when attempting to retrieve the ec2 hostname: #{e.to_s}" 264: rescue Timeout::Error => t 265: @log.warn "A timeout occurred while attempting to retrieve the ec2 hostname: #{t.to_s}" 266: end 267: false 268: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 37 37: def validate 38: @ec2_endpoints = EC2Helper::endpoints 39: 40: raise PluginValidationError, "You are trying to run this plugin on an invalid platform. You can run the EBS delivery plugin only on EC2." unless valid_platform? 41: 42: @current_availability_zone = EC2Helper::current_availability_zone 43: @current_instance_id = EC2Helper::current_instance_id 44: @current_region = EC2Helper::availability_zone_to_region(@current_availability_zone) 45: 46: set_default_config_value('kernel', false) 47: set_default_config_value('ramdisk', false) 48: 49: set_default_config_value('availability_zone', @current_availability_zone) 50: set_default_config_value('delete_on_termination', true) 51: set_default_config_value('overwrite', false) 52: set_default_config_value('snapshot', false) 53: set_default_config_value('preserve_snapshots', false) 54: set_default_config_value('terminate_instances', false) 55: validate_plugin_config(['access_key', 'secret_access_key', 'account_number'], 'http://boxgrinder.org/tutorials/boxgrinder-build-plugins/#EBS_Delivery_Plugin') 56: 57: raise PluginValidationError, "You can only convert to EBS type AMI appliances converted to EC2 format. Use '-p ec2' switch. For more info about EC2 plugin see http://boxgrinder.org/tutorials/boxgrinder-build-plugins/#EC2_Platform_Plugin." unless @previous_plugin_info[:name] == :ec2 58: raise PluginValidationError, "You selected #{@plugin_config['availability_zone']} availability zone, but your instance is running in #{@current_availability_zone} zone. Please change availability zone in plugin configuration file to #{@current_availability_zone} (see http://boxgrinder.org/tutorials/boxgrinder-build-plugins/#EBS_Delivery_Plugin) or use another instance in #{@plugin_config['availability_zone']} zone to create your EBS AMI." if @plugin_config['availability_zone'] != @current_availability_zone 59: 60: @plugin_config['account_number'].to_s.gsub!(/-/, '') 61: 62: AWS.config(:access_key_id => @plugin_config['access_key'], 63: :secret_access_key => @plugin_config['secret_access_key'], 64: :ec2_endpoint => @ec2_endpoints[@current_region][:endpoint], 65: :max_retries => 5, 66: :use_ssl => @plugin_config['use_ssl']) 67: 68: @ec2 = AWS::EC2.new 69: @ec2helper = EC2Helper.new(@ec2, :log => @log) 70: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 37 37: def validate 38: @ec2_endpoints = EC2Helper::endpoints 39: 40: raise PluginValidationError, "You are trying to run this plugin on an invalid platform. You can run the EBS delivery plugin only on EC2." unless valid_platform? 41: 42: @current_availability_zone = EC2Helper::current_availability_zone 43: @current_instance_id = EC2Helper::current_instance_id 44: @current_region = EC2Helper::availability_zone_to_region(@current_availability_zone) 45: 46: set_default_config_value('kernel', false) 47: set_default_config_value('ramdisk', false) 48: 49: set_default_config_value('availability_zone', @current_availability_zone) 50: set_default_config_value('delete_on_termination', true) 51: set_default_config_value('overwrite', false) 52: set_default_config_value('snapshot', false) 53: set_default_config_value('preserve_snapshots', false) 54: set_default_config_value('terminate_instances', false) 55: validate_plugin_config(['access_key', 'secret_access_key', 'account_number'], 'http://boxgrinder.org/tutorials/boxgrinder-build-plugins/#EBS_Delivery_Plugin') 56: 57: raise PluginValidationError, "You can only convert to EBS type AMI appliances converted to EC2 format. Use '-p ec2' switch. For more info about EC2 plugin see http://boxgrinder.org/tutorials/boxgrinder-build-plugins/#EC2_Platform_Plugin." unless @previous_plugin_info[:name] == :ec2 58: raise PluginValidationError, "You selected #{@plugin_config['availability_zone']} availability zone, but your instance is running in #{@current_availability_zone} zone. Please change availability zone in plugin configuration file to #{@current_availability_zone} (see http://boxgrinder.org/tutorials/boxgrinder-build-plugins/#EBS_Delivery_Plugin) or use another instance in #{@plugin_config['availability_zone']} zone to create your EBS AMI." if @plugin_config['availability_zone'] != @current_availability_zone 59: 60: @plugin_config['account_number'].to_s.gsub!(/-/, '') 61: 62: AWS.config(:access_key_id => @plugin_config['access_key'], 63: :secret_access_key => @plugin_config['secret_access_key'], 64: :ec2_endpoint => @ec2_endpoints[@current_region][:endpoint], 65: :max_retries => 5, 66: :use_ssl => @plugin_config['use_ssl']) 67: 68: @ec2 = AWS::EC2.new 69: @ec2helper = EC2Helper.new(@ec2, :log => @log) 70: end
# File lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb, line 240 240: def wait_for_volume_attachment(suffix) 241: @ec2helper.wait_with_timeout(POLL_FREQ, TIMEOUT){ device_for_suffix(suffix) != nil } 242: end