Class: PDK::Module::Release

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(module_path, options = {}) ⇒ Release

Returns a new instance of Release.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pdk/module/release.rb', line 14

def initialize(module_path, options = {})
  @options = options

  # TODO: Currently the release process can ONLY be run if the working directory IS the module root. However, in the future
  # this WILL change, so we have the API arguments for it, but only accept `nil` for the first parameter
  raise PDK::CLI::ExitWithError, _('Running the release process outside of the working directory is not supported') unless module_path.nil?

  if module_path.nil?
    module_path = PDK::Util.module_root
    raise PDK::CLI::ExitWithError, _('The module release process requires a valid module path') % { module_path: module_path } if module_path.nil?
  end
  raise PDK::CLI::ExitWithError, _('%{module_path} is not a valid module') % { module_path: module_path } unless PDK::Util.in_module_root?(module_path)
  @module_path = module_path
end

Instance Attribute Details

#module_pathObject (readonly)

Returns the value of attribute module_path.



12
13
14
# File 'lib/pdk/module/release.rb', line 12

def module_path
  @module_path
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/pdk/module/release.rb', line 10

def options
  @options
end

Class Method Details

.invoke(module_path, options = {}) ⇒ Object



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

def self.invoke(module_path, options = {})
  new(module_path, options).run
end

Instance Method Details

#default_package_filenameObject



102
103
104
105
106
# File 'lib/pdk/module/release.rb', line 102

def default_package_filename
  return @default_tarball_filename unless @default_tarball_filename.nil?
  builder = PDK::Module::Build.new(module_dir: module_path)
  @default_tarball_filename = builder.package_file
end

#force?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/pdk/module/release.rb', line 182

def force?
  options[:force]
end

#forge_compatible?Boolean

:nocov: These are just convenience methods and are tested elsewhere

Returns:

  • (Boolean)


239
240
241
# File 'lib/pdk/module/release.rb', line 239

def forge_compatible?
  .forge_ready?
end

#forge_tokenObject



218
219
220
# File 'lib/pdk/module/release.rb', line 218

def forge_token
  options[:'forge-token']
end

#forge_upload_urlObject



222
223
224
# File 'lib/pdk/module/release.rb', line 222

def forge_upload_url
  options[:'forge-upload-url']
end

#module_metadataObject



93
94
95
# File 'lib/pdk/module/release.rb', line 93

def 
  @module_metada ||= PDK::Module::Metadata.from_file(File.join(module_path, 'metadata.json'))
end

#pdk_compatible?Boolean

Returns:

  • (Boolean)


243
244
245
246
247
248
# File 'lib/pdk/module/release.rb', line 243

def pdk_compatible?
  return @pdk_compatible unless @pdk_compatible.nil?

  builder = PDK::Module::Build.new(module_dir: module_path)
  @pdk_compatible = builder.module_pdk_compatible?
end

#requires_forge_compatibility?Boolean

Returns:

  • (Boolean)


232
233
234
235
# File 'lib/pdk/module/release.rb', line 232

def requires_forge_compatibility?
  # Pushing to the for requires the metadata to be forge compatible
  !skip_publish?
end

#requires_pdk_compatibility?Boolean

Returns:

  • (Boolean)


226
227
228
229
230
# File 'lib/pdk/module/release.rb', line 226

def requires_pdk_compatibility?
  # Validation, Changelog and Dependency checks require the
  # module to be PDK Compatible
  !(skip_validation? && skip_changelog? && skip_dependency?)
end

#runObject



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
# File 'lib/pdk/module/release.rb', line 29

def run
  # Pre-release checks
  unless force?
    raise PDK::CLI::ExitWithError, _('The module is not PDK compatible') if requires_pdk_compatibility? && !pdk_compatible?
    raise PDK::CLI::ExitWithError, _('The module is not Forge compatible') if requires_forge_compatibility? && !forge_compatible?
  end

  # Note that these checks are duplicated in the run_publish method, however it's a much better
  # experience to fail early, than going through the whole process, only to error at the end knowing full well
  # it'll fail anyway.
  validate_publish_options!

  run_validations(options) unless skip_validation?

  PDK.logger.info _('Releasing %{module_name} - from version %{module_version}') % {
    module_name:    .data['name'],
    module_version: .data['version'],
  }

  PDK::Util::ChangelogGenerator.generate_changelog unless skip_changelog?

  # Calculate the new module version
  new_version = specified_version
  if new_version.nil? && !skip_changelog?
    new_version = PDK::Util::ChangelogGenerator.compute_next_version(.data['version'])
  end
  new_version = .data['version'] if new_version.nil?

  if new_version != .data['version']
    PDK.logger.info _('Updating version to %{module_version}') % {
      module_version: new_version,
    }

    # Set the new version in metadata file
    .data['version'] = new_version
    write_module_metadata!

    # Update the changelog with the correct version
    PDK::Util::ChangelogGenerator.generate_changelog unless skip_changelog?

    # Check if the versions match
    latest_version = PDK::Util::ChangelogGenerator.latest_version
    unless latest_version
      raise PDK::CLI::ExitWithError, _('%{new_version} does not match %{latest_version}') % { new_version: new_version, latest_version: latest_version } if new_version != latest_version
    end
  end

  run_documentation(options) unless skip_documentation?

  run_dependency_checker(options) unless skip_dependency?

  if skip_build?
    # Even if we're skipping the build, we still need the name of the tarball
    # Use the specified package path if set
    package_file = specified_package if package_file.nil?
    # Use the default as a last resort
    package_file = default_package_filename if package_file.nil?
  else
    package_file = run_build(options)
  end

  run_publish(options.dup, package_file) unless skip_publish?
end

#run_build(opts) ⇒ String

Returns Path to the built tarball.

Returns:

  • (String)

    Path to the built tarball



144
145
146
# File 'lib/pdk/module/release.rb', line 144

def run_build(opts)
  PDK::Module::Build.invoke(opts.dup)
end

#run_dependency_checker(_opts) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/pdk/module/release.rb', line 132

def run_dependency_checker(_opts)
  # run dependency-checker and output dependent modules list
  PDK.logger.info _('Running dependency checks')

  dep_command = PDK::CLI::Exec::Command.new('dependency-checker', 'metadata.json')
  dep_command.context = :module
  result = dep_command.execute!

  raise PDK::CLI::ExitWithError, _('An error occured checking the module dependencies: %{stdout}') % { stdout: result[:stdout] } unless result[:exit_code].zero?
end

#run_documentation(_opts) ⇒ Object



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

def run_documentation(_opts)
  PDK.logger.info _('Updating documentation using puppet strings')
  docs_command = PDK::CLI::Exec::InteractiveCommand.new(PDK::CLI::Exec.bundle_bin, 'exec', 'puppet', 'strings', 'generate', '--format', 'markdown', '--out', 'REFERENCE.md')
  docs_command.context = :module
  result = docs_command.execute!
  raise PDK::CLI::ExitWithError, _('An error occured generating the module documentation: %{stdout}') % { stdout: result[:stdout] } unless result[:exit_code].zero?
end

#run_publish(_opts, tarball_path) ⇒ Object



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
# File 'lib/pdk/module/release.rb', line 148

def run_publish(_opts, tarball_path)
  validate_publish_options!
  raise PDK::CLI::ExitWithError, _('Module tarball %{tarball_path} does not exist') % { tarball_path: tarball_path } unless PDK::Util::Filesystem.file?(tarball_path)

  # TODO: Replace this code when the upload functionality is added to the forge ruby gem
  require 'base64'
  file_data = Base64.encode64(PDK::Util::Filesystem.read_file(tarball_path, open_args: 'rb'))

  PDK.logger.info _('Uploading tarball to puppet forge...')
  uri = URI(forge_upload_url)
  require 'net/http'
  request = Net::HTTP::Post.new(uri.path)
  request['Authorization'] = 'Bearer ' + forge_token
  request['Content-Type'] = 'application/json'
  data = { file: file_data }

  request.body = data.to_json

  require 'openssl'
  use_ssl = uri.class == URI::HTTPS
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl) do |http|
    http.request(request)
  end

  raise PDK::CLI::ExitWithError, _('Error uploading to Puppet Forge: %{result}') % { result: response.body } unless response.is_a?(Net::HTTPSuccess)
  PDK.logger.info _('Publish to Forge was successful')
end

#run_validations(opts) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pdk/module/release.rb', line 108

def run_validations(opts)
  # TODO: Surely I can use a pre-existing class for this?
  PDK::CLI::Util.validate_puppet_version_opts(opts)

  PDK::CLI::Util.module_version_check

  puppet_env = PDK::CLI::Util.puppet_from_opts_or_env(opts)
  PDK::Util::PuppetVersion.fetch_puppet_dev if opts[:'puppet-dev']
  PDK::Util::RubyVersion.use(puppet_env[:ruby_version])

  PDK::Util::Bundler.ensure_bundle!(puppet_env[:gemset])

  validator_exit_code, = PDK::Validate.invoke_validators_by_name(PDK.context, PDK::Validate.validator_names, false, options)
  raise PDK::CLI::ExitWithError, _('An error occured during validation') unless validator_exit_code.zero?
end

#skip_build?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/pdk/module/release.rb', line 186

def skip_build?
  options[:'skip-build']
end

#skip_changelog?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/pdk/module/release.rb', line 190

def skip_changelog?
  options[:'skip-changelog']
end

#skip_dependency?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/pdk/module/release.rb', line 194

def skip_dependency?
  options[:'skip-dependency']
end

#skip_documentation?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/pdk/module/release.rb', line 198

def skip_documentation?
  options[:'skip-documentation']
end

#skip_publish?Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/pdk/module/release.rb', line 202

def skip_publish?
  options[:'skip-publish']
end

#skip_validation?Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/pdk/module/release.rb', line 206

def skip_validation?
  options[:'skip-validation']
end

#specified_packageObject



214
215
216
# File 'lib/pdk/module/release.rb', line 214

def specified_package
  options[:file]
end

#specified_versionObject



210
211
212
# File 'lib/pdk/module/release.rb', line 210

def specified_version
  options[:version]
end

#validate_publish_options!Object



176
177
178
179
180
# File 'lib/pdk/module/release.rb', line 176

def validate_publish_options!
  return if skip_publish?
  raise PDK::CLI::ExitWithError, _('Missing forge-upload-url option') unless forge_upload_url
  raise PDK::CLI::ExitWithError, _('Missing forge-token option') unless forge_token
end

#write_module_metadata!Object



97
98
99
100
# File 'lib/pdk/module/release.rb', line 97

def write_module_metadata!
  .write!(File.join(module_path, 'metadata.json'))
  clear_cached_data
end