File: ensure_packages.rb

package info (click to toggle)
puppet-module-puppetlabs-stdlib 9.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,448 kB
  • sloc: ruby: 3,522; sh: 46; makefile: 2
file content (61 lines) | stat: -rw-r--r-- 2,291 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true

# @summary Takes a list of packages and only installs them if they don't already exist.
#
# It optionally takes a hash as a second parameter that will be passed as the
# third argument to the ensure_resource() function.
Puppet::Functions.create_function(:'stdlib::ensure_packages', Puppet::Functions::InternalFunction) do
  # @param packages
  #   The packages to ensure are installed.
  # @param default_attributes
  #   Default attributes to be passed to the `ensure_resource()` function
  # @return [Undef] Returns nothing.
  dispatch :ensure_packages do
    scope_param
    param 'Variant[String[1], Array[String[1]]]', :packages
    optional_param 'Hash', :default_attributes
    return_type 'Undef'
  end

  # @param packages
  #   The packages to ensure are installed. The keys are packages and values are the attributes specific to that package.
  # @param default_attributes
  #   Default attributes. Package specific attributes from the `packages` parameter will take precedence.
  # @return [Undef] Returns nothing.
  dispatch :ensure_packages_hash do
    scope_param
    param 'Hash[String[1], Any]', :packages
    optional_param 'Hash', :default_attributes
    return_type 'Undef'
  end

  def ensure_packages(scope, packages, default_attributes = {})
    Array(packages).each do |package_name|
      defaults = { 'ensure' => 'installed' }.merge(default_attributes)

      # `present` and `installed` are aliases for the `ensure` attribute. If `ensure` is set to either of these values replace
      # with `installed` by default but `present` if this package is already in the catalog with `ensure => present`
      defaults['ensure'] = default_ensure(package_name) if ['present', 'installed'].include?(defaults['ensure'])

      scope.call_function('ensure_resource', ['package', package_name, defaults])
    end
    nil
  end

  def ensure_packages_hash(scope, packages, default_attributes = {})
    packages.each do |package, attributes|
      ensure_packages(scope, package, default_attributes.merge(attributes))
    end
    nil
  end

  private

  def default_ensure(package_name)
    if call_function('defined_with_params', "Package[#{package_name}]", { 'ensure' => 'present' })
      'present'
    else
      'installed'
    end
  end
end