Class: Vanagon::Component::DSL
- Inherits:
-
Object
- Object
- Vanagon::Component::DSL
- Defined in:
- lib/vanagon/component/dsl.rb
Instance Method Summary collapse
-
#_component ⇒ Vanagon::Component
Accessor for the component.
-
#add_debian_activate_triggers(activate_name) ⇒ Object
Adds activate trigger name to be watched.
-
#add_debian_interest_triggers(pkg_state, scripts, interest_name) ⇒ Object
Adds interest trigger based on the specified packaging state and interest name.
-
#add_postinstall_action(pkg_state, scripts) ⇒ Object
Adds action to run during the postinstall phase of packaging.
-
#add_postremove_action(pkg_state, scripts) ⇒ Object
Adds action to run during the postremoval phase of packaging.
-
#add_preinstall_action(pkg_state, scripts) ⇒ Object
Adds action to run during the preinstall phase of packaging.
-
#add_preremove_action(pkg_state, scripts) ⇒ Object
Adds action to run during the preremoval phase of packaging.
-
#add_rpm_install_triggers(pkg_state, scripts, pkg) ⇒ Object
Adds trigger for scripts to be run on specified pkg_state.
-
#add_source(uri, **options) ⇒ Object
This will add a source to the project and put it in the workdir alongside the other sources.
-
#apply_patch(patch, destination: @component.dirname, strip: 1, fuzz: 0, after: 'unpack') ⇒ Object
Add a patch to the list of patches to apply to the component’s source after unpacking.
-
#build(&block) ⇒ Object
Set or add to the build call for the component.
-
#build_dir(path) ⇒ Object
Set a build dir relative to the source directory.
-
#build_requires(build_requirement) ⇒ Object
build_requires adds a requirements to the list of build time dependencies that will need to be fetched from an external source before this component can be built.
-
#check(&block) ⇒ Object
Set or add to the check call for the component.
-
#check_pkg_state_array(pkg_state) ⇒ Object
Checks that the array of pkg_state is valid (install AND/OR upgrade).
-
#component(name, &block) {|_self, @component.settings, @component.platform| ... } ⇒ Object
Primary way of interacting with the DSL.
-
#configfile(file, mode: nil, owner: nil, group: nil) ⇒ Object
Marks a file as a configfile to ensure that it is not overwritten on upgrade if it has been modified.
-
#configure(&block) ⇒ Object
Set or add to the configure call for the component.
-
#conflicts(pkgname, version = nil) ⇒ Object
Indicates that this component conflicts with another package, so both cannot be installed at the same time.
-
#directory(dir, mode: nil, owner: nil, group: nil) ⇒ Object
Adds a directory to the list of directories provided by the project, to be included in any packages of the project.
-
#dirname(path) ⇒ Object
Set a source dir.
-
#environment(*env) ⇒ Object
Adds a set of environment overrides to the environment for a component.
-
#initialize(name, settings, platform) ⇒ Vanagon::Component::DSL
constructor
Constructor for the DSL object.
-
#install(&block) ⇒ Object
Set or add to the install call for the component.
-
#install_configfile(source, target, mode: '0644', owner: nil, group: nil) ⇒ Object
Shorthand to install a file and mark it as a configfile.
-
#install_file(source, target, mode: nil, owner: nil, group: nil) ⇒ Object
Copies a file from source to target during the install phase of the component.
- #install_only(install_only) ⇒ Object
-
#install_service(service_file, default_file = nil, service_name = @component.name, service_type: nil, link_target: nil) ⇒ Object
install_service adds the commands to install the various files on disk during the package build and registers the service with the project.
- #license(license) ⇒ Object
-
#link(source, target) ⇒ Object
link will add a command to the install to create a symlink from source to target.
-
#load_from_json(file) ⇒ Object
Loads and parses json from a file.
-
#method_missing(method_name, *args) ⇒ Object
All purpose getter.
-
#mirror(url) ⇒ Object
Sets a mirror url for the source of this component.
-
#provides(provide, version = nil) ⇒ Object
Indicates that this component provides a system level package.
-
#ref(the_ref) ⇒ Object
Sets the ref of the source for use in a git source.
-
#replaces(replacement, version = nil) ⇒ Object
Indicates that this component replaces a system level package.
-
#requires(requirement) ⇒ Object
requires adds a requirement to the list of runtime requirements for the component.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #sum(value) ⇒ Object (also: #md5sum, #sha1sum, #sha256sum, #sha512sum)
-
#url(uri) ⇒ Object
Sets the canonical URL or URI for the upstream source of this component.
-
#version(ver) ⇒ Object
Sets the version for the component.
Constructor Details
#initialize(name, settings, platform) ⇒ Vanagon::Component::DSL
Constructor for the DSL object
15 16 17 18 |
# File 'lib/vanagon/component/dsl.rb', line 15 def initialize(name, settings, platform) @name = name @component = Vanagon::Component.new(@name, settings, platform) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
All purpose getter. This object, which is passed to the component block, won’t have easy access to the attributes of the @component, so we make a getter for each attribute.
We only magically handle get_ methods, any other methods just get the standard method_missing treatment.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/vanagon/component/dsl.rb', line 42 def method_missing(method_name, *args) attribute_match = method_name.to_s.match(/get_(.*)/) if attribute_match attribute = attribute_match.captures.first else super end @component.send(attribute) end |
Instance Method Details
#_component ⇒ Vanagon::Component
Accessor for the component.
31 32 33 |
# File 'lib/vanagon/component/dsl.rb', line 31 def _component @component end |
#add_debian_activate_triggers(activate_name) ⇒ Object
Adds activate trigger name to be watched
470 471 472 |
# File 'lib/vanagon/component/dsl.rb', line 470 def add_debian_activate_triggers(activate_name) @component.activate_triggers << OpenStruct.new(:activate_name => activate_name) end |
#add_debian_interest_triggers(pkg_state, scripts, interest_name) ⇒ Object
Adds interest trigger based on the specified packaging state and interest name.
460 461 462 463 464 465 |
# File 'lib/vanagon/component/dsl.rb', line 460 def add_debian_interest_triggers(pkg_state, scripts, interest_name) pkg_state = Array(pkg_state) scripts = Array(scripts) check_pkg_state_array(pkg_state) @component.interest_triggers << OpenStruct.new(:pkg_state => pkg_state, :scripts => scripts, :interest_name => interest_name) end |
#add_postinstall_action(pkg_state, scripts) ⇒ Object
Adds action to run during the postinstall phase of packaging
479 480 481 482 483 484 |
# File 'lib/vanagon/component/dsl.rb', line 479 def add_postinstall_action(pkg_state, scripts) pkg_state = Array(pkg_state) scripts = Array(scripts) check_pkg_state_array(pkg_state) @component.postinstall_actions << OpenStruct.new(:pkg_state => pkg_state, :scripts => scripts) end |
#add_postremove_action(pkg_state, scripts) ⇒ Object
Adds action to run during the postremoval phase of packaging
506 507 508 509 510 511 512 513 514 |
# File 'lib/vanagon/component/dsl.rb', line 506 def add_postremove_action(pkg_state, scripts) pkg_state = Array(pkg_state) scripts = Array(scripts) if pkg_state.empty? || !(pkg_state - ["upgrade", "removal"]).empty? raise Vanagon::Error, "#{pkg_state} should be an array containing one or more of ['removal', 'upgrade']" end @component.postremove_actions << OpenStruct.new(:pkg_state => pkg_state, :scripts => scripts) end |
#add_preinstall_action(pkg_state, scripts) ⇒ Object
Adds action to run during the preinstall phase of packaging
434 435 436 437 438 439 |
# File 'lib/vanagon/component/dsl.rb', line 434 def add_preinstall_action(pkg_state, scripts) pkg_state = Array(pkg_state) scripts = Array(scripts) check_pkg_state_array(pkg_state) @component.preinstall_actions << OpenStruct.new(:pkg_state => pkg_state, :scripts => scripts) end |
#add_preremove_action(pkg_state, scripts) ⇒ Object
Adds action to run during the preremoval phase of packaging
491 492 493 494 495 496 497 498 499 |
# File 'lib/vanagon/component/dsl.rb', line 491 def add_preremove_action(pkg_state, scripts) pkg_state = Array(pkg_state) scripts = Array(scripts) if pkg_state.empty? || !(pkg_state - ["upgrade", "removal"]).empty? raise Vanagon::Error, "#{pkg_state} should be an array containing one or more of ['removal', 'upgrade']" end @component.preremove_actions << OpenStruct.new(:pkg_state => pkg_state, :scripts => scripts) end |
#add_rpm_install_triggers(pkg_state, scripts, pkg) ⇒ Object
Adds trigger for scripts to be run on specified pkg_state.
447 448 449 450 451 452 |
# File 'lib/vanagon/component/dsl.rb', line 447 def add_rpm_install_triggers(pkg_state, scripts, pkg) pkg_state = Array(pkg_state) scripts = Array(scripts) check_pkg_state_array(pkg_state) @component.install_triggers << OpenStruct.new(:pkg_state => pkg_state, :scripts => scripts, :pkg => pkg) end |
#add_source(uri, **options) ⇒ Object
This will add a source to the project and put it in the workdir alongside the other sources
371 372 373 |
# File 'lib/vanagon/component/dsl.rb', line 371 def add_source(uri, **) @component.sources << OpenStruct.new(.merge({ url: uri })) end |
#apply_patch(patch, destination: @component.dirname, strip: 1, fuzz: 0, after: 'unpack') ⇒ Object
Add a patch to the list of patches to apply to the component’s source after unpacking
92 93 94 |
# File 'lib/vanagon/component/dsl.rb', line 92 def apply_patch(patch, destination: @component.dirname, strip: 1, fuzz: 0, after: 'unpack') @component.patches << Vanagon::Patch.new(patch, strip, fuzz, after, destination) end |
#build(&block) ⇒ Object
Set or add to the build call for the component. The commands required to build the component before testing/installing it.
67 68 69 |
# File 'lib/vanagon/component/dsl.rb', line 67 def build(&block) @component.build << yield end |
#build_dir(path) ⇒ Object
Set a build dir relative to the source directory.
The build dir will be created before the configure block runs and configure/build/install commands will be run in the build dir.
334 335 336 337 338 339 340 |
# File 'lib/vanagon/component/dsl.rb', line 334 def build_dir(path) if Pathname.new(path).relative? @component.build_dir = path else raise Vanagon::Error, "build_dir should be a relative path, but '#{path}' looks to be absolute." end end |
#build_requires(build_requirement) ⇒ Object
build_requires adds a requirements to the list of build time dependencies that will need to be fetched from an external source before this component can be built. build_requires can also be satisfied by other components in the same project.
123 124 125 |
# File 'lib/vanagon/component/dsl.rb', line 123 def build_requires(build_requirement) @component.build_requires << build_requirement end |
#check(&block) ⇒ Object
Set or add to the check call for the component. The commands required to test the component before installing it.
74 75 76 |
# File 'lib/vanagon/component/dsl.rb', line 74 def check(&block) @component.check << yield end |
#check_pkg_state_array(pkg_state) ⇒ Object
Checks that the array of pkg_state is valid (install AND/OR upgrade). Returns vanagon error if invalid
423 424 425 426 427 |
# File 'lib/vanagon/component/dsl.rb', line 423 def check_pkg_state_array(pkg_state) if pkg_state.empty? || (pkg_state - ["install", "upgrade"]).any? raise Vanagon::Error, "#{pkg_state} should be an array containing one or more of ['install', 'upgrade']" end end |
#component(name, &block) {|_self, @component.settings, @component.platform| ... } ⇒ Object
Primary way of interacting with the DSL
24 25 26 |
# File 'lib/vanagon/component/dsl.rb', line 24 def component(name, &block) yield(self, @component.settings, @component.platform) end |
#configfile(file, mode: nil, owner: nil, group: nil) ⇒ Object
Marks a file as a configfile to ensure that it is not overwritten on upgrade if it has been modified
248 249 250 251 252 253 254 255 256 257 |
# File 'lib/vanagon/component/dsl.rb', line 248 def configfile(file, mode: nil, owner: nil, group: nil) # I AM SO SORRY @component.delete_file file if @component.platform.name =~ /solaris-10|osx/ @component.install << "mv '#{file}' '#{file}.pristine'" @component.add_file Vanagon::Common::Pathname.configfile("#{file}.pristine", mode: mode, owner: owner, group: group) else @component.add_file Vanagon::Common::Pathname.configfile(file, mode: mode, owner: owner, group: group) end end |
#configure(&block) ⇒ Object
Set or add to the configure call for the component. The commands required to configure the component before building it.
60 61 62 |
# File 'lib/vanagon/component/dsl.rb', line 60 def configure(&block) @component.configure << yield end |
#conflicts(pkgname, version = nil) ⇒ Object
Indicates that this component conflicts with another package, so both cannot be installed at the same time. Conflicts can be collected and used by the project and package.
157 158 159 |
# File 'lib/vanagon/component/dsl.rb', line 157 def conflicts(pkgname, version = nil) @component.conflicts << OpenStruct.new(:pkgname => pkgname, :version => version) end |
#directory(dir, mode: nil, owner: nil, group: nil) ⇒ Object
Adds a directory to the list of directories provided by the project, to be included in any packages of the project
381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/vanagon/component/dsl.rb', line 381 def directory(dir, mode: nil, owner: nil, group: nil) # rubocop:disable Metrics/AbcSize install_flags = ['-d'] if @component.platform.is_windows? unless mode.nil? && owner.nil? && group.nil? warn "You're trying to set the mode, owner, or group for windows. I don't know how to do that, ignoring!" end else install_flags << "-m '#{mode}'" unless mode.nil? end @component.install << "#{@component.platform.install} #{install_flags.join(' ')} '#{dir}'" @component.directories << Vanagon::Common::Pathname.new(dir, mode: mode, owner: owner, group: group) end |
#dirname(path) ⇒ Object
Set a source dir
The build dir will be created when the source archive is unpacked. This should be used when the unpacked directory name does not match the source archive name.
356 357 358 359 360 361 362 |
# File 'lib/vanagon/component/dsl.rb', line 356 def dirname(path) if Pathname.new(path).relative? @component.dirname = path else raise Vanagon::Error, "dirname should be a relative path, but '#{path}' looks to be absolute." end end |
#environment(*env) ⇒ Object
Adds a set of environment overrides to the environment for a component. This environment is included in the configure, build and install steps.
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/vanagon/component/dsl.rb', line 398 def environment(*env) # rubocop:disable Metrics/AbcSize if env.size == 1 && env.first.is_a?(Hash) warn <<-WARNING.undent the component DSL method signature #environment({Key => Value}) is deprecated and will be removed by Vanagon 1.0.0. Please update your project configurations to use the form: #environment(key, value) WARNING return @component.environment.merge!(env.first) elsif env.size == 2 return @component.environment[env.first] = env.last end raise ArgumentError, <<-WARNING.undent component DSL method #environment only accepts a single Hash (deprecated) or a key-value pair (preferred): environment({"KEY" => "value"}) environment("KEY", "value") WARNING end |
#install(&block) ⇒ Object
Set or add to the install call for the component. The commands required to install the component.
81 82 83 |
# File 'lib/vanagon/component/dsl.rb', line 81 def install(&block) @component.install << yield end |
#install_configfile(source, target, mode: '0644', owner: nil, group: nil) ⇒ Object
Shorthand to install a file and mark it as a configfile
263 264 265 266 |
# File 'lib/vanagon/component/dsl.rb', line 263 def install_configfile(source, target, mode: '0644', owner: nil, group: nil) install_file(source, target, mode: mode, owner: owner, group: group) configfile(target, mode: mode, owner: owner, group: group) end |
#install_file(source, target, mode: nil, owner: nil, group: nil) ⇒ Object
Copies a file from source to target during the install phase of the component
229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/vanagon/component/dsl.rb', line 229 def install_file(source, target, mode: nil, owner: nil, group: nil) # rubocop:disable Metrics/AbcSize @component.install << "#{@component.platform.install} -d '#{File.dirname(target)}'" @component.install << "#{@component.platform.copy} -p '#{source}' '#{target}'" if @component.platform.is_windows? unless mode.nil? && owner.nil? && group.nil? warn "You're trying to set the mode, owner, or group for windows. I don't know how to do that, ignoring!" end else mode ||= '0644' @component.install << "chmod #{mode} '#{target}'" end @component.add_file Vanagon::Common::Pathname.file(target, mode: mode, owner: owner, group: group) end |
#install_only(install_only) ⇒ Object
520 521 522 |
# File 'lib/vanagon/component/dsl.rb', line 520 def install_only(install_only) @component.install_only = install_only end |
#install_service(service_file, default_file = nil, service_name = @component.name, service_type: nil, link_target: nil) ⇒ Object
install_service adds the commands to install the various files on disk during the package build and registers the service with the project
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/vanagon/component/dsl.rb', line 169 def install_service(service_file, default_file = nil, service_name = @component.name, service_type: nil, link_target: nil) # rubocop:disable Metrics/AbcSize case @component.platform.servicetype when "sysv" target_service_file = File.join(@component.platform.servicedir, service_name) target_default_file = File.join(@component.platform.defaultdir, service_name) target_mode = '0755' default_mode = '0644' when "systemd" target_service_file = File.join(@component.platform.servicedir, "#{service_name}.service") target_default_file = File.join(@component.platform.defaultdir, service_name) target_mode = '0644' default_mode = '0644' when "launchd" target_service_file = File.join(@component.platform.servicedir, "#{service_name}.plist") target_mode = '0644' default_mode = '0644' when "smf" target_service_file = File.join(@component.platform.servicedir, service_type.to_s, "#{service_name}.xml") target_default_file = File.join(@component.platform.defaultdir, service_name) target_mode = '0644' default_mode = '0755' when "aix" @component.service = OpenStruct.new(:name => service_name, :service_command => File.read(service_file).chomp) # Return here because there is no file to install, just a string read in return when "windows" @component.service = OpenStruct.new(\ :bindir_id => "#{service_name.gsub(/[^A-Za-z0-9]/, '').upcase}BINDIR", \ :service_file => service_file, \ :component_group_id => "#{service_name.gsub(/[^A-Za-z0-9]/, '')}Component"\ ) # return here as we are just collecting the name of the service file to put into the harvest filter list. return else fail "Don't know how to install the #{@component.platform.servicetype}. Please teach #install_service how to do this." end # Install the service and default files if link_target install_file(service_file, link_target, mode: target_mode) link link_target, target_service_file else install_file(service_file, target_service_file, mode: target_mode) end if default_file install_file(default_file, target_default_file, mode: default_mode) configfile target_default_file end # Register the service for use in packaging @component.service = OpenStruct.new(:name => service_name, :service_file => target_service_file, :type => service_type) end |
#license(license) ⇒ Object
516 517 518 |
# File 'lib/vanagon/component/dsl.rb', line 516 def license(license) @component.license = license end |
#link(source, target) ⇒ Object
link will add a command to the install to create a symlink from source to target
272 273 274 275 276 277 278 279 |
# File 'lib/vanagon/component/dsl.rb', line 272 def link(source, target) @component.install << "#{@component.platform.install} -d '#{File.dirname(target)}'" # Use a bash conditional to only create the link if it doesn't already point to the correct source. # This allows rerunning the install step to be idempotent, rather than failing because the link # already exists. @component.install << "([[ '#{target}' -ef '#{source}' ]] || ln -s '#{source}' '#{target}')" @component.add_file Vanagon::Common::Pathname.file(target) end |
#load_from_json(file) ⇒ Object
Loads and parses json from a file. Will treat the keys in the json as methods to invoke on the component in question
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/vanagon/component/dsl.rb', line 101 def load_from_json(file) if File.exists?(file) data = JSON.parse(File.read(file)) raise "Hash required. Got '#{data.class}' when parsing '#{file}'" unless data.is_a?(Hash) data.each do |key, value| if self.respond_to?(key) self.send(key, value) else fail "Component does not have a '#{key}' method to invoke. Maybe your bespoke json has a typo?" end end else fail "Cannot load component data from '#{file}'. It does not exist." end end |
#mirror(url) ⇒ Object
Sets a mirror url for the source of this component
300 301 302 |
# File 'lib/vanagon/component/dsl.rb', line 300 def mirror(url) @component.mirrors << url end |
#provides(provide, version = nil) ⇒ Object
Indicates that this component provides a system level package. Provides can be collected and used by the project and package.
147 148 149 |
# File 'lib/vanagon/component/dsl.rb', line 147 def provides(provide, version = nil) @component.provides << OpenStruct.new(:provide => provide, :version => version) end |
#ref(the_ref) ⇒ Object
Sets the ref of the source for use in a git source
317 318 319 |
# File 'lib/vanagon/component/dsl.rb', line 317 def ref(the_ref) @component.[:ref] = the_ref end |
#replaces(replacement, version = nil) ⇒ Object
Indicates that this component replaces a system level package. Replaces can be collected and used by the project and package.
139 140 141 |
# File 'lib/vanagon/component/dsl.rb', line 139 def replaces(replacement, version = nil) @component.replaces << OpenStruct.new(:replacement => replacement, :version => version) end |
#requires(requirement) ⇒ Object
requires adds a requirement to the list of runtime requirements for the component
131 132 133 |
# File 'lib/vanagon/component/dsl.rb', line 131 def requires(requirement) @component.requires << requirement end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
53 54 55 |
# File 'lib/vanagon/component/dsl.rb', line 53 def respond_to_missing?(method_name, include_private = false) method_name.to_s.start_with?('get_') || super end |
#sum(value) ⇒ Object Also known as: md5sum, sha1sum, sha256sum, sha512sum
304 305 306 307 308 |
# File 'lib/vanagon/component/dsl.rb', line 304 def sum(value) type = __callee__.to_s.gsub(/sum$/, '') @component.[:sum] = value @component.[:sum_type] = type end |
#url(uri) ⇒ Object
Sets the canonical URL or URI for the upstream source of this component
292 293 294 |
# File 'lib/vanagon/component/dsl.rb', line 292 def url(uri) @component.url = uri.to_s end |
#version(ver) ⇒ Object
Sets the version for the component
284 285 286 |
# File 'lib/vanagon/component/dsl.rb', line 284 def version(ver) @component.version = ver end |