lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb in boxgrinder-build-0.9.0 vs lib/boxgrinder-build/plugins/delivery/ebs/ebs-plugin.rb in boxgrinder-build-0.9.1

- old
+ new

@@ -41,16 +41,13 @@ 'x86_64' => {:aki => 'aki-427d952b'} } } def after_init - begin + if valid_platform? @current_avaibility_zone = open('http://169.254.169.254/latest/meta-data/placement/availability-zone').string @region = @current_avaibility_zone.scan(/((\w+)-(\w+)-(\d+))/).flatten.first - rescue - @current_avaibility_zone = nil - @region = nil end set_default_config_value('availability_zone', @current_avaibility_zone) set_default_config_value('delete_on_termination', true) @@ -111,46 +108,21 @@ @log.debug "Waiting for EBS volume to be attached..." # wait for volume to be attached wait_for_volume_status('in-use', volume_id) - sleep 5 # let's wait to discover the attached volume by OS + sleep 10 # let's wait to discover the attached volume by OS @log.info "Copying data to EBS volume..." - ec2_disk_mount_dir = "#{@dir.tmp}/ec2-#{rand(9999999999).to_s.center(10, rand(9).to_s)}" - ebs_disk_mount_dir = "#{@dir.tmp}/ebs-#{rand(9999999999).to_s.center(10, rand(9).to_s)}" + @image_helper.customize([@previous_deliverables.disk, device_for_suffix(suffix)], :automount => false) do |guestfs, guestfs_helper| + @image_helper.sync_filesystem(guestfs, guestfs_helper) - FileUtils.mkdir_p(ec2_disk_mount_dir) - FileUtils.mkdir_p(ebs_disk_mount_dir) - - begin - ec2_mounts = @image_helper.mount_image(@previous_deliverables.disk, ec2_disk_mount_dir) - rescue => e - @log.debug e - raise "Error while mounting image. See logs for more info" + @log.debug "Adjusting /etc/fstab..." + adjust_fstab(guestfs) end - @log.debug "Creating filesystem on volume..." - - @image_helper.create_filesystem(device_for_suffix(suffix)) - @exec_helper.execute("mount #{device_for_suffix(suffix)} #{ebs_disk_mount_dir}") - - @log.debug "Syncing files..." - - @image_helper.sync_files(ec2_disk_mount_dir, ebs_disk_mount_dir) - - @log.debug "Adjusting /etc/fstab..." - - adjust_fstab(ebs_disk_mount_dir) - - @exec_helper.execute("umount #{ebs_disk_mount_dir}") - @image_helper.umount_image(@previous_deliverables.disk, ec2_disk_mount_dir, ec2_mounts) - - FileUtils.rm_rf(ebs_disk_mount_dir) - FileUtils.rm_rf(ec2_disk_mount_dir) - @log.debug "Detaching EBS volume..." @ec2.detach_volume(:device => "/dev/sd#{suffix}", :volume_id => volume_id, :instance_id => instance_id) @log.debug "Waiting for EBS volume to be available..." @@ -226,13 +198,13 @@ images['imagesSet']['item'].each { |image| return image['imageId'] if image['name'] == name } false end - def adjust_fstab(ebs_mount_dir) - @exec_helper.execute("cat #{ebs_mount_dir}/etc/fstab | grep -v '/mnt' | grep -v '/data' | grep -v 'swap' > #{ebs_mount_dir}/etc/fstab.new") - @exec_helper.execute("mv #{ebs_mount_dir}/etc/fstab.new #{ebs_mount_dir}/etc/fstab") + def adjust_fstab(guestfs) + guestfs.sh("cat /etc/fstab | grep -v '/mnt' | grep -v '/data' | grep -v 'swap' > /etc/fstab.new") + guestfs.mv("/etc/fstab.new", "/etc/fstab") end def wait_for_snapshot_status(status, snapshot_id) snapshot = @ec2.describe_snapshots(:snapshot_id => snapshot_id)['snapshotSet']['item'].first @@ -253,11 +225,11 @@ def device_for_suffix(suffix) return "/dev/sd#{suffix}" if File.exists?("/dev/sd#{suffix}") return "/dev/xvd#{suffix}" if File.exists?("/dev/xvd#{suffix}") - raise "Not found device for suffix #{suffix}" + raise "Device for suffix '#{suffix}' not found!" end def free_device_suffix ("f".."p").each do |suffix| return suffix unless File.exists?("/dev/sd#{suffix}") or File.exists?("/dev/xvd#{suffix}") @@ -266,15 +238,14 @@ raise "Found too many attached devices. Cannot attach EBS volume." end def valid_platform? begin - open("http://169.254.169.254/1.0/meta-data/local-ipv4") - true - rescue + return Resolv.getname("169.254.169.254").include?(".ec2.internal") + rescue Resolv::ResolvError false end end end end -plugin :class => BoxGrinder::EBSPlugin, :type => :delivery, :name => :ebs, :full_name => "Elastic Block Storage" +plugin :class => BoxGrinder::EBSPlugin, :type => :delivery, :name => :ebs, :full_name => "Elastic Block Storage"