| 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 152
152: def add_ec2_user(guestfs)
153: @log.debug "Adding ec2-user user..."
154: guestfs.sh("useradd ec2-user")
155: guestfs.sh("echo -e 'ec2-user\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers")
156: @log.debug "User ec2-user added."
157: end
issues.jboss.org/browse/BGBUILD-110
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 152
152: def add_ec2_user(guestfs)
153: @log.debug "Adding ec2-user user..."
154: guestfs.sh("useradd ec2-user")
155: guestfs.sh("echo -e 'ec2-user\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers")
156: @log.debug "User ec2-user added."
157: 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', '16'])
29: register_supported_os('centos', ['5'])
30: register_supported_os('sl', ['5', '6'])
31: register_supported_os('rhel', ['5', '6'])
32: 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', '16'])
29: register_supported_os('centos', ['5'])
30: register_supported_os('sl', ['5', '6'])
31: register_supported_os('rhel', ['5', '6'])
32: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 200
200: def change_configuration(guestfs_helper)
201: guestfs_helper.augeas do
202: # disable password authentication
203: set("/etc/ssh/sshd_config", "PasswordAuthentication", "no")
204:
205: # disable root login
206: set("/etc/ssh/sshd_config", "PermitRootLogin", "no")
207: end
208: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 200
200: def change_configuration(guestfs_helper)
201: guestfs_helper.augeas do
202: # disable password authentication
203: set("/etc/ssh/sshd_config", "PasswordAuthentication", "no")
204:
205: # disable root login
206: set("/etc/ssh/sshd_config", "PermitRootLogin", "no")
207: end
208: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 88
88: def create_devices(guestfs)
89: return if guestfs.exists('/sbin/MAKEDEV') == 0
90:
91: @log.debug "Creating required devices..."
92: guestfs.sh("/sbin/MAKEDEV -d /dev -x console")
93: guestfs.sh("/sbin/MAKEDEV -d /dev -x null")
94: guestfs.sh("/sbin/MAKEDEV -d /dev -x zero")
95: @log.debug "Devices created."
96: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 88
88: def create_devices(guestfs)
89: return if guestfs.exists('/sbin/MAKEDEV') == 0
90:
91: @log.debug "Creating required devices..."
92: guestfs.sh("/sbin/MAKEDEV -d /dev -x console")
93: guestfs.sh("/sbin/MAKEDEV -d /dev -x null")
94: guestfs.sh("/sbin/MAKEDEV -d /dev -x zero")
95: @log.debug "Devices created."
96: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 98
98: def disk_device_prefix
99: disk = 'xv'
100: disk = 's' if (@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5'
101:
102: disk
103: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 98
98: def disk_device_prefix
99: disk = 'xv'
100: disk = 's' if (@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5'
101:
102: disk
103: end
enable networking on default runlevels
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 160
160: def enable_networking(guestfs)
161: @log.debug "Enabling networking..."
162: guestfs.sh("/sbin/chkconfig network on")
163: guestfs.upload("#{File.dirname(__FILE__)}/src/ifcfg-eth0", "/etc/sysconfig/network-scripts/ifcfg-eth0")
164: @log.debug "Networking enabled."
165: end
enable networking on default runlevels
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 160
160: def enable_networking(guestfs)
161: @log.debug "Enabling networking..."
162: guestfs.sh("/sbin/chkconfig network on")
163: guestfs.upload("#{File.dirname(__FILE__)}/src/ifcfg-eth0", "/etc/sysconfig/network-scripts/ifcfg-eth0")
164: @log.debug "Networking enabled."
165: 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 144
144: def enable_nosegneg_flag(guestfs)
145: @log.debug "Enabling nosegneg flag..."
146: guestfs.sh("echo \"hwcap 1 nosegneg\" > /etc/ld.so.conf.d/libc6-xen.conf")
147: guestfs.sh("/sbin/ldconfig")
148: @log.debug "Nosegneg enabled."
149: 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 144
144: def enable_nosegneg_flag(guestfs)
145: @log.debug "Enabling nosegneg flag..."
146: guestfs.sh("echo \"hwcap 1 nosegneg\" > /etc/ld.so.conf.d/libc6-xen.conf")
147: guestfs.sh("/sbin/ldconfig")
148: @log.debug "Nosegneg enabled."
149: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 34
34: def execute
35: @linux_helper = LinuxHelper.new(:log => @log)
36:
37: @log.info "Converting #{@appliance_config.name} appliance image to EC2 format..."
38:
39: @image_helper.create_disk(@deliverables.disk, 10) # 10 GB destination disk
40:
41: @image_helper.customize([@previous_deliverables.disk, @deliverables.disk], :automount => false) do |guestfs, guestfs_helper|
42: @image_helper.sync_filesystem(guestfs, guestfs_helper)
43:
44: if (@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5'
45: # Remove normal kernel
46: guestfs.sh("yum -y remove kernel")
47: # because we need to install kernel-xen package
48: guestfs.sh("yum -y install kernel-xen")
49: # and add require modules
50: @linux_helper.recreate_kernel_image(guestfs, ['xenblk', 'xennet'])
51: end
52:
53: # TODO is this really needed?
54: @log.debug "Uploading '/etc/resolv.conf'..."
55: guestfs.upload("/etc/resolv.conf", "/etc/resolv.conf")
56: @log.debug "'/etc/resolv.conf' uploaded."
57:
58: create_devices(guestfs)
59:
60: guestfs.mkdir("/data") if @appliance_config.is64bit?
61:
62: upload_fstab(guestfs)
63: enable_networking(guestfs)
64: upload_rc_local(guestfs)
65: add_ec2_user(guestfs)
66: change_configuration(guestfs_helper)
67: install_menu_lst(guestfs)
68:
69: enable_nosegneg_flag(guestfs) if @appliance_config.os.name == 'fedora'
70:
71: execute_post(guestfs_helper)
72: end
73:
74: @log.info "Image converted to EC2 format."
75: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 34
34: def execute
35: @linux_helper = LinuxHelper.new(:log => @log)
36:
37: @log.info "Converting #{@appliance_config.name} appliance image to EC2 format..."
38:
39: @image_helper.create_disk(@deliverables.disk, 10) # 10 GB destination disk
40:
41: @image_helper.customize([@previous_deliverables.disk, @deliverables.disk], :automount => false) do |guestfs, guestfs_helper|
42: @image_helper.sync_filesystem(guestfs, guestfs_helper)
43:
44: if (@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5'
45: # Remove normal kernel
46: guestfs.sh("yum -y remove kernel")
47: # because we need to install kernel-xen package
48: guestfs.sh("yum -y install kernel-xen")
49: # and add require modules
50: @linux_helper.recreate_kernel_image(guestfs, ['xenblk', 'xennet'])
51: end
52:
53: # TODO is this really needed?
54: @log.debug "Uploading '/etc/resolv.conf'..."
55: guestfs.upload("/etc/resolv.conf", "/etc/resolv.conf")
56: @log.debug "'/etc/resolv.conf' uploaded."
57:
58: create_devices(guestfs)
59:
60: guestfs.mkdir("/data") if @appliance_config.is64bit?
61:
62: upload_fstab(guestfs)
63: enable_networking(guestfs)
64: upload_rc_local(guestfs)
65: add_ec2_user(guestfs)
66: change_configuration(guestfs_helper)
67: install_menu_lst(guestfs)
68:
69: enable_nosegneg_flag(guestfs) if @appliance_config.os.name == 'fedora'
70:
71: execute_post(guestfs_helper)
72: end
73:
74: @log.info "Image converted to EC2 format."
75: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 77
77: def execute_post(guestfs_helper)
78: unless @appliance_config.post['ec2'].nil?
79: @appliance_config.post['ec2'].each do |cmd|
80: guestfs_helper.sh(cmd, :arch => @appliance_config.hardware.arch)
81: end
82: @log.debug "Post commands from appliance definition file executed."
83: else
84: @log.debug "No commands specified, skipping."
85: end
86: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 77
77: def execute_post(guestfs_helper)
78: unless @appliance_config.post['ec2'].nil?
79: @appliance_config.post['ec2'].each do |cmd|
80: guestfs_helper.sh(cmd, :arch => @appliance_config.hardware.arch)
81: end
82: @log.debug "Post commands from appliance definition file executed."
83: else
84: @log.debug "No commands specified, skipping."
85: end
86: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 125
125: def install_menu_lst(guestfs)
126: @log.debug "Uploading '/boot/grub/menu.lst' file..."
127: menu_lst_data = File.open("#{File.dirname(__FILE__)}/src/menu.lst").read
128:
129: menu_lst_data.gsub!(/#TITLE#/, @appliance_config.name)
130: menu_lst_data.gsub!(/#KERNEL_VERSION#/, @linux_helper.kernel_version(guestfs))
131: menu_lst_data.gsub!(/#KERNEL_IMAGE_NAME#/, @linux_helper.kernel_image_name(guestfs))
132:
133: menu_lst = Tempfile.new('menu_lst')
134: menu_lst << menu_lst_data
135: menu_lst.flush
136:
137: guestfs.upload(menu_lst.path, "/boot/grub/menu.lst")
138:
139: menu_lst.close
140: @log.debug "'/boot/grub/menu.lst' file uploaded."
141: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 125
125: def install_menu_lst(guestfs)
126: @log.debug "Uploading '/boot/grub/menu.lst' file..."
127: menu_lst_data = File.open("#{File.dirname(__FILE__)}/src/menu.lst").read
128:
129: menu_lst_data.gsub!(/#TITLE#/, @appliance_config.name)
130: menu_lst_data.gsub!(/#KERNEL_VERSION#/, @linux_helper.kernel_version(guestfs))
131: menu_lst_data.gsub!(/#KERNEL_IMAGE_NAME#/, @linux_helper.kernel_image_name(guestfs))
132:
133: menu_lst = Tempfile.new('menu_lst')
134: menu_lst << menu_lst_data
135: menu_lst.flush
136:
137: guestfs.upload(menu_lst.path, "/boot/grub/menu.lst")
138:
139: menu_lst.close
140: @log.debug "'/boot/grub/menu.lst' file uploaded."
141: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 105
105: def upload_fstab(guestfs)
106: @log.debug "Uploading '/etc/fstab' file..."
107:
108: fstab_file = @appliance_config.is64bit? ? "#{File.dirname(__FILE__)}/src/fstab_64bit" : "#{File.dirname(__FILE__)}/src/fstab_32bit"
109:
110: fstab_data = File.open(fstab_file).read
111: fstab_data.gsub!(/#DISK_DEVICE_PREFIX#/, disk_device_prefix)
112: fstab_data.gsub!(/#FILESYSTEM_TYPE#/, @appliance_config.hardware.partitions['/']['type'])
113:
114: fstab = Tempfile.new('fstab')
115: fstab << fstab_data
116: fstab.flush
117:
118: guestfs.upload(fstab.path, "/etc/fstab")
119:
120: fstab.close
121:
122: @log.debug "'/etc/fstab' file uploaded."
123: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 105
105: def upload_fstab(guestfs)
106: @log.debug "Uploading '/etc/fstab' file..."
107:
108: fstab_file = @appliance_config.is64bit? ? "#{File.dirname(__FILE__)}/src/fstab_64bit" : "#{File.dirname(__FILE__)}/src/fstab_32bit"
109:
110: fstab_data = File.open(fstab_file).read
111: fstab_data.gsub!(/#DISK_DEVICE_PREFIX#/, disk_device_prefix)
112: fstab_data.gsub!(/#FILESYSTEM_TYPE#/, @appliance_config.hardware.partitions['/']['type'])
113:
114: fstab = Tempfile.new('fstab')
115: fstab << fstab_data
116: fstab.flush
117:
118: guestfs.upload(fstab.path, "/etc/fstab")
119:
120: fstab.close
121:
122: @log.debug "'/etc/fstab' file uploaded."
123: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 167
167: def upload_rc_local(guestfs)
168: @log.debug "Uploading '/etc/rc.d/rc.local' file..."
169: rc_local = Tempfile.new('rc_local')
170:
171: if guestfs.exists("/etc/rc.d/rc.local") == 1
172: # We're appending
173: rc_local << guestfs.read_file("/etc/rc.d/rc.local")
174: else
175: # We're creating new file
176: rc_local << "#!/bin/bash\n\n"
177: end
178:
179: rc_local << File.read("#{File.dirname(__FILE__)}/src/rc_local")
180: rc_local.flush
181:
182: guestfs.upload(rc_local.path, "/etc/rc.d/rc.local")
183:
184: rc_local.close
185:
186: # Fedora 16 doesn't have /etc/rc.local file and we need to
187: # enable rc.local compatibility with systemd
188: # We need to make sure that network is available when executing rc.local
189: if (@appliance_config.os.name == 'fedora' and @appliance_config.os.version >= '16')
190: guestfs.cp("/lib/systemd/system/rc-local.service", "/etc/systemd/system/")
191: guestfs.sh("sed -i '/^ConditionFileIsExecutable/a After=network.target' /etc/systemd/system/rc-local.service")
192: guestfs.sh("systemctl enable rc-local.service")
193: guestfs.ln_sf("/etc/rc.d/rc.local", "/etc/rc.local")
194: guestfs.chmod(0755, "/etc/rc.d/rc.local")
195: end
196:
197: @log.debug "'/etc/rc.d/rc.local' file uploaded."
198: end
# File lib/boxgrinder-build/plugins/platform/ec2/ec2-plugin.rb, line 167
167: def upload_rc_local(guestfs)
168: @log.debug "Uploading '/etc/rc.d/rc.local' file..."
169: rc_local = Tempfile.new('rc_local')
170:
171: if guestfs.exists("/etc/rc.d/rc.local") == 1
172: # We're appending
173: rc_local << guestfs.read_file("/etc/rc.d/rc.local")
174: else
175: # We're creating new file
176: rc_local << "#!/bin/bash\n\n"
177: end
178:
179: rc_local << File.read("#{File.dirname(__FILE__)}/src/rc_local")
180: rc_local.flush
181:
182: guestfs.upload(rc_local.path, "/etc/rc.d/rc.local")
183:
184: rc_local.close
185:
186: # Fedora 16 doesn't have /etc/rc.local file and we need to
187: # enable rc.local compatibility with systemd
188: # We need to make sure that network is available when executing rc.local
189: if (@appliance_config.os.name == 'fedora' and @appliance_config.os.version >= '16')
190: guestfs.cp("/lib/systemd/system/rc-local.service", "/etc/systemd/system/")
191: guestfs.sh("sed -i '/^ConditionFileIsExecutable/a After=network.target' /etc/systemd/system/rc-local.service")
192: guestfs.sh("systemctl enable rc-local.service")
193: guestfs.ln_sf("/etc/rc.d/rc.local", "/etc/rc.local")
194: guestfs.chmod(0755, "/etc/rc.d/rc.local")
195: end
196:
197: @log.debug "'/etc/rc.d/rc.local' file uploaded."
198: end