Class: TrainPlugins::VsphereGom::Connection

Inherits:
Train::Plugins::Transport::BaseConnection
  • Object
show all
Defined in:
lib/train-vsphere-gom/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Connection

Returns a new instance of Connection.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/train-vsphere-gom/connection.rb', line 13

def initialize(config = {})
  @config = config

  unless vm
    logger.error format("[VSphere-GOM] Could not find VM for '%<id>s'. Check power status, if searched via IP", id: config[:host])
    return
  end

  logger.debug format("[VSphere-GOM] Found %<id>s for %<search_type>s %<search>s",
                      id: @vm._ref,
                      search: config[:host],
                      search_type: search_type(config[:host]))

  super(config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/train-vsphere-gom/connection.rb', line 9

def config
  @config
end

#logger=(value) ⇒ Object

Sets the attribute logger

Parameters:

  • value

    the value to set the attribute logger to.



11
12
13
# File 'lib/train-vsphere-gom/connection.rb', line 11

def logger=(value)
  @logger = value
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/train-vsphere-gom/connection.rb', line 9

def options
  @options
end

#vim=(value) ⇒ Object

Sets the attribute vim

Parameters:

  • value

    the value to set the attribute vim to.



11
12
13
# File 'lib/train-vsphere-gom/connection.rb', line 11

def vim=(value)
  @vim = value
end

#vm=(value) ⇒ Object

Sets the attribute vm

Parameters:

  • value

    the value to set the attribute vm to.



11
12
13
# File 'lib/train-vsphere-gom/connection.rb', line 11

def vm=(value)
  @vm = value
end

#vm_guest=(value) ⇒ Object

Sets the attribute vm_guest

Parameters:

  • value

    the value to set the attribute vm_guest to.



11
12
13
# File 'lib/train-vsphere-gom/connection.rb', line 11

def vm_guest=(value)
  @vm_guest = value
end

Instance Method Details

#closeObject



29
30
31
32
33
34
35
36
# File 'lib/train-vsphere-gom/connection.rb', line 29

def close
  return unless vim

  vim.close
  logger.info format("[VSphere-GOM] Closed connection to %<vm>s (VCenter %<vcenter>s)",
                     vm: options[:host],
                     vcenter: options[:vcenter_server])
end

#download(remotes, local) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/train-vsphere-gom/connection.rb', line 71

def download(remotes, local)
  logger.debug format("[VSphere-GOM] Download %<remotes>s to %<local>s",
                      remotes: Array(Remotes).join(","),
                      local: local)

  Array(remotes).each do |remote|
    localfile = File.join(local, File.basename(remote))
    vm_guest.download_file(remote, localfile)
  end
end

#file_via_connection(path, *args) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/train-vsphere-gom/connection.rb', line 42

def file_via_connection(path, *args)
  if windows?
    Train::File::Remote::Windows.new(self, path, *args)
  else
    Train::File::Remote::Unix.new(self, path, *args)
  end
end

#run_command_via_connection(command) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/train-vsphere-gom/connection.rb', line 82

def run_command_via_connection(command)
  logger.debug format("[VSphere-GOM] Sending command to %<vm>s (VCenter %<vcenter>s)",
                      vm: options[:host],
                      vcenter: options[:vcenter_server])

  result = vm_guest.run(command, shell_type: config[:shell_type].to_sym, timeout: config[:timeout].to_i)

  if windows? && result.exit_status != 0
    logger.debug format("[VSphere-GOM] Received Windows exit code: %<hexcode>s",
                        hexcode: hexit_code(result.exit_status))
  end

  result
end

#upload(locals, remote) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/train-vsphere-gom/connection.rb', line 50

def upload(locals, remote)
  logger.debug format("[VSphere-GOM] Copy %<locals>s to %<remote>s",
                      locals: Array(locals).join(", "),
                      remote: remote)

  # Collect recursive list of directories and files
  all = Array(locals).map do |local|
    File.directory?(local) ? Dir.glob("#{local}/**/*") : local
  end.flatten

  dirs = all.select { |obj| File.directory? obj }
  dirs.each { |dir| tools.create_dir(dir) }

  # Then upload all files
  files = all.select { |obj| File.file? obj }
  files.each do |local|
    remotefile = path(remote, File.basename(local))
    vm_guest.upload_file(local, remotefile)
  end
end

#uriObject



38
39
40
# File 'lib/train-vsphere-gom/connection.rb', line 38

def uri
  "vsphere-gom://#{options[:user]}@#{options[:vcenter_server]}/#{options[:host]}"
end