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 22 def self.artifact?(path) 23 case File.basename(path) 24 when *ARTIFACTS 25 true 26 else 27 false 28 end 29 end
# File lib/puppet/module_tool.rb 91 def self.build_tree(mods, dir) 92 mods.each do |mod| 93 version_string = mod[:version].to_s.sub(/^(?!v)/, 'v') 94 95 if mod[:action] == :upgrade 96 previous_version = mod[:previous_version].to_s.sub(/^(?!v)/, 'v') 97 version_string = "#{previous_version} -> #{version_string}" 98 end 99 100 mod[:text] = "#{mod[:name]} (#{colorize(:cyan, version_string)})" 101 mod[:text] += " [#{mod[:path]}]" unless mod[:path].to_s == dir.to_s 102 103 deps = (mod[:dependencies] || []) 104 deps.sort! { |a, b| a[:name] <=> b[:name] } 105 build_tree(deps, dir) 106 end 107 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 146 def self.environment_from_options(options) 147 if options[:modulepath] 148 path = options[:modulepath].split(File::PATH_SEPARATOR) 149 Puppet::Node::Environment.create(:anonymous, path, '') 150 elsif options[:environment].is_a?(Puppet::Node::Environment) 151 options[:environment] 152 elsif options[:environment] 153 # This use of looking up an environment is correct since it honours 154 # a request to get a particular environment via environment name. 155 Puppet.lookup(:environments).get!(options[:environment]) 156 else 157 Puppet.lookup(:current_environment) 158 end 159 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 48 def self.find_module_root(path) 49 path = Pathname.new(path) if path.class == String 50 51 path.expand_path.ascend do |p| 52 return p if is_module_root?(p) 53 end 54 55 nil 56 end
Builds a formatted tree from a list of node hashes containing :text
and :dependencies
keys.
# File lib/puppet/module_tool.rb 71 def self.format_tree(nodes, level = 0) 72 str = '' 73 nodes.each_with_index do |node, i| 74 last_node = nodes.length - 1 == i 75 deps = node[:dependencies] || [] 76 77 str << (indent = " " * level) 78 str << (last_node ? "└" : "├") 79 str << "─" 80 str << (deps.empty? ? "─" : "┬") 81 str << " #{node[:text]}\n" 82 83 branch = format_tree(deps, level + 1) 84 branch.gsub!(/^#{indent} /, indent + '│') unless last_node 85 str << branch 86 end 87 88 return str 89 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 63 def self.is_module_root?(path) 64 path = Pathname.new(path) if path.class == String 65 66 FileTest.file?(path + 'metadata.json') 67 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 170 def self.parse_module_dependency(where, dep) 171 dep_name = dep['name'].tr('/', '-') 172 range = dep['version_requirement'] || '>= 0.0.0' 173 174 begin 175 parsed_range = Module.parse_range(range) 176 rescue ArgumentError => e 177 Puppet.debug "Error in #{where} parsing dependency #{dep_name} (#{e.message}); using empty range." 178 parsed_range = SemanticPuppet::VersionRange::EMPTY_RANGE 179 end 180 181 [ dep_name, parsed_range, range ] 182 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 123 def self.set_option_defaults(options) 124 current_environment = environment_from_options(options) 125 126 modulepath = [options[:target_dir]] + current_environment.full_modulepath 127 128 face_environment = current_environment.override_with(:modulepath => modulepath.compact) 129 130 options[:environment_instance] = face_environment 131 132 # Note: environment will have expanded the path 133 options[:target_dir] = face_environment.full_modulepath.first 134 135 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 33 def self.username_and_modname_from(full_module_name) 34 matcher = full_module_name.match(FULL_MODULE_NAME_PATTERN) 35 if matcher 36 return matcher.captures 37 else 38 raise ArgumentError, _("Not a valid full name: %{full_module_name}") % { full_module_name: full_module_name } 39 end 40 end