Class: Bunchr::Packages

Inherits:
Rake::TaskLib
  • Object
show all
Includes:
Utils
Defined in:
lib/bunchr/packages.rb

Constant Summary collapse

RPM_PLATFORMS =

only attempt to build .rpm’s on these platforms (as reported by ohai.platform)

%w[centos redhat fedora scientific suse]
DEB_PLATFORMS =

only attempt to build .deb’s on these platforms (as reported by ohai.platform)

%w[debian ubuntu]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#logger, #ohai, #sh

Constructor Details

#initialize {|_self| ... } ⇒ Packages

Returns a new instance of Packages.

Yields:

  • (_self)

Yield Parameters:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bunchr/packages.rb', line 23

def initialize
  @name = nil
  @version = nil
  @iteration = nil
  @arch = ohai['kernel']['machine']
  @license = nil
  @vendor = nil
  @category = nil
  @url = nil
  @description = nil
  @files = []
  @config_files = []
  @scripts = {}
  yield self if block_given?
  define unless name.nil? or version.nil?
end

Instance Attribute Details

#categoryObject

Returns the value of attribute category.



19
20
21
# File 'lib/bunchr/packages.rb', line 19

def category
  @category
end

#config_filesObject

Returns the value of attribute config_files.



21
22
23
# File 'lib/bunchr/packages.rb', line 21

def config_files
  @config_files
end

#descriptionObject

Returns the value of attribute description.



20
21
22
# File 'lib/bunchr/packages.rb', line 20

def description
  @description
end

#filesObject

Returns the value of attribute files.



21
22
23
# File 'lib/bunchr/packages.rb', line 21

def files
  @files
end

#iterationObject

Returns the value of attribute iteration.



19
20
21
# File 'lib/bunchr/packages.rb', line 19

def iteration
  @iteration
end

#licenseObject

Returns the value of attribute license.



19
20
21
# File 'lib/bunchr/packages.rb', line 19

def license
  @license
end

#nameObject

Returns the value of attribute name.



19
20
21
# File 'lib/bunchr/packages.rb', line 19

def name
  @name
end

#scriptsObject

Returns the value of attribute scripts.



21
22
23
# File 'lib/bunchr/packages.rb', line 21

def scripts
  @scripts
end

#urlObject

Returns the value of attribute url.



19
20
21
# File 'lib/bunchr/packages.rb', line 19

def url
  @url
end

#vendorObject

Returns the value of attribute vendor.



19
20
21
# File 'lib/bunchr/packages.rb', line 19

def vendor
  @vendor
end

#versionObject

Returns the value of attribute version.



19
20
21
# File 'lib/bunchr/packages.rb', line 19

def version
  @version
end

Instance Method Details

#archObject

returns the current architecture. Convert some architectures for the underlying packaging systems, such as i686 to i386.



43
44
45
46
47
48
49
50
# File 'lib/bunchr/packages.rb', line 43

def arch
  case @arch
  when 'i686', 'i386'
    'i386'
  else
    @arch
  end
end

#arch=(a) ⇒ Object

explicitly set @arch to ‘a`



53
54
55
# File 'lib/bunchr/packages.rb', line 53

def arch=(a)
  @arch = a
end

#defineObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bunchr/packages.rb', line 57

def define
  logger.debug "Defining tasks for package:#{name} #{version}"

  namespace 'packages' do
    namespace name do
      define_build_tarball
      define_build_rpm
      define_build_deb
      # TODO-future: build solaris pkgs, windows too?!

      define_build_all
      
      task :done    => "#{name}:build"
      task :default => "#{name}:done"
    end
    desc "Create bunchr packages for #{name} #{version}-#{iteration}"
    task name => "#{name}:default"
  end
end

#define_build_allObject



144
145
146
147
# File 'lib/bunchr/packages.rb', line 144

def define_build_all
  desc "Build all packages: #{name}-#{version}-#{iteration}-#{arch}"
  task :build => [:build_tarball, :build_rpm, :build_deb]
end

#define_build_debObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/bunchr/packages.rb', line 117

def define_build_deb
  desc "Build deb: #{name}-#{version}-#{iteration}-#{arch}"
  task :build_deb do

    if DEB_PLATFORMS.include? ohai.platform
      logger.info "Building DEB '#{name}-#{version}-#{iteration}-#{arch}'"

      sh "fpm -s dir -t deb -a #{arch} -n #{name} -v #{version} \
          --iteration #{iteration}                              \
          --url         '#{url}'                                \
          --description '#{description}'                        \
          --license     '#{license}'                            \
          --vendor      '#{vendor}'                             \
          --category    '#{category}'                           \
          #{fpm_scripts_args}                                   \
          #{fpm_config_files_args}                              \
          #{config_files.join(' ')}                             \
          #{files.join(' ')}"

      logger.info "DEB built."
    else
      logger.info "Not building DEB, platform [#{ohai.platform}] does not support it."
    end

  end
end

#define_build_rpmObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/bunchr/packages.rb', line 90

def define_build_rpm
  desc "Build RPM: #{name}-#{version}-#{iteration}-#{arch}"
  task :build_rpm do

    if RPM_PLATFORMS.include? ohai.platform
      logger.info "Building RPM '#{name}-#{version}-#{iteration}-#{arch}'"

      sh "fpm -s dir -t rpm -a #{arch} -n #{name} -v #{version} \
          --iteration #{iteration}                              \
          --url         '#{url}'                                \
          --description '#{description}'                        \
          --license     '#{license}'                            \
          --vendor      '#{vendor}'                             \
          --category    '#{category}'                           \
          #{fpm_scripts_args}                                   \
          #{fpm_config_files_args}                              \
          #{config_files.join(' ')}                             \
          #{files.join(' ')}"

      logger.info "RPM built."
    else
      logger.info "Not building RPM, platform [#{ohai.platform}] does not support it."
    end
    
  end
end

#define_build_tarballObject



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bunchr/packages.rb', line 77

def define_build_tarball
  tarball_name = "#{name}-#{version}-#{iteration}-#{arch}.tar.gz"

  desc "Build tarball: #{tarball_name}"
  task :build_tarball do

    logger.info "Building tarball '#{tarball_name}'"

    files_str = files.join(' ') + ' ' + config_files.join(' ')
    sh "tar czf #{tarball_name} #{files_str}"
  end
end

#fpm_config_files_argsObject

return an argument string for fpm with ‘–config-files’ prefixed to every file in the config_files array.

eg: '--config-files /etc/file1 --config-files /etc/file2'


161
162
163
# File 'lib/bunchr/packages.rb', line 161

def fpm_config_files_args
  config_files.map { |f| '--config-files ' + f }.join(' ')
end

#fpm_scripts_argsObject

returns an argument string for fpm with files from the scripts hash, eg:

scripts[:after_install] = 'file1'
scripts[:before_remove] = 'file2'
fpm_scripts_args() # => '--after-install file1 --before-remove file2'


169
170
171
172
173
# File 'lib/bunchr/packages.rb', line 169

def fpm_scripts_args
  scripts.map do |k,v|
    "--#{k.to_s.tr('_','-')} #{v}" << ' '
  end.join(' ')
end

#include_software(other_dependency) ⇒ Object

depend on a Software object to be built and installed



150
151
152
153
154
155
156
# File 'lib/bunchr/packages.rb', line 150

def include_software(other_dependency)
  namespace 'packages' do
    namespace name do
      task :build => "software:#{other_dependency}:done"
    end
  end
end