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, Logging::SYSLOG_LEVELS_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

add_outputters, 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, module_exclude_regex: 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

  • module_exclude_regex (Regex) (defaults to: nil)

    A regex to exclude modules from installation. Helpful in CI environments.



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
# File 'lib/r10k/module_loader/puppetfile.rb', line 35

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

  @basedir     = cleanpath(basedir)
  @moduledir   = resolve_path(@basedir, moduledir)
  @puppetfile_path  = resolve_path(@basedir, puppetfile)
  @overrides   = overrides
  @environment = environment
  @module_exclude_regex = module_exclude_regex
  @environment_name = @environment&.name
  @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

#environment_nameObject (readonly)

Returns the value of attribute environment_name.



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

def environment_name
  @environment_name
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.



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
# File 'lib/r10k/module_loader/puppetfile.rb', line 155

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, and it ostensibly
  # has already has been deployed to disk, use it. Otherwise create a
  # regular module to sync.
  unless mod.version &&
         mod.version == @existing_module_versions_by_name[mod.name] &&
         File.directory?(mod.path)
      mod = mod.to_implementation
  end

  @modules << mod
end

#add_module_metadata(name, info) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/r10k/module_loader/puppetfile.rb', line 114

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

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

  @existing_module_metadata << mod
end

#loadObject



61
62
63
64
65
# File 'lib/r10k/module_loader/puppetfile.rb', line 61

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

#load!Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/r10k/module_loader/puppetfile.rb', line 67

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)
  @modules = filter_modules(@modules, @module_exclude_regex) if @module_exclude_regex

  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



94
95
96
97
98
# File 'lib/r10k/module_loader/puppetfile.rb', line 94

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

#load_metadata!Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/r10k/module_loader/puppetfile.rb', line 100

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 ScriptError, StandardError => e
  logger.warn _("Unable to preload Puppetfile because of %{msg}" % { msg: e.message })

  @existing_module_metadata = []
  @existing_module_versions_by_name = {}
end

#set_forge(forge) ⇒ Object

Parameters:

  • forge (String)


127
128
129
130
131
132
133
134
# File 'lib/r10k/module_loader/puppetfile.rb', line 127

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)


137
138
139
# File 'lib/r10k/module_loader/puppetfile.rb', line 137

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