Skip to content

Commit a1a5a79

Browse files
authored
Merge pull request #596 from puppetlabs/CAT-2344
(CAT-2433) Adds option to return latest agent build
2 parents 7969ca4 + 369561a commit a1a5a79

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ in the project module root directory run `bundle exec matrix_from_metadata_v3`
7474
| --provision-include | NAME | all | Select provisioner |
7575
| --provision-exclude | NAME | provision_service | Filter provisioner |
7676
| --nightly | | | Install from the nightly puppetcore images |
77+
| --latest-agent | | | Return the latest agent version as part of the matrix. Note: This changes how the collection is returned |
7778

7879
> Refer to the [built-in matrix.json](https://github.com/puppetlabs/puppet_litmus/blob/main/exe/matrix.json) for a list of supported collection, provisioners, and platforms.
7980

exe/matrix_from_metadata_v3

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,14 @@ begin
163163
opt.on('--provision-exclude NAME', String, "Filter provisioner (default: #{default_options[:'provision-exclude'] || 'none'})") { |o| options.provision_exclude.push(*o.split(',')) }
164164

165165
opt.on('--nightly', TrueClass, 'Enable nightly builds') { |o| options.nightly = o }
166+
opt.on('--latest-agent', TrueClass, 'Return the latest agent build as part of the matrix') { |o| options.latest_agent = o }
166167
end.parse!
167168

168169
Action.config(debug: true) if options[:debug]
169170
Action.config(notice: false) if options[:quiet] && !options[:debug]
170171

172+
raise OptionParser::InvalidArgument, 'latest-agent and pe-include are mutually exclusive options' if options[:latest_agent] && options[:pe_include]
173+
171174
# validate provisioners
172175
options[:provision_include].select! do |p|
173176
options[:matrix]['provisioners'].key?(p) or raise OptionParser::InvalidArgument, "--provision-include '#{p}' not found in provisioners"
@@ -262,12 +265,42 @@ options[:metadata]['requirements']&.each do |req|
262265
# This assumes that such a boundary will always allow the latest actually existing puppet version of a release stream, trading off simplicity vs accuracy here.
263266
next unless gem_req.satisfied_by?(Gem::Version.new("#{collection['puppet'].to_i}.9999"))
264267

268+
# # If requested, return the latest agent version for the collection's puppet version
269+
# # If not requested, return the value latest
270+
agent_version = if options[:latest_agent] && collection['puppet'] != 'nightly'
271+
require 'net/http'
272+
require 'json'
273+
require 'uri'
274+
275+
uri = URI('https://forgeapi.puppet.com/private/versions/puppet-agent')
276+
response = Net::HTTP.get_response(uri)
277+
json_data = JSON.parse(response.body)
278+
all_versions = json_data.keys
279+
versionx = all_versions.select { |v| v.start_with?("#{collection['puppet'].to_i}.") }
280+
versionx.sort_by { |v| Gem::Version.new(v) }.last # rubocop:disable Style/RedundantSort
281+
end
282+
265283
version = collection['puppet'].to_i
266284
prefix = 'puppetcore'
267-
matrix[:collection] << if options[:nightly]
268-
"#{prefix}#{version}-nightly"
285+
group = if options[:nightly]
286+
"#{prefix}#{version}-nightly"
287+
else
288+
"#{prefix}#{version}"
289+
end
290+
matrix[:collection] << if options[:latest_agent] && collection['puppet'] != 'nightly'
291+
require 'net/http'
292+
require 'json'
293+
require 'uri'
294+
295+
uri = URI('https://forgeapi.puppet.com/private/versions/puppet-agent')
296+
response = Net::HTTP.get_response(uri)
297+
json_data = JSON.parse(response.body)
298+
all_versions = json_data.keys
299+
versionx = all_versions.select { |v| v.start_with?("#{collection['puppet'].to_i}.") }
300+
versionx.sort_by { |v| Gem::Version.new(v) }.last # rubocop:disable Style/RedundantSort
301+
{ collection: group, version: agent_version }
269302
else
270-
"#{prefix}#{version}"
303+
group
271304
end
272305

273306
spec_matrix[:include] << {

spec/exe/matrix_from_metadata_v3_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,25 @@
280280
)
281281
end
282282
end
283+
284+
context 'with argument --latest-agent' do
285+
let(:result) { run_matrix_from_metadata_v3(['--puppetlabs', '--latest-agent']) }
286+
287+
it 'run successfully' do
288+
expect(result.status_code).to eq 0
289+
end
290+
291+
it 'generates the matrix' do
292+
expect(result.stdout).to include(
293+
'::warning::CentOS-6 no provisioner found',
294+
'::warning::Ubuntu-14.04 no provisioner found',
295+
'::group::matrix',
296+
'::group::spec_matrix'
297+
)
298+
expect(github_output_content).to match(/{"collection":"puppetcore8","version":"\d+\.\d+\.\d+"}/)
299+
expect(github_output_content).to include(
300+
'spec_matrix={"include":[{"puppet_version":"~> 8.0","ruby_version":3.2}]}'
301+
)
302+
end
303+
end
283304
end

0 commit comments

Comments
 (0)