Clean up box packaging rake task

This commit is contained in:
Fabio Rehm 2013-05-05 10:57:12 -03:00
parent f141443564
commit f07179e582
2 changed files with 78 additions and 36 deletions

7
boxes/common/cleanup Executable file
View file

@ -0,0 +1,7 @@
#!/bin/bash
cache=`readlink -f .`
rootfs="${cache}/rootfs"
rm -rf $rootfs/tmp/*
chroot $rootfs apt-get clean

View file

@ -14,56 +14,48 @@ class BuildGenericBoxTask < ::Rake::TaskLib
@install_puppet = opts.fetch(:puppet, true) @install_puppet = opts.fetch(:puppet, true)
@install_babushka = opts.fetch(:babushka, true) @install_babushka = opts.fetch(:babushka, true)
@file = opts[:file] || default_box_file @file = opts[:file] || default_box_file
@scripts_path = Pathname(Dir.pwd).join('boxes')
desc "Build an #{distrib.upcase} #{release} #{arch} box" unless desc "Build an #{distrib.upcase} #{release} #{arch} box" unless
::Rake.application.last_comment ::Rake.application.last_comment
task name do task name do
RakeFileUtils.send(:verbose, true) do RakeFileUtils.send(:verbose, true) do
run_task build
end end
end end
end end
def run_task def default_box_file
if File.exists?("./boxes/output/#{@file}") require 'time'
puts 'Box has been built already!' "lxc-#{@release}-#{@arch}-#{Date.today}.box"
exit 1 end
def run(script_name, *args)
unless (script = @scripts_path.join(@distrib, script_name)).readable?
script = @scripts_path.join('common', script_name)
end end
FileUtils.mkdir_p 'boxes/temp' unless File.exist? 'base/temp' if script.readable?
if Dir.entries('boxes/temp').size > 2 sh "sudo #{script} #{args.join(' ')}"
puts 'There is a partially built box under ' + else
File.expand_path('./boxes/temp') + STDERR.puts "cannot execute #{install_path} (not found?)"
', please remove it before building a new box'
exit 1 exit 1
end end
end
def build
check_if_box_has_been_built!
FileUtils.mkdir_p 'boxes/temp' unless File.exist? 'base/temp'
check_for_partially_built_box!
pwd = Dir.pwd pwd = Dir.pwd
sh 'mkdir -p boxes/temp/' sh 'mkdir -p boxes/temp/'
Dir.chdir 'boxes/temp' do Dir.chdir 'boxes/temp' do
sh "sudo #{pwd}/boxes/#{@distrib}/download #{@arch} #{@release}" download
[ :puppet, :chef, :babushka ].each do |cfg_engine| install_cfg_engines
next unless instance_variable_get :"@install_#{cfg_engine}" prepare_package_contents pwd
script_name = "install-#{cfg_engine}" cleanup
install_path = File.join pwd, 'boxes', @distrib, script_name
unless File.readable? install_path
install_path = File.join pwd, 'boxes', 'common', script_name
end
if File.readable? install_path
sh "sudo #{install_path}"
else
STDERR.puts "cannot execute #{install_path} (not found?)"
end
end
sh 'sudo rm -f rootfs.tar.gz'
sh 'sudo tar --numeric-owner -czf rootfs.tar.gz ./rootfs/*'
sh 'sudo rm -rf rootfs'
sh "sudo chown #{ENV['USER']}:#{ENV['USER']} rootfs.tar.gz"
sh "cp #{pwd}/boxes/#{@distrib}/lxc-template ."
metadata = File.read("#{pwd}/boxes/#{@distrib}/metadata.json.template")
metadata.gsub!('ARCH', @arch)
metadata.gsub!('RELEASE', @release)
File.open('metadata.json', 'w') { |f| f.print metadata }
sh "tar -czf tmp-package.box ./*" sh "tar -czf tmp-package.box ./*"
end end
@ -72,9 +64,52 @@ class BuildGenericBoxTask < ::Rake::TaskLib
sh "rm -rf boxes/temp" sh "rm -rf boxes/temp"
end end
def default_box_file def check_if_box_has_been_built!
require 'time' return unless File.exists?("./boxes/output/#{@file}")
"lxc-#{@release}-#{@arch}-#{Date.today}.box"
puts 'Box has been built already!'
exit 1
end
def check_for_partially_built_box!
return unless Dir.entries('boxes/temp').size > 2
puts 'There is a partially built box under ' +
File.expand_path('./boxes/temp') +
', please remove it before building a new box'
exit 1
end
def download
run 'download', @arch, @release
end
def install_cfg_engines
[ :puppet, :chef, :babushka ].each do |cfg_engine|
next unless instance_variable_get :"@install_#{cfg_engine}"
script_name = "install-#{cfg_engine}"
run script_name
end
end
def prepare_package_contents(pwd)
sh 'sudo rm -f rootfs.tar.gz'
sh 'sudo tar --numeric-owner -czf rootfs.tar.gz ./rootfs/*'
sh "sudo chown #{ENV['USER']}:#{ENV['USER']} rootfs.tar.gz"
sh "cp #{pwd}/boxes/#{@distrib}/lxc-template ."
compile_metadata(pwd)
end
def compile_metadata(pwd)
metadata = File.read("#{pwd}/boxes/#{@distrib}/metadata.json.template")
metadata.gsub!('ARCH', @arch)
metadata.gsub!('RELEASE', @release)
File.open('metadata.json', 'w') { |f| f.print metadata }
end
def cleanup
run 'cleanup'
sh 'sudo rm -rf rootfs'
end end
end end