Class: Vanagon::Component

Inherits:
Object
  • Object
show all
Includes:
HashableAttributes, Utilities
Defined in:
lib/vanagon/component.rb,
lib/vanagon/component/dsl.rb,
lib/vanagon/component/rules.rb,
lib/vanagon/component/source.rb,
lib/vanagon/extensions/hashable.rb,
lib/vanagon/component/source/git.rb,
lib/vanagon/component/source/http.rb,
lib/vanagon/component/source/local.rb,
lib/vanagon/component/source/rewrite.rb

Defined Under Namespace

Classes: DSL, Rules, Source

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HashableAttributes

#to_hash, #to_json

Methods included from Utilities

#erb_file, #erb_string, #ex, #find_program_on_path, #get_md5sum, #get_sum, #http_request, #local_command, #remote_ssh_command, #retry_with_timeout, #rsync_from, #rsync_to, #ssh_command

Constructor Details

#initialize(name, settings, platform) ⇒ Vanagon::Component

Component constructor.

Parameters:

  • name (String)

    the name of the component

  • settings (Hash)

    the settings to be used in the component

  • platform (Vanagon::Platform)

    the platform to build the component for



153
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
# File 'lib/vanagon/component.rb', line 153

def initialize(name, settings, platform) # rubocop:disable Metrics/AbcSize
  @name = name
  @settings = settings
  @platform = platform
  @options = {}
  @build_requires = []
  @requires = []
  @configure = []
  @install = []
  @build = []
  @check = []
  @patches = []
  @files = Set.new
  @ghost_files = Set.new
  @directories = []
  @replaces = []
  @provides = []
  @conflicts = []
  @environment = Vanagon::Environment.new
  @sources = []
  @preinstall_actions = []
  @install_triggers = []
  @interest_triggers = []
  @activate_triggers = []
  @postinstall_actions = []
  @preremove_actions = []
  @postremove_actions = []
  @install_only = false
end

Instance Attribute Details

#activate_triggersObject

activate_triggers is a one-dimentional Array of Strings, describing scripts that should be executed when a package identifies an activate trigger



106
107
108
# File 'lib/vanagon/component.rb', line 106

def activate_triggers
  @activate_triggers
end

#buildObject

build will hold an Array of the commands required to build a given component



48
49
50
# File 'lib/vanagon/component.rb', line 48

def build
  @build
end

#build_dirObject

the optional name of a directory to build a component in; most likely to be used for cmake projects, which do not like to be configured or compiled in their own top-level directories.



45
46
47
# File 'lib/vanagon/component.rb', line 45

def build_dir
  @build_dir
end

#build_requiresObject

build_requires holds an Array with a list of the dependencies that a given component needs satisfied before it can be built.



82
83
84
# File 'lib/vanagon/component.rb', line 82

def build_requires
  @build_requires
end

#checkObject

check will hold an Array of the commands required to validate/test a given component



51
52
53
# File 'lib/vanagon/component.rb', line 51

def check
  @check
end

#cleanup_sourceObject

cleanup_source contains whatever value a given component’s Source has specified as instructions for cleaning up after a build is completed. usually a String, but not required to be.



119
120
121
# File 'lib/vanagon/component.rb', line 119

def cleanup_source
  @cleanup_source
end

#configureObject

how should this component be configured?



41
42
43
# File 'lib/vanagon/component.rb', line 41

def configure
  @configure
end

#conflictsObject

conflicts holds an Array of OpenStructs that describe a package that a given component will replace on installation.



94
95
96
# File 'lib/vanagon/component.rb', line 94

def conflicts
  @conflicts
end

#directoriesObject

directories holds an Array with a list of expected directories that will be packed into the resulting artifact’s bill of materials.



79
80
81
# File 'lib/vanagon/component.rb', line 79

def directories
  @directories
end

#dirnameObject

holds the expected directory name of a given component, once it’s been unpacked/decompressed. For git repos, it’s usually the directory that they were cloned to. For the outlying flat files, it’ll end up being defined explicitly as the string ‘./’



36
37
38
# File 'lib/vanagon/component.rb', line 36

def dirname
  @dirname
end

#environmentObject

holds a Vanagon::Environment object, to map out any desired environment variables that should be rendered into the Makefile



58
59
60
# File 'lib/vanagon/component.rb', line 58

def environment
  @environment
end

#extract_withArray

Returns the specific tool or command line invocations that should be used to extract a given component’s primary source.

Returns:

  • (Array)

    the specific tool or command line invocations that should be used to extract a given component’s primary source



39
40
41
# File 'lib/vanagon/component.rb', line 39

def extract_with
  @extract_with
end

#filesSet (readonly)

Retrieve all items from @files not marked as configuration files

Returns:

  • (Set)

    all files not marked as configuration files



# File 'lib/vanagon/component.rb', line 9

#homepageObject

Returns the value of attribute homepage.



26
27
28
# File 'lib/vanagon/component.rb', line 26

def homepage
  @homepage
end

#installObject

install will hold an Array of the commands required to install a given component



54
55
56
# File 'lib/vanagon/component.rb', line 54

def install
  @install
end

#install_onlyObject

When dealing with compiled artifacts generated by setting ‘project.generate_archives true`, you only need to install the component. By setting install_only to true the component ’build’ will exclude the unpack, patch, configure, build, and check steps.



125
126
127
# File 'lib/vanagon/component.rb', line 125

def install_only
  @install_only
end

#install_triggersObject

install_triggers is a one-dimensional Array of OpenStructs, describing scripts that should be executed when a package is installed or upgraded



100
101
102
# File 'lib/vanagon/component.rb', line 100

def install_triggers
  @install_triggers
end

#interest_triggersObject

interest_triggers is a one-dimensional Array of OpenStructs, describing scripts that should be executed when a package identifies an interest trigger



103
104
105
# File 'lib/vanagon/component.rb', line 103

def interest_triggers
  @interest_triggers
end

#licenseObject

Returns the value of attribute license.



25
26
27
# File 'lib/vanagon/component.rb', line 25

def license
  @license
end

#mirrorsSet

Returns a list of unique mirror URIs that should be used to retrieve the upstream source before attempting to retrieve from whatever URI was defined for #uri. If no mirrors are set and the deprecated rewrite system has been configured, this will return rewritten URIs.

Returns:

  • (Set)

    a list of unique mirror URIs that should be used to retrieve the upstream source before attempting to retrieve from whatever URI was defined for #uri. If no mirrors are set and the deprecated rewrite system has been configured, this will return rewritten URIs



240
241
242
# File 'lib/vanagon/component.rb', line 240

def mirrors
  @mirrors
end

#nameObject

The name, version, primary source, supplementary sources, associated patches, upstream URL (for fetching the source), homepage, and license of a given component



18
19
20
# File 'lib/vanagon/component.rb', line 18

def name
  @name
end

#optionsObject

used to hold the checksum settings or other weirdo metadata related to building a given component (git ref, sha, etc.). Probably conflicts or collides with #settings to some degree.



68
69
70
# File 'lib/vanagon/component.rb', line 68

def options
  @options
end

#patchesObject

Returns the value of attribute patches.



22
23
24
# File 'lib/vanagon/component.rb', line 22

def patches
  @patches
end

#platformObject

the platform that a given component will be built for – due to the fact that Ruby is pass-by-reference, it’s usually just a reference to the same Platform object that the overall Project object also contains. This is a definite code smell, and should be slated for refactoring ASAP because it’s going to have weird side-effects if the underlying pass-by-reference assumptions change.



75
76
77
# File 'lib/vanagon/component.rb', line 75

def platform
  @platform
end

#postinstall_actionsObject

postinstall_actions is a two-dimensional Array, describing scripts that should be executed after a given component is installed.



109
110
111
# File 'lib/vanagon/component.rb', line 109

def postinstall_actions
  @postinstall_actions
end

#postremove_actionsObject

preinstall_actions is a two-dimensional Array, describing scripts that should be executed after a given component is uninstalled.



115
116
117
# File 'lib/vanagon/component.rb', line 115

def postremove_actions
  @postremove_actions
end

#preinstall_actionsObject

preinstall_actions is a two-dimensional Array, describing scripts that should be executed before a given component is installed.



97
98
99
# File 'lib/vanagon/component.rb', line 97

def preinstall_actions
  @preinstall_actions
end

#preremove_actionsObject

preremove_actions is a two-dimensional Array, describing scripts that should be executed before a given component is uninstalled.



112
113
114
# File 'lib/vanagon/component.rb', line 112

def preremove_actions
  @preremove_actions
end

#providesObject

provides holds an Array of OpenStructs that describe any capabilities that a given component will provide beyond the its filesystem payload.



91
92
93
# File 'lib/vanagon/component.rb', line 91

def provides
  @provides
end

#replacesObject

replaces holds an Array of OpenStructs that describe a package that a given component will replace on installation.



88
89
90
# File 'lib/vanagon/component.rb', line 88

def replaces
  @replaces
end

#requiresObject

requires holds an Array with a list of all dependencies that a given component needs satisfied before it can be installed.



85
86
87
# File 'lib/vanagon/component.rb', line 85

def requires
  @requires
end

#serviceObject

holds an OpenStruct describing all of the particular details about how any services associated with a given component should be defined.



30
31
32
# File 'lib/vanagon/component.rb', line 30

def service
  @service
end

#settingsObject

holds a OpenStruct, or an Array, or maybe it’s a Hash? It’s often overloaded as a freeform key-value lookup for platforms that require additional configuration beyond the “basic” component attributes. it’s pretty heavily overloaded and should maybe be refactored before Vanagon 1.0.0 is tagged.



64
65
66
# File 'lib/vanagon/component.rb', line 64

def settings
  @settings
end

#sourceObject

Returns the value of attribute source.



20
21
22
# File 'lib/vanagon/component.rb', line 20

def source
  @source
end

#sourcesObject

Returns the value of attribute sources.



21
22
23
# File 'lib/vanagon/component.rb', line 21

def sources
  @sources
end

#urlObject

Returns the value of attribute url.



23
24
25
# File 'lib/vanagon/component.rb', line 23

def url
  @url
end

#versionObject

Returns the value of attribute version.



19
20
21
# File 'lib/vanagon/component.rb', line 19

def version
  @version
end

Class Method Details

.load_component(name, configdir, settings, platform) ⇒ Vanagon::Component

Loads a given component from the configdir

Parameters:

  • name (String)

    the name of the component

  • configdir (String)

    the path to the component config file

  • settings (Hash)

    the settings to be used in the component

  • platform (Vanagon::Platform)

    the platform to build the component for

Returns:

Raises:

  • if the instance_eval on Component fails, the exception is reraised



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/vanagon/component.rb', line 135

def self.load_component(name, configdir, settings, platform)
  compfile = File.join(configdir, "#{name}.rb")
  dsl = Vanagon::Component::DSL.new(name, settings, platform)
  dsl.instance_eval(File.read(compfile), compfile, 1)
  dsl._component
rescue StandardError => e
  warn "Error loading project '#{name}' using '#{compfile}':"
  warn e
  warn e.backtrace.join("\n")
  raise e
end

Instance Method Details

#add_file(file) ⇒ Set?

Adds the given file to the list of files and returns @files.

Parameters:

Returns:

  • (Set, nil)

    Returns @files if file is successfully added to @files or nil if file already exists



188
189
190
# File 'lib/vanagon/component.rb', line 188

def add_file(file)
  @files.add file
end

#add_rpm_ghost_file(file) ⇒ Set?

Adds the given file to the list of %ghost files to be added to an rpm spec’s %files.

Parameters:

Returns:

  • (Set, nil)

    Returns @ghost_files if the file is successfully added to @ghost_files or nil if the file already exists.



198
199
200
# File 'lib/vanagon/component.rb', line 198

def add_rpm_ghost_file(file)
  @ghost_files.add file
end

#configfilesSet

Retrieve all items from @files explicitly marked as configuration files

Returns:

  • (Set)

    all files explicitly marked as configuration files



223
224
225
# File 'lib/vanagon/component.rb', line 223

def configfiles
  @files.select(&:configfile?)
end

#delete_file(file) ⇒ Set?

Deletes the given file from the list of files and returns @files.

Parameters:

  • file (String)

    path of file to delete from a component’s list of files

Returns:

  • (Set, nil)

    Returns @files if file is successfully deleted from @files or nil if file doesn’t exist; this matches strictly on the path of a given file, and ignores other attributes like :mode, :owner, or :group.



209
210
211
# File 'lib/vanagon/component.rb', line 209

def delete_file(file)
  @files.delete_if { |this_file| this_file.path == file }
end

#environment_variablesObject



423
424
425
# File 'lib/vanagon/component.rb', line 423

def environment_variables
  environment.map { |key, value| %(export #{key}="#{value}") }
end

#fetch_mirrors(options) ⇒ Boolean

Retrieve upstream source file from a mirror, by randomly iterating through #mirrors until there’s no more mirrors left. Will #warn if the mirror’s URI cannot be resolved or if the URI cannot be retrieved. Does not suppress any errors from Vanagon::Component::Source.

Returns:

  • (Boolean)

    return True if the source can be retrieved, or False otherwise. This is because because each subclass of Vanagon::Component::Source returns an inconsistent value if #fetch is successful.



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/vanagon/component.rb', line 254

def fetch_mirrors(options)
  mirrors.to_a.shuffle.each do |mirror|
    begin
      warn %(Attempting to fetch from mirror URL "#{mirror}")
      @source = Vanagon::Component::Source.source(mirror, options)
      return true if source.fetch
    rescue SocketError
      # SocketError means that there was no DNS/name resolution
      # for whatever remote protocol the mirror tried to use.
      warn %(Unable to resolve mirror URL "#{mirror}")
    rescue RuntimeError
      # Source retrieval does not consistently return a meaningful
      # namespaced error message, which means we're brute-force rescuing
      # RuntimeError. Not a good look, and we should fix this.
      warn %(Unable to retrieve mirror URL "#{mirror}")
    end
  end
  false
end

#fetch_url(options) ⇒ Boolean

Retrieve upstream source file from the canonical URL. Does not suppress any errors from Vanagon::Component::Source.

Returns:

  • (Boolean)

    return True if the source can be retrieved, or False otherwise



279
280
281
282
283
284
285
286
# File 'lib/vanagon/component.rb', line 279

def fetch_url(options)
  warn %(Attempting to fetch from canonical URL "#{url}")
  @source = Vanagon::Component::Source.source(url, options)
  # Explicitly coerce the return value of #source.fetch,
  # because each subclass of Vanagon::Component::Source returns
  # an inconsistent value if #fetch is successful.
  !!source.fetch
end

#force_versionObject

Force version determination for components

If the component doesn’t already have a version set (which normally happens for git sources), the source will be fetched into a temporary directory to attempt to figure out the version if the source type supports :version. This directory will be cleaned once the get_sources method returns

Raises:

  • Vanagon::Error raises a vanagon error if we’re unable to determine the version



393
394
395
396
397
398
399
400
401
# File 'lib/vanagon/component.rb', line 393

def force_version
  if @version.nil?
    Dir.mktmpdir do |dir|
      get_source(dir)
    end
  end
  raise Vanagon::Error, "Unable to determine source version for component #{@name}!" if @version.nil?
  @version
end

#get_build_dirObject

Expands the build directory



333
334
335
336
337
338
339
# File 'lib/vanagon/component.rb', line 333

def get_build_dir
  if @build_dir
    File.join(@dirname, @build_dir)
  else
    @dirname
  end
end

#get_dependency_hashObject



341
342
343
# File 'lib/vanagon/component.rb', line 341

def get_dependency_hash
  { name => { 'version' => version, 'url' => url, 'ref' => options[:ref] }.delete_if { |_, v| !v } }
end

#get_environmentString

Deprecated.

Prints the environment in a way suitable for use in a Makefile or shell script. This is deprecated, because all Env. Vars. are moving directly into the Makefile (and out of recipe subshells).

Returns:

  • (String)

    environment suitable for inclusion in a Makefile



409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/vanagon/component.rb', line 409

def get_environment
  warn <<-WARNING.undent
    #get_environment is deprecated; environment variables have been moved
    into the Makefile, and should not be used within a Makefile's recipe.
    The #get_environment method will be removed by Vanagon 1.0.0.
  WARNING

  if environment.empty?
    ": no environment variables defined"
  else
    environment_variables
  end
end

#get_patches(patch_root) ⇒ Object

Fetches patches if any are provided for the project.

Parameters:

  • patch_root (String)

    working directory to put the patches into



373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/vanagon/component.rb', line 373

def get_patches(patch_root)
  return if @patches.empty?
  @patches.each do |patch|
    patch_assembly_path = File.join(patch_root, patch.assembly_path)
    if File.exist?(patch_assembly_path)
      raise Vanagon::Error, "Duplicate patch files detected, '#{patch.origin_path}' would have overwritten '#{patch.assembly_path}'. Ensure all patch file names within a component are unique."
    end
    patch_target_directory = File.dirname(patch_assembly_path)
    FileUtils.mkdir_p(patch_target_directory)
    FileUtils.cp(patch.origin_path, patch_assembly_path)
  end
end

#get_source(workdir) ⇒ Object

Fetches the primary source for the component. As a side effect, also sets @extract_with, @dirname and @version for the component for use in the makefile template

Parameters:

  • workdir (String)

    working directory to put the source into



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/vanagon/component.rb', line 303

def get_source(workdir) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
  opts = options.merge({ workdir: workdir })
  if url || !mirrors.empty?
    if ENV['VANAGON_USE_MIRRORS'] == 'n'
      fetch_url(opts)
    else
      fetch_mirrors(opts) || fetch_url(opts)
    end
    source.verify
    extract_with << source.extract(platform.tar) if source.respond_to? :extract

    @cleanup_source = source.cleanup if source.respond_to?(:cleanup)
    @dirname ||= source.dirname

    # Git based sources probably won't set the version, so we load it if it hasn't been already set
    if source.respond_to?(:version)
      @version ||= source.version
    end
  else
    warn "No source given for component '#{@name}'"

    # If there is no source, we don't want to try to change directories, so we just change to the current directory.
    @dirname = './'

    # If there is no source, there is nothing to do to extract
    extract_with << ': no source, so nothing to extract'
  end
end

#get_sources(workdir) ⇒ Object

Fetches secondary sources for the component. These are just dumped into the workdir currently.

Parameters:

  • workdir (String)

    working directory to put the source into



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/vanagon/component.rb', line 348

def get_sources(workdir) # rubocop:disable Metrics/AbcSize
  sources.each do |source|
    src = Vanagon::Component::Source.source(
      source.url, workdir: workdir, ref: source.ref, sum: source.sum
    )
    src.fetch
    src.verify
    if source.erb
      erb_file(src.file, File.join(File.dirname(src.file), File.basename(src.file, ".erb")), true)
    end
    # set src.file to only be populated with the basename instead of entire file path
    src.file = File.basename(src.url)
    extract_with << src.extract(platform.tar) if src.respond_to? :extract
  end
end

#rpm_ghost_filesArray

Retrieve all the files intended as %ghost entries for an rpm spec %files section.

Returns:

  • (Array)

    of all the rpm %ghost files.



231
232
233
# File 'lib/vanagon/component.rb', line 231

def rpm_ghost_files
  @ghost_files.to_a
end

#rules(project, platform) ⇒ Object



427
428
429
# File 'lib/vanagon/component.rb', line 427

def rules(project, platform)
  Vanagon::Component::Rules.new(self, project, platform)
end