Class: R10K::ModuleLoader::Puppetfile

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/r10k/module_loader/puppetfile.rb,
lib/r10k/module_loader/puppetfile/dsl.rb

Defined Under Namespace

Classes: DSL

Constant Summary collapse

DEFAULT_MODULEDIR =
'modules'
DEFAULT_PUPPETFILE_NAME =
'Puppetfile'

Constants included from Logging

Logging::LOG_LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

debug_formatter, default_formatter, default_outputter, #logger, #logger_name, parse_level

Constructor Details

#initialize(basedir:, moduledir: DEFAULT_MODULEDIR, puppetfile: DEFAULT_PUPPETFILE_NAME, overrides: {}, environment: nil) ⇒ Puppetfile

Returns a new instance of Puppetfile.

Parameters:

  • basedir (String)

    The path that contains the moduledir & Puppetfile by default. May be an environment, project, or simple directory.

  • puppetfile (String) (defaults to: DEFAULT_PUPPETFILE_NAME)

    The path to the Puppetfile, either an absolute full path or a relative path with regards to the basedir.

  • moduledir (String) (defaults to: DEFAULT_MODULEDIR)

    The path to the moduledir, either an absolute full path or a relative path with regards to the basedir.

  • forge (String)

    The url (without protocol) to the Forge

  • overrides (Hash) (defaults to: {})

    Configuration for loaded modules’ behavior

  • environment (R10K::Environment) (defaults to: nil)

    When provided, the environment in which loading takes place



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/r10k/module_loader/puppetfile.rb', line 32

def initialize(basedir:,
               moduledir: DEFAULT_MODULEDIR,
               puppetfile: DEFAULT_PUPPETFILE_NAME,
               overrides: {},
               environment: nil)

  @basedir     = cleanpath(basedir)
  @moduledir   = resolve_path(@basedir, moduledir)
  @puppetfile_path  = resolve_path(@basedir, puppetfile)
  @overrides   = overrides
  @environment = environment
  @default_branch_override = @overrides.dig(:environments, :default_branch_override)
  @allow_puppetfile_forge = @overrides.dig(:forge, :allow_puppetfile_override)

  @existing_module_metadata = []
  @existing_module_versions_by_name = {}
  @modules = []

  @managed_directories = []
  @desired_contents = []
  @purge_exclusions = []
end

Instance Attribute Details

#default_branch_overrideObject

Returns the value of attribute default_branch_override.



17
18
19
# File 'lib/r10k/module_loader/puppetfile.rb', line 17

def default_branch_override
  @default_branch_override
end

#desired_contentsObject (readonly)

Returns the value of attribute desired_contents.



18
19
20
# File 'lib/r10k/module_loader/puppetfile.rb', line 18

def desired_contents
  @desired_contents
end

#environmentObject

Returns the value of attribute environment.



17
18
19
# File 'lib/r10k/module_loader/puppetfile.rb', line 17

def environment
  @environment
end

#managed_directoriesObject (readonly)

Returns the value of attribute managed_directories.



18
19
20
# File 'lib/r10k/module_loader/puppetfile.rb', line 18

def managed_directories
  @managed_directories
end

#moduledirObject (readonly)

Returns the value of attribute moduledir.



18
19
20
# File 'lib/r10k/module_loader/puppetfile.rb', line 18

def moduledir
  @moduledir
end

#modulesObject (readonly)

Returns the value of attribute modules.



18
19
20
# File 'lib/r10k/module_loader/puppetfile.rb', line 18

def modules
  @modules
end

#puppetfile_pathObject (readonly)

Returns the value of attribute puppetfile_path.



18
19
20
# File 'lib/r10k/module_loader/puppetfile.rb', line 18

def puppetfile_path
  @puppetfile_path
end

#purge_exclusionsObject (readonly)

Returns the value of attribute purge_exclusions.



18
19
20
# File 'lib/r10k/module_loader/puppetfile.rb', line 18

def purge_exclusions
  @purge_exclusions
end

Instance Method Details

#add_module(name, info) ⇒ Object

Parameters:

  • name (String)
  • info (Hash, String, Symbol, nil)

    Calling with anything but a Hash is deprecated. The DSL will now convert String and Symbol versions to Hashes of the shape

    { version: <String or Symbol> }
    

    String inputs should be valid module versions, the Symbol ‘:latest` is allowed, as well as `nil`.

    Non-Hash inputs are only ever used by Forge modules. In future versions this method will require the caller (the DSL class, not the Puppetfile author) to do this conversion itself.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/r10k/module_loader/puppetfile.rb', line 145

def add_module(name, info)
  install_path, , spec_deletable = parse_module_definition(name, info)

  mod = R10K::Module.(name, install_path, , @environment)
  mod.origin = :puppetfile
  mod.spec_deletable = spec_deletable

  # Do not save modules if they would conflict with the attached
  # environment
  if @environment && @environment.module_conflicts?(mod)
    return @modules
  end

  # If this module's metadata has a static version and that version
  # matches the existing module declaration use it, otherwise create
  # a regular module to sync.
  unless mod.version && (mod.version == @existing_module_versions_by_name[mod.name])
    mod = mod.to_implementation
  end

  @modules << mod
end

#add_module_metadata(name, info) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/r10k/module_loader/puppetfile.rb', line 104

def (name, info)
  install_path, , _ = parse_module_definition(name, info)

  mod = R10K::Module.(name, install_path, , @environment)

  @existing_module_metadata << mod
end

#loadObject



55
56
57
58
59
# File 'lib/r10k/module_loader/puppetfile.rb', line 55

def load
  with_readable_puppetfile(@puppetfile_path) do
    self.load!
  end
end

#load!Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/r10k/module_loader/puppetfile.rb', line 61

def load!
  logger.info _("Using Puppetfile '%{puppetfile}'") % {puppetfile: @puppetfile_path}
  logger.debug _("Using moduledir '%{moduledir}'") % {moduledir: @moduledir}

  dsl = R10K::ModuleLoader::Puppetfile::DSL.new(self)
  dsl.instance_eval(puppetfile_content(@puppetfile_path), @puppetfile_path)

  validate_no_duplicate_names(@modules)

  managed_content = @modules.group_by(&:dirname)

  @managed_directories = determine_managed_directories(managed_content)
  @desired_contents = determine_desired_contents(managed_content)
  @purge_exclusions = determine_purge_exclusions(@managed_directories)

  {
    modules: @modules,
    managed_directories: @managed_directories,
    desired_contents: @desired_contents,
    purge_exclusions: @purge_exclusions
  }

rescue SyntaxError, LoadError, ArgumentError, NameError => e
  raise R10K::Error.wrap(e, _("Failed to evaluate %{path}") % {path: @puppetfile_path})
end

#load_metadataObject



87
88
89
90
91
# File 'lib/r10k/module_loader/puppetfile.rb', line 87

def 
  with_readable_puppetfile(@puppetfile_path) do
    self.load_metadata!
  end
end

#load_metadata!Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/r10k/module_loader/puppetfile.rb', line 93

def load_metadata!
  dsl = R10K::ModuleLoader::Puppetfile::DSL.new(self, metadata_only: true)
  dsl.instance_eval(puppetfile_content(@puppetfile_path), @puppetfile_path)

  @existing_module_versions_by_name = @existing_module_metadata.map {|mod| [ mod.name, mod.version ] }.to_h
  empty_load_output.merge(modules: @existing_module_metadata)

rescue SyntaxError, LoadError, ArgumentError, NameError => e
  logger.warn _("Unable to preload Puppetfile because of %{msg}" % { msg: e.message })
end

#set_forge(forge) ⇒ Object

Parameters:

  • forge (String)


117
118
119
120
121
122
123
124
# File 'lib/r10k/module_loader/puppetfile.rb', line 117

def set_forge(forge)
  if @allow_puppetfile_forge
    logger.debug _("Using Forge from Puppetfile: %{forge}") % { forge: forge }
    PuppetForge.host = forge
  else
    logger.debug _("Ignoring Forge declaration in Puppetfile, using value from settings: %{forge}.") % { forge: PuppetForge.host }
  end
end

#set_moduledir(moduledir) ⇒ Object

Parameters:

  • moduledir (String)


127
128
129
# File 'lib/r10k/module_loader/puppetfile.rb', line 127

def set_moduledir(moduledir)
  @moduledir = resolve_path(@basedir, moduledir)
end