Class: Ssh

Inherits:
Object
  • Object
show all
Defined in:
lib/vmfloaty/ssh.rb

Class Method Summary collapse

Class Method Details

.command_string(verbose, service, host_os, use_token, ondemand = nil) ⇒ Object



17
18
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
# File 'lib/vmfloaty/ssh.rb', line 17

def self.command_string(verbose, service, host_os, use_token, ondemand = nil)
  ssh_path = which('ssh')
  raise 'Could not determine path to ssh' unless ssh_path
  os_types = Utils.generate_os_hash([host_os])
  os_types[host_os] = 1

  response = service.retrieve(verbose, os_types, use_token, ondemand)
  raise "Could not get vm from #{service.type}:\n #{response}" unless response['ok']

  user = /win/.match?(host_os) ? 'Administrator' : 'root'

  if ondemand
    requestid = response['request_id']
    service.wait_for_request(verbose, requestid)
    hosts = service.check_ondemandvm(verbose, requestid, service.url)
    if hosts['domain'].nil?
      hostname = hosts[host_os]['hostname']
      hostname = hosts[host_os]['hostname'][0] if hosts[host_os]['hostname'].is_a?(Array)
    else
      # Provides backwards compatibility with VMPooler API v1
      hostname = "#{hosts[host_os]['hostname']}.#{hosts['domain']}"
      hostname = "#{hosts[host_os]['hostname'][0]}.#{hosts['domain']}" if hosts[host_os]['hostname'].is_a?(Array)
    end
  else
    if response['domain'].nil?
      hostname = response[host_os]['hostname']
      hostname = response[host_os]['hostname'][0] if response[host_os]['hostname'].is_a?(Array)
    else
      # Provides backwards compatibility with VMPooler API v1
      hostname = "#{response[host_os]['hostname']}.#{response['domain']}"
      hostname = "#{response[host_os]['hostname'][0]}.#{response['domain']}" if response[host_os]['hostname'].is_a?(Array)
    end
  end

  "#{ssh_path} #{user}@#{hostname}"
end

.ssh(verbose, service, host_os, use_token, ondemand) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/vmfloaty/ssh.rb', line 54

def self.ssh(verbose, service, host_os, use_token, ondemand)
  cmd = command_string(verbose, service, host_os, use_token, ondemand)
  # TODO: Should this respect more ssh settings? Can it be configured
  #       by users ssh config and does this respect those settings?
  Kernel.exec(cmd)
  nil
end

.which(cmd) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/vmfloaty/ssh.rb', line 4

def self.which(cmd)
  # Gets path of executable for given command

  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end
  nil
end