Class: Pkgr::Config

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/pkgr/config.rb

Constant Summary collapse

DISTRO_COMPATIBILITY_MAPPING =
{
  "ubuntu-lucid" => "ubuntu-10.04",
  "ubuntu-precise" => "ubuntu-12.04",
  "debian-squeeze" => "debian-6",
  "debian-wheezy" => "debian-7",
  "debian-jessie" => "debian-8"
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_file(path, distribution) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pkgr/config.rb', line 17

def load_file(path, distribution)
  config = YAML.load_file(path) || {}
  Pkgr.debug "Configuration from file: #{config.inspect} - Distribution: #{distribution.inspect}."

  targets = config.delete("targets") || {}

  # backward compatibility
  DISTRO_COMPATIBILITY_MAPPING.each do |from, to|
    if targets.has_key?(from)
      targets[to] = targets.delete(from)
    end
  end

  distro_config = targets[distribution.to_s]
  if distro_config.is_a?(Hash)
    distro_config.each do |k,v|
      config[k] = v
    end
  end

  self.new(config)
end

Instance Method Details

#after_hookObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/pkgr/config.rb', line 196

def after_hook
  if after_precompile.nil? || after_precompile.empty?
    after_steps = self.after || []

    if after_steps.empty?
      nil
    else
      tmpfile = Tempfile.new(["after_hook", ".sh"])
      after_steps.each{|step| tmpfile.puts step}
      tmpfile.close
      tmpfile.path
    end
  else
    after_precompile
  end
end

#after_installObject



218
219
220
221
# File 'lib/pkgr/config.rb', line 218

def after_install
  return nil if @table[:after_install].nil?
  Pathname.new(source_dir).join(@table[:after_install]).realpath.to_s
end

#after_removeObject



228
229
230
231
# File 'lib/pkgr/config.rb', line 228

def after_remove
  return nil if @table[:after_remove].nil?
  Pathname.new(source_dir).join(@table[:after_remove]).realpath.to_s
end

#architectureObject



115
116
117
# File 'lib/pkgr/config.rb', line 115

def architecture
  @table[:architecture] || "x86_64"
end

#before_hookObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/pkgr/config.rb', line 179

def before_hook
  if before_precompile.nil? || before_precompile.empty?
    before_steps = self.before || []

    if before_steps.empty?
      nil
    else
      tmpfile = Tempfile.new(["before_hook", ".sh"])
      before_steps.each{|step| tmpfile.puts step}
      tmpfile.close
      tmpfile.path
    end
  else
    before_precompile
  end
end

#before_installObject



213
214
215
216
# File 'lib/pkgr/config.rb', line 213

def before_install
  return nil if @table[:before_install].nil?
  Pathname.new(source_dir).join(@table[:before_install]).realpath.to_s
end

#before_removeObject



223
224
225
226
# File 'lib/pkgr/config.rb', line 223

def before_remove
  return nil if @table[:before_remove].nil?
  Pathname.new(source_dir).join(@table[:before_remove]).realpath.to_s
end

#buildpacksObject



139
140
141
# File 'lib/pkgr/config.rb', line 139

def buildpacks
  @table[:buildpack].is_a?(String) ? @table[:buildpack].split(",") : @table[:buildpack]
end

#cli?Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
# File 'lib/pkgr/config.rb', line 75

def cli?
  if disable_cli.nil?
    @table.has_key?(:cli) ? @table[:cli] : true
  else
    !disable_cli
  end
end

#cronsObject



175
176
177
# File 'lib/pkgr/config.rb', line 175

def crons
  @table[:crons] || []
end

#delete(key) ⇒ Object



67
68
69
# File 'lib/pkgr/config.rb', line 67

def delete(key)
  @table.delete(key)
end

#descriptionObject



123
124
125
# File 'lib/pkgr/config.rb', line 123

def description
  @table[:description] || "No description given"
end

#eachObject



60
61
62
63
64
65
# File 'lib/pkgr/config.rb', line 60

def each
  @table.each do |k,v|
    next if v.nil?
    yield k, v
  end
end

#envObject



135
136
137
# File 'lib/pkgr/config.rb', line 135

def env
  @table[:env].is_a?(Pkgr::Env) ? @table[:env] : Pkgr::Env.new(@table[:env])
end

#errorsObject



154
155
156
# File 'lib/pkgr/config.rb', line 154

def errors
  @errors ||= []
end

#groupObject



107
108
109
# File 'lib/pkgr/config.rb', line 107

def group
  @table[:group] || user
end

#homeObject



91
92
93
# File 'lib/pkgr/config.rb', line 91

def home
  @table[:home] || "/opt/#{name}"
end

#homepageObject



119
120
121
# File 'lib/pkgr/config.rb', line 119

def homepage
  @table[:homepage] || "http://example.com/no-uri-given"
end

#installerObject



158
159
160
161
# File 'lib/pkgr/config.rb', line 158

def installer
  return nil if @table[:installer].nil? || @table[:installer] == false
  @table[:installer]
end

#logrotate_backlogObject



99
100
101
# File 'lib/pkgr/config.rb', line 99

def logrotate_backlog
  @table[:logrotate_backlog] || 14
end

#logrotate_frequencyObject



95
96
97
# File 'lib/pkgr/config.rb', line 95

def logrotate_frequency
  @table[:logrotate_frequency] || "daily"
end

#maintainerObject



127
128
129
# File 'lib/pkgr/config.rb', line 127

def maintainer
  @table[:maintainer] || "<someone@pkgr>"
end

#merge(other) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pkgr/config.rb', line 45

def merge(other)
  new_config = self.class.new
  self.each{|k,v|
    new_value = case v
    when Array
      v | (other.delete(k) || [])
    else
      v
    end
    new_config.send("#{k}=".to_sym, new_value)
  }
  other.each{|k,v| new_config.send("#{k}=".to_sym, v)}
  new_config
end

#safe_nameObject



71
72
73
# File 'lib/pkgr/config.rb', line 71

def safe_name
  name.gsub("-", "_")
end

#sesameObject



41
42
43
# File 'lib/pkgr/config.rb', line 41

def sesame
  binding
end

#skip_default_dependencies?Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
# File 'lib/pkgr/config.rb', line 83

def skip_default_dependencies?
  if disable_default_dependencies.nil?
    @table[:default_dependencies] === false
  else
    disable_default_dependencies == true
  end
end

#tmpdirObject



111
112
113
# File 'lib/pkgr/config.rb', line 111

def tmpdir
  @table[:tmpdir]
end

#to_argsObject

TODO: DRY this with cli.rb



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/pkgr/config.rb', line 234

def to_args
  args = [
    "--name \"#{name}\"",
    "--version \"#{version}\"",
    "--user \"#{user}\"",
    "--group \"#{group}\"",
    "--iteration \"#{iteration}\"",
    "--homepage \"#{homepage}\"",
    "--home \"#{home}\"",
    "--architecture \"#{architecture}\"",
    "--description \"#{description}\"",
    "--maintainer \"#{maintainer}\"",
    "--vendor \"#{vendor}\""
  ]
  args.push "--dependencies #{dependencies.map{|d| "\"#{d}\""}.join}" unless dependencies.nil? || dependencies.empty?
  args.push "--build-dependencies #{build_dependencies.map{|d| "\"#{d}\""}.join}" unless build_dependencies.nil? || build_dependencies.empty?
  args.push "--compile-cache-dir \"#{compile_cache_dir}\"" unless compile_cache_dir.nil? || compile_cache_dir.empty?
  args.push "--before-precompile \"#{before_precompile}\"" unless before_precompile.nil? || before_precompile.empty?
  args.push "--after-precompile \"#{after_precompile}\"" unless after_precompile.nil? || after_precompile.empty?
  args.push "--before-install \"#{before_install}\"" unless before_install.nil? || before_install.empty?
  args.push "--after-install \"#{after_install}\"" unless after_install.nil? || after_install.empty?
  args.push "--before-remove \"#{before_remove}\"" unless before_remove.nil? || before_remove.empty?
  args.push "--after-remove \"#{after_remove}\"" unless after_remove.nil? || after_remove.empty?

  args.push "--license \"#{license}\"" unless license.nil? || license.empty?
  args.push "--buildpack \"#{buildpack}\"" unless buildpack.nil? || buildpack.empty?
  args.push "--buildpack_list \"#{buildpack_list}\"" unless buildpack_list.nil? || buildpack_list.empty?
  args.push "--force-os \"#{force_os}\"" unless force_os.nil? || force_os.empty?
  args.push "--runner \"#{runner}\"" unless runner.nil? || runner.empty?
  args.push "--logrotate-frequency \"#{logrotate_frequency}\"" unless logrotate_frequency.nil? || logrotate_frequency.empty?
  args.push "--logrotate-backlog \"#{logrotate_backlog}\"" unless logrotate_backlog.nil?
  args.push "--env #{env.variables.map{|v| "\"#{v}\""}.join(" ")}" if env.present?
  args.push "--auto" if auto
  args.push "--verbose" if verbose
  args.push "--store-cache" if store_cache
  args.push "--debug" if debug
  args.push "--verify" if verify
  args.push "--no-clean" if !clean
  args.push "--no-edge" if !edge
  args.push "--tmpdir" if !tmpdir
  args
end

#userObject



103
104
105
# File 'lib/pkgr/config.rb', line 103

def user
  @table[:user] || name
end

#valid?Boolean

Returns:

  • (Boolean)


143
144
145
146
147
148
149
150
151
152
# File 'lib/pkgr/config.rb', line 143

def valid?
  @errors = []
  @errors.push("name can't be blank") if name.nil? || name.empty?
  @errors.push("version can't be blank") if version.nil? || version.empty?
  @errors.push("version must start with a digit") if !version.empty? && version !~ /^\d/
  @errors.push("iteration can't be blank") if iteration.nil? || iteration.empty?
  @errors.push("user can't be blank") if user.nil? || user.empty?
  @errors.push("group can't be blank") if group.nil? || group.empty?
  @errors.empty?
end

#vendorObject



131
132
133
# File 'lib/pkgr/config.rb', line 131

def vendor
  @table[:vendor] || "pkgr <https://github.com/crohr/pkgr>"
end

#wizardsObject



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/pkgr/config.rb', line 163

def wizards
  @wizards ||= (@table[:wizards] || []).map do |wizard_string|
    wizard_string.split(/\s*\|\s*/).map do |wizard|
      if wizard.start_with?('.')
        wizard = Pathname.new(source_dir).join(wizard).realpath
      end

      Addon.new(wizard)
    end
  end
end