Class: R10K::Puppetfile
- Inherits:
-
Object
- Object
- R10K::Puppetfile
- Includes:
- Logging, Util::Purgeable
- Defined in:
- lib/r10k/puppetfile.rb
Defined Under Namespace
Classes: DSL
Constant Summary
Constants included from Logging
Instance Attribute Summary collapse
-
#basedir ⇒ Object
readonly
Returns the value of attribute basedir.
-
#environment ⇒ R10K::Environment
Optional R10K::Environment that this Puppetfile belongs to.
-
#forge ⇒ Object
readonly
Returns the value of attribute forge.
-
#moduledir ⇒ Object
readonly
Returns the value of attribute moduledir.
-
#modules ⇒ Object
readonly
Returns the value of attribute modules.
-
#puppetfile_path ⇒ Object
readonly
Returns the value of attribute puppetfile_path.
Instance Method Summary collapse
- #accept(visitor) ⇒ Object
- #add_module(name, args) ⇒ Object
-
#desired_contents ⇒ Array<String>
Returns an array of the full paths to all the content being managed.
-
#initialize(basedir, moduledir = nil, puppetfile_path = nil) ⇒ Puppetfile
constructor
A new instance of Puppetfile.
- #load ⇒ Object
- #load! ⇒ Object
- #managed_directories ⇒ Object
- #purge_exclusions ⇒ Object
- #set_forge(forge) ⇒ Object
- #set_moduledir(moduledir) ⇒ Object
- #validate_no_duplicate_names(modules) ⇒ Object
Methods included from Util::Purgeable
#current_contents, #logger, #pending_contents, #purge!, #stale_contents
Methods included from Logging
debug_formatter, default_formatter, default_outputter, #logger, #logger_name, parse_level
Constructor Details
#initialize(basedir, moduledir = nil, puppetfile_path = nil) ⇒ Puppetfile
Returns a new instance of Puppetfile.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/r10k/puppetfile.rb', line 39 def initialize(basedir, moduledir = nil, puppetfile_path = nil) @basedir = basedir @moduledir = moduledir || File.join(basedir, 'modules') @puppetfile_path = puppetfile_path || File.join(basedir, 'Puppetfile') @modules = [] @managed_content = {} @forge = 'forgeapi.puppetlabs.com' @loaded = false end |
Instance Attribute Details
#basedir ⇒ Object (readonly)
Returns the value of attribute basedir.
22 23 24 |
# File 'lib/r10k/puppetfile.rb', line 22 def basedir @basedir end |
#environment ⇒ R10K::Environment
Returns Optional R10K::Environment that this Puppetfile belongs to.
34 35 36 |
# File 'lib/r10k/puppetfile.rb', line 34 def environment @environment end |
#forge ⇒ Object (readonly)
Returns the value of attribute forge.
14 15 16 |
# File 'lib/r10k/puppetfile.rb', line 14 def forge @forge end |
#moduledir ⇒ Object (readonly)
Returns the value of attribute moduledir.
26 27 28 |
# File 'lib/r10k/puppetfile.rb', line 26 def moduledir @moduledir end |
#modules ⇒ Object (readonly)
Returns the value of attribute modules.
18 19 20 |
# File 'lib/r10k/puppetfile.rb', line 18 def modules @modules end |
#puppetfile_path ⇒ Object (readonly)
Returns the value of attribute puppetfile_path.
30 31 32 |
# File 'lib/r10k/puppetfile.rb', line 30 def puppetfile_path @puppetfile_path end |
Instance Method Details
#accept(visitor) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/r10k/puppetfile.rb', line 141 def accept(visitor) visitor.visit(:puppetfile, self) do modules.each do |mod| mod.accept(visitor) end end end |
#add_module(name, args) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/r10k/puppetfile.rb', line 95 def add_module(name, args) if args.is_a?(Hash) && install_path = args.delete(:install_path) install_path = resolve_install_path(install_path) validate_install_path(install_path, name) else install_path = @moduledir end # Keep track of all the content this Puppetfile is managing to enable purging. @managed_content[install_path] = Array.new unless @managed_content.has_key?(install_path) mod = R10K::Module.new(name, install_path, args, @environment) @managed_content[install_path] << mod.name @modules << mod end |
#desired_contents ⇒ Array<String>
This implements a required method for the Purgeable mixin
Returns an array of the full paths to all the content being managed.
123 124 125 126 127 128 129 |
# File 'lib/r10k/puppetfile.rb', line 123 def desired_contents self.load unless @loaded @managed_content.flat_map do |install_path, modnames| modnames.collect { |name| File.join(install_path, name) } end end |
#load ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/r10k/puppetfile.rb', line 51 def load if File.readable? @puppetfile_path self.load! else logger.debug _("Puppetfile %{path} missing or unreadable") % {path: @puppetfile_path.inspect} end end |
#load! ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/r10k/puppetfile.rb', line 59 def load! dsl = R10K::Puppetfile::DSL.new(self) dsl.instance_eval(puppetfile_contents, @puppetfile_path) validate_no_duplicate_names(@modules) @loaded = true rescue SyntaxError, LoadError, ArgumentError => e raise R10K::Error.wrap(e, _("Failed to evaluate %{path}") % {path: @puppetfile_path}) end |
#managed_directories ⇒ Object
114 115 116 117 118 |
# File 'lib/r10k/puppetfile.rb', line 114 def managed_directories self.load unless @loaded @managed_content.keys end |
#purge_exclusions ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'lib/r10k/puppetfile.rb', line 131 def purge_exclusions exclusions = managed_directories if environment && environment.respond_to?(:desired_contents) exclusions += environment.desired_contents end exclusions end |
#set_forge(forge) ⇒ Object
69 70 71 |
# File 'lib/r10k/puppetfile.rb', line 69 def set_forge(forge) @forge = forge end |
#set_moduledir(moduledir) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/r10k/puppetfile.rb', line 85 def set_moduledir(moduledir) @moduledir = if Pathname.new(moduledir).absolute? moduledir else File.join(basedir, moduledir) end end |
#validate_no_duplicate_names(modules) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/r10k/puppetfile.rb', line 74 def validate_no_duplicate_names(modules) dupes = modules .group_by { |mod| mod.name } .select { |_, v| v.size > 1 }.map(&:first) unless dupes.empty? logger.warn _('Puppetfiles should not contain duplicate module names and will result in an error in r10k v3.x.') logger.warn _("Remove the duplicates of the following modules: %{dupes}" % {dupes: dupes.join(" ")}) end end |