1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
|
# frozen_string_literal: true
require_relative '../puppet/util/logging'
require_relative 'module/task'
require_relative 'module/plan'
require_relative '../puppet/util/json'
require 'semantic_puppet/gem_version'
# Support for modules
class Puppet::Module
class Error < Puppet::Error; end
class MissingModule < Error; end
class IncompatibleModule < Error; end
class UnsupportedPlatform < Error; end
class IncompatiblePlatform < Error; end
class MissingMetadata < Error; end
class FaultyMetadata < Error; end
class InvalidName < Error; end
class InvalidFilePattern < Error; end
include Puppet::Util::Logging
FILETYPES = {
"manifests" => "manifests",
"files" => "files",
"templates" => "templates",
"plugins" => "lib",
"pluginfacts" => "facts.d",
"locales" => "locales",
"scripts" => "scripts",
}
# Find and return the +module+ that +path+ belongs to. If +path+ is
# absolute, or if there is no module whose name is the first component
# of +path+, return +nil+
def self.find(modname, environment = nil)
return nil unless modname
# Unless a specific environment is given, use the current environment
env = environment ? Puppet.lookup(:environments).get!(environment) : Puppet.lookup(:current_environment)
env.module(modname)
end
def self.is_module_directory?(name, path)
# it must be a directory
fullpath = File.join(path, name)
return false unless Puppet::FileSystem.directory?(fullpath)
is_module_directory_name?(name)
end
def self.is_module_directory_name?(name)
# it must match an installed module name according to forge validator
return true if name =~ /^[a-z][a-z0-9_]*$/
false
end
def self.is_module_namespaced_name?(name)
# it must match the full module name according to forge validator
return true if name =~ /^[a-zA-Z0-9]+-[a-z][a-z0-9_]*$/
false
end
# @api private
def self.parse_range(range)
SemanticPuppet::VersionRange.parse(range)
end
attr_reader :name, :environment, :path, :metadata
attr_writer :environment
attr_accessor :dependencies, :forge_name
attr_accessor :source, :author, :version, :license, :summary, :description, :project_page
def initialize(name, path, environment)
@name = name
@path = path
@environment = environment
assert_validity
load_metadata
@absolute_path_to_manifests = Puppet::FileSystem::PathPattern.absolute(manifests)
end
# @deprecated The puppetversion module metadata field is no longer used.
def puppetversion
nil
end
# @deprecated The puppetversion module metadata field is no longer used.
def puppetversion=(something)
end
# @deprecated The puppetversion module metadata field is no longer used.
def validate_puppet_version
nil
end
def has_metadata?
load_metadata
@metadata.is_a?(Hash) && !@metadata.empty?
rescue Puppet::Module::MissingMetadata
false
end
FILETYPES.each do |type, location|
# A boolean method to let external callers determine if
# we have files of a given type.
define_method(type + '?') do
type_subpath = subpath(location)
unless Puppet::FileSystem.exist?(type_subpath)
Puppet.debug { "No #{type} found in subpath '#{type_subpath}' (file / directory does not exist)" }
return false
end
true
end
# A method for returning a given file of a given type.
# e.g., file = mod.manifest("my/manifest.pp")
#
# If the file name is nil, then the base directory for the
# file type is passed; this is used for fileserving.
define_method(type.sub(/s$/, '')) do |file|
# If 'file' is nil then they're asking for the base path.
# This is used for things like fileserving.
if file
full_path = File.join(subpath(location), file)
else
full_path = subpath(location)
end
return nil unless Puppet::FileSystem.exist?(full_path)
full_path
end
# Return the base directory for the given type
define_method(type) do
subpath(location)
end
end
def tasks_directory
subpath("tasks")
end
def tasks
return @tasks if instance_variable_defined?(:@tasks)
if Puppet::FileSystem.exist?(tasks_directory)
@tasks = Puppet::Module::Task.tasks_in_module(self)
else
@tasks = []
end
end
# This is a re-implementation of the Filetypes singular type method (e.g.
# `manifest('my/manifest.pp')`. We don't implement the full filetype "API" for
# tasks since tasks don't map 1:1 onto files.
def task_file(name)
# If 'file' is nil then they're asking for the base path.
# This is used for things like fileserving.
if name
full_path = File.join(tasks_directory, name)
else
full_path = tasks_directory
end
if Puppet::FileSystem.exist?(full_path)
full_path
else
nil
end
end
def plans_directory
subpath("plans")
end
def plans
return @plans if instance_variable_defined?(:@plans)
if Puppet::FileSystem.exist?(plans_directory)
@plans = Puppet::Module::Plan.plans_in_module(self)
else
@plans = []
end
end
# This is a re-implementation of the Filetypes singular type method (e.g.
# `manifest('my/manifest.pp')`. We don't implement the full filetype "API" for
# plans.
def plan_file(name)
# If 'file' is nil then they're asking for the base path.
# This is used for things like fileserving.
if name
full_path = File.join(plans_directory, name)
else
full_path = plans_directory
end
if Puppet::FileSystem.exist?(full_path)
full_path
else
nil
end
end
def license_file
return @license_file if defined?(@license_file)
return @license_file = nil unless path
@license_file = File.join(path, "License")
end
def read_metadata
md_file = metadata_file
return {} if md_file.nil?
content = File.read(md_file, :encoding => 'utf-8')
content.empty? ? {} : Puppet::Util::Json.load(content)
rescue Errno::ENOENT
{}
rescue Puppet::Util::Json::ParseError => e
# TRANSLATORS 'metadata.json' is a specific file name and should not be translated.
msg = _("%{name} has an invalid and unparsable metadata.json file. The parse error: %{error}") % { name: name, error: e.message }
case Puppet[:strict]
when :off
Puppet.debug(msg)
when :warning
Puppet.warning(msg)
when :error
raise FaultyMetadata, msg
end
{}
end
def load_metadata
return if instance_variable_defined?(:@metadata)
@metadata = data = read_metadata
return if data.empty?
@forge_name = data['name'].tr('-', '/') if data['name']
[:source, :author, :version, :license, :dependencies].each do |attr|
value = data[attr.to_s]
raise MissingMetadata, "No #{attr} module metadata provided for #{name}" if value.nil?
if attr == :dependencies
unless value.is_a?(Array)
raise MissingMetadata, "The value for the key dependencies in the file metadata.json of the module #{name} must be an array, not: '#{value}'"
end
value.each do |dep|
name = dep['name']
dep['name'] = name.tr('-', '/') unless name.nil?
dep['version_requirement'] ||= '>= 0.0.0'
end
end
send(attr.to_s + "=", value)
end
end
# Return the list of manifests matching the given glob pattern,
# defaulting to 'init.pp' for empty modules.
def match_manifests(rest)
if rest
wanted_manifests = wanted_manifests_from(rest)
searched_manifests = wanted_manifests.glob.reject { |f| FileTest.directory?(f) }
else
searched_manifests = []
end
# (#4220) Always ensure init.pp in case class is defined there.
init_manifest = manifest("init.pp")
if !init_manifest.nil? && !searched_manifests.include?(init_manifest)
searched_manifests.unshift(init_manifest)
end
searched_manifests
end
def all_manifests
return [] unless Puppet::FileSystem.exist?(manifests)
Dir.glob(File.join(manifests, '**', '*.pp'))
end
def metadata_file
return @metadata_file if defined?(@metadata_file)
return @metadata_file = nil unless path
@metadata_file = File.join(path, "metadata.json")
end
def hiera_conf_file
unless defined?(@hiera_conf_file)
@hiera_conf_file = path.nil? ? nil : File.join(path, Puppet::Pops::Lookup::HieraConfig::CONFIG_FILE_NAME)
end
@hiera_conf_file
end
def has_hiera_conf?
hiera_conf_file.nil? ? false : Puppet::FileSystem.exist?(hiera_conf_file)
end
def modulepath
File.dirname(path) if path
end
# Find all plugin directories. This is used by the Plugins fileserving mount.
def plugin_directory
subpath("lib")
end
def plugin_fact_directory
subpath("facts.d")
end
# @return [String]
def locale_directory
subpath("locales")
end
# Returns true if the module has translation files for the
# given locale.
# @param [String] locale the two-letter language code to check
# for translations
# @return true if the module has a directory for the locale, false
# false otherwise
def has_translations?(locale)
Puppet::FileSystem.exist?(File.join(locale_directory, locale))
end
def has_external_facts?
File.directory?(plugin_fact_directory)
end
def supports(name, version = nil)
@supports ||= []
@supports << [name, version]
end
def to_s
result = "Module #{name}"
result += "(#{path})" if path
result
end
def dependencies_as_modules
dependent_modules = []
dependencies and dependencies.each do |dep|
_, dep_name = dep["name"].split('/')
found_module = environment.module(dep_name)
dependent_modules << found_module if found_module
end
dependent_modules
end
def required_by
environment.module_requirements[forge_name] || {}
end
# Identify and mark unmet dependencies. A dependency will be marked unmet
# for the following reasons:
#
# * not installed and is thus considered missing
# * installed and does not meet the version requirements for this module
# * installed and doesn't use semantic versioning
#
# Returns a list of hashes representing the details of an unmet dependency.
#
# Example:
#
# [
# {
# :reason => :missing,
# :name => 'puppetlabs-mysql',
# :version_constraint => 'v0.0.1',
# :mod_details => {
# :installed_version => '0.0.1'
# }
# :parent => {
# :name => 'puppetlabs-bacula',
# :version => 'v1.0.0'
# }
# }
# ]
#
def unmet_dependencies
unmet_dependencies = []
return unmet_dependencies unless dependencies
dependencies.each do |dependency|
name = dependency['name']
version_string = dependency['version_requirement'] || '>= 0.0.0'
dep_mod = begin
environment.module_by_forge_name(name)
rescue
nil
end
error_details = {
:name => name,
:version_constraint => version_string.gsub(/^(?=\d)/, "v"),
:parent => {
:name => forge_name,
:version => version.gsub(/^(?=\d)/, "v")
},
:mod_details => {
:installed_version => dep_mod.nil? ? nil : dep_mod.version
}
}
unless dep_mod
error_details[:reason] = :missing
unmet_dependencies << error_details
next
end
next unless version_string
begin
required_version_semver_range = self.class.parse_range(version_string)
actual_version_semver = SemanticPuppet::Version.parse(dep_mod.version)
rescue ArgumentError
error_details[:reason] = :non_semantic_version
unmet_dependencies << error_details
next
end
next if required_version_semver_range.include? actual_version_semver
error_details[:reason] = :version_mismatch
unmet_dependencies << error_details
next
end
unmet_dependencies
end
def ==(other)
name == other.name &&
version == other.version &&
path == other.path &&
environment == other.environment
end
private
def wanted_manifests_from(pattern)
begin
extended = File.extname(pattern).empty? ? "#{pattern}.pp" : pattern
relative_pattern = Puppet::FileSystem::PathPattern.relative(extended)
rescue Puppet::FileSystem::PathPattern::InvalidPattern => error
raise Puppet::Module::InvalidFilePattern.new(
"The pattern \"#{pattern}\" to find manifests in the module \"#{name}\" " \
"is invalid and potentially unsafe.", error
)
end
relative_pattern.prefix_with(@absolute_path_to_manifests)
end
def subpath(type)
File.join(path, type)
end
def assert_validity
if !Puppet::Module.is_module_directory_name?(@name) && !Puppet::Module.is_module_namespaced_name?(@name)
raise InvalidName, _(<<-ERROR_STRING).chomp % { name: @name }
Invalid module name '%{name}'; module names must match either:
An installed module name (ex. modulename) matching the expression /^[a-z][a-z0-9_]*$/ -or-
A namespaced module name (ex. author-modulename) matching the expression /^[a-zA-Z0-9]+[-][a-z][a-z0-9_]*$/
ERROR_STRING
end
end
end
|