module Puppet::ModuleTool
Constants
- ARTIFACTS
Directory and names that should not be checksummed.
- FULL_MODULE_NAME_PATTERN
- REPOSITORY_URL
Public Class Methods
Is this a directory that shouldn't be checksummed?
TODO: Should this be part of Checksums? TODO: Rename this method to reflect its purpose? TODO: Shouldn't this be used when building packages too?
# File lib/puppet/module_tool.rb 23 def self.artifact?(path) 24 case File.basename(path) 25 when *ARTIFACTS 26 true 27 else 28 false 29 end 30 end
# File lib/puppet/module_tool.rb 92 def self.build_tree(mods, dir) 93 mods.each do |mod| 94 version_string = mod[:version].to_s.sub(/^(?!v)/, 'v') 95 96 if mod[:action] == :upgrade 97 previous_version = mod[:previous_version].to_s.sub(/^(?!v)/, 'v') 98 version_string = "#{previous_version} -> #{version_string}" 99 end 100 101 mod[:text] = "#{mod[:name]} (#{colorize(:cyan, version_string)})" 102 mod[:text] += " [#{mod[:path]}]" unless mod[:path].to_s == dir.to_s 103 104 deps = (mod[:dependencies] || []) 105 deps.sort! { |a, b| a[:name] <=> b[:name] } 106 build_tree(deps, dir) 107 end 108 end
Given a hash of options, we should discover or create a {Puppet::Node::Environment} instance that reflects the provided options.
Generally speaking, the `:modulepath` parameter should supersede all others, the `:environment` parameter should follow after that, and we should default to Puppet's current environment.
@param options [{Symbol => Object}] the options to derive environment from @return [Puppet::Node::Environment] the environment described by the options
# File lib/puppet/module_tool.rb 147 def self.environment_from_options(options) 148 if options[:modulepath] 149 path = options[:modulepath].split(File::PATH_SEPARATOR) 150 Puppet::Node::Environment.create(:anonymous, path, '') 151 elsif options[:environment].is_a?(Puppet::Node::Environment) 152 options[:environment] 153 elsif options[:environment] 154 # This use of looking up an environment is correct since it honours 155 # a request to get a particular environment via environment name. 156 Puppet.lookup(:environments).get!(options[:environment]) 157 else 158 Puppet.lookup(:current_environment) 159 end 160 end
Find the module root when given a path by checking each directory up from its current location until it finds one that satisfies is_module_root?
@param path [Pathname, String] path to start from @return [Pathname, nil] the root path of the module directory or nil if
we cannot find one
# File lib/puppet/module_tool.rb 49 def self.find_module_root(path) 50 path = Pathname.new(path) if path.class == String 51 52 path.expand_path.ascend do |p| 53 return p if is_module_root?(p) 54 end 55 56 nil 57 end
Builds a formatted tree from a list of node hashes containing :text and :dependencies keys.
# File lib/puppet/module_tool.rb 72 def self.format_tree(nodes, level = 0) 73 str = String.new 74 nodes.each_with_index do |node, i| 75 last_node = nodes.length - 1 == i 76 deps = node[:dependencies] || [] 77 78 str << (indent = " " * level) 79 str << (last_node ? "└" : "├") 80 str << "─" 81 str << (deps.empty? ? "─" : "┬") 82 str << " #{node[:text]}\n" 83 84 branch = format_tree(deps, level + 1) 85 branch.gsub!(/^#{indent} /, indent + '│') unless last_node 86 str << branch 87 end 88 89 return str 90 end
Analyse path to see if it is a module root directory by detecting a file named 'metadata.json'
@param path [Pathname, String] path to analyse @return [Boolean] true if the path is a module root, false otherwise
# File lib/puppet/module_tool.rb 64 def self.is_module_root?(path) 65 path = Pathname.new(path) if path.class == String 66 67 FileTest.file?(path + 'metadata.json') 68 end
Handles parsing of module dependency expressions into proper {SemanticPuppet::VersionRange}s, including reasonable error handling.
@param where [String] a description of the thing we're parsing the
dependency expression for
@param dep [Hash] the dependency description to parse @return [Array(String, SemanticPuppet::VersionRange, String)] a tuple of the
dependent module's name, the version range dependency, and the unparsed range expression.
# File lib/puppet/module_tool.rb 171 def self.parse_module_dependency(where, dep) 172 dep_name = dep['name'].tr('/', '-') 173 range = dep['version_requirement'] || '>= 0.0.0' 174 175 begin 176 parsed_range = Module.parse_range(range) 177 rescue ArgumentError => e 178 Puppet.debug "Error in #{where} parsing dependency #{dep_name} (#{e.message}); using empty range." 179 parsed_range = SemanticPuppet::VersionRange::EMPTY_RANGE 180 end 181 182 [ dep_name, parsed_range, range ] 183 end
@param options [Hash<Symbol,String>] This hash will contain any
command-line arguments that are not Settings, as those will have already been extracted by the underlying application code.
@note Unfortunately the whole point of this method is the side effect of modifying the options parameter. This same hash is referenced both when_invoked and when_rendering. For this reason, we are not returning a duplicate. @todo Validate the above note…
An :environment_instance and a :target_dir are added/updated in the options parameter.
@api private
# File lib/puppet/module_tool.rb 124 def self.set_option_defaults(options) 125 current_environment = environment_from_options(options) 126 127 modulepath = [options[:target_dir]] + current_environment.full_modulepath 128 129 face_environment = current_environment.override_with(:modulepath => modulepath.compact) 130 131 options[:environment_instance] = face_environment 132 133 # Note: environment will have expanded the path 134 options[:target_dir] = face_environment.full_modulepath.first 135 136 end
Return the username and modname for a given full_module_name, or raise an ArgumentError if the argument isn't parseable.
# File lib/puppet/module_tool.rb 34 def self.username_and_modname_from(full_module_name) 35 matcher = full_module_name.match(FULL_MODULE_NAME_PATTERN) 36 if matcher 37 return matcher.captures 38 else 39 raise ArgumentError, _("Not a valid full name: %{full_module_name}") % { full_module_name: full_module_name } 40 end 41 end