seancribbs (owner)

Forks

Revisions

gist: 134930 Download_button fork
public
Description:
Definition and library for Chef, allowing automatic installation of packages from source tarballs.
Public Clone URL: git://gist.github.com/134930.git
Embed All Files: show embed
autoconf.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
source_package.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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