Class: Pkg::Config

Inherits:
Object show all
Defined in:
ext/packaging/lib/packaging/config.rb

Overview

This class is meant to encapsulate all of the data we know about a build invoked with

`rake package:<build>` or `rake pl:<build>`. It can read in this data via a yaml file,
have it set via accessors, and serialize it back to yaml for easy transport.

Class Method Summary collapse

Class Method Details

.config(args = {:target => nil, :format => :hash}) ⇒ Object

By default return a hash of the names, values of current Pkg::Config instance variables. With :format => :yaml, write a yaml file containing the current names,values of Pkg::Config class instance variables



55
56
57
58
59
60
61
62
# File 'ext/packaging/lib/packaging/config.rb', line 55

def config(args={:target => nil, :format => :hash})
  case args[:format]
    when :hash
      self.config_to_hash
    when :yaml
      self.config_to_yaml(args[:target])
  end
end

.config_from_hash(data = {}) ⇒ Object

Take a hash of Config parameters, and iterate over them, setting the

value for each Config param to the corresponding hash key,value.


31
32
33
34
35
36
37
38
39
# File 'ext/packaging/lib/packaging/config.rb', line 31

def config_from_hash(data = {})
  data.each do |param, value|
    if Pkg::Params::BUILD_PARAMS.include?(param.to_sym)
      self.instance_variable_set("@#{param}", value)
    else
      warn "Warning - No build data parameter found for '#{param}'. Perhaps you have an erroneous entry in your yaml file?"
    end
  end
end

.config_from_yaml(file) ⇒ Object

Load a yaml file and use its contents to set the values for Pkg::Config class instance variables



45
46
47
48
# File 'ext/packaging/lib/packaging/config.rb', line 45

def config_from_yaml(file)
  build_data = Pkg::Util::Serialization.load_yaml(file)
  config_from_hash(build_data)
end

.config_to_hashObject

Return a hash of all build parameters and their values, nil if unassigned.



67
68
69
70
71
72
73
# File 'ext/packaging/lib/packaging/config.rb', line 67

def config_to_hash
  data = {}
  Pkg::Params::BUILD_PARAMS.each do |param|
    data.store(param, self.instance_variable_get("@#{param}"))
  end
  data
end

.config_to_yaml(target = nil) ⇒ Object

Write all build parameters to a yaml file, either one specified or in a temporary location. Print the path to the file and return it as a string. Accept an argument for the write target file. If not specified, the name of the params file is the current git commit sha or tag.



81
82
83
84
85
86
87
88
89
90
# File 'ext/packaging/lib/packaging/config.rb', line 81

def config_to_yaml(target=nil)
  file = "#{self.ref}.yaml"
  target = target.nil? ? File.join(Pkg::Util::File.mktemp, "#{self.ref}.yaml") : File.join(target, file)
  Pkg::Util::File.file_writable?(File.dirname(target), :required => true)
  File.open(target, 'w') do |f|
    f.puts self.config_to_hash.to_yaml
  end
  puts target
  target
end

.cow_listObject

Return the names of all of the cows for the project, taking off the base prefix, the architecture, and the .cow suffix. This is helpful in the debian changelog.



104
105
106
107
108
# File 'ext/packaging/lib/packaging/config.rb', line 104

def cow_list
  self.cows.split(' ').map do
    |cow| cow.split('-')[1]
  end.uniq.join(' ')
end

.default_packaging_rootObject



124
125
126
127
128
129
130
131
132
# File 'ext/packaging/lib/packaging/config.rb', line 124

def default_packaging_root
  # It is really quite unsafe to assume github.com/puppetlabs/packaging has been
  # cloned into $project_root/ext/packaging even if it has _always_ been the
  # default location. Here we use the PACKAGING_ROOT constant defined in
  # packaging.rake if it is available, or use the parent directory of the
  # current file as the packaging_root.
  #
  defined?(PACKAGING_ROOT) ? File.expand_path(PACKAGING_ROOT) : File.expand_path(File.join(LIBDIR, ".."))
end

.default_project_rootObject



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'ext/packaging/lib/packaging/config.rb', line 110

def default_project_root
  # It is really quite unsafe to assume github.com/puppetlabs/packaging has been
  # cloned into $project_root/ext/packaging even if it has _always_ been the
  # default location. We really don't have much choice as of this moment but to
  # assume this directory, or assume the user has passed in the correct one via
  # ENV['PROJECT_ROOT']. It is critical we have the correct $project_root, because
  # we get all of the package versioning from the `git-describe` of $project. If we
  # assume $project/ext/packaging, it means packaging/lib/packaging.rb is
  # three subdirectories below $project_root, e.g.,
  # $project_root/ext/packaging/lib/packaging.rb.
  #
  ENV['PROJECT_ROOT'] || File.expand_path(File.join(LIBDIR, "..","..",".."))
end

.get_bindingObject

Return the binding of class context. Used for erb templates.



23
24
25
# File 'ext/packaging/lib/packaging/config.rb', line 23

def get_binding
  return binding
end

.issue_deprecationsObject

Quite a few variables we also want to issue custom warnings about.

These are they.


268
269
270
271
272
273
274
# File 'ext/packaging/lib/packaging/config.rb', line 268

def issue_deprecations
  Pkg::Params::DEPRECATIONS.each do |v|
    if self.instance_variable_get("@#{v[:var]}")
      warn v[:message]
    end
  end
end

.issue_reassignmentsObject

We also have renamed various variables as part of deprecations, and

if any of these are still in use, we want to assign the values to the
new variables. However, we skip this if they target variable is already
populated, to avoid overwriting in the case that the user has started
by populating the new variable name but left the old crufty one behind.


254
255
256
257
258
259
260
261
262
# File 'ext/packaging/lib/packaging/config.rb', line 254

def issue_reassignments
  Pkg::Params::REASSIGNMENTS.each do |v|
    oldval = self.instance_variable_get("@#{v[:oldvar]}")
    newval = self.instance_variable_get("@#{v[:newvar]}")
    if newval.nil? and not oldval.nil?
      self.instance_variable_set("@#{v[:newvar]}", oldval)
    end
  end
end

.load_default_configsObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'ext/packaging/lib/packaging/config.rb', line 134

def load_default_configs
  default_project_data = File.join(@project_root, "ext", "project_data.yaml")
  default_build_defaults = File.join(@project_root, "ext", "build_defaults.yaml")

  [default_project_data, default_build_defaults].each do |config|
    if File.readable? config
      self.config_from_yaml(config)
    else
      puts "Skipping load of expected default config #{config}, cannot read file."
      #   Since the default configuration files are not readable, most
      #   likely not present, at this point we assume the project_root
      #   isn't what we hoped it would be, and unset it.
      @project_root = nil
    end
  end

  if @project_root
    self.config
  end
end

.load_defaultsObject

We supply several values by default, if they haven’t been specified

already by config or environment variable. This includes the project
root as the default project root, which is relative to the
packaging path


209
210
211
212
213
214
215
216
217
218
# File 'ext/packaging/lib/packaging/config.rb', line 209

def load_defaults
  @project_root   ||= default_project_root
  @packaging_root ||= default_packaging_root

  Pkg::Params::DEFAULTS.each do |v|
    unless self.instance_variable_get("@#{v[:var]}")
      self.instance_variable_set("@#{v[:var]}", v[:val])
    end
  end
end

.load_envvarsObject

Since we’re dealing with rake, much of the parameter override support

is via environment variables passed on the command line to a rake task.
These override any existing values of Pkg::Config class instance
variables


191
192
193
194
195
196
197
198
199
200
201
# File 'ext/packaging/lib/packaging/config.rb', line 191

def load_envvars
  Pkg::Params::ENV_VARS.each do |v|
    if var = ENV[v[:envvar].to_s]
      if v[:type] == :bool
        self.instance_variable_set("@#{v[:var]}", Pkg::Util.boolean_value(var))
      else
        self.instance_variable_set("@#{v[:var]}", var)
      end
    end
  end
end

.load_overridesObject

Several workflows rely on being able to supply an optional yaml

parameters file that overrides all set values with its data. This has
always been supplied as an environment variable, "PARAMS_FILE." To
honor this, we have a method in config to override values as
expected. There is, however, a twist - it is absolutely essential
that the overrides do not override the project_root or packaging_root
settings, because this is environment-specific, and any value in a
params file is going to be wrong. Thus, if we have a project root or
packaging root before we begin overriding, we save it and restore it
after overrides.


233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'ext/packaging/lib/packaging/config.rb', line 233

def load_overrides
  if ENV['PARAMS_FILE'] && ENV['PARAMS_FILE'] != ''
    if File.readable?(ENV['PARAMS_FILE'])
      project_root = self.instance_variable_get("@project_root")
      packaging_root = self.instance_variable_get("@packaging_root")
      self.config_from_yaml(ENV['PARAMS_FILE'])
      self.instance_variable_set("@project_root", project_root) if project_root
      self.instance_variable_set("@packaging_root", packaging_root) if packaging_root
    else
      fail "PARAMS_FILE was set, but not to the path to a readable file."
    end
  end
end

.load_versioningObject

Set all aspects of how the package will be versioned. Versioning

relies exclusively on the git describe of the project, which will
fail if either Pkg::Config.project_root is nil, isn't in a git repo,
or is in a git repo, but there are no tags in the repo, in which case
git-describe will fail.

It probably seems odd to load packaging-specific version
determinations, such as rpmversion here, at the top-level, and it is.
The reason for this that the creation of the most basic package
composition, the tarball, includes the generation of many different
packaging-specific files from templates in the source, and if faced
with loading rpmversion in the Tar object vs rpmversion in the
Config, I opt for the latter. It's basically a lose-lose, since it
really belongs in the Rpm object.


170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'ext/packaging/lib/packaging/config.rb', line 170

def load_versioning
  if @project_root and Pkg::Util::Version.git_tagged?
    @ref         = Pkg::Util::Version.git_sha_or_tag
    @version     = Pkg::Util::Version.get_dash_version
    @gemversion  = Pkg::Util::Version.get_dot_version
    @ipsversion  = Pkg::Util::Version.get_ips_version
    @debversion  = Pkg::Util::Version.get_debversion
    @origversion = Pkg::Util::Version.get_origversion
    @rpmversion  = Pkg::Util::Version.get_rpmversion
    @rpmrelease  = Pkg::Util::Version.get_rpmrelease
  else
    puts "Skipping determination of version via git describe, Pkg::Config.project_root is not set to the path of a tagged git repo."
  end
end

Print the names and values of all the params known to the build object



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

def print_config
  self.config_to_hash.each { |k,v| puts "#{k}: #{v}" }
end