Class | BoxGrinder::EC2Plugin |
In: |
lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb
lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb |
Parent: | BasePlugin |
issues.jboss.org/browse/BGBUILD-110
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 200 200: def add_ec2_user(guestfs) 201: @log.debug "Adding ec2-user user..." 202: guestfs.sh("useradd ec2-user") 203: guestfs.sh("echo -e 'ec2-user\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers") 204: @log.debug "User ec2-user added." 205: end
issues.jboss.org/browse/BGBUILD-110
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 200 200: def add_ec2_user(guestfs) 201: @log.debug "Adding ec2-user user..." 202: guestfs.sh("useradd ec2-user") 203: guestfs.sh("echo -e 'ec2-user\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers") 204: @log.debug "User ec2-user added." 205: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 25 25: def after_init 26: register_deliverable(:disk => "#{@appliance_config.name}.ec2") 27: 28: register_supported_os('fedora', ['13', '14', '15']) 29: register_supported_os('centos', ['5']) 30: register_supported_os('rhel', ['5', '6']) 31: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 25 25: def after_init 26: register_deliverable(:disk => "#{@appliance_config.name}.ec2") 27: 28: register_supported_os('fedora', ['13', '14', '15']) 29: register_supported_os('centos', ['5']) 30: register_supported_os('rhel', ['5', '6']) 31: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 227 227: def change_configuration(guestfs_helper) 228: guestfs_helper.augeas do 229: # disable password authentication 230: set("/etc/ssh/sshd_config", "PasswordAuthentication", "no") 231: 232: # disable root login 233: set("/etc/ssh/sshd_config", "PermitRootLogin", "no") 234: end 235: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 227 227: def change_configuration(guestfs_helper) 228: guestfs_helper.augeas do 229: # disable password authentication 230: set("/etc/ssh/sshd_config", "PasswordAuthentication", "no") 231: 232: # disable root login 233: set("/etc/ssh/sshd_config", "PermitRootLogin", "no") 234: end 235: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 136 136: def create_devices(guestfs) 137: return if guestfs.exists('/sbin/MAKEDEV') == 0 138: 139: @log.debug "Creating required devices..." 140: guestfs.sh("/sbin/MAKEDEV -d /dev -x console") 141: guestfs.sh("/sbin/MAKEDEV -d /dev -x null") 142: guestfs.sh("/sbin/MAKEDEV -d /dev -x zero") 143: @log.debug "Devices created." 144: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 136 136: def create_devices(guestfs) 137: return if guestfs.exists('/sbin/MAKEDEV') == 0 138: 139: @log.debug "Creating required devices..." 140: guestfs.sh("/sbin/MAKEDEV -d /dev -x console") 141: guestfs.sh("/sbin/MAKEDEV -d /dev -x null") 142: guestfs.sh("/sbin/MAKEDEV -d /dev -x zero") 143: @log.debug "Devices created." 144: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 108 108: def create_ec2_disk 109: begin 110: # TODO using whole 10GB is fine? 111: @image_helper.create_disk(@deliverables.disk, 10) 112: @image_helper.create_filesystem(@deliverables.disk) 113: rescue => e 114: @log.error e 115: raise "Error while preparing EC2 disk image. See logs for more info." 116: end 117: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 108 108: def create_ec2_disk 109: begin 110: # TODO using whole 10GB is fine? 111: @image_helper.create_disk(@deliverables.disk, 10) 112: @image_helper.create_filesystem(@deliverables.disk) 113: rescue => e 114: @log.error e 115: raise "Error while preparing EC2 disk image. See logs for more info." 116: end 117: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 146 146: def disk_device_prefix 147: disk = 'xv' 148: disk = 's' if (@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5' 149: 150: disk 151: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 146 146: def disk_device_prefix 147: disk = 'xv' 148: disk = 's' if (@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5' 149: 150: disk 151: end
enable networking on default runlevels
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 208 208: def enable_networking(guestfs) 209: @log.debug "Enabling networking..." 210: guestfs.sh("/sbin/chkconfig network on") 211: guestfs.upload("#{File.dirname(__FILE__)}/src/ifcfg-eth0", "/etc/sysconfig/network-scripts/ifcfg-eth0") 212: @log.debug "Networking enabled." 213: end
enable networking on default runlevels
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 208 208: def enable_networking(guestfs) 209: @log.debug "Enabling networking..." 210: guestfs.sh("/sbin/chkconfig network on") 211: guestfs.upload("#{File.dirname(__FILE__)}/src/ifcfg-eth0", "/etc/sysconfig/network-scripts/ifcfg-eth0") 212: @log.debug "Networking enabled." 213: end
This fixes issues with Fedora 14 on EC2: bugzilla.redhat.com/show_bug.cgi?id=651861#c39
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 192 192: def enable_nosegneg_flag(guestfs) 193: @log.debug "Enabling nosegneg flag..." 194: guestfs.sh("echo \"hwcap 1 nosegneg\" > /etc/ld.so.conf.d/libc6-xen.conf") 195: guestfs.sh("/sbin/ldconfig") 196: @log.debug "Nosegneg enabled." 197: end
This fixes issues with Fedora 14 on EC2: bugzilla.redhat.com/show_bug.cgi?id=651861#c39
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 192 192: def enable_nosegneg_flag(guestfs) 193: @log.debug "Enabling nosegneg flag..." 194: guestfs.sh("echo \"hwcap 1 nosegneg\" > /etc/ld.so.conf.d/libc6-xen.conf") 195: guestfs.sh("/sbin/ldconfig") 196: @log.debug "Nosegneg enabled." 197: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 33 33: def execute 34: @linux_helper = LinuxHelper.new(:log => @log) 35: 36: @log.info "Converting #{@appliance_config.name} appliance image to EC2 format..." 37: 38: create_ec2_disk 39: sync_files 40: 41: @image_helper.customize(@deliverables.disk) do |guestfs, guestfs_helper| 42: if (@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5' 43: # Not sure why it's messed but this prevents booting on AWS 44: recreate_journal(guestfs) 45: 46: # Remove normal kernel 47: guestfs.sh("yum -y remove kernel") 48: # because we need to install kernel-xen package 49: guestfs.sh("yum -y install kernel-xen") 50: # and add require modules 51: @linux_helper.recreate_kernel_image(guestfs, ['xenblk', 'xennet']) 52: end 53: 54: # TODO is this really needed? 55: @log.debug "Uploading '/etc/resolv.conf'..." 56: guestfs.upload("/etc/resolv.conf", "/etc/resolv.conf") 57: @log.debug "'/etc/resolv.conf' uploaded." 58: 59: create_devices(guestfs) 60: 61: guestfs.mkdir("/data") if @appliance_config.is64bit? 62: 63: upload_fstab(guestfs) 64: enable_networking(guestfs) 65: upload_rc_local(guestfs) 66: add_ec2_user(guestfs) 67: change_configuration(guestfs_helper) 68: install_menu_lst(guestfs) 69: 70: enable_nosegneg_flag(guestfs) if @appliance_config.os.name == 'fedora' 71: 72: execute_post(guestfs_helper) 73: end 74: 75: @log.info "Image converted to EC2 format." 76: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 33 33: def execute 34: @linux_helper = LinuxHelper.new(:log => @log) 35: 36: @log.info "Converting #{@appliance_config.name} appliance image to EC2 format..." 37: 38: create_ec2_disk 39: sync_files 40: 41: @image_helper.customize(@deliverables.disk) do |guestfs, guestfs_helper| 42: if (@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5' 43: # Not sure why it's messed but this prevents booting on AWS 44: recreate_journal(guestfs) 45: 46: # Remove normal kernel 47: guestfs.sh("yum -y remove kernel") 48: # because we need to install kernel-xen package 49: guestfs.sh("yum -y install kernel-xen") 50: # and add require modules 51: @linux_helper.recreate_kernel_image(guestfs, ['xenblk', 'xennet']) 52: end 53: 54: # TODO is this really needed? 55: @log.debug "Uploading '/etc/resolv.conf'..." 56: guestfs.upload("/etc/resolv.conf", "/etc/resolv.conf") 57: @log.debug "'/etc/resolv.conf' uploaded." 58: 59: create_devices(guestfs) 60: 61: guestfs.mkdir("/data") if @appliance_config.is64bit? 62: 63: upload_fstab(guestfs) 64: enable_networking(guestfs) 65: upload_rc_local(guestfs) 66: add_ec2_user(guestfs) 67: change_configuration(guestfs_helper) 68: install_menu_lst(guestfs) 69: 70: enable_nosegneg_flag(guestfs) if @appliance_config.os.name == 'fedora' 71: 72: execute_post(guestfs_helper) 73: end 74: 75: @log.info "Image converted to EC2 format." 76: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 119 119: def execute_post(guestfs_helper) 120: unless @appliance_config.post['ec2'].nil? 121: @appliance_config.post['ec2'].each do |cmd| 122: guestfs_helper.sh(cmd, :arch => @appliance_config.hardware.arch) 123: end 124: @log.debug "Post commands from appliance definition file executed." 125: else 126: @log.debug "No commands specified, skipping." 127: end 128: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 119 119: def execute_post(guestfs_helper) 120: unless @appliance_config.post['ec2'].nil? 121: @appliance_config.post['ec2'].each do |cmd| 122: guestfs_helper.sh(cmd, :arch => @appliance_config.hardware.arch) 123: end 124: @log.debug "Post commands from appliance definition file executed." 125: else 126: @log.debug "No commands specified, skipping." 127: end 128: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 173 173: def install_menu_lst(guestfs) 174: @log.debug "Uploading '/boot/grub/menu.lst' file..." 175: menu_lst_data = File.open("#{File.dirname(__FILE__)}/src/menu.lst").read 176: 177: menu_lst_data.gsub!(/#TITLE#/, @appliance_config.name) 178: menu_lst_data.gsub!(/#KERNEL_VERSION#/, @linux_helper.kernel_version(guestfs)) 179: menu_lst_data.gsub!(/#KERNEL_IMAGE_NAME#/, @linux_helper.kernel_image_name(guestfs)) 180: 181: menu_lst = Tempfile.new('menu_lst') 182: menu_lst << menu_lst_data 183: menu_lst.flush 184: 185: guestfs.upload(menu_lst.path, "/boot/grub/menu.lst") 186: 187: menu_lst.close 188: @log.debug "'/boot/grub/menu.lst' file uploaded." 189: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 173 173: def install_menu_lst(guestfs) 174: @log.debug "Uploading '/boot/grub/menu.lst' file..." 175: menu_lst_data = File.open("#{File.dirname(__FILE__)}/src/menu.lst").read 176: 177: menu_lst_data.gsub!(/#TITLE#/, @appliance_config.name) 178: menu_lst_data.gsub!(/#KERNEL_VERSION#/, @linux_helper.kernel_version(guestfs)) 179: menu_lst_data.gsub!(/#KERNEL_IMAGE_NAME#/, @linux_helper.kernel_image_name(guestfs)) 180: 181: menu_lst = Tempfile.new('menu_lst') 182: menu_lst << menu_lst_data 183: menu_lst.flush 184: 185: guestfs.upload(menu_lst.path, "/boot/grub/menu.lst") 186: 187: menu_lst.close 188: @log.debug "'/boot/grub/menu.lst' file uploaded." 189: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 130 130: def recreate_journal(guestfs) 131: @log.debug "Recreating EXT3 journal on root partition." 132: guestfs.sh("tune2fs -j #{guestfs.list_devices.first}") 133: @log.debug "Journal recreated." 134: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 130 130: def recreate_journal(guestfs) 131: @log.debug "Recreating EXT3 journal on root partition." 132: guestfs.sh("tune2fs -j #{guestfs.list_devices.first}") 133: @log.debug "Journal recreated." 134: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 78 78: def sync_files 79: ec2_disk_mount_dir = "#{@dir.tmp}/ec2-#{rand(9999999999).to_s.center(10, rand(9).to_s)}" 80: raw_disk_mount_dir = "#{@dir.tmp}/raw-#{rand(9999999999).to_s.center(10, rand(9).to_s)}" 81: 82: tmp_disk = "#{@dir.tmp}/#{@appliance_config.name}.raw" 83: 84: @log.debug "Conveting disk to RAW format..." 85: @image_helper.convert_disk(@previous_deliverables.disk, 'raw', tmp_disk) 86: 87: begin 88: ec2_mounts = @image_helper.mount_image(@deliverables.disk, ec2_disk_mount_dir) 89: raw_mounts = @image_helper.mount_image(tmp_disk, raw_disk_mount_dir) 90: rescue => e 91: @log.error e 92: @log.error "Mouting failed, trying to clean up." 93: 94: @image_helper.umount_image(tmp_disk, raw_disk_mount_dir, raw_mounts) unless raw_mounts.nil? 95: @image_helper.umount_image(@deliverables.disk, ec2_disk_mount_dir, ec2_mounts) unless ec2_mounts.nil? 96: 97: raise "Error while mounting image. See logs for more info." 98: end 99: 100: @image_helper.sync_files(raw_disk_mount_dir, ec2_disk_mount_dir) 101: 102: @image_helper.umount_image(tmp_disk, raw_disk_mount_dir, raw_mounts) 103: @image_helper.umount_image(@deliverables.disk, ec2_disk_mount_dir, ec2_mounts) 104: 105: FileUtils.rm_rf tmp_disk 106: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 78 78: def sync_files 79: ec2_disk_mount_dir = "#{@dir.tmp}/ec2-#{rand(9999999999).to_s.center(10, rand(9).to_s)}" 80: raw_disk_mount_dir = "#{@dir.tmp}/raw-#{rand(9999999999).to_s.center(10, rand(9).to_s)}" 81: 82: tmp_disk = "#{@dir.tmp}/#{@appliance_config.name}.raw" 83: 84: @log.debug "Conveting disk to RAW format..." 85: @image_helper.convert_disk(@previous_deliverables.disk, 'raw', tmp_disk) 86: 87: begin 88: ec2_mounts = @image_helper.mount_image(@deliverables.disk, ec2_disk_mount_dir) 89: raw_mounts = @image_helper.mount_image(tmp_disk, raw_disk_mount_dir) 90: rescue => e 91: @log.error e 92: @log.error "Mouting failed, trying to clean up." 93: 94: @image_helper.umount_image(tmp_disk, raw_disk_mount_dir, raw_mounts) unless raw_mounts.nil? 95: @image_helper.umount_image(@deliverables.disk, ec2_disk_mount_dir, ec2_mounts) unless ec2_mounts.nil? 96: 97: raise "Error while mounting image. See logs for more info." 98: end 99: 100: @image_helper.sync_files(raw_disk_mount_dir, ec2_disk_mount_dir) 101: 102: @image_helper.umount_image(tmp_disk, raw_disk_mount_dir, raw_mounts) 103: @image_helper.umount_image(@deliverables.disk, ec2_disk_mount_dir, ec2_mounts) 104: 105: FileUtils.rm_rf tmp_disk 106: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 153 153: def upload_fstab(guestfs) 154: @log.debug "Uploading '/etc/fstab' file..." 155: 156: fstab_file = @appliance_config.is64bit? ? "#{File.dirname(__FILE__)}/src/fstab_64bit" : "#{File.dirname(__FILE__)}/src/fstab_32bit" 157: 158: fstab_data = File.open(fstab_file).read 159: fstab_data.gsub!(/#DISK_DEVICE_PREFIX#/, disk_device_prefix) 160: fstab_data.gsub!(/#FILESYSTEM_TYPE#/, @appliance_config.hardware.partitions['/']['type']) 161: 162: fstab = Tempfile.new('fstab') 163: fstab << fstab_data 164: fstab.flush 165: 166: guestfs.upload(fstab.path, "/etc/fstab") 167: 168: fstab.close 169: 170: @log.debug "'/etc/fstab' file uploaded." 171: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 153 153: def upload_fstab(guestfs) 154: @log.debug "Uploading '/etc/fstab' file..." 155: 156: fstab_file = @appliance_config.is64bit? ? "#{File.dirname(__FILE__)}/src/fstab_64bit" : "#{File.dirname(__FILE__)}/src/fstab_32bit" 157: 158: fstab_data = File.open(fstab_file).read 159: fstab_data.gsub!(/#DISK_DEVICE_PREFIX#/, disk_device_prefix) 160: fstab_data.gsub!(/#FILESYSTEM_TYPE#/, @appliance_config.hardware.partitions['/']['type']) 161: 162: fstab = Tempfile.new('fstab') 163: fstab << fstab_data 164: fstab.flush 165: 166: guestfs.upload(fstab.path, "/etc/fstab") 167: 168: fstab.close 169: 170: @log.debug "'/etc/fstab' file uploaded." 171: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 215 215: def upload_rc_local(guestfs) 216: @log.debug "Uploading '/etc/rc.local' file..." 217: rc_local = Tempfile.new('rc_local') 218: rc_local << guestfs.read_file("/etc/rc.local") + File.read("#{File.dirname(__FILE__)}/src/rc_local") 219: rc_local.flush 220: 221: guestfs.upload(rc_local.path, "/etc/rc.local") 222: 223: rc_local.close 224: @log.debug "'/etc/rc.local' file uploaded." 225: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 215 215: def upload_rc_local(guestfs) 216: @log.debug "Uploading '/etc/rc.local' file..." 217: rc_local = Tempfile.new('rc_local') 218: rc_local << guestfs.read_file("/etc/rc.local") + File.read("#{File.dirname(__FILE__)}/src/rc_local") 219: rc_local.flush 220: 221: guestfs.upload(rc_local.path, "/etc/rc.local") 222: 223: rc_local.close 224: @log.debug "'/etc/rc.local' file uploaded." 225: end