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)

%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 raspbian]
PKG_PLATFORMS =

only attempt to build .pkg's on these platforms (as reported by ohai)

%w[freebsd]

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:


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bunchr/packages.rb', line 27

def initialize
  @name = nil
  @version = nil
  @iteration = nil
  @arch = ohai[:kernel][:machine]
  @license = nil
  @vendor = nil
  @maintainer = 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


23
24
25
# File 'lib/bunchr/packages.rb', line 23

def category
  @category
end

#config_filesObject

Returns the value of attribute config_files


25
26
27
# File 'lib/bunchr/packages.rb', line 25

def config_files
  @config_files
end

#descriptionObject

Returns the value of attribute description


24
25
26
# File 'lib/bunchr/packages.rb', line 24

def description
  @description
end

#filesObject

Returns the value of attribute files


25
26
27
# File 'lib/bunchr/packages.rb', line 25

def files
  @files
end

#iterationObject

Returns the value of attribute iteration


23
24
25
# File 'lib/bunchr/packages.rb', line 23

def iteration
  @iteration
end

#licenseObject

Returns the value of attribute license


23
24
25
# File 'lib/bunchr/packages.rb', line 23

def license
  @license
end

#maintainerObject

Returns the value of attribute maintainer


23
24
25
# File 'lib/bunchr/packages.rb', line 23

def maintainer
  @maintainer
end

#nameObject

Returns the value of attribute name


23
24
25
# File 'lib/bunchr/packages.rb', line 23

def name
  @name
end

#scriptsObject

Returns the value of attribute scripts


25
26
27
# File 'lib/bunchr/packages.rb', line 25

def scripts
  @scripts
end

#urlObject

Returns the value of attribute url


23
24
25
# File 'lib/bunchr/packages.rb', line 23

def url
  @url
end

#vendorObject

Returns the value of attribute vendor


23
24
25
# File 'lib/bunchr/packages.rb', line 23

def vendor
  @vendor
end

#versionObject

Returns the value of attribute version


23
24
25
# File 'lib/bunchr/packages.rb', line 23

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.


48
49
50
51
52
53
54
55
# File 'lib/bunchr/packages.rb', line 48

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

#arch=(a) ⇒ Object

explicitly set @arch to `a`


58
59
60
# File 'lib/bunchr/packages.rb', line 58

def arch=(a)
  @arch = a
end

#defineObject


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bunchr/packages.rb', line 62

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
      define_build_pkg
      # 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


184
185
186
187
# File 'lib/bunchr/packages.rb', line 184

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

#define_build_debObject


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/bunchr/packages.rb', line 124

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

    if DEB_PLATFORMS.include? ohai[:platform]
      # Instead of guessing architecture from the kernel, ask the packager
      @arch = `dpkg --print-architecture`.strip()
      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}'                           \
          --maintainer  '#{maintainer}'                         \
          #{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_pkgObject


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/bunchr/packages.rb', line 154

def define_build_pkg
  desc "Build pkg: #{name}-#{version}-#{iteration}-#{arch}"
  task :build_pkg do

    if PKG_PLATFORMS.include? ohai[:platform]
      logger.info "Building PKG '#{name}-#{version}-#{iteration}-#{arch}'"

      cmd = "fpm -s dir -t freebsd -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 cmd.inspect
      sh cmd

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

  end
end

#define_build_rpmObject


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/bunchr/packages.rb', line 96

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}'                           \
          --maintainer  '#{maintainer}'                         \
          #{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


83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bunchr/packages.rb', line 83

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'

201
202
203
# File 'lib/bunchr/packages.rb', line 201

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'

209
210
211
212
213
# File 'lib/bunchr/packages.rb', line 209

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


190
191
192
193
194
195
196
# File 'lib/bunchr/packages.rb', line 190

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