Class: R10K::Environment::WithModules
- Includes:
- Util::Purgeable
- Defined in:
- lib/r10k/environment/with_modules.rb
Overview
This abstract base class implements an environment that can include module content
Constant Summary
Constants included from Util::Purgeable
Util::Purgeable::FN_MATCH_OPTS, Util::Purgeable::HIDDEN_FILE
Constants included from Logging
Instance Attribute Summary collapse
- #moduledir ⇒ Object readonly
Attributes inherited from Base
#basedir, #dirname, #loader, #managed_directories, #name, #path, #puppetfile, #puppetfile_name
Instance Method Summary collapse
- #accept(visitor) ⇒ Object
- #add_module(name, args) ⇒ Object
- #deploy ⇒ Object
-
#desired_contents ⇒ Array<String>
Returns an array of the full paths of filenames that should exist.
-
#initialize(name, basedir, dirname, options = {}) ⇒ WithModules
constructor
Initialize the given environment.
- #load_modules(module_hash) ⇒ Object
- #module_conflicts?(mod_b) ⇒ Boolean
-
#modules ⇒ Array<R10K::Module::Base>
All modules associated with this environment.
- #purge_exclusions ⇒ Object
Methods included from Util::Purgeable
#current_contents, #logger, #managed_directories, #matches?, #pending_contents, #potentially_purgeable, #purge!, #stale_contents
Methods included from Logging
debug_formatter, default_formatter, default_outputter, #logger, #logger_name, parse_level
Methods inherited from Base
#determine_purge_exclusions, #generate_types!, #info, #load_puppetfile_modules, #signature, #status, #sync, #whitelist
Constructor Details
#initialize(name, basedir, dirname, options = {}) ⇒ WithModules
Initialize the given environment.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/r10k/environment/with_modules.rb', line 23 def initialize(name, basedir, dirname, = {}) super @all_modules = nil @managed_content = {} @modules = [] @moduledir = case [:moduledir] when nil File.join(@basedir, @dirname, 'modules') when File.absolute_path([:moduledir]) .delete(:moduledir) else File.join(@basedir, @dirname, .delete(:moduledir)) end modhash = .delete(:modules) load_modules(modhash) unless modhash.nil? end |
Instance Attribute Details
#moduledir ⇒ Object (readonly)
12 13 14 |
# File 'lib/r10k/environment/with_modules.rb', line 12 def moduledir @moduledir end |
Instance Method Details
#accept(visitor) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/r10k/environment/with_modules.rb', line 78 def accept(visitor) visitor.visit(:environment, self) do @modules.each do |mod| mod.sync end puppetfile.accept(visitor) end end |
#add_module(name, args) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/r10k/environment/with_modules.rb', line 108 def add_module(name, args) # symbolize keys in the args hash args = args.inject({}) { |memo,(k,v)| memo[k.to_sym] = v; memo } args[:overrides] = @overrides if 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 environment 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, self.name) mod.origin = :environment @managed_content[install_path] << mod.name @modules << mod end |
#deploy ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/r10k/environment/with_modules.rb', line 88 def deploy @modules.each do |mod| mod.sync end super end |
#desired_contents ⇒ Array<String>
This implements a required method for the Purgeable mixin
Returns an array of the full paths of filenames that should exist. Files inside managed_directories that are not listed in desired_contents will be purged.
137 138 139 140 141 142 |
# File 'lib/r10k/environment/with_modules.rb', line 137 def desired_contents list = @managed_content.keys list += @managed_content.flat_map do |install_path, modnames| modnames.collect { |name| File.join(install_path, name) } end end |
#load_modules(module_hash) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/r10k/environment/with_modules.rb', line 96 def load_modules(module_hash) module_hash.each do |name, args| if !args.is_a?(Hash) args = { version: args } end add_module(name, args) end end |
#module_conflicts?(mod_b) ⇒ Boolean
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/r10k/environment/with_modules.rb', line 55 def module_conflicts?(mod_b) conflict = @modules.any? { |mod_a| mod_a.name == mod_b.name } return false unless conflict msg_vars = {src: mod_b.origin, name: mod_b.name} msg_error = _('Environment and %{src} both define the "%{name}" module' % msg_vars) msg_continue = _("#{msg_error}. The %{src} definition will be ignored" % msg_vars) case conflict_opt = @options[:module_conflicts] when 'override_and_warn', nil logger.warn msg_continue when 'override' logger.debug msg_continue when 'error' raise R10K::Error, msg_error else raise R10K::Error, _('Unexpected value for `module_conflicts` setting in %{env} ' \ 'environment: %{val}' % {env: self.name, val: conflict_opt}) end true end |
#modules ⇒ Array<R10K::Module::Base>
Returns All modules associated with this environment. Modules may originate from either:
- The r10k environment object
- A Puppetfile in the environment's content.
46 47 48 49 50 51 52 53 |
# File 'lib/r10k/environment/with_modules.rb', line 46 def modules if @all_modules.nil? puppetfile_modules = super() @all_modules = @modules + puppetfile_modules end @all_modules end |
#purge_exclusions ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/r10k/environment/with_modules.rb', line 144 def purge_exclusions super + @managed_content.flat_map do |install_path, modnames| modnames.map do |name| File.join(install_path, name, '**', '*') end end end |