class MachineConfigure::Importer

The Importer takes a zip file, processes the files, and extracts them into the proper directories.

Public Class Methods

new() click to toggle source
# File lib/machine_configure/importer.rb, line 9
def initialize
  @contents     = nil
  @machine_name = nil
end

Public Instance Methods

import_from(zip_file) click to toggle source

Import given zip_file as a new docker-machine.

# File lib/machine_configure/importer.rb, line 16
def import_from zip_file
  VALIDATOR.validate_zip_file_import zip_file
  @contents     = get_contents_from_zip zip_file
  @machine_name = @contents[MACHINE_NAME_FILENAME]
  @contents.delete MACHINE_NAME_FILENAME
  @dir = {
    machine: DM_MACHINES_PATH.join(@machine_name),
    certs:   DM_CERTS_PATH.join(@machine_name)
  }
  #VALIDATOR.validate_directories_dont_exist *@dir.values
  VALIDATOR.validate_no_machine_name @machine_name
  config_json_path = get_config_json_path
  @contents[config_json_path] = insert_home_in @contents[config_json_path]
  write_contents
  # Finally, check that the newly imported machine is recognized by docker-machine.
  VALIDATOR.validate_machine_name @machine_name
  message(
    "Successfully imported docker-machine configuration files from archive",
    "  `#{zip_file.to_s}'",
    "for docker-machine",
    "  `#{@machine_name}'"
  )
end

Private Instance Methods

get_contents_from_zip(zip_file) click to toggle source

Reads the zip_file and returns a Hash with each file's path as the key and the file's content as the value.

# File lib/machine_configure/importer.rb, line 45
def get_contents_from_zip zip_file
  contents = {}
  Zip::File.open(zip_file) do |zip|
    zip.each_entry do |entry|
      entry.get_input_stream do |entryfile|
        contents[entry.name] = entryfile.read
      end
    end
  end
  return contents
end
write_contents() click to toggle source

Write @contents to proper paths.

# File lib/machine_configure/importer.rb, line 58
def write_contents
  @contents.each do |relative_filepath, content|
    filepath = DM_STORAGE_PATH.join relative_filepath
    filedir  = filepath.dirname
    filedir.mkpath     unless (filedir.directory?)
    permission = 0644
    permission = 0600  if (filepath.basename.to_path == 'id_rsa')
    file = File.open filepath.to_path, ?w, permission
    file.write content
    file.close
  end
end