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



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_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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/r10k/module_loader/puppetfile.rb', line 152

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



111
112
113
114
115
116
117
# File 'lib/r10k/module_loader/puppetfile.rb', line 111

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

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

   << mod
end

#basedirString

Deprecated.

Returns The base directory that contains the Puppetfile.



180
181
182
183
# File 'lib/r10k/module_loader/puppetfile.rb', line 180

def basedir
  logger.warn _('"basedir" is deprecated. Please use "environment_name" instead. "basedir" will be removed in a future version.')
  @basedir
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
# 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 = .map {|mod| [ mod.name, mod.version ] }.to_h
  empty_load_output.merge(modules: )

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

#set_forge(forge) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/r10k/module_loader/puppetfile.rb', line 124

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



134
135
136
# File 'lib/r10k/module_loader/puppetfile.rb', line 134

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