Module: PdkSync::Utils

Defined in:
lib/pdksync/utils.rb

Constant Summary collapse

OPERATINGSYSTEM =
'operatingsystem'.freeze
OPERATINGSYSTEMRELEASE =
'operatingsystemrelease'.freeze
OPERATINGSYSTEM_SUPPORT =
'operatingsystem_support'.freeze
NAME =
'name'.freeze
REQUIREMENTS =
'requirements'.freeze

Class Method Summary collapse

Class Method Details

.add_label(client, repo_name, issue_number, label) ⇒ Object

Parameters:

  • client (PdkSync::GitPlatformClient)

    The Git Platform client used to gain access to and manipulate the repository.

  • repo_name (String)

    The name of the repository on which the commit is to be made.

  • issue_number (Integer)

    The id of the issue (i.e. pull request) to add the label to.

  • label (String)

    The label to add.



54
55
56
57
58
59
# File 'lib/pdksync/utils.rb', line 54

def self.add_label(client, repo_name, issue_number, label)
  client.update_issue(repo_name, issue_number, labels: [label])
rescue StandardError => error
  PdkSync::Logger.info "Adding label to #{repo_name} issue #{issue_number} has failed. #{error}"
  return false
end

.add_platform_to_metadata(module_path, os_to_add, version_to_add) ⇒ Object

Parameters:

  • module_path

    Path to the root dir of the module

  • os_to_add

    OS we want to add

  • version_to_add

    Version we want to add



1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
# File 'lib/pdksync/utils.rb', line 1187

def self.(module_path, os_to_add, version_to_add)
  os_to_add = normalize_os(os_to_add)
   = (module_path)
  updated_existing_entry = false
  [OPERATINGSYSTEM_SUPPORT].each do |os_vers|
    next unless (os = normalize_os(os_vers[OPERATINGSYSTEM]))
    next unless os == os_to_add
    PdkSync::Logger.info "Adding #{os_to_add} version #{version_to_add} to existing entry"
    os_vers[OPERATINGSYSTEMRELEASE] << version_to_add
    os_vers[OPERATINGSYSTEMRELEASE].uniq!
    os_vers[OPERATINGSYSTEMRELEASE].sort_by!(&:to_f)
    updated_existing_entry = true
    break
  end
  unless updated_existing_entry
    PdkSync::Logger.info "Adding #{os_to_add} version #{version_to_add} to new entry"
    supported_platform_entry = {}
    supported_platform_entry[OPERATINGSYSTEM] = os_to_add
    supported_platform_entry[OPERATINGSYSTEMRELEASE] = [version_to_add]
    [OPERATINGSYSTEM_SUPPORT] << supported_platform_entry
  end
  (module_path, )
end

.add_provision_list(module_path, key, provisioner, images) ⇒ Boolean

Returns True if entry was successfully added to ‘provision.yaml’ False if ‘provision.yaml’ does not exist or is an empty file.

Parameters:

  • module_path (String)

    Path to the module root dir

  • key (String)

    Key name in ‘provision.yaml’ (e.g. “release_checks_7)

  • provisioner (String)

    The value for the provisioner key (e.g. “abs”)

  • images (Array)

    The list of images for the images key (e.g. [‘ubuntu-1804-x86_64, ubuntu-2004-x86_64’, ‘centos-8-x86_64’])

Returns:

  • (Boolean)

    True if entry was successfully added to ‘provision.yaml’ False if ‘provision.yaml’ does not exist or is an empty file



1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
# File 'lib/pdksync/utils.rb', line 1015

def self.add_provision_list(module_path, key, provisioner, images)
  path_to_provision_yaml = "#{module_path}/provision.yaml"
  return false unless File.exist? path_to_provision_yaml
  PdkSync::Logger.info "Updating #{path_to_provision_yaml}"
  provision_yaml = YAML.safe_load(File.read(path_to_provision_yaml))
  return false if provision_yaml.nil?
  provision_yaml[key] = {}
  provision_yaml[key]['provisioner'] = provisioner
  provision_yaml[key]['images'] = images
  File.write(path_to_provision_yaml, YAML.dump(provision_yaml))
end

.add_staged_files(git_repo) ⇒ Object

Parameters:

  • git_repo (Git::Base)

    A git object representing the local repository to be staged.



107
108
109
110
111
112
113
114
115
116
# File 'lib/pdksync/utils.rb', line 107

def self.add_staged_files(git_repo)
  if !git_repo.status.changed.empty?
    git_repo.add(all: true)
    PdkSync::Logger.info 'All files have been staged.'
    true
  else
    PdkSync::Logger.info 'Nothing to commit.'
    false
  end
end

.adhoc_urls(job_name, jenkins_server_urls) ⇒ Object

return jenkins job urls



798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
# File 'lib/pdksync/utils.rb', line 798

def self.adhoc_urls(job_name, jenkins_server_urls)
  adhoc_urls = []
  # get adhoc jobs
  adhoc_urls.push("#{jenkins_server_urls}/job/#{job_name}")
  adhoc_urls.each do |url|
    conn = Faraday::Connection.new "#{url}/api/json"
    res = conn.get
    build_job_data = JSON.parse(res.body.to_s)
    downstream_job = build_job_data['downstreamProjects']
    break if downstream_job.empty?
    downstream_job.each do |item|
      next if item.nil?
      adhoc_urls.push(item['url']) unless item['url'].nil? && item['url'].include?('skippable_adhoc')
    end
  end
  adhoc_urls
end

.analyse_jenkins_report(url, module_name, build_id) ⇒ Object

analyse jenkins report



905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
# File 'lib/pdksync/utils.rb', line 905

def self.analyse_jenkins_report(url, module_name, build_id)
  # builds don't have the same build_id. That's why just the init build will be identified by id, rest of them by lastBuild
  last_build_job_data = JSON.parse(Faraday.get("#{url}/#{build_id}/api/json").body.to_s) if url.include?('init-manual-parameters_adhoc')
  last_build_job_data = JSON.parse(Faraday.get("#{url}/lastBuild/api/json").body.to_s) unless url.include?('init-manual-parameters_adhoc')

  # status = 'not_built' unless last_build_job_data
  if last_build_job_data['result'].nil?
    status = 'in progress'
    execution_time = 'running'
  else
    status = last_build_job_data['result']
    execution_time = duration_hrs_and_mins(last_build_job_data['duration'].to_i)
  end

  # execution_time = 0 unless last_build_job_data
  logs_link = "#{url}/#{build_id}/" if url.include?('init-manual-parameters_adhoc')
  logs_link = "#{url}lastBuild/" unless url.include?('init-manual-parameters_adhoc')
  @data = "Job title =#{last_build_job_data['fullDisplayName']}\n logs_link = #{logs_link}\n status = #{status}\n"
  return_data = [status, execution_time]
  write_to_file("results_#{module_name}.out", @data)
  return_data
end

.change_module_template_url(url, ref, metadata_file = 'metadata.json') ⇒ String

Returns - the updated url.

Parameters:

  • - (String)

    the url of the pdk-templates

  • - (String)

    the ref of the pdk templates you want to change to

Returns:

  • (String)
    • the updated url



313
314
315
316
317
318
319
320
# File 'lib/pdksync/utils.rb', line 313

def self.change_module_template_url(url, ref,  = 'metadata.json')
  content = File.read()
  uri = PDK::Util::TemplateURI.uri_safe(url.to_s + "##{ref}")
  data_hash = JSON.parse(content)
  data_hash['template-url'] = uri
  File.write(, data_hash.to_json)
  uri.to_s
end

.check_for_label(client, repo_name, label) ⇒ Boolean

Returns A boolean stating whether the label was found.

Parameters:

  • client (PdkSync::GitPlatformClient)

    The Git platform client used to gain access to and manipulate the repository.

  • repo_name (String)

    The name of the repository on which the commit is to be made.

  • label (String)

    The label to check for.

Returns:

  • (Boolean)

    A boolean stating whether the label was found.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pdksync/utils.rb', line 71

def self.check_for_label(client, repo_name, label)
  # Get labels from repository
  repo_labels = client.labels(repo_name)

  # Look for label in the repository's labels
  match = false
  repo_labels.each do |repo_label|
    if repo_label.name == label
      match = true
      break
    end
  end

  # Raise error if a match was not found else return true
  (match == false) ? (raise StandardError, "Label '#{label}' not found in #{repo_name}") : (return true)
rescue StandardError => error
  PdkSync::Logger.fatal "Retrieving labels for #{repo_name} has failed. #{error}"
  return false
end

.check_gem_latest_version(gem_to_test) ⇒ Object

Parameters:

  • gem_to_test (String)

    The gem to test The current version of the gem



933
934
935
936
937
938
# File 'lib/pdksync/utils.rb', line 933

def self.check_gem_latest_version(gem_to_test)
  remote_version = Octokit.tags("puppetlabs/#{gem_to_test}").first[:name]
rescue StandardError => error
  puts "(WARNING) Unable to check latest gem version. #{error}".red
  remote_version
end

.check_pdk_versionBoolean

Returns true if the remote version is less than or equal to local version.

Returns:

  • (Boolean)

    true if the remote version is less than or equal to local version



335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/pdksync/utils.rb', line 335

def self.check_pdk_version
  stdout, _stderr, status = Open3.capture3("#{return_pdk_path} --version")
  PdkSync::Logger.fatal "Unable to find pdk at '#{return_pdk_path}'." unless status.exitstatus
  local_version = stdout.strip
  remote_version = Octokit.tags('puppetlabs/pdk').first[:name][1..-1]
  up2date = Gem::Version.new(remote_version) <= Gem::Version.new(local_version)
  unless up2date
    PdkSync::Logger.warn "The current version of pdk is #{remote_version} however you are using #{local_version}"
  end
  up2date
rescue StandardError => error
  PdkSync::Logger.warn "Unable to check latest pdk version. #{error}"
end

.checkout_branch(git_repo, branch_suffix) ⇒ Object

Parameters:

  • git_repo (Git::Base)

    A git object representing the local repository to be branched.

  • branch_suffix (String)

    The string that is appended on the branch name. eg template_ref or a friendly name



328
329
330
# File 'lib/pdksync/utils.rb', line 328

def self.checkout_branch(git_repo, branch_suffix)
  git_repo.branch("pdksync_#{branch_suffix}").checkout
end

.clean_env(output_path) ⇒ Object

Parameters:

  • output_path (String)

    The repository that is to be deleted.



203
204
205
206
# File 'lib/pdksync/utils.rb', line 203

def self.clean_env(output_path)
  # If a local copy already exists it is removed
  FileUtils.rm_rf(output_path)
end

.clone_directory(namespace, module_name, output_path) ⇒ Git::Base

Returns A git object representing the local repository or true if already exist.

Parameters:

  • namespace (String)

    The namespace the repository is located in.

  • module_name (String)

    The name of the repository.

  • output_path (String)

    The location the repository is to be cloned to.

Returns:

  • (Git::Base)

    A git object representing the local repository or true if already exist



218
219
220
221
222
223
224
225
# File 'lib/pdksync/utils.rb', line 218

def self.clone_directory(namespace, module_name, output_path)
  # not all urls are public facing so we need to conditionally use the correct separator
  sep = configuration.git_base_uri.start_with?('git@') ? ':' : '/'
  clone_url = "#{configuration.git_base_uri}#{sep}#{namespace}/#{module_name}.git"
  Git.clone(clone_url, output_path.to_s)
rescue ::Git::GitExecuteError => error
  PdkSync::Logger.fatal "Cloning #{module_name} has failed. #{error}"
end

.commit_staged_files(git_repo, template_ref, commit_message = nil) ⇒ Object

Parameters:

  • git_repo (Git::Base)

    A git object representing the local repository against which the commit is to be made.

  • template_ref (String)

    The unique template_ref that is used as part of the commit name.

  • commit_message (String) (defaults to: nil)

    If specified it will be the message for the commit.



126
127
128
129
130
131
132
133
# File 'lib/pdksync/utils.rb', line 126

def self.commit_staged_files(git_repo, template_ref, commit_message = nil)
  message = if commit_message.nil?
              "pdksync_#{template_ref}"
            else
              commit_message
            end
  git_repo.commit(message)
end

.configurationObject



16
17
18
# File 'lib/pdksync/utils.rb', line 16

def self.configuration
  @configuration ||= PdkSync::Configuration.new
end

.create_commit(git_repo, branch_name, commit_message) ⇒ Object

Returns:



194
195
196
197
# File 'lib/pdksync/utils.rb', line 194

def self.create_commit(git_repo, branch_name, commit_message)
  checkout_branch(git_repo, branch_name)
  commit_staged_files(git_repo, branch_name, commit_message) if add_staged_files(git_repo)
end

.create_filespaceObject



351
352
353
354
# File 'lib/pdksync/utils.rb', line 351

def self.create_filespace
  FileUtils.mkdir_p configuration.pdksync_dir unless Dir.exist?(configuration.pdksync_dir)
  configuration.pdksync_dir
end

.create_filespace_gemObject



358
359
360
361
# File 'lib/pdksync/utils.rb', line 358

def self.create_filespace_gem
  FileUtils.mkdir_p configuration.pdksync_gem_dir unless Dir.exist?(configuration.pdksync_gem_dir)
  configuration.pdksync_gem_dir
end

.create_pr(client, repo_name, template_ref, pdk_version, pr_title = nil) ⇒ Object

Parameters:

  • client (PdkSync::GitPlatformClient)

    The Git platform client used to gain access to and manipulate the repository.

  • repo_name (String)

    The name of the repository on which the commit is to be made.

  • template_ref (String)

    The unique reference that that represents the template the update has ran against.

  • pdk_version (String)

    The current version of the pdk on which the update is run.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/pdksync/utils.rb', line 159

def self.create_pr(client, repo_name, template_ref, pdk_version, pr_title = nil)
  if pr_title.nil?
    title = "pdksync - Update using #{pdk_version}"
    message = "pdk version: `#{pdk_version}` \n pdk template ref: `#{template_ref}`"
    head = "pdksync_#{template_ref}"
  else
    title = "pdksync - #{pr_title}"
    message = "#{pr_title}\npdk version: `#{pdk_version}` \n"
    head = template_ref
  end
  client.create_pull_request(repo_name, configuration.create_pr_against,
                             head,
                             title,
                             message)
rescue StandardError => error
  PdkSync::Logger.fatal "PR creation for #{repo_name} has failed. #{error}"
  nil
end

.delete_branch(client, repo_name, branch_name) ⇒ Object

Parameters:

  • client (PdkSync::GitPlatformClient)

    The Git platform client used to gain access to and manipulate the repository.

  • repo_name (String)

    The name of the repository from which the branch is to be deleted.

  • branch_name (String)

    The name of the branch that is to be deleted.



38
39
40
41
42
# File 'lib/pdksync/utils.rb', line 38

def self.delete_branch(client, repo_name, branch_name)
  client.delete_branch(repo_name, branch_name)
rescue StandardError => error
  PdkSync::Logger.fatal "Deleting #{branch_name} in #{repo_name} failed. #{error}"
end

.duration_hrs_and_mins(ms) ⇒ Object

convert duration from ms to format h m s ms



789
790
791
792
793
794
795
# File 'lib/pdksync/utils.rb', line 789

def self.duration_hrs_and_mins(ms)
  return '' unless ms
  hours, ms   = ms.divmod(1000 * 60 * 60)
  minutes, ms = ms.divmod(1000 * 60)
  seconds, ms = ms.divmod(1000)
  "#{hours}h #{minutes}m #{seconds}s #{ms}ms"
end

.fetch_test_results_locally(output_path, module_type, module_name, report_rows) ⇒ Integer

Returns The status code of the pdk update run.

Parameters:

  • output_path (String)

    The location that the command is to be run from.

  • module_type (String)

    The module type (litmus or traditional)

  • module_name (String)

    The module name

  • report_rows (String)

    The module test results

Returns:

  • (Integer)

    The status code of the pdk update run.



618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/pdksync/utils.rb', line 618

def self.fetch_test_results_locally(output_path, module_type, module_name, report_rows)
  # Save the current path
  old_path = Dir.pwd
  if module_type != 'litmus'
    PdkSync::Logger.warn "(WARNING) Fetching test results locally supports only for litmus'"
  end

  # Run the tests
  Dir.chdir(old_path)
  lines = IO.readlines("#{output_path}/litmusacceptance.out")[-10..-1]
  if lines.find { |e| %r{exit} =~ e } # rubocop:disable Style/ConditionalAssignment
    report_rows << if lines.find { |e| %r{^Failed} =~ e } || lines.find { |e| %r{--trace} =~ e }
                     [module_name, 'FAILED', "Results are available in the following path #{output_path}/litmusacceptance.out"]
                   else
                     [module_name, 'SUCCESS', "Results are available in the following path #{output_path}/litmusacceptance.out"]
                   end
  else
    report_rows << if lines.find { |e| %r{^Failed} =~ e } || lines.find { |e| %r{--trace} =~ e } || lines.find { |e| %r{rake aborted} =~ e }
                     [module_name, 'FAILED', "Results are available in the following path #{output_path}/litmusacceptance.out"]
                   else
                     [module_name, 'PROGRESS', "Results will be available in the following path #{output_path}/litmusacceptance.out"]
                   end
  end
  return report_rows if module_type == 'litmus'
end

.gem_file_update(output_path, gem_to_test, gem_line, gem_sha_finder, gem_sha_replacer, gem_version_finder, gem_version_replacer, gem_branch_finder, gem_branch_replacer, main_path) ⇒ Object

Parameters:

  • output_path (String)

    The location that the command is to be run from.

  • gem_to_test (String)

    The Gem to test.

  • gem_line (String)

    The gem line to replace

  • gem_sha_finder (String)

    The gem sha to find

  • gem_sha_replacer (String)

    The gem sha to replace

  • gem_version_finder (String)

    The gem version to find

  • gem_version_replacer (String)

    The gem version to replace

  • gem_branch_finder (String)

    The gem branch to find

  • gem_branch_replacer (String)

    The gem branch to replace



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/pdksync/utils.rb', line 436

def self.gem_file_update(output_path, gem_to_test, gem_line, gem_sha_finder, gem_sha_replacer, gem_version_finder, gem_version_replacer, gem_branch_finder, gem_branch_replacer, main_path)
  gem_file_name = 'Gemfile'
  validate_gem_update_module(gem_to_test, gem_line, output_path, main_path)

  if (gem_line.nil? == false) && (gem_sha_replacer != '\"\"')
    new_data = get_source_test_gem(gem_to_test, gem_line)
    new_data.each do |data|
      if data.include?('branch')
        gem_branch_replacer = data.split(' ')[1].strip.chomp('"').delete("'")
      elsif data.include?('ref')
        gem_sha_replacer = data.split(' ')[1].strip.chomp('').delete("'")
      elsif data =~ %r{~>|=|>=|<=|<|>}
        delimiters = ['>', '<', '>=', '<=', '=']
        version_to_check = data.split(Regexp.union(delimiters))[1].chomp('""').delete("'")
        validate_gem_version_replacer(version_to_check.to_s, gem_to_test)
      end
    end
  end

  if gem_sha_replacer.nil? == false && gem_sha_replacer != '\"\"' && gem_sha_replacer != ''
    validate_gem_sha_replacer(gem_sha_replacer.chomp('"').reverse.chomp('"').reverse, gem_to_test)
  end
  if gem_branch_replacer.nil? == false && gem_branch_replacer != '\"\"'
    validate_gem_branch_replacer(gem_branch_replacer.chomp('"').reverse.chomp('"').reverse, gem_to_test)
  end
  if gem_version_replacer.nil? == false && gem_version_replacer != '\"\"' && gem_version_replacer != ''
    delimiters = ['<', '>', '<=', '>=', '=']
    version_to_check = gem_version_replacer.split(Regexp.union(delimiters))
    version_to_check.each do |version|
      next if version.nil?
      validate_gem_version_replacer(version.to_s, gem_to_test) unless version == ''
    end
  end

  Dir.chdir(output_path) unless Dir.pwd == output_path

  line_number = 1
  gem_update_sha = [
    { finder: "ref: '#{gem_sha_finder}'",
      replacer: "ref: '#{gem_sha_replacer}'" }
  ]
  gem_update_version = [
    { finder: gem_version_finder,
      replacer: gem_version_replacer }
  ]
  gem_update_branch = [
    { finder: "branch: '#{gem_branch_finder}'",
      replacer: "branch: '#{gem_branch_replacer}'" }
  ]
  # gem_line option is passed

  if gem_line.nil? == false && (gem_line != '' || gem_line != '\"\"')

    # Delete the gem in the Gemfile to add the new line
    gem_test = gem_to_test.chomp('"').reverse.chomp('"').reverse
    File.open(temp_file_path, 'w') do |out_file|
      File.foreach(gem_file_name) do |line|
        out_file.puts line unless line =~ %r{#{gem_test}}
      end
    end
    FileUtils.mv(temp_file_path, gem_file_name)

    # Insert the new Gem to test
    file = File.open(gem_file_name)
    contents = file.readlines.map(&:chomp)
    contents.insert(line_number, gem_line.chomp('"').reverse.chomp('"').reverse)
    File.open(gem_file_name, 'w') { |f| f.write contents.join("\n") }
  end

  # gem_sha_finder and gem_sha_replacer options are passed
  if gem_sha_finder.nil? == false && gem_sha_replacer.nil? == false && gem_sha_finder != '' && gem_sha_finder != '\"\"' && gem_sha_replacer != '' && gem_sha_replacer != '\"\"'
    # Replace with SHA
    file = File.open(gem_file_name)
    contents = file.readlines.join
    gem_update_sha.each do |regex|
      contents = contents.gsub(%r{#{regex[:finder]}}, regex[:replacer])
    end
    File.open(gem_file_name, 'w') { |f| f.write contents.to_s }
  end

  # gem_version_finder and gem_version_replacer options are passed
  if gem_version_finder.nil? == false && gem_version_replacer.nil? == false && gem_version_finder != '' && gem_version_finder != '\"\"' && gem_version_replacer != '' && gem_version_replacer != '\"\"' # rubocop:disable Metrics/LineLength
    # Replace with version
    file = File.open(gem_file_name)
    contents = file.readlines.join
    gem_update_version.each do |regex|
      contents = contents.gsub(%r{#{regex[:finder]}}, regex[:replacer])
    end
    File.open(gem_file_name, 'w') { |f| f.write contents.to_s }
  end

  # gem_branch_finder and gem_branch_replacer options are passed
  if gem_branch_finder.nil? == false && gem_branch_replacer.nil? == false && gem_branch_finder != '' && gem_branch_finder != '\"\"' && gem_branch_replacer != '' && gem_branch_replacer != '\"\"' # rubocop:disable Metrics/LineLength, Style/GuardClause
    # Replace with branch
    file = File.open(gem_file_name)
    contents = file.readlines.join
    gem_update_branch.each do |regex|
      contents = contents.gsub(%r{#{regex[:finder]}}, regex[:replacer]) # unless contents =~ %r{#{gem_to_test}}
    end
    File.open(gem_file_name, 'w') { |f| f.write contents.to_s }
  end
end

.generate_report_table(report_rows, url, data) ⇒ Object

generate report table when running tests on jenkins



846
847
848
849
850
851
852
853
854
# File 'lib/pdksync/utils.rb', line 846

def self.generate_report_table(report_rows, url, data)
  if @failed
    report_rows << ['FAILED', url, data[1]] unless data.nil?
  elsif @aborted
    report_rows << ['ABORTED', url, data[1]] unless data.nil?
  else
    report_rows << [data[0], url, data[1]] unless data.nil?
  end
end

.generate_vmpooler_release_checks(module_path, puppet_version) ⇒ Object

Parameters:

  • module_path (String)

    Path to the module root dir

  • puppet_version (String)

    Puppet version we are generating platform checks for



1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
# File 'lib/pdksync/utils.rb', line 1253

def self.generate_vmpooler_release_checks(module_path, puppet_version)
  PdkSync::Logger.info "Generating release checks provision.yaml key for Puppet version #{puppet_version}"
  # This YAML is where the compatible platforms for each Puppet version is stored
  agent_test_platforms_yaml_file_path = 'lib/pdksync/conf/puppet_abs_supported_platforms.yaml'
  agent_test_platforms = YAML.safe_load(File.read(agent_test_platforms_yaml_file_path))
  raise "No configuration for Puppet #{puppet_version} found in #{agent_test_platforms_yaml_file_path}" unless agent_test_platforms.key? puppet_version
  agent_test_platforms = agent_test_platforms[puppet_version]
  module_supported_platforms = module_supported_platforms(module_path)
  images = []
  PdkSync::Logger.debug 'Processing compatible platforms from metadata.json'
  module_supported_platforms.each do |os_vers|
    os = os_vers['operatingsystem'].downcase
    # 'Windows' and 'OracleLinux' are the definitions in 'metadata.json', however the VMPooler images are 'win' and 'oracle'
    os = 'win' if os == 'windows'
    os = 'oracle' if os == 'oraclelinux'
    vers = os_vers['operatingsystemrelease']
    if agent_test_platforms.keys.select { |k| k.start_with? os }.empty?
      PdkSync::Logger.warn "'#{os}' is a compatible platform but was not defined as test platform for Puppet #{puppet_version} in #{agent_test_platforms_yaml_file_path}"
      next
    end
    vers.each do |ver|
      PdkSync::Logger.debug "Checking '#{os} #{ver}'"
      if os == 'win'
        win_ver = normalize_win_version(ver)
        PdkSync::Logger.debug "Normalised Windows version: #{win_ver}"
        next unless agent_test_platforms['win'].include? win_ver
        PdkSync::Logger.debug "'#{os} #{ver}' SUPPORTED by Puppet #{puppet_version}"
        images << "win-#{win_ver}-x86_64"
      else
        next unless agent_test_platforms[os].include? ver
        PdkSync::Logger.debug "'#{os} #{ver}' SUPPORTED by Puppet #{puppet_version}"
        images << "#{os}-#{ver.delete('.')}-x86_64"
      end
    end
  end
  images.uniq!
  result = add_provision_list(module_path, "release_checks_#{puppet_version}", 'abs', images)
  PdkSync::Logger.warn "#{module_path}/provision.yaml does not exist" unless result
end

.get_data_build(url, build_id, module_name) ⇒ Object

for each build from adhoc jobs, get data if multiple platforms under current url, get data for each platform



858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
# File 'lib/pdksync/utils.rb', line 858

def self.get_data_build(url, build_id, module_name)
  current_build_data = JSON.parse(Faraday.get("#{url}/api/json").body.to_s)
  if current_build_data['activeConfigurations'].nil?
    returned_data = analyse_jenkins_report(url, module_name, build_id)
    if returned_data[0] == 'in progress'
      @in_progress = true
    elsif returned_data[0] == 'FAILURE'
      @failed = true
    elsif returned_data[0] == 'ABORTED'
      @aborted = true
    end
  else
    platforms_list = []
    current_build_data['activeConfigurations'].each do |url_child|
      next if url_child['color'] == 'notbuilt'
      platforms_list.push(url_child['url'])
    end

    platforms_list.each do |platform_build|
      returned_data = analyse_jenkins_report(platform_build, module_name, build_id)
      if returned_data[0] == 'in progress'
        @in_progress = true
      elsif returned_data[0] == 'FAILURE'
        @failed = true
      elsif returned_data[0] == 'ABORTED'
        @aborted = true
      end
    end
  end

  @data = "\nFAILURE. Fix the failures and rerun tests!\n" if @failed
  @data = "\nIN PROGRESS. Please check test report after the execution is done!\n" if @in_progress
  write_to_file("results_#{module_name}.out", @data) if @failed || @in_progress
  PdkSync::Logger.info 'Failed status! Fix errors and rerun.'.red if @failed
  PdkSync::Logger.info 'Aborted status! Fix errors and rerun.'.red if @aborted
  PdkSync::Logger.info 'Tests are still running! You can fetch the results later by using this task: test_results_jenkins'.blue if @in_progress
  returned_data
end

.get_source_test_gem(gem_to_test, gem_line) ⇒ String

Returns The source location of the gem to test.

Parameters:

  • gem_to_test (String)

    The gem to test

  • gem_line (String)

    TThe line to update in the Gemfile

Returns:

  • (String)

    The source location of the gem to test



652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/pdksync/utils.rb', line 652

def self.get_source_test_gem(gem_to_test, gem_line)
  return gem_line.split(',') if gem_line
  return gem_to_test unless gem_to_test

  gemfile_line = File.readlines('Gemfile').find do |line|
    line.include?(gem_to_test.to_s)
  end

  return "https://github.com/puppetlabs/#{gem_to_test}" unless gemfile_line
  gemfile_line =~ %r{(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?}
  line.split(',')[1].strip.to_s if line
end

.metadata_json(module_path) ⇒ JSON

Returns JSON of the metadata.json.

Parameters:

  • module_path

    Path to the root dir of the module

Returns:

  • (JSON)

    JSON of the metadata.json



1106
1107
1108
1109
1110
# File 'lib/pdksync/utils.rb', line 1106

def self.(module_path)
   = "#{module_path}/metadata.json"
  raise 'Could not locate metadata.json' unless File.exist? 
  JSON.parse(File.read())
end

.module_supported_platforms(module_path) ⇒ Hash

Returns The compatible OSs defined in the ‘operatingsystem_support’ key of the ‘metadata.json’.

Parameters:

  • module_path (String)

    Path to the module root dir

Returns:

  • (Hash)

    The compatible OSs defined in the ‘operatingsystem_support’ key of the ‘metadata.json’



1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'lib/pdksync/utils.rb', line 1033

def self.module_supported_platforms(module_path)
  PdkSync::Logger.info 'Determining supported platforms from metadata.json'
  os_support_key = 'operatingsystem_support'
   = "#{module_path}/metadata.json"
  raise 'Could not locate metadata.json' unless File.exist? 
   = JSON.parse(File.read())
  raise "Could not locate '#{os_support_key}' key from #{}" unless .key? os_support_key
  [os_support_key]
end

.module_templates_url(metadata_file = 'metadata.json') ⇒ String

Returns A string value that represents the current pdk tempalate-url.

Parameters:

  • metadata_file (String) (defaults to: 'metadata.json')

    An optional input that can be used to set the location of the metadata file.

  • url (String)
    • the url of the pdk-templates repo

Returns:

  • (String)

    A string value that represents the current pdk tempalate-url.



304
305
306
307
308
# File 'lib/pdksync/utils.rb', line 304

def self.module_templates_url( = 'metadata.json')
  file = File.read()
  data_hash = JSON.parse(file)
  data_hash['template-url']
end

.module_type(output_path, repo_name) ⇒ Object

Parameters:

  • output_path (String)

    The location that the command is to be run from.

  • repo_name (String)

    The module name to identify the type



545
546
547
548
549
550
551
552
553
554
# File 'lib/pdksync/utils.rb', line 545

def self.module_type(output_path, repo_name)
  if repo_name.nil? == false
    module_type = if File.exist?("#{output_path}/provision.yaml")
                    'litmus'
                  else
                    'traditional'
                  end
  end
  module_type
end

.normalize_metadata_supported_platforms(module_path) ⇒ Object

Parameters:

  • module_path

    Path to the root dir of the module



1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
# File 'lib/pdksync/utils.rb', line 1132

def self.(module_path)
   = (module_path)

  [OPERATINGSYSTEM_SUPPORT].each do |os_vers|
    normalized_os = normalize_os(os_vers[OPERATINGSYSTEM])
    unless normalized_os == os_vers[OPERATINGSYSTEM]
      PdkSync::Logger.info "Corrected OS Name: '#{os_vers[OPERATINGSYSTEM]}' -> '#{normalized_os}'"
      os_vers[OPERATINGSYSTEM] = normalized_os
    end
    next unless normalized_os == 'Windows'
    normalized_vers = os_vers[OPERATINGSYSTEMRELEASE].map { |v| normalize_win_version(v) }
    unless normalized_vers == os_vers[OPERATINGSYSTEMRELEASE]
      PdkSync::Logger.info "Corrected OS Versions: #{os_vers[OPERATINGSYSTEMRELEASE]} -> #{normalized_vers}"
      os_vers[OPERATINGSYSTEMRELEASE] = normalized_vers
    end
  end

  (module_path, )
end

.normalize_os(os) ⇒ String

Returns Normalized os name.

Parameters:

  • os

    The OS name to normalize

Returns:

  • (String)

    Normalized os name



1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
# File 'lib/pdksync/utils.rb', line 1065

def self.normalize_os(os)
  case os
  when %r{aix}i
    'AIX'
  when %r{cent}i
    'CentOS'
  when %r{darwin}i
    'Darwin'
  when %r{deb}i
    'Debian'
  when %r{fedora}i
    'Fedora'
  when %r{oracle}i
    'OracleLinux'
  when %r{osx}i
    'OSX'
  when %r{pan}i
    'PAN-OS'
  when %r{red}i
    'RedHat'
  when %r{sci}i
    'Scientific'
  when %r{suse|sles}i
    'SLES'
  when %r{sol}i
    'Solaris'
  when %r{ubuntu}i
    'Ubuntu'
  when %r{win}i
    'Windows'
  else
    raise "Could not normalize OS value: #{os}"
  end
end

.normalize_win_version(ver) ⇒ String

Returns Normalised version that is used by VMPooler templates.

Parameters:

  • ver

    Version from ‘metadata.json’

Returns:

  • (String)

    Normalised version that is used by VMPooler templates



1050
1051
1052
1053
1054
1055
1056
1057
# File 'lib/pdksync/utils.rb', line 1050

def self.normalize_win_version(ver)
  PdkSync::Logger.debug "Normalising Windows version from metadata.json: #{ver}"
  win_ver_matcher = ver.match(%r{(?:Server\s)?(?<ver>\d+)(?:\s(?<rel>R\d))?})
  raise "Unable to determine Windows version from metadata.json: #{ver}" unless win_ver_matcher
  normalized_version = win_ver_matcher['ver']
  normalized_version += " #{win_ver_matcher['rel'].upcase}" if win_ver_matcher['rel']
  normalized_version
end

.on_windows?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/pdksync/utils.rb', line 20

def self.on_windows?
  # Ruby only sets File::ALT_SEPARATOR on Windows and the Ruby standard
  # library uses that to test what platform it's on.
  !!File::ALT_SEPARATOR # rubocop:disable Style/DoubleNegation
end

.pdk_update(output_path) ⇒ Integer

Returns The status code of the pdk update run.

Parameters:

  • output_path (String)

    The location that the command is to be run from.

Returns:

  • (Integer)

    The status code of the pdk update run.



272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/pdksync/utils.rb', line 272

def self.pdk_update(output_path)
  # Runs the pdk update command
  Dir.chdir(output_path) do
    _, module_temp_ref = module_templates_url.split('#')
    module_temp_ref ||= configuration.pdk_templates_ref
    template_ref = configuration.module_is_authoritive ? module_temp_ref : configuration.pdk_templates_ref
    change_module_template_url(configuration.pdk_templates_url, template_ref) unless configuration.module_is_authoritive
    _stdout, stderr, status = Open3.capture3("#{return_pdk_path} update --force --template-ref=#{template_ref}")
    PdkSync::Logger.fatal "Unable to run `pdk update`: #{stderr}" unless status.exitstatus.zero?
    status.exitstatus
  end
end

.push_staged_files(git_repo, current_branch, repo_name) ⇒ Object

Parameters:

  • git_repo (Git::Base)

    A git object representing the local repository againt which the push is to be made.

  • template_ref (String)

    The unique reference that that represents the template the update has ran against.

  • repo_name (String)

    The name of the repository on which the commit is to be made.



143
144
145
146
147
# File 'lib/pdksync/utils.rb', line 143

def self.push_staged_files(git_repo, current_branch, repo_name)
  git_repo.push(configuration.push_file_destination, current_branch)
rescue StandardError => error
  PdkSync::Logger.error "Pushing to #{configuration.push_file_destination} for #{repo_name} has failed. #{error}"
end

.remove_platform_from_metadata(module_path, os_to_remove, version_to_remove) ⇒ Object

Parameters:

  • module_path

    Path to the root dir of the module

  • os_to_remove

    OS we want to remove version from

  • version_to_remove

    Version from OS we want to remove



1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
# File 'lib/pdksync/utils.rb', line 1162

def self.(module_path, os_to_remove, version_to_remove)
   = (module_path)
  [OPERATINGSYSTEM_SUPPORT].each do |os_vers|
    if (os = normalize_os(os_vers[OPERATINGSYSTEM]))
      next unless os == os_to_remove
      vers = os_vers[OPERATINGSYSTEMRELEASE]
      next unless (ver_index = vers.find_index(version_to_remove))
      PdkSync::Logger.info "Removing #{os} #{vers[ver_index]} from metadata.json"
      vers.delete_at(ver_index)
    else
      PdkSync::Logger.info 'No entry in metadata.json to replace'
      return true
    end
  end
  (module_path, )
end

.return_modulesArray

Returns An array of different module names.

Returns:

  • (Array)

    An array of different module names.



395
396
397
398
# File 'lib/pdksync/utils.rb', line 395

def self.return_modules
  raise "File '#{configuration.managed_modules}' is empty/does not exist" if File.size?(configuration.managed_modules).nil?
  YAML.safe_load(File.open(configuration.managed_modules))
end

.return_pdk_pathObject

Returns String Path to the pdk executable.

Returns:

  • String Path to the pdk executable



182
183
184
185
186
187
188
189
190
191
# File 'lib/pdksync/utils.rb', line 182

def self.return_pdk_path
  full_path = '/opt/puppetlabs/pdk/bin/pdk'
  path = if File.executable?(full_path)
           full_path
         else
           PdkSync::Logger.warn "(WARNING) Using pdk on PATH not '#{full_path}'"
           'pdk'
         end
  path
end

.return_pdk_version(metadata_file = 'metadata.json') ⇒ String

Returns A string value that represents the current pdk version.

Parameters:

  • metadata_file (String) (defaults to: 'metadata.json')

    An optional input that can be used to set the location of the metadata file.

Returns:

  • (String)

    A string value that represents the current pdk version.



97
98
99
100
101
# File 'lib/pdksync/utils.rb', line 97

def self.return_pdk_version( = 'metadata.json')
  file = File.read()
  data_hash = JSON.parse(file)
  data_hash['pdk-version']
end

.return_template_ref(metadata_file = 'metadata.json') ⇒ String

Returns A string value that represents the current pdk template.

Parameters:

  • metadata_file (String) (defaults to: 'metadata.json')

    An optional input that can be used to set the location of the metadata file.

Returns:

  • (String)

    A string value that represents the current pdk template.



291
292
293
294
295
# File 'lib/pdksync/utils.rb', line 291

def self.return_template_ref( = 'metadata.json')
  file = File.read()
  data_hash = JSON.parse(file)
  data_hash['template-ref']
end

.run_command(output_path, command, option) ⇒ Integer

Returns The status code of the command run.

Parameters:

  • output_path (String)

    The location that the command is to be run from.

  • command (String)

    The command to be run.

Returns:

  • (Integer)

    The status code of the command run.



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/pdksync/utils.rb', line 235

def self.run_command(output_path, command, option)
  stdout = ''
  stderr = ''
  status = Process::Status
  pid = ''
  Dir.chdir(output_path) unless Dir.pwd == output_path

  # Environment cleanup required due to Ruby subshells using current Bundler environment
  if option.nil? == true
    if command =~ %r{^bundle}
      Bundler.with_clean_env do
        stdout, stderr, status = Open3.capture3(command)
      end
    else
      stdout, stderr, status = Open3.capture3(command)
    end
    PdkSync::Logger.info "\n#{stdout}\n"
    PdkSync::Logger.crit "Unable to run command '#{command}': #{stderr}" unless status.exitstatus.zero?
    status.exitstatus
  else
    # Environment cleanup required due to Ruby subshells using current Bundler environment
    if command =~ %r{^sh }
      Bundler.with_clean_env do
        pid = spawn(command, out: 'run_command.out', err: 'run_command.err')
        Process.detach(pid)
      end
    end
    pid
  end
end

.run_tests_jenkins(jenkins_client, repo_name, current_branch, github_user, job_name) ⇒ Object

Parameters:

  • client (PdkSync::GitPlatformClient)

    The Git platform client used to gain access to and manipulate the repository.

  • ouput_path (String)

    The location that the command is to be run from.

  • jenkins_client (String)

    Jenkins authentication.

  • repo_name (String)

    Module to run on Jenkins

  • current_branch (String)

    The branch against which the user needs to run the jenkin jobs



776
777
778
779
780
781
782
783
784
785
786
# File 'lib/pdksync/utils.rb', line 776

def self.run_tests_jenkins(jenkins_client, repo_name, current_branch, github_user, job_name)
  if jenkins_client.nil? == false || repo_name.nil? == false || current_branch.nil? == false
    pr = jenkins_client.create_adhoc_job(repo_name,
                                         current_branch,
                                         github_user,
                                         job_name)
    pr
  end
rescue StandardError => error
  puts "(FAILURE) Jenkins Job creation for #{repo_name} has failed. #{error}".red
end

.run_tests_locally(output_path, module_type, provision_type, module_name, puppet_collection) ⇒ Integer

Returns The status code of the pdk update run.

Parameters:

  • output_path (String)

    The location that the command is to be run from.

  • module_type (String)

    The module type (litmus or traditional)

  • module_name (String)

    The module name

  • puppet (String)

    collection The puppet collection

Returns:

  • (Integer)

    The status code of the pdk update run.



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/pdksync/utils.rb', line 568

def self.run_tests_locally(output_path, module_type, provision_type, module_name, puppet_collection)
  provision_type = provision_type.chomp('"').reverse.chomp('"').reverse
  status = Process::Status
  # Save the current path
  old_path = Dir.pwd

  # Create the acceptance scripts
  file = File.open('acc.sh', 'w')
  file.puts '#!/bin/sh'

  if puppet_collection
    file.puts "export PUPPET_GEM_VERSION='~> #{puppet_collection}'"
  end
  file.puts "rm -rf #{output_path}/Gemfile.lock;rm -rf #{output_path}/.bundle"
  file.puts 'bundle install --path .bundle/gems/ --jobs 4'
  file.puts "bundle exec rake 'litmus:provision_list[#{provision_type}]'"
  file.puts 'bundle exec rake litmus:install_agent'
  file.puts 'bundle exec rake litmus:install_module'
  file.puts 'bundle exec rake litmus:acceptance:parallel'
  file.puts 'bundle exec rake litmus:tear_down'
  file.close

  # Runs the module tests command
  if module_type == 'litmus'
    run_command(output_path, 'cp ../../acc.sh .', nil)
    Dir.chdir(old_path)
    run_command(output_path, 'chmod 777 acc.sh', nil)
    Dir.chdir(old_path)
    status = run_command(output_path, 'sh acc.sh 2>&1 | tee litmusacceptance.out', 'background')
    if status != 0
      PdkSync::Logger.info "SUCCESS:Kicking of module Acceptance tests to run for the module #{module_name} - SUCCEED.Results will be available in the following path #{output_path}/litmusacceptance.out.Process id is #{status}"
    else
      PdkSync::Logger.fatal "FAILURE:Kicking of module Acceptance tests to run for the module #{module_name} - FAILED.Results will be available in the following path #{output_path}/litmusacceptance.out."
    end
  end
  PdkSync::Logger.warn "(WARNING) Executing testcases locally supports only for litmus'" if module_type != 'litmus'
end

.setup_clientPdkSync::GitPlatformClient

Returns client The Git platform client that has been created.

Returns:



367
368
369
370
371
# File 'lib/pdksync/utils.rb', line 367

def self.setup_client
  PdkSync::GitPlatformClient.new(configuration.git_platform, configuration.git_platform_access_settings)
rescue StandardError => error
  raise "Git platform access not set up correctly: #{error}"
end

.setup_jenkins_client(jenkins_server_url) ⇒ PdkSync::JenkinsClient

Returns client The Git platform client that has been created.

Returns:



377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/pdksync/utils.rb', line 377

def self.setup_jenkins_client(jenkins_server_url)
  require 'pdksync/jenkinsclient'
  if configuration.jenkins_platform_access_settings[:jenkins_username].nil?
    raise ArgumentError, "Jenkins access token for #{configuration.jenkins_platform.capitalize} not set"\
        " - use 'export #{configuration.jenkins_platform.upcase}_USERNAME=\"<your username>\"' to set"
  elsif configuration.jenkins_platform_access_settings[:jenkins_password].nil?
    raise ArgumentError, "Jenkins access token for #{jenkins_platform.capitalize} not set"\
    " - use 'export #{jenkins_platform.upcase}_PASSWORD=\"<your password>\"' to set"
  end
  PdkSync::JenkinsClient.new(jenkins_server_url, configuration.jenkins_platform_access_settings)
rescue StandardError => error
  raise "Jenkins platform access not set up correctly: #{error}"
end

.temp_file_pathObject



26
27
28
# File 'lib/pdksync/utils.rb', line 26

def self.temp_file_path
  @temp_file_path ||= on_windows? ? "#{ENV['TEMP']}\\out.tmp" : '/tmp/out.tmp'
end

.test_results_jenkins(jenkins_server_url, build_id, job_name, module_name) ⇒ Object

test_results_jenkins



817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
# File 'lib/pdksync/utils.rb', line 817

def self.test_results_jenkins(jenkins_server_url, build_id, job_name, module_name)
  PdkSync::Logger.info 'Fetch results from jenkins'
  # remove duplicates and sort the list
  adhoc_urls = adhoc_urls(job_name, jenkins_server_url).uniq.sort_by { |url| JSON.parse(Faraday.get("#{url}/api/json").body.to_s)['fullDisplayName'].scan(%r{[0-9]{2}\s}).first.to_i }
  report_rows = []
  @failed = false
  @in_progress = false
  @aborted = false

  File.delete("results_#{module_name}.out") if File.exist?("results_#{module_name}.out")
  # analyse each build result - get status, execution time, logs_link
  @data = "MODULE_NAME=#{module_name}\nBUILD_ID=#{build_id}\nINITIAL_job=#{jenkins_server_url}/job/#{job_name}/#{build_id}\n\n"
  write_to_file("results_#{module_name}.out", @data)
  PdkSync::Logger.info "Analyse test execution report \n"
  adhoc_urls.each do |url|
    # next if skipped in build name
    current_build_data = JSON.parse(Faraday.get("#{url}/api/json").body.to_s)
    next if url.include?('skippable_adhoc') || current_build_data['color'] == 'notbuilt'
    next if current_build_data['fullDisplayName'].downcase.include?('skipped')
    returned_data = get_data_build(url, build_id, module_name) unless @failed || @in_progress
    generate_report_table(report_rows, url, returned_data)
  end

  table = Terminal::Table.new title: "Module Test Results for: #{module_name}\nCheck results in #{Dir.pwd}/results_#{module_name}.out ", headings: %w[Status Result Execution_Time], rows: report_rows
  PdkSync::Logger.info "SUCCESSFUL test results!\n".green unless @failed || @in_progress
  PdkSync::Logger.info "\n#{table} \n"
end

.update_gem_latest_version_by_one(gem_version) ⇒ Object

Parameters:

  • gem_version (String)

    The current version of the gem The bump version by one of the gem



945
946
947
948
949
950
951
# File 'lib/pdksync/utils.rb', line 945

def self.update_gem_latest_version_by_one(gem_version)
  current_version = Gem::Version.new gem_version
  new_version = current_version.bump
rescue StandardError => error
  puts "(WARNING) Unable to check latest gem version. #{error}".red
  new_version
end

.update_gemfile_multigem(output_path, gem_name, gemfury_token, gemfury_user) ⇒ Object

Parameters:

  • output_path (String)

    The location that the command is to be run from.

  • gem_name (String)

    The gem name

  • gemfury_token (String)

    The gemfury token

  • gemfury_user (String)

    The gemfury user



963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# File 'lib/pdksync/utils.rb', line 963

def self.update_gemfile_multigem(output_path, gem_name, gemfury_token, gemfury_user)
  gem_file_name = 'Gemfile'
  gem_source_line = "source \"https://#{gemfury_token}@gem.fury.io/#{gemfury_user}/\""
  Dir.chdir(output_path) unless Dir.pwd == output_path

  if gem_name.nil? == false && gemfury_token.nil? == false && gemfury_user.nil? == false # rubocop:disable Style/GuardClause
    # Append the gem with new source location
    gem_name = gem_name.chomp('"').reverse.chomp('"').reverse
    begin
      File.open(temp_file_path, 'w') do |out_file|
        File.foreach(gem_file_name) do |line|
          if line =~ %r{#{gem_name}}
            line = line.chomp
            if line =~ %r{"https://#{gemfury_token}@gem.fury.io/#{gemfury_user}/"}
              puts 'GemFile Already updated'.green
              out_file.puts line.to_s
            else
              out_file.puts "#{line} , :source => \"https://#{gemfury_token}@gem.fury.io/#{gemfury_user}/\""
            end
          else
            out_file.puts line
          end
        end
      end
      FileUtils.mv(temp_file_path, gem_file_name)

      # Insert the new source Gem location to Gemfile
      file = File.open(gem_file_name)
      contents = file.readlines.map(&:chomp)
      contents.insert(2, gem_source_line) unless contents.include?(gem_source_line)
      File.open(gem_file_name, 'w') { |f| f.write contents.join("\n") }
    rescue Errno::ENOENT => e
      raise "Couldn't find file: #{gem_file_name} #{e} in your repository: #{gem_file_name}".red
    rescue Errno::EACCES => e
      raise "Does not have required permissions to the #{gem_file_name} #{e} in your repository: #{gem_file_name}".red
    end
  end
end

.update_requirements(module_path, name, key, value) ⇒ Object

Parameters:

  • module_path

    Path to the root dir of the module

  • name

    Name attribute of the requirement

  • key

    The key name of a K/V pair to be added / updated in the requirement

  • value

    The value of the key to be added / updated in the requirement



1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
# File 'lib/pdksync/utils.rb', line 1226

def self.update_requirements(module_path, name, key, value)
   = (module_path)
  updated_existing_entry = false
  [REQUIREMENTS].each do |requirement|
    next unless requirement[NAME] == name
    PdkSync::Logger.info "Updating [#{requirement['name']}] #{requirement.key? key ? "dependency's existing" : 'with a new'} key [#{key}] to value [#{value}]"
    requirement[key] = value
    updated_existing_entry = true
  end
  unless updated_existing_entry
    PdkSync::Logger.info "Adding new requirement [#{name}] with key [#{key}] of value [#{value}]"
    new_requirement = {}
    new_requirement[NAME] = name
    new_requirement[key] = value
    [REQUIREMENTS] << new_requirement
  end
  (module_path, )
end

.validate_gem_branch_replacer(gem_branch_replacer, gem_to_test) ⇒ Object

Parameters:

  • gem_to_test (String)

    The gem to test

  • gem_branch_replacer (String)

    The branch to update in the Gemfile



744
745
746
747
# File 'lib/pdksync/utils.rb', line 744

def self.validate_gem_branch_replacer(gem_branch_replacer, gem_to_test)
  raise "Couldn't find branch: #{gem_branch_replacer} in your repository: #{gem_to_test}".red unless @all_branches.include?(gem_branch_replacer)
  puts "Branch #{gem_branch_replacer} valid.\n".green
end

.validate_gem_sha_replacer(gem_sha_replacer, gem_to_test) ⇒ Object

Parameters:

  • gem_to_test (String)

    The gem to test

  • gem_sha_replacer (String)

    The sha to update in the Gemfile



729
730
731
732
733
734
735
736
# File 'lib/pdksync/utils.rb', line 729

def self.validate_gem_sha_replacer(gem_sha_replacer, gem_to_test)
  found = false
  @all_refs.split(' ').each do |sha|
    puts "SHA #{gem_sha_replacer} valid.\n".green if gem_sha_replacer == sha
    found = true if gem_sha_replacer == sha
  end
  raise "Couldn't find sha: #{gem_sha_replacer} in your repository: #{gem_to_test}".red if found == false
end

.validate_gem_update_module(gem_to_test, gem_line, output_path, main_path) ⇒ Object

Parameters:

  • gem_to_test (String)

    The gem to test

  • gem_line (String)

    The line to update in the Gemfile



671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/pdksync/utils.rb', line 671

def self.validate_gem_update_module(gem_to_test, gem_line, output_path, main_path)
  gem_to_test = gem_to_test.chomp('"').reverse.chomp('"').reverse
  Dir.chdir(main_path)
  output_path = "#{configuration.pdksync_dir}/#{gem_to_test}"
  clean_env(output_path) if Dir.exist?(output_path)
  print 'delete module directory, '

  # when gem_line is specified, we need to parse the line and identify all the values
  # - we can have source url or we need to
  # - sha, branch, version
  if gem_line
    git_repo = get_source_test_gem(gem_to_test, gem_line)
    i = 0
    git_repo.each do |item|
      i += 1
      if item =~ %r{((git@|http(s)?:\/\/)([\w\.@]+)(\/|:))([\w,\-,\_]+)\/([\w,\-,\_]+)(.git){0,1}((\/){0,1})}
        git_repo = item.split('git:')[1].strip.delete("'")
        break
      elsif git_repo.size == i
        # git_repo = "https://github.com/puppetlabs#{gem_to_test}"
        sep = configuration.git_base_uri.start_with?('git@') ? ':' : '/'
        git_repo = "#{configuration.git_base_uri}#{sep}#{configuration.namespace}/#{gem_to_test}"
      end
    end
    print 'clone module directory, '
    git_repo = run_command(configuration.pdksync_dir.to_s, "git clone #{git_repo}", nil)
  elsif gem_to_test
    git_repo = clone_directory(configuration.namespace, gem_to_test, output_path.to_s)
  end

  Dir.chdir(main_path)
  raise "Unable to clone repo for #{gem_to_test}. Check repository's url to be correct!".red if git_repo.nil?

  @all_versions = ''
  @all_refs = ''
  @all_branches = ''

  Dir.chdir(output_path)

  stdout_refs, stderr_refs, status_refs = Open3.capture3('git show-ref -s')
  @all_refs = stdout_refs
  stdout_branches, stderr_branches, status_branches = Open3.capture3('git branch -a')
  @all_branches = stdout_branches
  stdout_versions, stderr_versions, status_versions = Open3.capture3('git tag')
  @all_versions = stdout_versions

  raise "Couldn't get references due to #{stderr_refs}".red unless status_refs.exitstatus.zero?
  raise "Couldn't get branches due to #{stderr_branches}".red unless status_branches.exitstatus.zero?
  raise "Couldn't get versions due to #{stderr_versions}".red unless status_versions.exitstatus.zero?
  Dir.chdir(main_path)
end

.validate_gem_version_replacer(gem_version_replacer, gem_to_test) ⇒ Object

Parameters:

  • gem_to_test (String)

    The gem to test

  • gem_version_replacer (String)

    The version to update in the Gemfile



755
756
757
758
759
760
761
762
# File 'lib/pdksync/utils.rb', line 755

def self.validate_gem_version_replacer(gem_version_replacer, gem_to_test)
  found = false
  @all_versions.split(' ').each do |version|
    puts "Version #{gem_version_replacer} valid.\n".green if gem_version_replacer == version
    found = true if gem_version_replacer == version
  end
  raise "Couldn't find version: #{gem_version_replacer} in your repository: #{gem_to_test}".red if found == false
end

.validate_modules_exist(client, module_names) ⇒ Object

Parameters:

  • client (PdkSync::GitPlatformClient)

    The Git platform client used to get a repository.

  • module_names (Array)

    String array of the names of Git platform repos



408
409
410
411
412
413
414
# File 'lib/pdksync/utils.rb', line 408

def self.validate_modules_exist(client, module_names)
  raise "Error reading in modules. Check syntax of '#{configuration.managed_modules}'." unless !module_names.nil? && module_names.is_a?(Array)
  invalid = module_names.reject { |name| client.repository?("#{configuration.namespace}/#{name}") }
  # Raise error if any invalid matches were found
  raise "Could not find the following repositories: #{invalid}" unless invalid.empty?
  true
end

.write_metadata_json(module_path, metadata_json) ⇒ Object

Parameters:

  • module_path

    Path to the root dir of the module

  • metadata_json

    Metadata in JSON format to write to the module root dir



1122
1123
1124
1125
1126
# File 'lib/pdksync/utils.rb', line 1122

def self.(module_path, )
  File.open(File.join(module_path, 'metadata.json'), 'w') do |f|
    f.write(JSON.pretty_generate() + "\n")
  end
end

.write_to_file(file, _data) ⇒ Object

write test report to file



898
899
900
901
902
# File 'lib/pdksync/utils.rb', line 898

def self.write_to_file(file, _data)
  File.open(file, 'a') do |f|
    f.write @data
  end
end