Class: PuppetMetadata::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_metadata/metadata.rb

Overview

An abstraction over Puppet metadata

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata, validate = true) ⇒ Metadata

Returns a new instance of Metadata.

Parameters:

  • metadata (Hash[String, Any])

    The metadata as a data structure

  • validate (Boolean) (defaults to: true)

    Whether to validate the metadata using metadata-json-lint

Raises:

See Also:



34
35
36
37
38
39
40
41
42
# File 'lib/puppet_metadata/metadata.rb', line 34

def initialize(, validate = true)
  if validate
    require 'metadata_json_lint'
    schema_errors = MetadataJsonLint::Schema.new.validate()
    raise InvalidMetadataException, schema_errors if schema_errors.any?
  end

  @metadata = 
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



23
24
25
# File 'lib/puppet_metadata/metadata.rb', line 23

def 
  @metadata
end

Instance Method Details

#dependenciesHash[String, SemanticPuppet::VersionRange]

A hash representation of the dependencies

Every element in the original array is converted. The name is used as a key while version_requirement is used as a value. No value indicates any version is accepted.

Examples:

 = Metadata.new(data)
.dependencies.each do |os, releases|
  if releases
    releases.each do |release|
      puts "#{.name} supports #{os} #{release}"
    end
  else
    puts "#{.name} supports all #{os} releases"
  end
end

Returns:

  • (Hash[String, SemanticPuppet::VersionRange])

    The dependencies of the module

See Also:



178
179
180
# File 'lib/puppet_metadata/metadata.rb', line 178

def dependencies
  @dependencies ||= build_version_requirement_hash(['dependencies'])
end

#eol_operatingsystems(at = nil) ⇒ Hash[String, Array[String]]

Parameters:

  • at (Date) (defaults to: nil)

    The date to check the EOL time. Today is used when nil.

Returns:

  • (Hash[String, Array[String]])


97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/puppet_metadata/metadata.rb', line 97

def eol_operatingsystems(at = nil)
  at ||= Date.today

  unsupported = operatingsystems.map do |os, rels|
    next unless rels

    eol = rels.select { |rel| OperatingSystem.eol?(os, rel, at) }
    [os, eol] if eol.any?
  end

  unsupported.compact.to_h
end

#github_actions(options) ⇒ PuppetMetadata::GithubActions

Returns A GithubActions instance.

Parameters:

  • options (Hash)

    A hash containing the command line options

Returns:



191
192
193
# File 'lib/puppet_metadata/metadata.rb', line 191

def github_actions(options)
  PuppetMetadata::GithubActions.new(self, options)
end

#licenseString

The license

Returns:

  • (String)

    The license



58
59
60
# File 'lib/puppet_metadata/metadata.rb', line 58

def license
  ['license']
end

#nameString

The name

Returns:

  • (String)

    The name



46
47
48
# File 'lib/puppet_metadata/metadata.rb', line 46

def name
  ['name']
end

#operatingsystemsHash[String, Array[String]]

Returns The supported operating system and its major releases.

Returns:

  • (Hash[String, Array[String]])

    The supported operating system and its major releases



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/puppet_metadata/metadata.rb', line 64

def operatingsystems
  @operatingsystems ||= begin
    return {} if ['operatingsystem_support'].nil?

    supported = ['operatingsystem_support'].map do |os|
      next unless os['operatingsystem']

      [os['operatingsystem'], os['operatingsystemrelease']]
    end

    supported.compact.to_h
  end
end

#os_release_supported?(operatingsystem, release) ⇒ Boolean

Returns whether an operating system’s release is supported

Parameters:

  • operatingsystem (String)

    The operating system

  • release (String)

    The major release of the OS

Returns:

  • (Boolean)

    Whether an operating system’s release is supported



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/puppet_metadata/metadata.rb', line 83

def os_release_supported?(operatingsystem, release)
  # If no OS is present, everything is supported. An example of this is
  # modules with only functions.
  return true if operatingsystems.empty?

  # if the key present, nil indicates all releases are supported
  return false unless operatingsystems.key?(operatingsystem)

  releases = operatingsystems[operatingsystem]
  releases.nil? || releases.include?(release)
end

#puppet_major_versionsArray[Integer]

Returns Supported major Puppet versions.

Returns:

  • (Array[Integer])

    Supported major Puppet versions

Raises:

  • (Exception)

See Also:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/puppet_metadata/metadata.rb', line 142

def puppet_major_versions
  requirement = requirements['puppet']
  raise Exception, 'No Puppet requirement found' unless requirement

  # Current latest major is 7. It is highly recommended that modules
  # actually specify exact bounds, but this prevents an infinite loop.
  end_major = (requirement.end == SemanticPuppet::Version::MAX) ? 7 : requirement.end.major

  (requirement.begin.major..end_major).select do |major|
    requirement.include?(SemanticPuppet::Version.new(major, 0,
                                                     0)) || requirement.include?(SemanticPuppet::Version.new(major,
                                                                                                             99, 99))
  end
end

#requirementsHash[String, SemanticPuppet::VersionRange]

A hash representation of the requirements

Every element in the original array is converted. The name is used as a key while version_requirement is used as a value. No value indicates any version is accepted.

Examples:

 = Metadata.new(data)
.requirements.each do |requirement, version_requirement|
  if version_requirement
    puts "#{.name} requires #{requirement} #{version_requirement}"
  else
    puts "#{.name} requires any #{requirement}"
  end
end

Returns:

  • (Hash[String, SemanticPuppet::VersionRange])

    The requirements of the module

See Also:



129
130
131
# File 'lib/puppet_metadata/metadata.rb', line 129

def requirements
  @requirements ||= build_version_requirement_hash(['requirements'])
end

#satisfies_dependency?(name, version) ⇒ Boolean

Parameters:

  • name (String)

    The name of the dependency

  • version (String)

    The version of the dependency

Returns:

  • (Boolean)

See Also:



185
186
187
# File 'lib/puppet_metadata/metadata.rb', line 185

def satisfies_dependency?(name, version)
  matches?(dependencies[name], version)
end

#satisfies_requirement?(name, version) ⇒ Boolean

Parameters:

  • name (String)

    The name of the requirement

  • version (String)

    The version of the requirement

Returns:

  • (Boolean)

See Also:



136
137
138
# File 'lib/puppet_metadata/metadata.rb', line 136

def satisfies_requirement?(name, version)
  matches?(requirements[name], version)
end

#versionString

The version

Returns:

  • (String)


52
53
54
# File 'lib/puppet_metadata/metadata.rb', line 52

def version
  ['version']
end