Class: RefArchSetup::Install

Inherits:
Object
  • Object
show all
Defined in:
lib/ref_arch_setup/install.rb

Overview

Installation helper

TODO: review the value for ClassLength rubocop:disable Metrics/ClassLength

Author:

  • Randell Pelak

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_master) ⇒ void

Initialize class

Parameters:

  • target_master (string)

    Host to install on

Author:

  • Randell Pelak



27
28
29
# File 'lib/ref_arch_setup/install.rb', line 27

def initialize(target_master)
  @target_master = target_master
end

Instance Attribute Details

#target_masterstring

Host to install on

Returns:

  • (string)

    the current value of target_master



19
20
21
# File 'lib/ref_arch_setup/install.rb', line 19

def target_master
  @target_master
end

Instance Method Details

#bootstrap(pe_conf_path, pe_tarball, pe_version) ⇒ true, false

Runs the initial bootstrapping install

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

Parameters:

  • pe_conf_path (string)

    Path to pe.conf

  • pe_tarball (string)

    Path or URL for the pe tarball

Returns:

  • (true, false)

    Based on exit status of the bolt task

Raises:

  • (RuntimeError)

    Unless either a pe_tarball or pe_version is specified

  • (RuntimeError)

    If a valid pe_tarball is not available

  • (RuntimeError)

    If a RAS working directory can not be created

  • (BoltCommandError)

    If the bolt command is not successful or the output is nil

Author:

  • Randell Pelak



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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

#copy_pe_tarball_on_target_master(tarball_path_on_target_master) ⇒ true, false

Copies the PE tarball from the specified location to the temp working directory on the target master

Parameters:

  • tarball_path_on_target_master (string)

    The pe tarball path on the target master

Returns:

  • (true, false)

    Based on exit status of the bolt task

Author:

  • Bill Claytor



310
311
312
313
314
315
316
317
318
319
320
321
322
# 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

#download_and_move_pe_tarball(url) ⇒ true, false

Downloads the PE tarball locally and moves it to the target master

Parameters:

  • url (string)

    The pe tarball URL

Returns:

  • (true, false)

    Based on exit status of the bolt task

Author:

  • Bill Claytor



258
259
260
261
262
263
# 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

#download_pe_tarball(url, nodes) ⇒ true, false

Runs the download_pe_tarball Bolt task

TODO: update to return the download path (SLV-186)

Parameters:

  • url (string)

    The pe tarball URL

  • nodes (string)

    Nodes where the task should be run

Returns:

  • (true, false)

    Based on exit status of the bolt task

Author:

  • Bill Claytor



238
239
240
241
242
243
244
245
246
247
248
249
# 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

#file_exist_on_target_master?(path) ⇒ true, false

Determines whether the specified file exists on the target master

TODO: SLV-187 - combine with copy_pe_tarball_on_target_master

Parameters:

  • path (string)

    Path to PE tarball file

Returns:

  • (true, false)

    Based on whether the file exists on the target master

Author:

  • Bill Claytor



214
215
216
217
218
219
220
221
222
223
224
225
# 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

#handle_pe_conf(pe_conf_path) ⇒ string

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

Parameters:

  • pe_conf_path (string)

    Path to pe.conf file or dir

Returns:

  • (string)

    The path to the pe.conf file on the target master

Raises:

  • (RuntimeError)

    If a pe.conf file is not found

  • (RuntimeError)

    If the file is not uploaded successfully

Author:

  • Randell Pelak



101
102
103
104
105
106
107
108
109
110
111
112
113
# 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

#handle_pe_conf_path(pe_conf_path) ⇒ string

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

Parameters:

  • pe_conf_path (string)

    Path to pe.conf file or dir

Returns:

  • (string)

    The path to the pe.conf file

Raises:

  • (RuntimeError)

    If a directory is specified and a pe.conf file is not found

  • (RuntimeError)

    If the specified file is not named pe.conf

  • (RuntimeError)

    If the specified file is not found

Author:

  • Randell Pelak



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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

#handle_pe_tarball(pe_tarball) ⇒ string

Handles the PE tarball based on the the path (URL / file) and target master (local / remote)

Parameters:

  • pe_tarball (string)

    Path to PE tarball file

Returns:

  • (string)

    The tarball path on the master after copying if successful

Raises:

  • (RuntimeError)

    If the specified tarball is not handled successfully

Author:

  • Bill Claytor



391
392
393
394
395
396
397
398
399
400
401
402
403
# 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

#handle_tarball_path(path) ⇒ string

Handles the specified tarball path based on the target_master

TODO: improve “host to install on”? (“host where PE will be installed?”)

Parameters:

  • path (string)

    The PE tarball path

Returns:

  • (string)

    The tarball path on the target master

Raises:

  • (RuntimeError)

    If the specified tarball is not found

  • (RuntimeError)

    If the specified tarball is not uploaded successfully

Author:

  • Bill Claytor



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# 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

#handle_tarball_path_with_remote_target_master(path) ⇒ true, false

Handles the specified tarball path when the target master is not localhost

Parameters:

  • path (string)

    The pe tarball path

Returns:

  • (true, false)

    Based on exit status of the bolt task

Raises:

  • (RuntimeError)

    If the specified tarball is not found

Author:

  • Bill Claytor



333
334
335
336
337
338
339
340
341
342
343
344
345
346
# 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

#handle_tarball_url(url) ⇒ string

Handles the specified PE tarball URL by either downloading directly to the target master or downloading locally and copying to the target master

rubocop:disable Metrics/MethodLength

Parameters:

  • url (string)

    The PE tarball URL

Returns:

  • (string)

    The tarball path on the target master

Raises:

  • (RuntimeError)

    If the tarball is not handled successfully

Author:

  • Bill Claytor



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# 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

#make_tmp_work_dirtrue, false

Creates a tmp work dir for ref_arch_setup on the target_host Doesn’t fail if the dir is already there.

Returns:

  • (true, false)

    Based on the output returned from the bolt command

Raises:

  • (BoltCommandError)

    If the bolt command is not successful or the output is nil

Author:

  • Randell Pelak



413
414
415
# File 'lib/ref_arch_setup/install.rb', line 413

def make_tmp_work_dir
  BoltHelper.make_dir(TMP_WORK_DIR, @target_master)
end

#parse_url(url) ⇒ true

Parses the specified URL

Parameters:

  • url (string)

    URL for the PE tarball file

Returns:

  • (true)

    Based on the validity of the URL

Raises:

  • (RuntimeError)

    If the specified URL can not be parsed

Author:

  • Bill Claytor



165
166
167
168
169
170
171
172
173
# 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

#specified_option?(value) ⇒ true, false

Determines whether the option is ‘specified’ (not nil and not empty)

Parameters:

  • value (string)

    The option value to evaluate

Returns:

  • (true, false)

    Based on the evaluation of the value

Author:

  • Bill Claytor



83
84
85
86
# File 'lib/ref_arch_setup/install.rb', line 83

def specified_option?(value)
  specified = value.nil? || value.empty? ? false : true
  return specified
end

#target_master_is_localhost?true, false

Determines whether the target master is localhost

TODO: (SLV-185) Improve check for localhost

Returns:

  • (true, false)

    Based on whether the target master is localhost

Author:

  • Bill Claytor



198
199
200
201
202
# 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_pe_conf(src_pe_conf_path = "#{RAS_FIXTURES_PATH}/pe.conf", dest_pe_conf_path = "#{TMP_WORK_DIR}/pe.conf", target_master = @target_master) ⇒ true, false

Upload the pe.conf to the target_host

Parameters:

  • src_pe_conf_path (string) (defaults to: "#{RAS_FIXTURES_PATH}/pe.conf")

    Path to the source copy of the pe.conf file

  • dest_pe_conf_path (string) (defaults to: "#{TMP_WORK_DIR}/pe.conf")

    Path to put the pe.conf at on the target host

  • target_master (string) (defaults to: @target_master)

    Host to upload to

Returns:

  • (true, false)

    Based on exit status of the bolt task

Author:

  • Randell Pelak



426
427
428
429
430
431
432
433
# 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_pe_tarball(src_pe_tarball_path) ⇒ true, false

Upload the pe tarball to the target_host

Parameters:

  • src_pe_tarball_path (string)

    Path to the source copy of the tarball file

Returns:

  • (true, false)

    Based on exit status of the bolt task

Author:

  • Randell Pelak



442
443
444
445
446
447
448
449
450
451
452
453
# 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

#valid_url?(pe_tarball) ⇒ true, false

Determines whether the specified path is a valid URL

Parameters:

  • pe_tarball (string)

    Path to PE tarball file

Returns:

  • (true, false)

    Based on whether the path is a valid URL

Author:

  • Bill Claytor



150
151
152
153
154
# 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

#validate_tarball_extension(pe_tarball) ⇒ true

Determines whether the specified path / url has a valid extension (.gz)

Parameters:

  • pe_tarball (string)

    Path to PE tarball file

Returns:

  • (true)

    Based on the validity of the extension

Raises:

  • (RuntimeError)

    If the extension of the specified tarball is invalid

Author:

  • Bill Claytor



184
185
186
187
188
# 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