class RefArchSetup::DownloadHelper

Download helper methods

Constants

BASE_PROD_URL

the base of the URL where prod PE tarballs are hosted

MIN_PROD_VERSION

the minimum prod version supported by RAS

PE_PLATFORMS

the supported platforms for PE installation using RAS

PE_VERSIONS_URL

the 'Puppet Enterprise Version History' url

Public Class Methods

build_prod_tarball_url(version = "latest", host = "localhost", platform = "default") click to toggle source

Builds the prod tarball URL for the specified or default version of PE

@author Bill Claytor

@param [string] version The desired version of PE (default = “latest”) @param [string] host The target host for this PE tarball (default = “localhost”) @param [string] platform The target platform for this PE tarball

*** Specifying the host will determine and validate the platform for the host *** Specifying the platform will ignore the host value and only perform the validation

@return [string] the prod tarball URL

@example: url = build_prod_tarball_url() url = build_prod_tarball_url(“2018.1.4”, “master.mydomain.net”, “”) url = build_prod_tarball_url(“2018.1.4”, “value_is_ignored”, “sles-12-x86_64”)

# File lib/ref_arch_setup/download_helper.rb, line 59
def self.build_prod_tarball_url(version = "latest", host = "localhost", platform = "default")
  init
  pe_version = handle_prod_version(version)
  pe_platform = handle_platform(host, platform)
  url = "#{@base_prod_url}/#{pe_version}/puppet-enterprise-#{pe_version}-#{pe_platform}.tar.gz"
  puts "URL: #{url}"
  return url
end
ensure_supported_prod_version(version) click to toggle source

Determines whether the specified version is supported by RAS

@author Bill Claytor

@param [string] version The specified version of PE

@raise [RuntimeError] If the specified version is not supported

@return [true, false] Whether the specified version is supported

@example:

result = ensure_supported_prod_version("2018.1.4")
# File lib/ref_arch_setup/download_helper.rb, line 152
def self.ensure_supported_prod_version(version)
  major_version = version.split(".")[0]
  supported_version = @min_prod_version.split(".")[0]

  supported = major_version >= supported_version ? true : false
  puts "Specified version #{version} is supported by RAS" if supported
  puts "The minimum supported version is #{@min_prod_version}" unless supported

  raise "Specified version #{version} is not supported by RAS" unless supported

  return supported
end
ensure_valid_prod_version(version) click to toggle source

Determines whether the specified version is an actual production version of PE

@author Bill Claytor

@param [string] version The specified version of PE

@raise [RuntimeError] If the specified version is not found

@return [true, false] Whether the specified version was found

@example:

result = ensure_valid_prod_version("2018.1.4")
# File lib/ref_arch_setup/download_helper.rb, line 127
def self.ensure_valid_prod_version(version)
  puts "Verifying specified PE version: #{version}"

  result = parse_prod_versions_url("//table/tbody/tr/td[contains(., '#{version}')]")
  found = result.text == version ? true : false

  puts "Specified version #{version} was found" if found
  raise "Specified version not found: #{version}" unless found

  return found
end
fetch_prod_versions() click to toggle source

Fetches the list of production PE versions from the 'Puppet Enterprise Version History' *note: this is no longer used but has been left as an example @author Bill Claytor

@return [Oga::XML::NodeSet] The versions list

@example:

versions = fetch_prod_versions
# File lib/ref_arch_setup/download_helper.rb, line 174
def self.fetch_prod_versions
  versions = parse_prod_versions_url
  puts "Versions:"

  versions.each do |version|
    version_text = version.text
    puts version_text
  end
  puts

  return versions
end
get_host_platform(host) click to toggle source

Handles the platform for the specified host using the host's facts

@author Bill Claytor

@param [string] host The target host

@raise [RuntimeError] If the facts status is 'failure' @raise [RuntimeError] If the platform can't be determined

@return [string] The corresponding platform for the specified host

@example:

platform = get_host_platform("localhost")

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity

# File lib/ref_arch_setup/download_helper.rb, line 310
def self.get_host_platform(host)
  facts = retrieve_facts(host)

  status = facts[0]["status"]
  raise "Facts indicate that status for host #{host} is failure" if status == "failure"

  os = facts[0]["result"]["os"]
  os_name = os["name"]
  os_family = os["family"]
  platform_type = nil

  case os_family
  when "RedHat"
    platform_type = "el"
    platform_arch = "x86_64"
    release = os["release"]["major"]
  when "SLES"
    platform_type = "sles"
    platform_arch = "x86_64"
    release = os["release"]["major"]
  when "Debian"
    if os_name == "Ubuntu"
      platform_type = "ubuntu"
      platform_arch = "amd64"
      release = os["release"]["full"]
    end
  end

  raise "Unable to determine platform for host: #{host}" unless platform_type
  platform = "#{platform_type}-#{release}-#{platform_arch}"
  puts "Host platform: #{platform}"

  return platform
end
handle_platform(host, platform) click to toggle source

Handles the host and platform and determines the appropriate PE platform

@author Bill Claytor

@param [string] host The target host for this PE tarball (default = “localhost”) @param [string] platform The target platform for this PE tarball

*** Specifying the host will determine and validate the platform for the host *** Specifying the platform will ignore the host value and only perform the validation

@raise [RuntimeError] If the platform is not valid

@return [string] The PE platform

@example:

handle_platform("my_host", "default")
handle_platform("value_is_ignored", "sles-12-x86_64")
# File lib/ref_arch_setup/download_helper.rb, line 281
def self.handle_platform(host, platform)
  if platform == "default"
    puts "Default platform specified; determining platform for host: #{host}"
    pe_platform = get_host_platform(host)
  else
    puts "Specified platform: #{platform}"
    pe_platform = platform
  end
  raise "Invalid PE platform: #{pe_platform}" unless valid_platform?(pe_platform)
  return pe_platform
end
handle_prod_version(version) click to toggle source

Retrieves the latest production PE version or verifies a user-specified version

@author Bill Claytor

@param [string] version The desired version of PE

@raise [RuntimeError] If the specified version is not valid

@return [string] The corresponding PE version

@example:

version = handle_prod_version("latest")
version = handle_prod_version("2018.1.4")
# File lib/ref_arch_setup/download_helper.rb, line 82
def self.handle_prod_version(version)
  if version == "latest"
    pe_version = latest_prod_version
    puts "The latest version is #{pe_version}"
  else
    success = ensure_valid_prod_version(version) && ensure_supported_prod_version(version)
    raise "Invalid version: #{version}" unless success

    pe_version = version
    puts "Proceeding with specified version: #{pe_version}"
  end

  puts

  return pe_version
end
init() click to toggle source

Initializes the instance variables to the defaults or values specified by environment variables

@author Bill Claytor

@example: init

# File lib/ref_arch_setup/download_helper.rb, line 34
def self.init
  @pe_versions_url = ENV["PE_VERSIONS_URL"] ? ENV["PE_VERSIONS_URL"] : PE_VERSIONS_URL
  @base_prod_url = ENV["BASE_PROD_URL"] ? ENV["BASE_PROD_URL"] : BASE_PROD_URL
  @min_prod_version = ENV["MIN_PROD_VERSION"] ? ENV["MIN_PROD_VERSION"] : MIN_PROD_VERSION
  @pe_platforms = PE_PLATFORMS
end
latest_prod_version() click to toggle source

Retrieves the latest production version of PE from the 'Puppet Enterprise Version History'

@author Bill Claytor

@return [string] The latest production version of PE

@example:

latest_version = latest_prod_version
# File lib/ref_arch_setup/download_helper.rb, line 108
def self.latest_prod_version
  result = parse_prod_versions_url("//table/tbody/tr[1]/td[1]")
  latest_version = result.text
  return latest_version
end
parse_prod_versions_url(xpath = "//table/tbody/tr/td[1]") click to toggle source

Parses the 'Puppet Enterprise Version History' using the specified xpath and returns the result

@author Bill Claytor

@param [string] xpath The xpath to use when parsing the version history

@return [Oga::XML::NodeSet] The resulting Oga NodeSet

@example:

versions = parse_prod_versions_url
latest_version = parse_prod_versions_url("//table/tbody/tr[1]/td[1]")
result = parse_prod_versions_url("//table/tbody/tr/td[contains(., '#{version}')]")
# File lib/ref_arch_setup/download_helper.rb, line 201
def self.parse_prod_versions_url(xpath = "//table/tbody/tr/td[1]")
  puts "Checking Puppet Enterprise Version History: #{@pe_versions_url}"

  uri = URI.parse(@pe_versions_url)
  response = Net::HTTP.get_response(uri)
  validate_response(response)

  document = Oga.parse_html(response.body)
  result = document.xpath(xpath)
  return result
end
retrieve_facts(hosts) click to toggle source

Retrieves the facts for the specified host(s) using the facts::retrieve plan

@author Bill Claytor

@param [string] hosts The host(s) from which to retrieve facts

@raise [JSON::ParserError] If the output from the bolt plan can't be parsed @raise [BoltCommandError] If the bolt command is not successful or the output is nil

@return [Array<Hash] The retrieved facts

@example:

facts = retrieve_facts("localhost")
# File lib/ref_arch_setup/download_helper.rb, line 362
def self.retrieve_facts(hosts)
  plan = "facts::retrieve"
  puts "Retrieving facts for hosts: #{hosts}"

  output = BoltHelper.run_forge_plan_with_bolt(plan, nil, hosts)

  begin
    facts = JSON.parse(output)
  rescue
    puts "Unable to parse bolt output"
    raise
  end

  return facts
end
valid_platform?(platform) click to toggle source

Determines whether the specified platform is a valid PE platform

@author Bill Claytor

@param [string] platform The platform

@return [true,false] Based on validity of the specified platform

@example:

is_valid = valid_platform?("sles-12-x86_64")

TODO: validate for a specified version?

# File lib/ref_arch_setup/download_helper.rb, line 391
def self.valid_platform?(platform)
  valid = @pe_platforms.include?(platform) ? true : false
  puts "Platform #{platform} is valid" if valid
  puts "Platform #{platform} is not valid" unless valid
  puts "Valid platforms are: #{@pe_platforms}" unless valid
  return valid
end
validate_response(res, valid_response_codes = ["200"], invalid_response_bodies = ["", nil]) click to toggle source

Determines whether the response is valid

@author Bill Claytor

@param [Net::HTTPResponse] res The HTTP response to evaluate @param [Array] valid_response_codes The list of valid response codes @param [Array] invalid_response_bodies The list of invalid response bodies

@raise [RuntimeError] If the response is not valid

@return [true,false] Based on whether the response is valid

@example

valid = validate_response(res)
valid = validate_response(res, ["200", "123"], ["", nil])

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity

# File lib/ref_arch_setup/download_helper.rb, line 233
def self.validate_response(res, valid_response_codes = ["200"],
                           invalid_response_bodies = ["", nil])
  is_valid_response = false

  if res.nil?
    puts "Invalid response:"
    puts "nil"
    puts
  elsif !valid_response_codes.include?(res.code) ||
        invalid_response_bodies.include?(res.body)
    code = res.code.nil? ? "nil" : res.code
    body = res.body.nil? ? "nil" : res.body

    puts "Invalid response:"
    puts "code: #{code}"
    puts "body: #{body}"
    puts
  else
    is_valid_response = true
  end

  raise "Invalid response" unless is_valid_response

  return is_valid_response
end