Class: PDK::Module::Metadata

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

Constant Summary collapse

OPERATING_SYSTEMS =
{
  'RedHat based Linux' => [
    {
      'operatingsystem' => 'CentOS',
      'operatingsystemrelease' => ['7', '8', '9']
    },
    {
      'operatingsystem' => 'OracleLinux',
      'operatingsystemrelease' => ['7']
    },
    {
      'operatingsystem' => 'RedHat',
      'operatingsystemrelease' => ['7', '8', '9']
    },
    {
      'operatingsystem' => 'Scientific',
      'operatingsystemrelease' => ['7']
    },
    {
      'operatingsystem' => 'Rocky',
      'operatingsystemrelease' => ['8']
    },
    {
      'operatingsystem' => 'AlmaLinux',
      'operatingsystemrelease' => ['8']
    }
  ],
  'Debian based Linux' => [
    {
      'operatingsystem' => 'Debian',
      'operatingsystemrelease' => ['10', '11', '12']
    },
    {
      'operatingsystem' => 'Ubuntu',
      'operatingsystemrelease' => ['18.04', '20.04', '22.04']
    }
  ],
  'Fedora' => {
    'operatingsystem' => 'Fedora',
    'operatingsystemrelease' => ['40']
  },
  'OSX' => {
    'operatingsystem' => 'Darwin',
    'operatingsystemrelease' => ['21', '22', '23']
  },
  'SLES' => {
    'operatingsystem' => 'SLES',
    'operatingsystemrelease' => ['15']
  },
  'Solaris' => {
    'operatingsystem' => 'Solaris',
    'operatingsystemrelease' => ['11']
  },
  'Windows' => {
    'operatingsystem' => 'windows',
    'operatingsystemrelease' => ['2019', '2022', '10', '11']
  },
  'AIX' => {
    'operatingsystem' => 'AIX',
    'operatingsystemrelease' => ['7.2', '7.3']
  }
}.freeze
DEFAULT_OPERATING_SYSTEMS =
[
  'RedHat based Linux',
  'Debian based Linux',
  'Windows'
].freeze
DEFAULTS =
{
  'name' => nil,
  'version' => '0.1.0',
  'author' => nil,
  'summary' => '',
  'license' => 'Apache-2.0',
  'source' => '',
  'project_page' => nil,
  'issues_url' => nil,
  'dependencies' => [],
  'data_provider' => nil,
  'operatingsystem_support' => DEFAULT_OPERATING_SYSTEMS.dup,
  'requirements' => [
    { 'name' => 'puppet', 'version_requirement' => '>= 7.24 < 9.0.0' }
  ]
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Metadata

Returns a new instance of Metadata.



94
95
96
97
# File 'lib/pdk/module/metadata.rb', line 94

def initialize(params = {})
  @data = DEFAULTS.dup
  update!(params) if params
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/pdk/module/metadata.rb', line 6

def data
  @data
end

Class Method Details

.from_file(metadata_json_path) ⇒ Object

Raises:

  • (ArgumentError)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/pdk/module/metadata.rb', line 99

def self.from_file()
  raise ArgumentError, 'Cannot read metadata from file: no path to file was given.' if .nil?

  raise ArgumentError, format("'%{file}' does not exist or is not a file.", file: ) unless PDK::Util::Filesystem.file?()

  raise ArgumentError, format("Unable to open '%{file}' for reading.", file: ) unless PDK::Util::Filesystem.readable?()

  require 'json'
  begin
    data = JSON.parse(PDK::Util::Filesystem.read_file())
  rescue JSON::JSONError => e
    raise ArgumentError, format('Invalid JSON in metadata.json: %{msg}', msg: e.message)
  end

  require 'pdk/util'
  data['template-url'] = PDK::Util::TemplateURI.default_template_uri. if PDK::Util.package_install? && data['template-url'] == PDK::Util::TemplateURI::PACKAGED_TEMPLATE_KEYWORD
  new(data)
end

Instance Method Details

#forge_ready?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/pdk/module/metadata.rb', line 135

def forge_ready?
  missing_fields.empty?
end

#interview_for_forge!Object



139
140
141
142
143
# File 'lib/pdk/module/metadata.rb', line 139

def interview_for_forge!
  require 'pdk/generate/module'

  PDK::Generate::Module.module_interview(self, only_ask: missing_fields)
end

#missing_fieldsObject



164
165
166
167
# File 'lib/pdk/module/metadata.rb', line 164

def missing_fields
  fields = DEFAULTS.keys - ['data_provider', 'requirements', 'dependencies']
  fields.select { |key| @data[key].nil? || @data[key].empty? }
end

#puppet_requirementObject



158
159
160
161
162
# File 'lib/pdk/module/metadata.rb', line 158

def puppet_requirement
  @data['requirements'].find do |r|
    r.key?('name') && r['name'] == 'puppet'
  end
end

#to_json(*_args) ⇒ Object



125
126
127
128
129
# File 'lib/pdk/module/metadata.rb', line 125

def to_json(*_args)
  require 'json'

  JSON.pretty_generate(@data.dup.delete_if { |_key, value| value.nil? })
end

#update!(data) ⇒ Object



118
119
120
121
122
123
# File 'lib/pdk/module/metadata.rb', line 118

def update!(data)
  # TODO: validate all data
  process_name(data) if data['name']
  @data.merge!(data)
  self
end

#validate_puppet_version_requirement!Object

Raises:

  • (ArgumentError)


145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/pdk/module/metadata.rb', line 145

def validate_puppet_version_requirement!
  msgs = {
    no_reqs: 'Module metadata does not contain any requirements.',
    no_puppet_req: 'Module metadata does not contain a "puppet" requirement.',
    no_puppet_ver: 'The "puppet" requirement in module metadata does not specify a "version_requirement".'
  }

  raise ArgumentError, msgs[:no_reqs] unless @data.key?('requirements')
  raise ArgumentError, msgs[:no_puppet_req] if puppet_requirement.nil?
  raise ArgumentError, msgs[:no_puppet_ver] unless puppet_requirement.key?('version_requirement')
  raise ArgumentError, msgs[:no_puppet_ver] if puppet_requirement['version_requirement'].empty?
end

#write!(path) ⇒ Object



131
132
133
# File 'lib/pdk/module/metadata.rb', line 131

def write!(path)
  PDK::Util::Filesystem.write_file(path, to_json)
end