vagrant-lxc-ng/tasks/boxes.v2.rake

182 lines
5 KiB
Ruby
Raw Permalink Normal View History

2013-05-06 13:42:30 +00:00
require 'pathname'
2013-04-22 00:31:22 +00:00
require 'rake/tasklib'
class BuildGenericBoxTaskV2 < ::Rake::TaskLib
2013-04-22 00:31:22 +00:00
include ::Rake::DSL
attr_reader :name
def initialize(name, distrib, release, arch, opts = {})
@name = name
@distrib = distrib
@release = release.to_s
@arch = arch.to_s
@install_chef = opts.fetch(:chef, false)
@install_puppet = opts.fetch(:puppet, true)
@install_babushka = opts.fetch(:babushka, true)
@file = opts[:file] || default_box_file
2013-05-05 13:57:12 +00:00
@scripts_path = Pathname(Dir.pwd).join('boxes')
desc "Build an #{distrib.upcase} #{release} #{arch} box" unless
::Rake.application.last_comment
2013-04-22 00:31:22 +00:00
task name do
RakeFileUtils.send(:verbose, true) do
2013-05-05 13:57:12 +00:00
build
end
2013-04-22 00:31:22 +00:00
end
end
2013-05-05 13:57:12 +00:00
def default_box_file
require 'time'
"lxc-#{@release}-#{@arch}-#{Date.today}.box"
end
def run(script_name, *args)
unless (script = @scripts_path.join(@distrib, script_name)).readable?
script = @scripts_path.join('common', script_name)
2013-04-22 00:31:22 +00:00
end
2013-05-05 13:57:12 +00:00
if script.readable?
sh "sudo #{script} #{args.join(' ')}"
else
2013-06-08 00:32:48 +00:00
STDERR.puts "cannot execute #{script_name} (not found?)"
2013-04-22 00:59:24 +00:00
exit 1
end
2013-05-05 13:57:12 +00:00
end
def build
check_if_box_has_been_built!
FileUtils.mkdir_p 'boxes/temp' unless File.exist? 'base/temp'
check_for_partially_built_box!
2013-04-22 00:59:24 +00:00
2013-04-30 13:45:27 +00:00
pwd = Dir.pwd
2013-04-22 00:31:22 +00:00
sh 'mkdir -p boxes/temp/'
Dir.chdir 'boxes/temp' do
2013-05-05 13:57:12 +00:00
download
install_cfg_engines
prepare_package_contents pwd
2013-05-06 13:42:30 +00:00
sh 'sudo rm -rf rootfs'
2013-04-22 00:59:24 +00:00
sh "tar -czf tmp-package.box ./*"
2013-04-22 00:31:22 +00:00
end
2013-04-22 00:59:24 +00:00
sh 'mkdir -p boxes/output'
2013-04-22 00:59:24 +00:00
sh "cp boxes/temp/tmp-package.box boxes/output/#{@file}"
sh "rm -rf boxes/temp"
end
2013-05-05 13:57:12 +00:00
def check_if_box_has_been_built!
return unless File.exists?("./boxes/output/#{@file}")
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)
2013-05-06 13:42:30 +00:00
run 'cleanup'
2013-05-05 13:57:12 +00:00
sh 'sudo rm -f rootfs.tar.gz'
sh 'sudo tar --numeric-owner -czf rootfs.tar.gz ./rootfs/*'
2013-05-30 17:56:01 +00:00
sh "sudo chown #{ENV['USER']}:#{`id -gn`.strip} rootfs.tar.gz"
2013-05-05 13:57:12 +00:00
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
2013-04-22 00:31:22 +00:00
end
class BuildDebianBoxTaskV2 < BuildGenericBoxTaskV2
def initialize(name, release, arch, opts = {})
super(name, 'debian', release, arch, opts)
end
end
class BuildUbuntuBoxTaskV2 < BuildGenericBoxTaskV2
def initialize(name, release, arch, opts = {})
super(name, 'ubuntu', release, arch, opts)
end
end
chef = ENV['CHEF'] == '1'
puppet = ENV['PUPPET'] == '1'
babushka = ENV['BABUSHKA'] == '1'
2013-04-22 00:31:22 +00:00
namespace :boxes do
namespace :v2 do
namespace :ubuntu do
namespace :build do
desc 'Build an Ubuntu Precise 64 bits box'
BuildUbuntuBoxTaskV2.
new(:precise64,
:precise, 'amd64', chef: chef, puppet: puppet, babushka: babushka)
desc 'Build an Ubuntu Quantal 64 bits box'
BuildUbuntuBoxTaskV2.
new(:quantal64,
:quantal, 'amd64', chef: chef, puppet: puppet, babushka: babushka)
# FIXME: Find out how to install chef on raring
desc 'Build an Ubuntu Raring 64 bits box'
BuildUbuntuBoxTaskV2.
new(:raring64,
2013-06-08 00:40:39 +00:00
:raring, 'amd64', chef: chef, puppet: puppet, babushka: babushka)
desc 'Build all Ubuntu boxes'
task :all => %w( precise64 quantal64 raring64 )
end
end
# FIXME: Find out how to install chef on debian boxes
namespace :debian do
namespace :build do
desc 'Build an Debian Squeeze 64 bits box'
BuildDebianBoxTaskV2.
new(:squeeze64,
:squeeze, 'amd64', chef: false, puppet: puppet, babushka: babushka)
desc 'Build an Debian Wheezy 64 bits box'
BuildDebianBoxTaskV2.
new(:wheezy64,
:wheezy, 'amd64', chef: false, puppet: puppet, babushka: babushka)
desc 'Build an Debian Sid/unstable 64 bits box'
BuildDebianBoxTaskV2.
new(:sid64,
:sid, 'amd64', chef: false, puppet: puppet, babushka: babushka)
desc 'Build all Debian boxes'
task :all => %w( squeeze64 wheezy64 sid64 )
end
end
2013-05-05 14:11:49 +00:00
desc 'Build all base boxes for release'
task :build_all => %w( ubuntu:build:all debian:build:all )
end
end