class RefArchSetup::Install
Installation helper
@author Randell Pelak
@attr [string] target_master Host to install on
TODO: review the value for ClassLength rubocop:disable Metrics/ClassLength
Public Class Methods
Initialize class
@author Randell Pelak
@param [string] target_master Host to install on
@return [void]
# File lib/ref_arch_setup/install.rb, line 27 def initialize(target_master) @target_master = target_master end
Public Instance Methods
Runs the initial bootstrapping install
@author Randell Pelak
@param [string] pe_conf_path Path to pe.conf @param [string] pe_tarball Path or URL for the pe tarball
@raise [RuntimeError] Unless either a pe_tarball or pe_version is specified @raise [RuntimeError] If a valid pe_tarball is not available @raise [RuntimeError] If a RAS working directory can not be created @raise [BoltCommandError] If the bolt command is not successful or the output is nil
@return [true,false] Based on exit status of the bolt task rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize
# File lib/ref_arch_setup/install.rb, line 46 def bootstrap(pe_conf_path, pe_tarball, pe_version) if specified_option?(pe_tarball) puts "Proceeding with specified pe_tarball: #{pe_tarball}" @pe_tarball = pe_tarball else options_error = "Either a pe_version or pe_tarball must be specified" raise options_error unless specified_option?(pe_version) puts "Proceeding with specified pe_version: #{pe_version}" @pe_tarball = RefArchSetup::DownloadHelper.build_prod_tarball_url(pe_version, @target_master) end raise "Unable to create RAS working directory" unless make_tmp_work_dir conf_path_on_master = handle_pe_conf(pe_conf_path) tarball_path_on_master = handle_pe_tarball(@pe_tarball) params = {} params["pe_conf_path"] = conf_path_on_master params["pe_tarball_path"] = tarball_path_on_master output = BoltHelper.run_task_with_bolt(INSTALL_PE_TASK, params, @target_master) success = output.nil? ? false : true return success end
Copies the PE tarball from the specified location to the temp working directory on the target master
@author Bill Claytor
@param [string] tarball_path_on_target_master The pe tarball path on the target master
@return [true,false] Based on exit status of the bolt task
# File lib/ref_arch_setup/install.rb, line 310 def copy_pe_tarball_on_target_master(tarball_path_on_target_master) filename = File.basename(tarball_path_on_target_master) success = if tarball_path_on_target_master == "#{TMP_WORK_DIR}/#{filename}" puts "Not copying the tarball as the source and destination are the same" true else command = "cp #{tarball_path_on_target_master} #{TMP_WORK_DIR}" output = BoltHelper.run_cmd_with_bolt(command, @target_master) output.nil? ? false : true end return success end
Downloads the PE tarball locally and moves it to the target master
@author Bill Claytor
@param [string] url The pe tarball URL
@return [true,false] Based on exit status of the bolt task
# File lib/ref_arch_setup/install.rb, line 258 def download_and_move_pe_tarball(url) download_path = "#{TMP_WORK_DIR}/#{@pe_tarball_filename}" success = download_pe_tarball(url, "localhost") success = upload_pe_tarball(download_path) if success return success end
Runs the download_pe_tarball
Bolt task
@author Bill Claytor
@param [string] url The pe tarball URL @param [string] nodes Nodes where the task should be run
@return [true,false] Based on exit status of the bolt task
TODO: update to return the download path (SLV-186)
# File lib/ref_arch_setup/install.rb, line 238 def download_pe_tarball(url, nodes) puts "Attempting to download #{url} to #{nodes}" puts params = {} params["url"] = url params["destination"] = TMP_WORK_DIR output = BoltHelper.run_task_with_bolt(DOWNLOAD_PE_TARBALL_TASK, params, nodes) success = output.nil? ? false : true return success end
Determines whether the specified file exists on the target master
@author Bill Claytor
@param [string] path Path to PE tarball file
@return [true,false] Based on whether the file exists on the target master
TODO: SLV-187 - combine with copy_pe_tarball_on_target_master
# File lib/ref_arch_setup/install.rb, line 214 def file_exist_on_target_master?(path) command = "[ -f #{path} ]" exists = true begin BoltHelper.run_cmd_with_bolt(command, @target_master) rescue exists = false end return exists end
Handles user inputted pe.conf or if nil assumes it is in the CWD Validates file exists (allows just a dir to be given if pe.conf is in it) TODO Ensure it is valid once we have a reader/validator class Move it to the target_master
@author Randell Pelak
@param [string] pe_conf_path Path to pe.conf file or dir
@raise [RuntimeError] If a pe.conf file is not found @raise [RuntimeError] If the file is not uploaded successfully
@return [string] The path to the pe.conf file on the target master
# File lib/ref_arch_setup/install.rb, line 101 def handle_pe_conf(pe_conf_path) conf_path_on_master = "#{TMP_WORK_DIR}/pe.conf" if pe_conf_path.nil? file_path = Dir.pwd + "/pe.conf" raise("No pe.conf file found in current working directory") unless File.exist?(file_path) else file_path = handle_pe_conf_path(pe_conf_path) end success = upload_pe_conf(file_path) raise "Unable to upload pe.conf file to #{@target_master}" unless success return conf_path_on_master end
Handles user inputted pe.conf Validates file exists (allows just a dir to be given if pe.conf is in it) Also ensures the file is named pe.conf
@author Randell Pelak
@param [string] pe_conf_path Path to pe.conf file or dir
@raise [RuntimeError] If a directory is specified and a pe.conf file is not found @raise [RuntimeError] If the specified file is not named pe.conf @raise [RuntimeError] If the specified file is not found
@return [string] The path to the pe.conf file
# File lib/ref_arch_setup/install.rb, line 128 def handle_pe_conf_path(pe_conf_path) file_path = File.expand_path(pe_conf_path) if File.directory?(file_path) full_path = file_path + "/pe.conf" raise("No pe.conf file found in directory: #{file_path}") unless File.exist?(full_path) file_path = full_path else filename = File.basename(file_path) raise("Specified file is not named pe.conf #{file_path}") unless filename.eql?("pe.conf") raise("pe.conf file not found #{file_path}") unless File.exist?(file_path) end return file_path end
Handles the PE tarball based on the the path (URL / file) and target master (local / remote)
@author Bill Claytor
@param [string] pe_tarball Path to PE tarball file
@raise [RuntimeError] If the specified tarball is not handled successfully
@return [string] The tarball path on the master after copying if successful
# File lib/ref_arch_setup/install.rb, line 391 def handle_pe_tarball(pe_tarball) error = "Unable to handle the specified PE tarball path: #{pe_tarball}" validate_tarball_extension(pe_tarball) tarball_path_on_master = if valid_url?(pe_tarball) handle_tarball_url(pe_tarball) else handle_tarball_path(pe_tarball) end raise error unless tarball_path_on_master return tarball_path_on_master end
Handles the specified tarball path based on the target_master
@author Bill Claytor
@param [string] path The PE tarball path
@raise [RuntimeError] If the specified tarball is not found @raise [RuntimeError] If the specified tarball is not uploaded successfully
@return [string] The tarball path on the target master
TODO: improve “host to install on”? (“host where PE will be installed?”)
# File lib/ref_arch_setup/install.rb, line 360 def handle_tarball_path(path) filename = File.basename(path) tarball_path_on_master = "#{TMP_WORK_DIR}/#{filename}" file_not_found_error = "File not found: #{path}" upload_error = "Unable to upload tarball to the RAS working directory on #{@target_master}" copy_error = "Unable to copy tarball to the RAS working directory on #{@target_master}" if target_master_is_localhost? raise file_not_found_error unless File.exist?(path) success = upload_pe_tarball(path) error = copy_error unless success else success = handle_tarball_path_with_remote_target_master(path) error = upload_error unless success end raise error unless success return tarball_path_on_master end
Handles the specified tarball path when the target master is not localhost
@author Bill Claytor
@param [string] path The pe tarball path
@raise [RuntimeError] If the specified tarball is not found
@return [true,false] Based on exit status of the bolt task
# File lib/ref_arch_setup/install.rb, line 333 def handle_tarball_path_with_remote_target_master(path) remote_flag = "#{@target_master}:" if path.start_with?(remote_flag) actual_path = path.sub!(remote_flag, "") success = file_exist_on_target_master?(actual_path) success = copy_pe_tarball_on_target_master(actual_path) if success else raise "File not found: #{path}" unless File.exist?(path) success = upload_pe_tarball(path) end return success end
Handles the specified PE tarball URL by either downloading directly to the target master or downloading locally and copying to the target master
@author Bill Claytor
@param [string] url The PE tarball URL
@raise [RuntimeError] If the tarball is not handled successfully
@return [string] The tarball path on the target master rubocop:disable Metrics/MethodLength
# File lib/ref_arch_setup/install.rb, line 276 def handle_tarball_url(url) parse_url(url) tarball_path_on_master = "#{TMP_WORK_DIR}/#{@pe_tarball_filename}" success = false if target_master_is_localhost? success = download_pe_tarball(url, "localhost") raise "Failed downloading #{url} to localhost" unless success else begin # if downloading to the target master fails success = download_pe_tarball(url, @target_master) rescue # try to download locally and then upload puts "Unable to download the tarball directly to #{@target_master}" success = download_and_move_pe_tarball(url) ensure raise "Failed downloading #{url} locally and moving to #{@target_master}" unless success end end return tarball_path_on_master end
Creates a tmp work dir for ref_arch_setup on the target_host Doesn't fail if the dir is already there.
@author Randell Pelak
@raise [BoltCommandError] If the bolt command is not successful or the output is nil
@return [true,false] Based on the output returned from the bolt command
# File lib/ref_arch_setup/install.rb, line 413 def make_tmp_work_dir BoltHelper.make_dir(TMP_WORK_DIR, @target_master) end
Parses the specified URL
@author Bill Claytor
@param [string] url URL for the PE tarball file
@raise [RuntimeError] If the specified URL can not be parsed
@return [true] Based on the validity of the URL
# File lib/ref_arch_setup/install.rb, line 165 def parse_url(url) begin @pe_tarball_uri = URI.parse(url) @pe_tarball_filename = File.basename(@pe_tarball_uri.path) rescue raise "Unable to parse the specified URL: #{url}" end return true end
Determines whether the option is 'specified' (not nil and not empty)
@author Bill Claytor
@param [string] value The option value to evaluate
@return [true, false] Based on the evaluation of the value
# File lib/ref_arch_setup/install.rb, line 83 def specified_option?(value) specified = value.nil? || value.empty? ? false : true return specified end
Determines whether the target master is localhost
@author Bill Claytor
@return [true,false] Based on whether the target master is localhost
TODO: (SLV-185) Improve check for localhost
# File lib/ref_arch_setup/install.rb, line 198 def target_master_is_localhost? is_localhost = false is_localhost = true if @target_master.include?("localhost") return is_localhost end
Upload the pe.conf to the target_host
@author Randell Pelak
@param [string] src_pe_conf_path Path to the source copy of the pe.conf file @param [string] dest_pe_conf_path Path to put the pe.conf at on the target host @param [string] target_master Host to upload to
@return [true,false] Based on exit status of the bolt task
# File lib/ref_arch_setup/install.rb, line 426 def upload_pe_conf(src_pe_conf_path = "#{RAS_FIXTURES_PATH}/pe.conf", dest_pe_conf_path = "#{TMP_WORK_DIR}/pe.conf", target_master = @target_master) output = BoltHelper.upload_file(src_pe_conf_path, dest_pe_conf_path, target_master) success = output.nil? ? false : true return success end
Upload the pe tarball to the target_host
@author Randell Pelak
@param [string] src_pe_tarball_path Path to the source copy of the tarball file
@return [true,false] Based on exit status of the bolt task
# File lib/ref_arch_setup/install.rb, line 442 def upload_pe_tarball(src_pe_tarball_path) file_name = File.basename(src_pe_tarball_path) dest_pe_tarball_path = "#{TMP_WORK_DIR}/#{file_name}" puts "Attempting upload from #{src_pe_tarball_path} " \ "to #{dest_pe_tarball_path} on #{@target_master}" output = BoltHelper.upload_file(src_pe_tarball_path, dest_pe_tarball_path, @target_master) success = output.nil? ? false : true return success end
Determines whether the specified path is a valid URL
@author Bill Claytor
@param [string] pe_tarball Path to PE tarball file
@return [true,false] Based on whether the path is a valid URL
# File lib/ref_arch_setup/install.rb, line 150 def valid_url?(pe_tarball) valid = false valid = true if pe_tarball =~ /\A#{URI.regexp(%w[http https])}\z/ return valid end
Determines whether the specified path / url has a valid extension (.gz)
@author Bill Claytor
@param [string] pe_tarball Path to PE tarball file
@raise [RuntimeError] If the extension of the specified tarball is invalid
@return [true] Based on the validity of the extension
# File lib/ref_arch_setup/install.rb, line 184 def validate_tarball_extension(pe_tarball) message = "Invalid extension for tarball: #{pe_tarball}; extension must be .tar or .tar.gz" raise(message) unless pe_tarball =~ /.*\.(tar|tar\.gz)$/ return true end