Skip to content

Commit 58627dc

Browse files
committed
Allow to use mdbci generate repositories command to update a single release for a product (refs #23932)
1 parent 289459d commit 58627dc

File tree

4 files changed

+102
-51
lines changed

4 files changed

+102
-51
lines changed

console.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# frozen_string_literal: true
22

33
require 'pry'
4-
current_path = File.expand_path('../core', __FILE__)
4+
current_path = File.expand_path('./core', __dir__)
55
source_files = Dir.glob("#{current_path}/**/*.rb")
66
source_files.each do |file|
77
require file
88
end
99
# rubocop:disable Lint/Debugger
1010
binding.pry
11+
puts '' # Needed to make pry work
1112
# rubocop:enable Lint/Debugger

core/commands/generate_product_repositories_command.rb

+96-46
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
require 'optparse'
55
require 'nokogiri'
66
require 'pp'
7-
require 'tmpdir'
87
require 'json'
98
require 'fileutils'
109
require 'logger'
@@ -13,6 +12,7 @@
1312
require_relative 'base_command'
1413

1514
# The command generates the repository configuration
15+
# rubocop:disable Metrics/ClassLength
1616
class GenerateProductRepositoriesCommand < BaseCommand
1717
CONFIGURATION_FILE = 'generate_repository_config.yaml'
1818
PRODUCTS_DIR_NAMES = {
@@ -48,27 +48,31 @@ def show_help
4848
4949
Supported options:
5050
51-
--maxscale-ci name of the repository on the MaxScale CI server to add to MaxScale CI product configuration. Required if generating configuration for MaxScale CI server.
5251
--configuration-file path to the configuration file to use during generation. Optional.
5352
--product name of the product to generate repository configuration for. Optional.
53+
--product-version version of the product to generate configuration for.
5454
--attempts number of attempts to try to get data from the remote repository. Default is 3 attempts.
5555
5656
In order to generate repo.d for all products using the default configuration.
5757
58-
mdbci #{COMMAND_NAME} --maxscale-ci develop
58+
mdbci #{COMMAND_NAME}
5959
6060
You can create custom configuration file and use it during the repository creation:
6161
62-
mdbci #{COMMAND_NAME} --maxscale-ci develop --configuration-file ~/mdbci/config/generate_repository_config.yaml
62+
mdbci #{COMMAND_NAME} --configuration-file ~/mdbci/config/generate_repository_config.yaml
6363
6464
In order to specify the target directory pass it as the first parameter to the script.
6565
66-
mdbci #{COMMAND_NAME} --maxscale-ci develop ~/mdbci/repo.d
66+
mdbci #{COMMAND_NAME} ~/mdbci/repo.d
6767
6868
In orded to generate configuration for a specific product use --product option.
6969
7070
mdbci #{COMMAND_NAME} --product mdbe
7171
72+
In order to generate configuration for a specific product version use --product-version option. You must also specify the name of the product to generate configuration for.
73+
74+
mdbci #{COMMAND_NAME} --product maxscale-ci --product-version develop
75+
7276
In order to specify the number of retries for repository configuration use --attempts option.
7377
7478
mdbci generate-product-repositories --product columnstore --attempts 5
@@ -115,8 +119,8 @@ def load_configuration_file
115119
def determine_products_to_parse
116120
if @env.nodeProduct
117121
unless PRODUCTS_DIR_NAMES.key?(@env.nodeProduct)
118-
show_error_and_help("Unknown product #{@env.nodeProduct}.\n"\
119-
"Known products: #{PRODUCTS_DIR_NAMES.keys.join(', ')}")
122+
error_and_log("Unknown product #{@env.nodeProduct}.\n"\
123+
"Known products: #{PRODUCTS_DIR_NAMES.keys.join(', ')}")
120124
return false
121125
end
122126
@products = [@env.nodeProduct]
@@ -127,6 +131,17 @@ def determine_products_to_parse
127131
true
128132
end
129133

134+
def determine_product_version_to_parse
135+
if @env.productVersion
136+
unless @env.nodeProduct
137+
error_and_log('When specifying product version you must also specify the product.')
138+
return false
139+
end
140+
@product_version = @env.productVersion
141+
end
142+
true
143+
end
144+
130145
def determine_number_of_attempts
131146
@attempts = if @env.attempts
132147
@env.attempts.to_i
@@ -147,11 +162,15 @@ def setup_destination_directory
147162

148163
# Use data provided via constructor to configure the command.
149164
def configure_command
165+
if @env.show_help
166+
show_help
167+
return false
168+
end
150169
load_configuration_file
151170
return false unless determine_products_to_parse
171+
return false unless determine_product_version_to_parse
152172
determine_number_of_attempts
153173
setup_destination_directory
154-
@maxscale_ci = @env.maxscale_ci
155174
Workers.pool.resize(10)
156175
true
157176
end
@@ -182,7 +201,7 @@ def parse_maxscale_ci(config)
182201
releases = []
183202
releases.concat(parse_maxscale_ci_rpm_repository(config['repo']['rpm']))
184203
releases.concat(parse_maxscale_ci_deb_repository(config['repo']['deb']))
185-
write_repository(releases)
204+
releases
186205
end
187206

188207
def parse_maxscale_ci_rpm_repository(config)
@@ -219,7 +238,7 @@ def parse_maxscale(config)
219238
releases = []
220239
releases.concat(parse_maxscale_rpm_repository(config['repo']['rpm']))
221240
releases.concat(parse_maxscale_deb_repository(config['repo']['deb']))
222-
write_repository(releases)
241+
releases
223242
end
224243

225244
def parse_maxscale_rpm_repository(config)
@@ -254,7 +273,7 @@ def parse_mdbe(config)
254273
releases = []
255274
releases.concat(parse_mdbe_rpm_repository(config['repo']['rpm']))
256275
releases.concat(parse_mdbe_deb_repository(config['repo']['deb']))
257-
write_repository(releases)
276+
releases
258277
end
259278

260279
def parse_mdbe_rpm_repository(config)
@@ -292,15 +311,15 @@ def parse_galera(config)
292311
version_regexp = %r{^(\p{Digit}+\.\p{Digit}+)\-galera\/?$}
293312
releases.concat(parse_mariadb_rpm_repository(config['repo']['rpm'], 'galera', version_regexp))
294313
releases.concat(parse_mariadb_deb_repository(config['repo']['deb'], 'galera', version_regexp))
295-
write_repository(releases)
314+
releases
296315
end
297316

298317
def parse_mariadb(config)
299318
releases = []
300319
version_regexp = %r{^(\p{Digit}+\.\p{Digit}+(\.\p{Digit}+)?)\/?$}
301320
releases.concat(parse_mariadb_rpm_repository(config['repo']['rpm'], 'mariadb', version_regexp))
302321
releases.concat(parse_mariadb_deb_repository(config['repo']['deb'], 'mariadb', version_regexp))
303-
write_repository(releases)
322+
releases
304323
end
305324

306325
def parse_mariadb_rpm_repository(config, product, version_regexp)
@@ -335,7 +354,7 @@ def parse_columnstore(config)
335354
releases = []
336355
releases.concat(parse_columnstore_rpm_repository(config['repo']['rpm']))
337356
releases.concat(parse_columnstore_deb_repository(config['repo']['deb']))
338-
write_repository(releases)
357+
releases
339358
end
340359

341360
def parse_columnstore_rpm_repository(config)
@@ -372,7 +391,7 @@ def parse_mysql(config)
372391
releases = []
373392
releases.concat(parse_mysql_rpm_repository(config['repo']['rpm']))
374393
releases.concat(parse_mysql_deb_repository(config['repo']['deb']))
375-
write_repository(releases)
394+
releases
376395
end
377396

378397
def parse_mysql_deb_repository(config)
@@ -405,23 +424,6 @@ def parse_mysql_rpm_repository(config)
405424
)
406425
end
407426

408-
# Write all information about releases to the JSON documents
409-
def write_repository(releases)
410-
platforms = releases.map { |release| release[:platform] }.uniq
411-
platforms.each do |platform|
412-
FileUtils.mkdir_p("#{@directory}/#{platform}")
413-
releases_by_version = Hash.new { |hash, key| hash[key] = [] }
414-
releases.each do |release|
415-
next if release[:platform] != platform
416-
releases_by_version[release[:version]] << extract_release_fields(release)
417-
end
418-
releases_by_version.each_pair do |version, version_releases|
419-
File.write("#{@directory}/#{platform}/#{version}.json",
420-
JSON.pretty_generate(version_releases))
421-
end
422-
end
423-
end
424-
425427
STORED_KEYS = %i[repo repo_key platform platform_version product version].freeze
426428
# Extract only required fields from the passed release before writing it to the file
427429
def extract_release_fields(release)
@@ -431,6 +433,18 @@ def extract_release_fields(release)
431433
end
432434
end
433435

436+
# Method filters releases, removing empty ones and by version, if it required
437+
def filter_releases(releases)
438+
next_releases = releases.reject(&:nil?)
439+
next_releases.select do |release|
440+
if @product_version.nil? || release[:version].nil?
441+
true
442+
else
443+
release[:version] == @product_version
444+
end
445+
end
446+
end
447+
434448
# Parse the repository and provide required configurations
435449
def parse_repository(base_url, key, product, *steps)
436450
# Recursively go through the site and apply steps on each level
@@ -444,10 +458,17 @@ def parse_repository(base_url, key, product, *steps)
444458
end
445459
apply_step_to_links(step, links, release)
446460
end
447-
next_releases.flatten.reject(&:nil?)
461+
filter_releases(next_releases.flatten)
448462
end
449-
# Add repository key and product to all releases
450-
result.each do |release|
463+
add_key_and_product_to_releases(result, key, product)
464+
end
465+
466+
# Append key and product to the releases
467+
# @param releases [Array<Hash>] list of releases
468+
# @param key [String] text to put into key field
469+
# @param product [String] name of the product
470+
def add_key_and_product_to_releases(releases, key, product)
471+
releases.each do |release|
451472
release[:repo_key] = key
452473
release[:product] = product
453474
end
@@ -554,22 +575,53 @@ def append_url(paths, key = nil, save_path = false)
554575
end
555576
end
556577

557-
# Create repository
578+
# Write all information about releases to the JSON documents
579+
def write_repository(product_dir, releases)
580+
platforms = releases.map { |release| release[:platform] }.uniq
581+
platforms.each do |platform|
582+
configuration_directory = File.join(product_dir, platform)
583+
FileUtils.mkdir_p(configuration_directory)
584+
releases_by_version = Hash.new { |hash, key| hash[key] = [] }
585+
releases.each do |release|
586+
next if release[:platform] != platform
587+
releases_by_version[release[:version]] << extract_release_fields(release)
588+
end
589+
releases_by_version.each_pair do |version, version_releases|
590+
File.write(File.join(configuration_directory, "#{version}.json"),
591+
JSON.pretty_generate(version_releases))
592+
end
593+
end
594+
end
595+
596+
# Create repository in the target directory.
597+
def write_product(product, releases)
598+
info_and_log("Writing generated configuration for #{product} to the repository.")
599+
product_name = PRODUCTS_DIR_NAMES[product]
600+
product_dir = File.join(@destination, product_name)
601+
if @product_version.nil?
602+
FileUtils.rm_rf(product_dir, secure: true)
603+
FileUtils.mkdir_p(product_dir)
604+
else
605+
releases = releases.select do |release|
606+
release[:version] == @product_version
607+
end
608+
end
609+
write_repository(product_dir, releases)
610+
end
611+
612+
# Create repository by calling appropriate method using reflection and writing results
613+
# @param product [String] name of the product to generate
614+
# @returns [Boolean] whether generation was succesfull or not
558615
def create_repository(product)
559616
info_and_log("Generating repository configuration for #{product}")
560-
FileUtils.rm_rf("#{@directory}/.", secure: true)
561617
begin
562-
send("parse_#{product}".to_sym, @config[product])
618+
releases = send("parse_#{product}".to_sym, @config[product])
563619
rescue StandardError => error
564620
error_and_log("#{product} was not generated. Try again.")
565621
error_and_log_error(error)
566622
return false
567623
end
568-
info_and_log("Copying generated configuration for #{product} to the repository.")
569-
product_name = PRODUCTS_DIR_NAMES[product]
570-
FileUtils.mkdir_p("#{@destination}/#{product_name}")
571-
FileUtils.rm_rf("#{@destination}/#{product_name}", secure: true)
572-
FileUtils.cp_r("#{@directory}/.", "#{@destination}/#{product_name}")
624+
write_product(product, releases)
573625
true
574626
end
575627

@@ -586,8 +638,6 @@ def print_summary(products_with_errors)
586638
# Starting point of the application
587639
def execute
588640
return ARGUMENT_ERROR_RESULT unless configure_command
589-
@directory = Dir.mktmpdir(COMMAND_NAME)
590-
info_and_log("Writing intermediate results to #{@directory}")
591641
remainning_products = @products.dup
592642
@attempts.times do
593643
remainning_products = remainning_products.reject do |product|
@@ -596,7 +646,7 @@ def execute
596646
break if remainning_products.empty?
597647
end
598648
print_summary(remainning_products)
599-
FileUtils.rm_rf(@directory)
600649
SUCCESS_RESULT
601650
end
602651
end
652+
# rubocop:enable Metrics/ClassLength

core/session.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class Session
4646
attr_accessor :boxes_dir
4747
attr_accessor :mdbciDir
4848
attr_accessor :mdbci_dir
49-
attr_accessor :maxscale_ci
5049
attr_accessor :starting_dir
5150
attr_accessor :working_dir
5251
attr_accessor :nodeProduct
@@ -59,6 +58,7 @@ class Session
5958
attr_accessor :node_name
6059
attr_accessor :snapshot_name
6160
attr_accessor :ipv6
61+
attr_accessor :show_help
6262

6363
PLATFORM = 'platform'
6464
VAGRANT_NO_PARALLEL = '--no-parallel'

mdbci

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ opts = GetoptLong.new(
1616
['--command', '-c', GetoptLong::REQUIRED_ARGUMENT],
1717
['--configuration-file', GetoptLong::REQUIRED_ARGUMENT],
1818
['--field', '-f', GetoptLong::REQUIRED_ARGUMENT],
19+
['--help', GetoptLong::NO_ARGUMENT],
1920
['--ipv6', GetoptLong::NO_ARGUMENT],
2021
['--key', '-k', GetoptLong::REQUIRED_ARGUMENT],
2122
['--keep-template', GetoptLong::NO_ARGUMENT],
22-
['--maxscale-ci', GetoptLong::REQUIRED_ARGUMENT],
2323
['--node-name', GetoptLong::REQUIRED_ARGUMENT],
2424
['--override', '-w', GetoptLong::NO_ARGUMENT],
2525
['--path-to-nodes', GetoptLong::REQUIRED_ARGUMENT],
@@ -117,8 +117,8 @@ begin
117117
when '--configuration-file'
118118
session.configuration_file = File.absolute_path(arg)
119119

120-
when '--maxscale-ci'
121-
session.maxscale_ci = arg
120+
when '--help'
121+
session.show_help = true
122122

123123
else
124124
out.error 'Option is not supported: ' + opt

0 commit comments

Comments
 (0)