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
28
# 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' if module_path.nil?
  end
  raise PDK::CLI::ExitWithError, format('%{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.



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

def options
  @options
end

Class Method Details

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



8
9
10
# File 'lib/pdk/module/release.rb', line 8

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

Instance Method Details

#default_package_filenameObject



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

def default_package_filename
  return @default_tarball_filename unless @default_tarball_filename.nil?

  builder = Puppet::Modulebuilder::Builder.new(module_path, nil, PDK.logger)
  @default_tarball_filename = builder.package_file
end

#force?Boolean

Returns:

  • (Boolean)


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

def force?
  options[:force]
end

#forge_compatible?Boolean

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

Returns:

  • (Boolean)


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

def forge_compatible?
  .forge_ready?
end

#forge_tokenObject



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

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

#forge_upload_urlObject



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

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

#module_metadataObject



87
88
89
# File 'lib/pdk/module/release.rb', line 87

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

#pdk_compatible?Boolean

Returns:

  • (Boolean)


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

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

  @pdk_compatible = PDK::Util.module_pdk_compatible?(module_path)
end

#requires_forge_compatibility?Boolean

Returns:

  • (Boolean)


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

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

#requires_pdk_compatibility?Boolean

Returns:

  • (Boolean)


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

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

#runObject



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

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 format('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
  new_version = PDK::Util::ChangelogGenerator.compute_next_version(.data['version']) if new_version.nil? && !skip_changelog?
  new_version = .data['version'] if new_version.nil?

  if new_version != .data['version']
    PDK.logger.info format('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
    if !latest_version && (new_version != latest_version)
      raise PDK::CLI::ExitWithError, format('%{new_version} does not match %{latest_version}', new_version: new_version, latest_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
  end

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

#run_buildString

Returns Path to the built tarball.

Returns:

  • (String)

    Path to the built tarball



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

def run_build
  module_dir = PDK::Util::Filesystem.expand_path(module_path || Dir.pwd)
  target_dir = File.join(module_dir, 'pkg')
  builder = Puppet::Modulebuilder::Builder.new(module_dir, target_dir, PDK.logger)
  builder.build
end

#run_dependency_checker(_opts) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/pdk/module/release.rb', line 127

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, format('An error occured checking the module dependencies: %{stdout}', stdout: result[:stdout]) unless result[:exit_code].zero?
end

#run_documentation(_opts) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/pdk/module/release.rb', line 119

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, format('An error occured generating the module documentation: %{stdout}', stdout: result[:stdout]) unless result[:exit_code].zero?
end

#run_publish(_opts, tarball_path) ⇒ Object



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

def run_publish(_opts, tarball_path)
  validate_publish_options!
  raise PDK::CLI::ExitWithError, format('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.instance_of?(URI::HTTPS)
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl) do |http|
    http.request(request)
  end

  raise PDK::CLI::ExitWithError, format('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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/pdk/module/release.rb', line 103

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)


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

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

#skip_changelog?Boolean

Returns:

  • (Boolean)


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

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

#skip_dependency?Boolean

Returns:

  • (Boolean)


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

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

#skip_documentation?Boolean

Returns:

  • (Boolean)


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

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

#skip_publish?Boolean

Returns:

  • (Boolean)


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

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

#skip_validation?Boolean

Returns:

  • (Boolean)


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

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

#specified_packageObject



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

def specified_package
  options[:file]
end

#specified_versionObject



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

def specified_version
  options[:version]
end

#validate_publish_options!Object



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

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



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

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