| Class | BoxGrinder::EC2Plugin |
| In: |
lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb
lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb |
| Parent: | BasePlugin |
| REGIONS | = | {'us_east' => 'url'} |
| KERNELS | = | { 'centos' => { '5' => { 'i386' => {:rpm => 'http://repo.oddthesis.org/packages/other/kernel-xen-2.6.21.7-2.fc8.i686.rpm'}, 'x86_64' => {:rpm => 'http://repo.oddthesis.org/packages/other/kernel-xen-2.6.21.7-2.fc8.x86_64.rpm'} |
| REGIONS | = | {'us_east' => 'url'} |
| KERNELS | = | { 'centos' => { '5' => { 'i386' => {:rpm => 'http://repo.oddthesis.org/packages/other/kernel-xen-2.6.21.7-2.fc8.i686.rpm'}, 'x86_64' => {:rpm => 'http://repo.oddthesis.org/packages/other/kernel-xen-2.6.21.7-2.fc8.x86_64.rpm'} |
issues.jboss.org/browse/BGBUILD-110
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 192
192: def add_ec2_user(guestfs)
193: @log.debug "Adding ec2-user user..."
194: guestfs.sh("useradd ec2-user")
195: guestfs.sh("echo -e 'ec2-user\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers")
196: @log.debug "User ec2-user added."
197: end
issues.jboss.org/browse/BGBUILD-110
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 192
192: def add_ec2_user(guestfs)
193: @log.debug "Adding ec2-user user..."
194: guestfs.sh("useradd ec2-user")
195: guestfs.sh("echo -e 'ec2-user\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers")
196: @log.debug "User ec2-user added."
197: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 42
42: def after_init
43: register_deliverable(:disk => "#{@appliance_config.name}.ec2")
44:
45: register_supported_os('fedora', ['13', '14'])
46: register_supported_os('centos', ['5'])
47: register_supported_os('rhel', ['5'])
48: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 42
42: def after_init
43: register_deliverable(:disk => "#{@appliance_config.name}.ec2")
44:
45: register_supported_os('fedora', ['13', '14'])
46: register_supported_os('centos', ['5'])
47: register_supported_os('rhel', ['5'])
48: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 117
117: def cache_rpms(rpms)
118: for name in rpms.keys
119: cache_file = "#{@config.dir.src_cache}/#{name}"
120:
121: @exec_helper.execute "mkdir -p #{@config.dir.src_cache}"
122: @exec_helper.execute "wget #{rpms[name]} -O #{cache_file}" unless File.exist?(cache_file)
123: end
124: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 117
117: def cache_rpms(rpms)
118: for name in rpms.keys
119: cache_file = "#{@config.dir.src_cache}/#{name}"
120:
121: @exec_helper.execute "mkdir -p #{@config.dir.src_cache}"
122: @exec_helper.execute "wget #{rpms[name]} -O #{cache_file}" unless File.exist?(cache_file)
123: end
124: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 246
246: def change_configuration(guestfs_helper)
247: guestfs_helper.augeas do
248: # disable password authentication
249: set("/etc/ssh/sshd_config", "PasswordAuthentication", "no")
250:
251: # disable root login
252: set("/etc/ssh/sshd_config", "PermitRootLogin", "no")
253: end
254: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 246
246: def change_configuration(guestfs_helper)
247: guestfs_helper.augeas do
248: # disable password authentication
249: set("/etc/ssh/sshd_config", "PasswordAuthentication", "no")
250:
251: # disable root login
252: set("/etc/ssh/sshd_config", "PermitRootLogin", "no")
253: end
254: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 126
126: def create_devices(guestfs)
127: @log.debug "Creating required devices..."
128: guestfs.sh("/sbin/MAKEDEV -d /dev -x console")
129: guestfs.sh("/sbin/MAKEDEV -d /dev -x null")
130: guestfs.sh("/sbin/MAKEDEV -d /dev -x zero")
131: @log.debug "Devices created."
132: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 126
126: def create_devices(guestfs)
127: @log.debug "Creating required devices..."
128: guestfs.sh("/sbin/MAKEDEV -d /dev -x console")
129: guestfs.sh("/sbin/MAKEDEV -d /dev -x null")
130: guestfs.sh("/sbin/MAKEDEV -d /dev -x zero")
131: @log.debug "Devices created."
132: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 134
134: def disk_device_prefix
135: disk = 's'
136:
137: case @appliance_config.os.name
138: when 'fedora'
139: disk = 'xv' if @appliance_config.os.version != '11'
140: end
141:
142: disk
143: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 134
134: def disk_device_prefix
135: disk = 's'
136:
137: case @appliance_config.os.name
138: when 'fedora'
139: disk = 'xv' if @appliance_config.os.version != '11'
140: end
141:
142: disk
143: end
enable networking on default runlevels
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 200
200: def enable_networking(guestfs)
201: @log.debug "Enabling networking..."
202: guestfs.sh("/sbin/chkconfig network on")
203: guestfs.upload("#{File.dirname(__FILE__)}/src/ifcfg-eth0", "/etc/sysconfig/network-scripts/ifcfg-eth0")
204: @log.debug "Networking enabled."
205: end
enable networking on default runlevels
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 200
200: def enable_networking(guestfs)
201: @log.debug "Enabling networking..."
202: guestfs.sh("/sbin/chkconfig network on")
203: guestfs.upload("#{File.dirname(__FILE__)}/src/ifcfg-eth0", "/etc/sysconfig/network-scripts/ifcfg-eth0")
204: @log.debug "Networking enabled."
205: end
This fixes issues with Fedora 14 on EC2: bugzilla.redhat.com/show_bug.cgi?id=651861#c39
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 184
184: def enable_nosegneg_flag(guestfs)
185: @log.debug "Enabling nosegneg flag..."
186: guestfs.sh("echo \"hwcap 1 nosegneg\" > /etc/ld.so.conf.d/libc6-xen.conf")
187: guestfs.sh("/sbin/ldconfig")
188: @log.debug "Nosegneg enabled."
189: end
This fixes issues with Fedora 14 on EC2: bugzilla.redhat.com/show_bug.cgi?id=651861#c39
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 184
184: def enable_nosegneg_flag(guestfs)
185: @log.debug "Enabling nosegneg flag..."
186: guestfs.sh("echo \"hwcap 1 nosegneg\" > /etc/ld.so.conf.d/libc6-xen.conf")
187: guestfs.sh("/sbin/ldconfig")
188: @log.debug "Nosegneg enabled."
189: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 50
50: def execute
51: unless is_supported_os?
52: @log.error "EC2 delivery plugin supports following operating systems: #{supported_oses}. Your OS is #{@appliance_config.os.name} #{@appliance_config.os.version}."
53: return
54: end
55:
56: @linux_helper = LinuxHelper.new(:log => @log)
57:
58: @log.info "Converting #{@appliance_config.name} appliance image to EC2 format..."
59:
60: begin
61: # TODO using whole 10GB is fine?
62: @image_helper.create_disk(@deliverables.disk, 10)
63: @image_helper.create_filesystem(@deliverables.disk)
64: rescue => e
65: @log.error "Error while preparing EC2 disk image. See logs for more info"
66: raise e
67: end
68:
69: ec2_disk_mount_dir = "#{@dir.tmp}/ec2-#{rand(9999999999).to_s.center(10, rand(9).to_s)}"
70: raw_disk_mount_dir = "#{@dir.tmp}/raw-#{rand(9999999999).to_s.center(10, rand(9).to_s)}"
71:
72: begin
73: ec2_mounts = @image_helper.mount_image(@deliverables.disk, ec2_disk_mount_dir)
74: raw_mounts = @image_helper.mount_image(@previous_deliverables.disk, raw_disk_mount_dir)
75: rescue => e
76: @log.debug e
77: raise "Error while mounting image. See logs for more info"
78: end
79:
80: @image_helper.sync_files(raw_disk_mount_dir, ec2_disk_mount_dir)
81:
82: @image_helper.umount_image(@previous_deliverables.disk, raw_disk_mount_dir, raw_mounts)
83: @image_helper.umount_image(@deliverables.disk, ec2_disk_mount_dir, ec2_mounts)
84:
85: @image_helper.customize(@deliverables.disk) do |guestfs, guestfs_helper|
86: # TODO is this really needed?
87: @log.debug "Uploading '/etc/resolv.conf'..."
88: guestfs.upload("/etc/resolv.conf", "/etc/resolv.conf")
89: @log.debug "'/etc/resolv.conf' uploaded."
90:
91: create_devices(guestfs)
92: upload_fstab(guestfs)
93:
94: guestfs.mkdir("/data") if @appliance_config.is64bit?
95:
96: enable_networking(guestfs)
97: upload_rc_local(guestfs)
98: enable_nosegneg_flag(guestfs)
99: add_ec2_user(guestfs)
100: install_additional_packages(guestfs)
101: change_configuration(guestfs_helper)
102: install_menu_lst(guestfs)
103:
104: unless @appliance_config.post['ec2'].nil?
105: @appliance_config.post['ec2'].each do |cmd|
106: guestfs_helper.sh(cmd, :arch => @appliance_config.hardware.arch)
107: end
108: @log.debug "Post commands from appliance definition file executed."
109: else
110: @log.debug "No commands specified, skipping."
111: end
112: end
113:
114: @log.info "Image converted to EC2 format."
115: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 50
50: def execute
51: unless is_supported_os?
52: @log.error "EC2 delivery plugin supports following operating systems: #{supported_oses}. Your OS is #{@appliance_config.os.name} #{@appliance_config.os.version}."
53: return
54: end
55:
56: @linux_helper = LinuxHelper.new(:log => @log)
57:
58: @log.info "Converting #{@appliance_config.name} appliance image to EC2 format..."
59:
60: begin
61: # TODO using whole 10GB is fine?
62: @image_helper.create_disk(@deliverables.disk, 10)
63: @image_helper.create_filesystem(@deliverables.disk)
64: rescue => e
65: @log.error "Error while preparing EC2 disk image. See logs for more info"
66: raise e
67: end
68:
69: ec2_disk_mount_dir = "#{@dir.tmp}/ec2-#{rand(9999999999).to_s.center(10, rand(9).to_s)}"
70: raw_disk_mount_dir = "#{@dir.tmp}/raw-#{rand(9999999999).to_s.center(10, rand(9).to_s)}"
71:
72: begin
73: ec2_mounts = @image_helper.mount_image(@deliverables.disk, ec2_disk_mount_dir)
74: raw_mounts = @image_helper.mount_image(@previous_deliverables.disk, raw_disk_mount_dir)
75: rescue => e
76: @log.debug e
77: raise "Error while mounting image. See logs for more info"
78: end
79:
80: @image_helper.sync_files(raw_disk_mount_dir, ec2_disk_mount_dir)
81:
82: @image_helper.umount_image(@previous_deliverables.disk, raw_disk_mount_dir, raw_mounts)
83: @image_helper.umount_image(@deliverables.disk, ec2_disk_mount_dir, ec2_mounts)
84:
85: @image_helper.customize(@deliverables.disk) do |guestfs, guestfs_helper|
86: # TODO is this really needed?
87: @log.debug "Uploading '/etc/resolv.conf'..."
88: guestfs.upload("/etc/resolv.conf", "/etc/resolv.conf")
89: @log.debug "'/etc/resolv.conf' uploaded."
90:
91: create_devices(guestfs)
92: upload_fstab(guestfs)
93:
94: guestfs.mkdir("/data") if @appliance_config.is64bit?
95:
96: enable_networking(guestfs)
97: upload_rc_local(guestfs)
98: enable_nosegneg_flag(guestfs)
99: add_ec2_user(guestfs)
100: install_additional_packages(guestfs)
101: change_configuration(guestfs_helper)
102: install_menu_lst(guestfs)
103:
104: unless @appliance_config.post['ec2'].nil?
105: @appliance_config.post['ec2'].each do |cmd|
106: guestfs_helper.sh(cmd, :arch => @appliance_config.hardware.arch)
107: end
108: @log.debug "Post commands from appliance definition file executed."
109: else
110: @log.debug "No commands specified, skipping."
111: end
112: end
113:
114: @log.info "Image converted to EC2 format."
115: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 219
219: def install_additional_packages(guestfs)
220: rpms = {}
221:
222: begin
223: kernel_rpm = KERNELS[@appliance_config.os.name][@appliance_config.os.version][@appliance_config.hardware.base_arch][:rpm]
224: rpms[File.basename(kernel_rpm)] = kernel_rpm
225: rescue
226: end
227:
228: return if rpms.empty?
229:
230: cache_rpms(rpms)
231:
232: @log.debug "Installing additional packages (#{rpms.keys.join(", ")})..."
233: guestfs.mkdir_p("/tmp/rpms")
234:
235: for name in rpms.keys
236: cache_file = "#{@config.dir.src_cache}/#{name}"
237: guestfs.upload(cache_file, "/tmp/rpms/#{name}")
238: end
239:
240: guestfs.sh("rpm -ivh --nodeps /tmp/rpms/*.rpm")
241: guestfs.rm_rf("/tmp/rpms")
242:
243: @log.debug "Additional packages installed."
244: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 219
219: def install_additional_packages(guestfs)
220: rpms = {}
221:
222: begin
223: kernel_rpm = KERNELS[@appliance_config.os.name][@appliance_config.os.version][@appliance_config.hardware.base_arch][:rpm]
224: rpms[File.basename(kernel_rpm)] = kernel_rpm
225: rescue
226: end
227:
228: return if rpms.empty?
229:
230: cache_rpms(rpms)
231:
232: @log.debug "Installing additional packages (#{rpms.keys.join(", ")})..."
233: guestfs.mkdir_p("/tmp/rpms")
234:
235: for name in rpms.keys
236: cache_file = "#{@config.dir.src_cache}/#{name}"
237: guestfs.upload(cache_file, "/tmp/rpms/#{name}")
238: end
239:
240: guestfs.sh("rpm -ivh --nodeps /tmp/rpms/*.rpm")
241: guestfs.rm_rf("/tmp/rpms")
242:
243: @log.debug "Additional packages installed."
244: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 164
164: def install_menu_lst(guestfs)
165: @log.debug "Uploading '/boot/grub/menu.lst' file..."
166: menu_lst_data = File.open("#{File.dirname(__FILE__)}/src/menu.lst").read
167:
168: menu_lst_data.gsub!(/#TITLE#/, @appliance_config.name)
169: menu_lst_data.gsub!(/#KERNEL_VERSION#/, @linux_helper.kernel_version(guestfs))
170: menu_lst_data.gsub!(/#KERNEL_IMAGE_NAME#/, @linux_helper.kernel_image_name(guestfs))
171: menu_lst_data.gsub!(/#DISK_DEVICE_PREFIX#/, disk_device_prefix)
172:
173: menu_lst = Tempfile.new('menu_lst')
174: menu_lst << menu_lst_data
175: menu_lst.flush
176:
177: guestfs.upload(menu_lst.path, "/boot/grub/menu.lst")
178:
179: menu_lst.close
180: @log.debug "'/boot/grub/menu.lst' file uploaded."
181: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 164
164: def install_menu_lst(guestfs)
165: @log.debug "Uploading '/boot/grub/menu.lst' file..."
166: menu_lst_data = File.open("#{File.dirname(__FILE__)}/src/menu.lst").read
167:
168: menu_lst_data.gsub!(/#TITLE#/, @appliance_config.name)
169: menu_lst_data.gsub!(/#KERNEL_VERSION#/, @linux_helper.kernel_version(guestfs))
170: menu_lst_data.gsub!(/#KERNEL_IMAGE_NAME#/, @linux_helper.kernel_image_name(guestfs))
171: menu_lst_data.gsub!(/#DISK_DEVICE_PREFIX#/, disk_device_prefix)
172:
173: menu_lst = Tempfile.new('menu_lst')
174: menu_lst << menu_lst_data
175: menu_lst.flush
176:
177: guestfs.upload(menu_lst.path, "/boot/grub/menu.lst")
178:
179: menu_lst.close
180: @log.debug "'/boot/grub/menu.lst' file uploaded."
181: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 145
145: def upload_fstab(guestfs)
146: @log.debug "Uploading '/etc/fstab' file..."
147:
148: fstab_file = @appliance_config.is64bit? ? "#{File.dirname(__FILE__)}/src/fstab_64bit" : "#{File.dirname(__FILE__)}/src/fstab_32bit"
149:
150: fstab_data = File.open(fstab_file).read
151: fstab_data.gsub!(/#DISK_DEVICE_PREFIX#/, disk_device_prefix)
152:
153: fstab = Tempfile.new('fstab')
154: fstab << fstab_data
155: fstab.flush
156:
157: guestfs.upload(fstab.path, "/etc/fstab")
158:
159: fstab.close
160:
161: @log.debug "'/etc/fstab' file uploaded."
162: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 145
145: def upload_fstab(guestfs)
146: @log.debug "Uploading '/etc/fstab' file..."
147:
148: fstab_file = @appliance_config.is64bit? ? "#{File.dirname(__FILE__)}/src/fstab_64bit" : "#{File.dirname(__FILE__)}/src/fstab_32bit"
149:
150: fstab_data = File.open(fstab_file).read
151: fstab_data.gsub!(/#DISK_DEVICE_PREFIX#/, disk_device_prefix)
152:
153: fstab = Tempfile.new('fstab')
154: fstab << fstab_data
155: fstab.flush
156:
157: guestfs.upload(fstab.path, "/etc/fstab")
158:
159: fstab.close
160:
161: @log.debug "'/etc/fstab' file uploaded."
162: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 207
207: def upload_rc_local(guestfs)
208: @log.debug "Uploading '/etc/rc.local' file..."
209: rc_local = Tempfile.new('rc_local')
210: rc_local << guestfs.read_file("/etc/rc.local") + File.read("#{File.dirname(__FILE__)}/src/rc_local")
211: rc_local.flush
212:
213: guestfs.upload(rc_local.path, "/etc/rc.local")
214:
215: rc_local.close
216: @log.debug "'/etc/rc.local' file uploaded."
217: end
# File lib/boxgrinder-build-ec2-platform-plugin/ec2-plugin.rb, line 207
207: def upload_rc_local(guestfs)
208: @log.debug "Uploading '/etc/rc.local' file..."
209: rc_local = Tempfile.new('rc_local')
210: rc_local << guestfs.read_file("/etc/rc.local") + File.read("#{File.dirname(__FILE__)}/src/rc_local")
211: rc_local.flush
212:
213: guestfs.upload(rc_local.path, "/etc/rc.local")
214:
215: rc_local.close
216: @log.debug "'/etc/rc.local' file uploaded."
217: end