Module: CloudFlock::Interface::CLI::App::Common::Servers

Includes:
Target::Servers
Included in:
Servers::Migrate, Servers::Profile
Defined in:
lib/cloudflock/interface/cli/app/common/servers.rb

Overview

Public: The ServersCommon module provides common methods for CLI interaction pertaining to interaction with remote (Unix) servers.

Constant Summary collapse

SSH =
CloudFlock::Remote::SSH
CLI =
CloudFlock::Interface::CLI::Console

Instance Method Summary collapse

Instance Method Details

#define_destinationObject

Internal: Collect information about the destination server to target in a migration – only used for resume functions.

Returns a Hash containing information pertinent to logging in to a host.



60
61
62
63
64
65
66
67
68
69
# File 'lib/cloudflock/interface/cli/app/common/servers.rb', line 60

def define_destination
  host = Hash.new

  host[:host] = CLI.prompt("Destination host")
  host[:password] = CLI.prompt("Destination root password")
  host[:pre_steps] = CLI.prompt_yn("Perform pre-migration steps? (Y/N)")
  host[:username] = "root"

  host
end

#define_source(opts) ⇒ Object

Internal: Collect information about the source server to be migrated.

opts - Hash containing any applicable options mappings for the server in

question.

Returns a Hash containing information pertinent to logging in to a host.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cloudflock/interface/cli/app/common/servers.rb', line 19

def define_source(opts)
  host = {}

  host[:host] = opts[:source_host] || CLI.prompt("Source host")
  host[:port] = opts[:source_port] || CLI.prompt("Source SSH port",
                                                 default_answer: "22")
  host[:username] = opts[:source_user] || CLI.prompt("Source username",
                                                     default_answer: "root")
  host[:password] = opts[:source_pass] || CLI.prompt("Source password",
                                                     default_answer: "")

  until host[:public_key].kind_of?(String)
    key = opts[:public_key] || CLI.prompt("SSH Key", default_answer: "")
    if File.file?(File.expand_path(key)) || key.empty?
      host[:public_key] = key
    end
  end

  # Only need to use sudo if the user isn't root
  if host[:username] == "root"
    host[:sudo] = false
  elsif !opts[:source_sudo].nil?
    host[:sudo] = opts[:source_sudo]
  else
    host[:sudo] = CLI.prompt_yn("Use sudo? (Y/N)", default_answer: "Y")
  end

  # We need the root pass if non-root and no sudo
  if host[:username] == "root" || host[:sudo]
    host[:root_pass] = host[:password]
  else
    host[:root_pass] = CLI.prompt("Password for root")
  end

  host
end

#destination_login(host) ⇒ Object

Internal: Initiate a connection to a destination host.

host - Hash containing information for connecting to the host:

:host      - String containing the location to which to connect.
:password  - String containing the password for the above user.
:verbose   - Boolean value defining whether to flush output to
             STDOUT. (default: false)

Returns an SSH object. Raises ArgumentError unless at least host and user are defined.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cloudflock/interface/cli/app/common/servers.rb', line 111

def (host)
  host[:username] = "root"
  message = "Connecting to destination host (password: #{host[:password]})"
  r = 0

  destination_host = CLI.spinner(message) do
    begin
      (host)
    rescue Timeout::Error
      if (r += 1) < 5
        sleep 300
        retry
      end
      raise
    end
  end
end

#host_login(host) ⇒ Object

Internal: Initiate a connection to a given host and obtain root privileges.

host - Hash containing information for connecting to the host:

:host      - String containing the location to which to connect.
:port      - String or Fixnum containing the port on which ssh
             listens. (default: "22")
:username  - String containing the username to use when logging in.
:password  - String containing the password for the above user.
:sudo      - Boolean value defining whether to use sudo to obtain
             root. (default: false)
:root_pass - String containing the password to use to obtain root,
             if the user isn't root and sudo isn't used.
:verbose   - Boolean value defining whether to flush output to
             STDOUT. (default: false)

Returns an SSH object. Raises ArgumentError unless at least host and user are defined.



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cloudflock/interface/cli/app/common/servers.rb', line 88

def (host)
  if host[:host].nil? || host[:username].nil?
    raise ArgumentError, "Need at least host and username defined"
  end

  host[:flush_buffer] = host[:verbose] || false

  ssh = SSH.new(host)
  ssh.get_root(host[:root_pass], host[:sudo])

  ssh
end