Module: Capistrano::Configuration::Resources::PlatformResources

Defined in:
lib/capistrano/configuration/resources/platform_resources.rb,
lib/capistrano/configuration/resources/platform_resources/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.extended(configuration) ⇒ Object



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
# File 'lib/capistrano/configuration/resources/platform_resources.rb', line 8

def self.extended(configuration)
  configuration.load {
    namespace(:platform) {
      _cset(:platform_identifier) { platform.identifier(fetch(:platform_identifier_options, {})) }
      def identifier(options={})
        capture((<<-EOS).gsub(/\s+/, " "), options).strip.to_sym
          if test -f /etc/debian_version; then
            if test -f /etc/lsb-release && grep -i -q DISTRIB_ID=Ubuntu /etc/lsb-release; then
              echo ubuntu;
            else
              echo debian;
            fi;
          elif test -f /etc/redhat-release; then
            if test -f /etc/centos-release; then
              echo centos;
            else
              echo redhat;
            fi;
          else
            echo unknown;
          fi;
        EOS
      end

      _cset(:platform_architecture) { platform.architecture(fetch(:platform_architecture_options, {})) }
      def architecture(options={})
        arch = capture("uname -m", options).strip.to_sym
        case arch
        when /^(i[3-6]86|pentium)$/ then :i386
        when /^(amd64|x86_64)$/     then :x86_64
        else
          arch.to_sym
        end
      end

      namespace(:packages) {
        def installed?(packages=[], options={})
          packages = [ packages ].flatten
          identifier = ( options.delete(:identifier) || fetch(:platform_identifier) )
          if packages.empty?
            true
          else
            not /not-installed/ =~ case identifier
              when :debian, :ubuntu
                capture("dpkg-query -s #{packages.map { |x| x.dump }.join(" ")} 1>/dev/null 2>&1 || echo not-installed")
              when :centos, :redhat
                capture("rpm -qi #{packages.map { |x| x.dump }.join(" ")} 1>/dev/null 2>&1 || echo not-installed")
              end
          end
        end

        def install(packages=[], options={})
          packages = [ packages ].flatten
          identifier = ( options.delete(:identifier) || fetch(:platform_identifier) )
          unless packages.empty?
            case identifier
            when :debian, :ubuntu
              sudo("apt-get install -q -y #{packages.map { |x| x.dump }.join(" ")}", options)
            when :centos, :redhat
              sudo("yum install -q -y #{packages.map { |x| x.dump }.join(" ")}", options)
            end
          end
        end
      }
    }
  }
end