Class: PDK::Module::Release
- Inherits:
-
Object
- Object
- PDK::Module::Release
- Defined in:
- lib/pdk/module/release.rb
Instance Attribute Summary collapse
-
#module_path ⇒ Object
readonly
Returns the value of attribute module_path.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #default_package_filename ⇒ Object
- #force? ⇒ Boolean
-
#forge_compatible? ⇒ Boolean
:nocov: These are just convenience methods and are tested elsewhere.
- #forge_token ⇒ Object
- #forge_upload_url ⇒ Object
-
#initialize(module_path, options = {}) ⇒ Release
constructor
A new instance of Release.
- #module_metadata ⇒ Object
- #pdk_compatible? ⇒ Boolean
- #requires_forge_compatibility? ⇒ Boolean
- #requires_pdk_compatibility? ⇒ Boolean
- #run ⇒ Object
-
#run_build(opts) ⇒ String
Path to the built tarball.
- #run_dependency_checker(_opts) ⇒ Object
- #run_documentation(_opts) ⇒ Object
- #run_publish(_opts, tarball_path) ⇒ Object
- #run_validations(opts) ⇒ Object
- #skip_build? ⇒ Boolean
- #skip_changelog? ⇒ Boolean
- #skip_dependency? ⇒ Boolean
- #skip_documentation? ⇒ Boolean
- #skip_publish? ⇒ Boolean
- #skip_validation? ⇒ Boolean
- #specified_package ⇒ Object
- #specified_version ⇒ Object
- #validate_publish_options! ⇒ Object
- #write_module_metadata! ⇒ Object
Constructor Details
#initialize(module_path, options = {}) ⇒ Release
Returns a new instance of Release.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/pdk/module/release.rb', line 15 def initialize(module_path, = {}) @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_path ⇒ Object (readonly)
Returns the value of attribute module_path.
13 14 15 |
# File 'lib/pdk/module/release.rb', line 13 def module_path @module_path end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/pdk/module/release.rb', line 11 def @options end |
Class Method Details
.invoke(module_path, options = {}) ⇒ Object
7 8 9 |
# File 'lib/pdk/module/release.rb', line 7 def self.invoke(module_path, = {}) new(module_path, ).run end |
Instance Method Details
#default_package_filename ⇒ Object
103 104 105 106 107 |
# File 'lib/pdk/module/release.rb', line 103 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
183 184 185 |
# File 'lib/pdk/module/release.rb', line 183 def force? [:force] end |
#forge_compatible? ⇒ Boolean
:nocov: These are just convenience methods and are tested elsewhere
240 241 242 |
# File 'lib/pdk/module/release.rb', line 240 def forge_compatible? .forge_ready? end |
#forge_token ⇒ Object
219 220 221 |
# File 'lib/pdk/module/release.rb', line 219 def forge_token [:'forge-token'] end |
#forge_upload_url ⇒ Object
223 224 225 |
# File 'lib/pdk/module/release.rb', line 223 def forge_upload_url [:'forge-upload-url'] end |
#module_metadata ⇒ Object
94 95 96 |
# File 'lib/pdk/module/release.rb', line 94 def @module_metada ||= PDK::Module::Metadata.from_file(File.join(module_path, 'metadata.json')) end |
#pdk_compatible? ⇒ Boolean
244 245 246 247 248 249 |
# File 'lib/pdk/module/release.rb', line 244 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
233 234 235 236 |
# File 'lib/pdk/module/release.rb', line 233 def requires_forge_compatibility? # Pushing to the for requires the metadata to be forge compatible !skip_publish? end |
#requires_pdk_compatibility? ⇒ Boolean
227 228 229 230 231 |
# File 'lib/pdk/module/release.rb', line 227 def requires_pdk_compatibility? # Validation, Changelog and Dependency checks require the # module to be PDK Compatible !(skip_validation? && skip_changelog? && skip_dependency?) end |
#run ⇒ Object
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 |
# 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. run_validations() 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 # 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() unless skip_documentation? run_dependency_checker() 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(.dup, package_file) unless skip_publish? end |
#run_build(opts) ⇒ String
Returns Path to the built tarball.
145 146 147 |
# File 'lib/pdk/module/release.rb', line 145 def run_build(opts) PDK::Module::Build.invoke(opts.dup) end |
#run_dependency_checker(_opts) ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/pdk/module/release.rb', line 133 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
125 126 127 128 129 130 131 |
# File 'lib/pdk/module/release.rb', line 125 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
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 |
# File 'lib/pdk/module/release.rb', line 149 def run_publish(_opts, tarball_path) 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
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/pdk/module/release.rb', line 109 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, ) raise PDK::CLI::ExitWithError, 'An error occured during validation' unless validator_exit_code.zero? end |
#skip_build? ⇒ Boolean
187 188 189 |
# File 'lib/pdk/module/release.rb', line 187 def skip_build? [:'skip-build'] end |
#skip_changelog? ⇒ Boolean
191 192 193 |
# File 'lib/pdk/module/release.rb', line 191 def skip_changelog? [:'skip-changelog'] end |
#skip_dependency? ⇒ Boolean
195 196 197 |
# File 'lib/pdk/module/release.rb', line 195 def skip_dependency? [:'skip-dependency'] end |
#skip_documentation? ⇒ Boolean
199 200 201 |
# File 'lib/pdk/module/release.rb', line 199 def skip_documentation? [:'skip-documentation'] end |
#skip_publish? ⇒ Boolean
203 204 205 |
# File 'lib/pdk/module/release.rb', line 203 def skip_publish? [:'skip-publish'] end |
#skip_validation? ⇒ Boolean
207 208 209 |
# File 'lib/pdk/module/release.rb', line 207 def skip_validation? [:'skip-validation'] end |
#specified_package ⇒ Object
215 216 217 |
# File 'lib/pdk/module/release.rb', line 215 def specified_package [:file] end |
#specified_version ⇒ Object
211 212 213 |
# File 'lib/pdk/module/release.rb', line 211 def specified_version [:version] end |
#validate_publish_options! ⇒ Object
177 178 179 180 181 |
# File 'lib/pdk/module/release.rb', line 177 def 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
98 99 100 101 |
# File 'lib/pdk/module/release.rb', line 98 def .write!(File.join(module_path, 'metadata.json')) clear_cached_data end |