Puppet Function: require_package

Defined in:
modules/wmflib/lib/puppet/parser/functions/require_package.rb
Function type:
Ruby 3.x API

Overview

require_package()Any

Returns:

  • (Any)


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
# File 'modules/wmflib/lib/puppet/parser/functions/require_package.rb', line 21

newfunction(:require_package, :arity => -2) do |args|
  Puppet::Parser::Functions.function :create_resources
  args.flatten.each do |package_name|
    # Puppet class names are alphanumeric + underscore
    # 'g++' package would yield: 'packages::g__'
    class_name = 'packages::' + package_name.tr('-+', '_')

    # Create host class
    host = compiler.topscope.find_hostclass(class_name)
    unless host
      host = Puppet::Resource::Type.new(:hostclass, class_name)
      compiler.environment.known_resource_types.add_hostclass(host)
    end

    # Create class scope
    cls = Puppet::Parser::Resource.new(
        'class', class_name, :scope => compiler.topscope)
    begin
      catalog.add_resource(cls)
    rescue
      nil
    end
    begin
      host.evaluate_code(cls)
    rescue
      nil
    end

    # Create package resource
    begin
      host_scope = compiler.topscope.class_scope(host)
      host_scope.call_function(:create_resources,
                               ['package', { package_name => { :ensure => 'present' } }])
    rescue Puppet::Resource::Catalog::DuplicateResourceError
      nil
    end

    # Declare dependency
    call_function :require, [class_name]
  end
end