| Class | BoxGrinder::RPMBasedOSPlugin |
| In: |
lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb
lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb |
| Parent: | BasePlugin |
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 27
27: def after_init
28: set_default_config_value('format', 'raw')
29:
30: register_deliverable(
31: :disk => "#{@appliance_config.name}-sda.#{@plugin_config['format']}",
32: :descriptor => "#{@appliance_config.name}.xml"
33: )
34:
35: @linux_helper = LinuxHelper.new(:log => @log)
36: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 27
27: def after_init
28: set_default_config_value('format', 'raw')
29:
30: register_deliverable(
31: :disk => "#{@appliance_config.name}-sda.#{@plugin_config['format']}",
32: :descriptor => "#{@appliance_config.name}.xml"
33: )
34:
35: @linux_helper = LinuxHelper.new(:log => @log)
36: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 193
193: def apply_root_password(guestfs)
194: @log.debug "Applying root password..."
195: guestfs.sh("/usr/bin/passwd -d root")
196: guestfs.sh("/usr/sbin/usermod -p '#{@appliance_config.os.password.crypt((0...8).map { 65.+(rand(25)).chr }.join)}' root")
197: @log.debug "Password applied."
198: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 193
193: def apply_root_password(guestfs)
194: @log.debug "Applying root password..."
195: guestfs.sh("/usr/bin/passwd -d root")
196: guestfs.sh("/usr/sbin/usermod -p '#{@appliance_config.os.password.crypt((0...8).map { 65.+(rand(25)).chr }.join)}' root")
197: @log.debug "Password applied."
198: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 67
67: def build_with_appliance_creator(appliance_definition_file, repos = {})
68: if File.extname(appliance_definition_file).eql?('.ks')
69: kickstart_file = appliance_definition_file
70: else
71: kickstart_file = Kickstart.new(@config, @appliance_config, repos, @dir, :log => @log).create
72: end
73:
74: RPMDependencyValidator.new(@config, @appliance_config, @dir, kickstart_file, @options).resolve_packages
75:
76: @log.info "Building #{@appliance_config.name} appliance..."
77:
78: execute_appliance_creator(kickstart_file)
79:
80: FileUtils.mv(Dir.glob("#{@dir.tmp}/#{@appliance_config.name}/*"), @dir.tmp)
81: FileUtils.rm_rf("#{@dir.tmp}/#{@appliance_config.name}/")
82:
83: @image_helper.customize([@deliverables.disk]) do |guestfs, guestfs_helper|
84: # TODO is this really needed?
85: @log.debug "Uploading '/etc/resolv.conf'..."
86: guestfs.upload("/etc/resolv.conf", "/etc/resolv.conf")
87: @log.debug "'/etc/resolv.conf' uploaded."
88:
89: change_configuration(guestfs_helper)
90: # TODO check if this is still required
91: apply_root_password(guestfs)
92: use_labels_for_partitions(guestfs)
93: disable_firewall(guestfs)
94: set_motd(guestfs)
95: install_repos(guestfs)
96:
97: guestfs.sh("chkconfig firstboot off") if guestfs.exists('/etc/init.d/firstboot') != 0
98:
99: @log.info "Executing post operations after build..."
100:
101: unless @appliance_config.post['base'].nil?
102: @appliance_config.post['base'].each do |cmd|
103: guestfs_helper.sh(cmd, :arch => @appliance_config.hardware.arch)
104: end
105: @log.debug "Post commands from appliance definition file executed."
106: else
107: @log.debug "No commands specified, skipping."
108: end
109:
110: yield guestfs, guestfs_helper if block_given?
111:
112: @log.info "Post operations executed."
113: end
114:
115: @log.info "Base image for #{@appliance_config.name} appliance was built successfully."
116: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 67
67: def build_with_appliance_creator(appliance_definition_file, repos = {})
68: if File.extname(appliance_definition_file).eql?('.ks')
69: kickstart_file = appliance_definition_file
70: else
71: kickstart_file = Kickstart.new(@config, @appliance_config, repos, @dir, :log => @log).create
72: end
73:
74: RPMDependencyValidator.new(@config, @appliance_config, @dir, kickstart_file, @options).resolve_packages
75:
76: @log.info "Building #{@appliance_config.name} appliance..."
77:
78: execute_appliance_creator(kickstart_file)
79:
80: FileUtils.mv(Dir.glob("#{@dir.tmp}/#{@appliance_config.name}/*"), @dir.tmp)
81: FileUtils.rm_rf("#{@dir.tmp}/#{@appliance_config.name}/")
82:
83: @image_helper.customize([@deliverables.disk]) do |guestfs, guestfs_helper|
84: # TODO is this really needed?
85: @log.debug "Uploading '/etc/resolv.conf'..."
86: guestfs.upload("/etc/resolv.conf", "/etc/resolv.conf")
87: @log.debug "'/etc/resolv.conf' uploaded."
88:
89: change_configuration(guestfs_helper)
90: # TODO check if this is still required
91: apply_root_password(guestfs)
92: use_labels_for_partitions(guestfs)
93: disable_firewall(guestfs)
94: set_motd(guestfs)
95: install_repos(guestfs)
96:
97: guestfs.sh("chkconfig firstboot off") if guestfs.exists('/etc/init.d/firstboot') != 0
98:
99: @log.info "Executing post operations after build..."
100:
101: unless @appliance_config.post['base'].nil?
102: @appliance_config.post['base'].each do |cmd|
103: guestfs_helper.sh(cmd, :arch => @appliance_config.hardware.arch)
104: end
105: @log.debug "Post commands from appliance definition file executed."
106: else
107: @log.debug "No commands specified, skipping."
108: end
109:
110: yield guestfs, guestfs_helper if block_given?
111:
112: @log.info "Post operations executed."
113: end
114:
115: @log.info "Base image for #{@appliance_config.name} appliance was built successfully."
116: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 200
200: def change_configuration(guestfs_helper)
201: guestfs_helper.augeas do
202: set('/etc/ssh/sshd_config', 'UseDNS', 'no')
203: set('/etc/sysconfig/selinux', 'SELINUX', 'permissive')
204: end
205: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 200
200: def change_configuration(guestfs_helper)
201: guestfs_helper.augeas do
202: set('/etc/ssh/sshd_config', 'UseDNS', 'no')
203: set('/etc/sysconfig/selinux', 'SELINUX', 'permissive')
204: end
205: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 127
127: def cleanup_after_appliance_creator(pid)
128: @log.debug "Sending TERM signal to process '#{pid}'..."
129: Process.kill("TERM", pid)
130:
131: @log.debug "Waiting for process to be terminated..."
132: Process.wait(pid)
133:
134: @log.debug "Cleaning appliance-creator mount points..."
135:
136: Dir["#{@dir.tmp}/imgcreate-*"].each do |dir|
137: dev_mapper = @exec_helper.execute "mount | grep #{dir} | awk '{print $1}'"
138:
139: mappings = {}
140:
141: dev_mapper.each do |mapping|
142: if mapping =~ /(loop\d+)p(\d+)/
143: mappings[$1] = [] if mappings[$1].nil?
144: mappings[$1] << $2 unless mappings[$1].include?($2)
145: end
146: end
147:
148: (['/var/cache/yum', '/dev/shm', '/dev/pts', '/proc', '/sys'] + @appliance_config.hardware.partitions.keys.reverse).each do |mount_point|
149: @log.trace "Unmounting '#{mount_point}'..."
150: @exec_helper.execute "umount -d #{dir}/install_root#{mount_point}"
151: end
152:
153: mappings.each do |loop, partitions|
154: @log.trace "Removing mappings from loop device #{loop}..."
155: @exec_helper.execute "/sbin/kpartx -d /dev/#{loop}"
156: @exec_helper.execute "losetup -d /dev/#{loop}"
157:
158: partitions.each do |part|
159: @log.trace "Removing mapping for partition #{part} from loop device #{loop}..."
160: @exec_helper.execute "rm /dev/#{loop}#{part}"
161: end
162: end
163: end
164:
165: @log.debug "Cleaned up after appliance-creator."
166: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 127
127: def cleanup_after_appliance_creator(pid)
128: @log.debug "Sending TERM signal to process '#{pid}'..."
129: Process.kill("TERM", pid)
130:
131: @log.debug "Waiting for process to be terminated..."
132: Process.wait(pid)
133:
134: @log.debug "Cleaning appliance-creator mount points..."
135:
136: Dir["#{@dir.tmp}/imgcreate-*"].each do |dir|
137: dev_mapper = @exec_helper.execute "mount | grep #{dir} | awk '{print $1}'"
138:
139: mappings = {}
140:
141: dev_mapper.each do |mapping|
142: if mapping =~ /(loop\d+)p(\d+)/
143: mappings[$1] = [] if mappings[$1].nil?
144: mappings[$1] << $2 unless mappings[$1].include?($2)
145: end
146: end
147:
148: (['/var/cache/yum', '/dev/shm', '/dev/pts', '/proc', '/sys'] + @appliance_config.hardware.partitions.keys.reverse).each do |mount_point|
149: @log.trace "Unmounting '#{mount_point}'..."
150: @exec_helper.execute "umount -d #{dir}/install_root#{mount_point}"
151: end
152:
153: mappings.each do |loop, partitions|
154: @log.trace "Removing mappings from loop device #{loop}..."
155: @exec_helper.execute "/sbin/kpartx -d /dev/#{loop}"
156: @exec_helper.execute "losetup -d /dev/#{loop}"
157:
158: partitions.each do |part|
159: @log.trace "Removing mapping for partition #{part} from loop device #{loop}..."
160: @exec_helper.execute "rm /dev/#{loop}#{part}"
161: end
162: end
163: end
164:
165: @log.debug "Cleaned up after appliance-creator."
166: end
issues.jboss.org/browse/BGBUILD-177
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 169
169: def disable_firewall(guestfs)
170: @log.debug "Disabling firewall..."
171: guestfs.sh("lokkit -q --disabled")
172: @log.debug "Firewall disabled."
173: end
issues.jboss.org/browse/BGBUILD-177
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 169
169: def disable_firewall(guestfs)
170: @log.debug "Disabling firewall..."
171: guestfs.sh("lokkit -q --disabled")
172: @log.debug "Firewall disabled."
173: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 118
118: def execute_appliance_creator(kickstart_file)
119: begin
120: @exec_helper.execute "appliance-creator -d -v -t '#{@dir.tmp}' --cache=#{@config.dir.cache}/rpms-cache/#{@appliance_config.path.main} --config '#{kickstart_file}' -o '#{@dir.tmp}' --name '#{@appliance_config.name}' --vmem #{@appliance_config.hardware.memory} --vcpu #{@appliance_config.hardware.cpus} --format #{@plugin_config['format']}"
121: rescue InterruptionError => e
122: cleanup_after_appliance_creator(e.pid)
123: abort
124: end
125: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 118
118: def execute_appliance_creator(kickstart_file)
119: begin
120: @exec_helper.execute "appliance-creator -d -v -t '#{@dir.tmp}' --cache=#{@config.dir.cache}/rpms-cache/#{@appliance_config.path.main} --config '#{kickstart_file}' -o '#{@dir.tmp}' --name '#{@appliance_config.name}' --vmem #{@appliance_config.hardware.memory} --vcpu #{@appliance_config.hardware.cpus} --format #{@plugin_config['format']}"
121: rescue InterruptionError => e
122: cleanup_after_appliance_creator(e.pid)
123: abort
124: end
125: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 224
224: def install_repos(guestfs)
225: @log.debug "Installing repositories from appliance definition file..."
226: @appliance_config.repos.each do |repo|
227: if repo['ephemeral']
228: @log.debug "Repository '#{repo['name']}' is an ephemeral repo. It'll not be installed in the appliance."
229: next
230: end
231:
232: @log.debug "Installing #{repo['name']} repo..."
233: repo_file = File.read("#{File.dirname(__FILE__)}/src/base.repo").gsub(/#NAME#/, repo['name'])
234:
235: ['baseurl', 'mirrorlist'].each do |type|
236: repo_file << ("#{type}=#{repo[type]}\n") unless repo[type].nil?
237: end
238:
239: guestfs.write_file("/etc/yum.repos.d/#{repo['name']}.repo", repo_file, 0)
240: end
241: @log.debug "Repositories installed."
242: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 224
224: def install_repos(guestfs)
225: @log.debug "Installing repositories from appliance definition file..."
226: @appliance_config.repos.each do |repo|
227: if repo['ephemeral']
228: @log.debug "Repository '#{repo['name']}' is an ephemeral repo. It'll not be installed in the appliance."
229: next
230: end
231:
232: @log.debug "Installing #{repo['name']} repo..."
233: repo_file = File.read("#{File.dirname(__FILE__)}/src/base.repo").gsub(/#NAME#/, repo['name'])
234:
235: ['baseurl', 'mirrorlist'].each do |type|
236: repo_file << ("#{type}=#{repo[type]}\n") unless repo[type].nil?
237: end
238:
239: guestfs.write_file("/etc/yum.repos.d/#{repo['name']}.repo", repo_file, 0)
240: end
241: @log.debug "Repositories installed."
242: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 38
38: def read_file(file)
39: read_kickstart(file) if File.extname(file).eql?('.ks')
40: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 38
38: def read_file(file)
39: read_kickstart(file) if File.extname(file).eql?('.ks')
40: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 42
42: def read_kickstart(file)
43: appliance_config = ApplianceConfig.new
44:
45: appliance_config.name = File.basename(file, '.ks')
46:
47: name = nil
48: version = nil
49:
50: File.read(file).each do |line|
51: n = line.scan(/^# bg_os_name: (.*)/).flatten.first
52: v = line.scan(/^# bg_os_version: (.*)/).flatten.first
53:
54: name = n unless n.nil?
55: version = v unless v.nil?
56: end
57:
58: raise "No operating system name specified, please add comment to you kickstrt file like this: # bg_os_name: fedora" if name.nil?
59: raise "No operating system version specified, please add comment to you kickstrt file like this: # bg_os_version: 14" if version.nil?
60:
61: appliance_config.os.name = name
62: appliance_config.os.version = version
63:
64: appliance_config
65: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 42
42: def read_kickstart(file)
43: appliance_config = ApplianceConfig.new
44:
45: appliance_config.name = File.basename(file, '.ks')
46:
47: name = nil
48: version = nil
49:
50: File.read(file).each do |line|
51: n = line.scan(/^# bg_os_name: (.*)/).flatten.first
52: v = line.scan(/^# bg_os_version: (.*)/).flatten.first
53:
54: name = n unless n.nil?
55: version = v unless v.nil?
56: end
57:
58: raise "No operating system name specified, please add comment to you kickstrt file like this: # bg_os_name: fedora" if name.nil?
59: raise "No operating system version specified, please add comment to you kickstrt file like this: # bg_os_version: 14" if version.nil?
60:
61: appliance_config.os.name = name
62: appliance_config.os.version = version
63:
64: appliance_config
65: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 189
189: def read_label(guestfs, partition)
190: (guestfs.respond_to?(:vfs_label) ? guestfs.vfs_label(partition) : guestfs.sh("/sbin/e2label #{partition}").chomp.strip).gsub('_', '')
191: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 189
189: def read_label(guestfs, partition)
190: (guestfs.respond_to?(:vfs_label) ? guestfs.vfs_label(partition) : guestfs.sh("/sbin/e2label #{partition}").chomp.strip).gsub('_', '')
191: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 220
220: def recreate_kernel_image(guestfs, modules = [])
221: @linux_helper.recreate_kernel_image(guestfs, modules)
222: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 220
220: def recreate_kernel_image(guestfs, modules = [])
221: @linux_helper.recreate_kernel_image(guestfs, modules)
222: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 207
207: def set_motd(guestfs)
208: @log.debug "Setting up '/etc/motd'..."
209: # set nice banner for SSH
210: motd_file = "/etc/init.d/motd"
211: guestfs.upload("#{File.dirname(__FILE__)}/src/motd.init", motd_file)
212: guestfs.sh("sed -i s/#VERSION#/'#{@appliance_config.version}.#{@appliance_config.release}'/ #{motd_file}")
213: guestfs.sh("sed -i s/#APPLIANCE#/'#{@appliance_config.name} appliance'/ #{motd_file}")
214:
215: guestfs.sh("/bin/chmod +x #{motd_file}")
216: guestfs.sh("/sbin/chkconfig --add motd")
217: @log.debug "'/etc/motd' is nice now."
218: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 207
207: def set_motd(guestfs)
208: @log.debug "Setting up '/etc/motd'..."
209: # set nice banner for SSH
210: motd_file = "/etc/init.d/motd"
211: guestfs.upload("#{File.dirname(__FILE__)}/src/motd.init", motd_file)
212: guestfs.sh("sed -i s/#VERSION#/'#{@appliance_config.version}.#{@appliance_config.release}'/ #{motd_file}")
213: guestfs.sh("sed -i s/#APPLIANCE#/'#{@appliance_config.name} appliance'/ #{motd_file}")
214:
215: guestfs.sh("/bin/chmod +x #{motd_file}")
216: guestfs.sh("/sbin/chkconfig --add motd")
217: @log.debug "'/etc/motd' is nice now."
218: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 175
175: def use_labels_for_partitions(guestfs)
176: device = guestfs.list_devices.first
177:
178: # /etc/fstab
179: if fstab = guestfs.read_file('/etc/fstab').gsub!(/^(\/dev\/sda.)/) { |path| "LABEL=#{read_label(guestfs, path.gsub('/dev/sda', device))}" }
180: guestfs.write_file('/etc/fstab', fstab, 0)
181: end
182:
183: # /boot/grub/grub.conf
184: if grub = guestfs.read_file('/boot/grub/grub.conf').gsub!(/(\/dev\/sda.)/) { |path| "LABEL=#{read_label(guestfs, path.gsub('/dev/sda', device))}" }
185: guestfs.write_file('/boot/grub/grub.conf', grub, 0)
186: end
187: end
# File lib/boxgrinder-build/plugins/os/rpm-based/rpm-based-os-plugin.rb, line 175
175: def use_labels_for_partitions(guestfs)
176: device = guestfs.list_devices.first
177:
178: # /etc/fstab
179: if fstab = guestfs.read_file('/etc/fstab').gsub!(/^(\/dev\/sda.)/) { |path| "LABEL=#{read_label(guestfs, path.gsub('/dev/sda', device))}" }
180: guestfs.write_file('/etc/fstab', fstab, 0)
181: end
182:
183: # /boot/grub/grub.conf
184: if grub = guestfs.read_file('/boot/grub/grub.conf').gsub!(/(\/dev\/sda.)/) { |path| "LABEL=#{read_label(guestfs, path.gsub('/dev/sda', device))}" }
185: guestfs.write_file('/boot/grub/grub.conf', grub, 0)
186: end
187: end