class MachineConfigure::Validator

This class should validate that dependencies are installed (docker-machine).

Constants

BASE_APPS

Default command-line apps, which need to be available.

Public Class Methods

new() click to toggle source
# File lib/machine_configure/validator.rb, line 11
def initialize
  @validated_apps = []
end

Public Instance Methods

validate_apps(*apps) click to toggle source

Check if given apps (or default BASE_APPS), are available from the command-line. Throw an error and exit if any aren't available.

# File lib/machine_configure/validator.rb, line 23
def validate_apps *apps
  apps.flatten.each do |appname|
    validate_app appname
  end
end
validate_base_apps() click to toggle source

Calls validate_apps for BASE_APPS.

# File lib/machine_configure/validator.rb, line 16
def validate_base_apps
  validate_apps *BASE_APPS
end
validate_directories(*directories) click to toggle source

Check that the given directories exist, and are directories.

# File lib/machine_configure/validator.rb, line 47
def validate_directories *directories
  directories.flatten.each do |directory|
    validate_directory directory
  end
end
validate_directories_dont_exist(*directories) click to toggle source

Check that the given directories do NOT exist.

# File lib/machine_configure/validator.rb, line 54
def validate_directories_dont_exist *directories
  directories.flatten.each do |directory|
    validate_directory_doesnt_exist directory
  end
end
validate_machine_name(name) click to toggle source

Check that the given name exists for docker-machine.

# File lib/machine_configure/validator.rb, line 31
def validate_machine_name name
  validate_app 'docker-machine'
  error(
    "Docker machine `#{name}' is not available."
  )  unless (docker_machine_exists? name)
end
validate_no_machine_name(name) click to toggle source

Check that the given name does not exist for docker-machine.

# File lib/machine_configure/validator.rb, line 40
def validate_no_machine_name name
  validate_app 'docker-machine'
  prompt_to_replace_docker_machine name  if (docker_machine_exists? name)
end
validate_zip_file_export(zip_file) click to toggle source

Check that the given zip_file doesn't exist already but that the path leading to the file does exist.

# File lib/machine_configure/validator.rb, line 63
def validate_zip_file_export zip_file
  path = File.dirname zip_file
  error(
    "The path to the zip file `#{path.to_path}' doesn't exist."
  )  unless (is_directory? path)
  prompt_to_replace_file zip_file  if (is_file? zip_file)
end
validate_zip_file_import(zip_file) click to toggle source

Similar to validate_zip_file_export, but don't prompt for overwriting, etc. The zip file has to exist in this case.

# File lib/machine_configure/validator.rb, line 74
def validate_zip_file_import zip_file
  error(
    "The zip file `#{zip_file.to_s}' doesn't exist or is a directory."
  )  unless (is_file? zip_file)
end

Private Instance Methods

app_available?(name) click to toggle source
# File lib/machine_configure/validator.rb, line 94
def app_available? name
  return system("which #{name} &> /dev/null")
end
backup_docker_machine(name) click to toggle source
# File lib/machine_configure/validator.rb, line 181
def backup_docker_machine name
  backup_name_date = "#{name}.#{Time.now.strftime('%Y-%m-%d')}"
  mk_backup_directories
  machine_path        = DM_MACHINES_PATH.join name
  cert_path           = DM_CERTS_PATH.join    name

  #backup_machine_path = DM_BACKUP_MACHINES_PATH.join backup_name_date
  #backup_cert_path    = DM_BACKUP_CERTS_PATH.join    backup_name_date

  backup_machine_path = backup_cert_path = nil
  backup_machine_path = get_backup_directory_for DM_BACKUP_MACHINES_PATH.join(backup_name_date)  if (machine_path.directory?)
  backup_cert_path    = get_backup_directory_for DM_BACKUP_CERTS_PATH.join(backup_name_date)     if (cert_path.directory?)

  msg = [ "Backing-up configuration files for `#{name}' to" ]
  msg << "  `#{backup_machine_path}'"  if (backup_machine_path)
  msg[-1] += ', and'                   if (backup_machine_path && backup_cert_path)
  msg << "  `#{backup_cert_path}'"     if (backup_cert_path)
  message msg
  FileUtils.mv machine_path, backup_machine_path  if (backup_machine_path)
  FileUtils.mv cert_path,    backup_cert_path     if (backup_cert_path)
end
docker_machine_exists?(name) click to toggle source
# File lib/machine_configure/validator.rb, line 98
def docker_machine_exists? name
  return system("docker-machine inspect #{name} &> /dev/null")
end
get_backup_directory_for(base_backup_directory) click to toggle source
# File lib/machine_configure/validator.rb, line 208
def get_backup_directory_for base_backup_directory
  base_backup_directory = Pathname.new base_backup_directory  unless (base_backup_directory.is_a? Pathname)
  return base_backup_directory                                unless (base_backup_directory.directory?)
  base_backup_directory_tmp = base_backup_directory.dup
  counter = 0
  while (base_backup_directory_tmp.directory?)
    counter += 1
    base_backup_directory_tmp = Pathname.new "#{base_backup_directory.to_path}_#{counter}"
  end
  return base_backup_directory_tmp
end
is_directory?(directory) click to toggle source
# File lib/machine_configure/validator.rb, line 115
def is_directory? directory
  return File.directory? directory
end
is_file?(file) click to toggle source
# File lib/machine_configure/validator.rb, line 119
def is_file? file
  return File.file? file
end
mk_backup_directories() click to toggle source
# File lib/machine_configure/validator.rb, line 203
def mk_backup_directories
  DM_BACKUP_MACHINES_PATH.mkpath  unless (DM_BACKUP_MACHINES_PATH.directory?)
  DM_BACKUP_CERTS_PATH.mkpath     unless (DM_BACKUP_CERTS_PATH.directory?)
end
prompt_to_replace_docker_machine(name) click to toggle source
# File lib/machine_configure/validator.rb, line 152
def prompt_to_replace_docker_machine name
  options = {
    backup:    ?B,
    overwrite: ?o,
    nothing:   ?n
  }
  warning_print(
    "Docker machine `#{name}' already exists. What do you want to do?",
    "  Backup current configurations and create new one from import? [#{options[:backup]}]",
    "    (Backup to #{DM_BACKUP_PATH})",
    "  Overwrite existing files? [#{options[:overwrite]}]",
    "  Do nothing, abort. [#{options[:nothing]}]",
    "[#{options.values.join(?/)}] "
  )
  answer = STDIN.gets[0].strip.downcase
  case answer
  when options[:overwrite].downcase
    message "Overwriting existing configuration files for `#{name}'."
  when options[:backup].downcase, ''
    backup_docker_machine name
  when options[:nothing].downcase
    message "Exiting."
    abort
  else
    prompt_to_replace_docker_machine name
    return
  end
end
prompt_to_replace_file(file) click to toggle source
# File lib/machine_configure/validator.rb, line 123
def prompt_to_replace_file file
  options = {
    overwrite: ?o,
    append:    ?a,
    nothing:   ?N
  }
  warning_print(
    "File `#{file.to_s}' already exists. What do you want to do?",
    "  Overwrite file? (Remove existing and create new archive.) [#{options[:overwrite]}]",
    "  Append content to existing archive? [#{options[:append]}]",
    "  Do nothing, abort. [#{options[:nothing]}]",
    "[#{options.values.join(?/)}] "
  )
  answer = STDIN.gets[0].strip.downcase
  case answer
  when options[:overwrite].downcase
    message "Overwriting archive `#{file.to_s}'."
    File.delete file
  when options[:append].downcase
    message "Appending to archive `#{file.to_s}'."
  when options[:nothing].downcase, ''
    message "Exiting."
    abort
  else
    prompt_to_replace_file file
    return
  end
end
validate_app(appname) click to toggle source
# File lib/machine_configure/validator.rb, line 82
def validate_app appname
  return  if (@validated_apps.include? appname)
  if (app_available? appname)
    @validated_apps << appname
    return
  end
  error(
    "`#{appname}' is not available.",
    "Please make sure you have it installed."
  )
end
validate_directory(directory) click to toggle source
# File lib/machine_configure/validator.rb, line 102
def validate_directory directory
  return  if (is_directory? directory)
  error(
    "Directory `#{directory.to_s}' does not exist or is a file."
  )
end
validate_directory_doesnt_exist(directory) click to toggle source
# File lib/machine_configure/validator.rb, line 109
def validate_directory_doesnt_exist directory
  error(
    "Directory `#{directory.to_s}' already exists."
  )  if (is_directory?(directory) || is_file?(directory))
end