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.
-
#force ⇒ Boolean
Overwrite any locally made changes.
-
#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_name = nil, force = 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_name = nil, force = nil) ⇒ Puppetfile
Returns a new instance of Puppetfile.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/r10k/puppetfile.rb', line 45 def initialize(basedir, moduledir = nil, puppetfile_path = nil, puppetfile_name = nil, force = nil ) @basedir = basedir @force = force || false @moduledir = moduledir || File.join(basedir, 'modules') @puppetfile_name = puppetfile_name || 'Puppetfile' @puppetfile_path = puppetfile_path || File.join(basedir, @puppetfile_name) logger.info _("Using Puppetfile '%{puppetfile}'") % {puppetfile: @puppetfile_path} @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 |
#force ⇒ Boolean
Returns Overwrite any locally made changes.
38 39 40 |
# File 'lib/r10k/puppetfile.rb', line 38 def force @force 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
154 155 156 157 158 159 160 |
# File 'lib/r10k/puppetfile.rb', line 154 def accept(visitor) visitor.visit(:puppetfile, self) do modules.each do |mod| mod.accept(visitor) end end end |
#add_module(name, args) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/r10k/puppetfile.rb', line 108 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.
136 137 138 139 140 141 142 |
# File 'lib/r10k/puppetfile.rb', line 136 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
61 62 63 64 65 66 67 |
# File 'lib/r10k/puppetfile.rb', line 61 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
69 70 71 72 73 74 75 76 |
# File 'lib/r10k/puppetfile.rb', line 69 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, NameError => e raise R10K::Error.wrap(e, _("Failed to evaluate %{path}") % {path: @puppetfile_path}) end |
#managed_directories ⇒ Object
127 128 129 130 131 |
# File 'lib/r10k/puppetfile.rb', line 127 def managed_directories self.load unless @loaded @managed_content.keys end |
#purge_exclusions ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/r10k/puppetfile.rb', line 144 def purge_exclusions exclusions = managed_directories if environment && environment.respond_to?(:desired_contents) exclusions += environment.desired_contents end exclusions end |
#set_forge(forge) ⇒ Object
93 94 95 |
# File 'lib/r10k/puppetfile.rb', line 93 def set_forge(forge) @forge = forge end |
#set_moduledir(moduledir) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/r10k/puppetfile.rb', line 98 def set_moduledir(moduledir) @moduledir = if Pathname.new(moduledir).absolute? moduledir else File.join(basedir, moduledir) end end |
#validate_no_duplicate_names(modules) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/r10k/puppetfile.rb', line 79 def validate_no_duplicate_names(modules) dupes = modules .group_by { |mod| mod.name } .select { |_, v| v.size > 1 } .map(&:first) unless dupes.empty? msg = _('Puppetfiles cannot contain duplicate module names.') msg += ' ' msg += _("Remove the duplicates of the following modules: %{dupes}" % { dupes: dupes.join(' ') }) raise R10K::Error.new(msg) end end |