Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Definition and library for Chef, allowing automatic installation of packages from source tarballs.
module Autoconf
extend self
def switches(config=nil)
case config
when String
switch(config)
when Enumerable
config.map {|item| switch(item) }.join(" ").strip
else
""
end
end
private
def switch(*items)
items = items.flatten
case items.size
when 1
plain_switch(items.first)
when 2
value_switch(items.first, items.last)
else
""
end
end
def plain_switch(sw)
if sw.respond_to?(:to_s)
if sw.to_s =~ /^\s*(-|$)/
sw
else
"--#{sw}"
end
else
""
end
end
def value_switch(key, value)
if value && key.respond_to?(:to_s) && value.respond_to?(:to_s)
value == true ? plain_switch(key) : "--#{key}=#{value}"
else
""
end
end
end
if $0 == __FILE__
require 'rubygems'
require 'spec/autorun'
describe "Autoconf" do
describe "switches" do
it "should return an empty string by default" do
Autoconf.switches.should == ""
end
it "should process an empty string untouched" do
Autoconf.switches("").should == ""
end
it "should process a string without embedded switches as prefixed with --" do
Autoconf.switches("debug").should == "--debug"
end
it "should process a string with embedded switches untouched" do
Autoconf.switches("--debug").should == "--debug"
end
it "should process a hash as switches joined with =" do
Autoconf.switches(:prefix => "/usr/local").should == "--prefix=/usr/local"
end
it "should process a hash with true values as singleton switches" do
Autoconf.switches('with-ssl' => true, 'with-ssl-proxy' => true).should == "--with-ssl --with-ssl-proxy"
end
it "should process a hash with nil or false values ignoring those switches" do
Autoconf.switches('debug' => false, 'with-poll' => nil).should == ""
end
it "should process an array with nil values ignoring them" do
Autoconf.switches([nil]).should == ""
Autoconf.switches(["debug", nil]).should == "--debug"
end
end
end
end
# Installs packages from source
define :source_package, :action => :install, :configure => true,
:build_command => "make", :install_command => "make install" do
%w{tar gzip bzip2 unzip}.each do |p|
package p
end
directory "/usr/local/src" do
recursive true
end
directory "/etc/source-packages" do
recursive true
end
%w{configured built installed}.each do |stage|
file "/etc/source-packages/#{basename}-#{stage}" do
action :nothing
end
end
filename = params[:url].split("/").last
basename, extension = $1, $2.downcase if filename =~ /(.*)(\.tgz|\.tar(?:\.gz|\.bz(?:ip)?2)?|\.zip)$/i
unpack_target = params[:unpacks_to] || basename
unpack_target_dir = "/usr/local/src/#{unpack_target}"
unzip_command ||= case
when params[:type] == :gzip || [".tar.gz", ".tgz"].include?(extension)
"tar xzf"
when params[:type] == :bzip2 || extension =~ /\.bz(ip)?2/
"tar xjf"
when params[:type] == :zip || extension == ".zip"
params[:zip_includes_directory] ? "unzip" : "unzip -d #{unpack_target_dir}"
end
if [:download, :unpack, :configure, :build, :install].include? params[:action]
remote_file "/usr/local/src/#{filename}" do
source params[:url]
end
end
if [:unpack, :configure, :build, :install].include? params[:action]
execute "Unpack source package for #{params[:name]}" do
cwd "/usr/local/src"
creates unpack_target_dir unless params[:force]
command "#{unzip_command} #{filename}"
end
end
if [:configure, :build, :install].include? params[:action]
execute "Configure source package for #{params[:name]}" do
command "./configure #{Autoconf.switches(params[:configure])}"
cwd unpack_target_dir
environment params[:environment] if params[:environment]
creates "/etc/source-packages/#{basename}-configured" unless params[:force]
only_if params[:configure]
notifies :touch, resources(:file => "/etc/source-packages/#{basename}-configured"), :immediate
end
end
if [:build, :install].include? params[:action]
execute "Build source package for #{params[:name]}" do
cwd unpack_target_dir
environment params[:environment] if params[:environment]
command params[:build_command]
creates "/etc/source-packages/#{basename}-built" unless params[:force]
notifies :touch, resources(:file => "/etc/source-packages/#{basename}-built"), :immediate
end
end
if params[:action] == :install
execute "Install source package for #{params[:name]}" do
cwd unpack_target_dir
command params[:install_command]
environment params[:environment] if params[:environment]
creates "/etc/source-packages/#{basename}-installed" unless params[:force]
notifies :touch, resources(:file => "/etc/source-packages/#{basename}-installed"), :immediate
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment