Class: RSpecSystem::NodeSet::Vsphere

Inherits:
Base
  • Object
show all
Includes:
Log
Defined in:
lib/rspec-system/node_set/vsphere.rb

Overview

A NodeSet implementation for VSphere

Constant Summary collapse

ENV_TYPE =
'vsphere'

Instance Attribute Summary

Attributes inherited from Base

#config, #nodes, #setname

NodeSet Methods collapse

Instance Method Summary collapse

Methods included from Log

#log

Methods inherited from Base

#default_node, #env_type, #randmac, #random_string, #ssh_exec!, #tmppath

Constructor Details

#initialize(setname, config) ⇒ Vsphere

Creates a new instance of RSpecSystem::NodeSet::Vsphere

Parameters:

  • setname (String)

    name of the set to instantiate

  • config (Hash)

    nodeset configuration hash



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rspec-system/node_set/vsphere.rb', line 18

def initialize(setname, config)
  super
  @vim = RbVmomi::VIM.connect(
    :host => ENV["RSPEC_VSPHERE_HOST"],
    :user => ENV["RSPEC_VSPHERE_USER"],
    :password => ENV["RSPEC_VSPHERE_PASS"],
    :ssl => true,
    :insecure => true
  )

  # Initialize node storage if not already
  RSpec.configuration.rspec_storage[:nodes] ||= {}
end

Instance Method Details

#rcp(opts) ⇒ Boolean

TODO:

This is damn ugly, because we ssh in as vagrant, we copy to a temp path then move it later. Its slow and brittle and we need a better solution. Its also very Linux-centrix in its use of temp dirs.

Transfer files to a host in the NodeSet.

Parameters:

  • opts (Hash)

    options

Returns:

  • (Boolean)

    returns true if command succeeded, false otherwise



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rspec-system/node_set/vsphere.rb', line 172

def rcp(opts)
  #log.debug("[Vagrant@rcp] called with #{opts.inspect}")

  dest = opts[:d].name
  source = opts[:sp]
  dest_path = opts[:dp]

  # Grab a remote path for temp transfer
  tmpdest = tmppath

  # Do the copy and print out results for debugging
  ssh = RSpec.configuration.rspec_storage[:nodes][dest][:ssh]
  ssh.scp.upload! source.to_s, tmpdest.to_s, :recursive => true

  # Now we move the file into their final destination
  result = run(:n => opts[:d], :c => "mv #{tmpdest} #{dest_path}")
  if result[:exit_code] == 0
    return true
  else
    return false
  end
end

#run(opts) ⇒ Hash

Run a command on a host in the NodeSet.

Parameters:

  • opts (Hash)

    options

Returns:

  • (Hash)

    a hash containing :exit_code, :stdout and :stderr



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rspec-system/node_set/vsphere.rb', line 153

def run(opts)
  dest = opts[:n].name
  cmd = opts[:c]

  ssh = RSpec.configuration.rspec_storage[:nodes][dest][:ssh]
  puts "-----------------"
  puts "#{dest}$ #{cmd}"
  result = ssh_exec!(ssh, "cd /tmp && sudo sh -c '#{cmd}'")
  puts "-----------------"
  result
end

#setupvoid

This method returns an undefined value.

Setup the NodeSet by starting all nodes.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rspec-system/node_set/vsphere.rb', line 37

def setup
  log.info "[Vsphere#setup] Setup begins"

  dest_dir = ENV['RSPEC_VSPHERE_DEST_DIR']
  template_dir = ENV['RSPEC_VSPHERE_TEMPLATE_DIR']

  si = @vim.serviceInstance
  dc = si.find_datacenter

  rp = dc.find_compute_resource('general').resourcePool.find(ENV["RSPEC_VSPHERE_RPOOL"])
  relocateSpec = RbVmomi::VIM.VirtualMachineRelocateSpec(:pool => rp)
  spec = RbVmomi::VIM.VirtualMachineCloneSpec(
    :location => relocateSpec,
    :powerOn => true,
    :template => false
  )

  vm_folder = dc.vmFolder
  vm_newfolder = vm_folder.find(dest_dir)

  log.info "[Vsphere#setup] launching instances one by one"
  nodes.each do |k,v|
    ps = v.provider_specifics['vsphere']

    raise 'No provider specifics for this prefab' if ps.nil?

    template = ps['template']

    raise "No template specified for this prefab" if template.nil?

    log.info "[Vsphere#setup] launching instance #{k} with template #{template}"

    vm = vm_folder.find(ENV['RSPEC_VSPHERE_TEMPLATE_DIR']).find(template)

    raise "No template found" if vm.nil?

    vm_name = "rspec-system-#{k}-#{random_string(10)}"

    log.info "[Vsphere#setup] Cloning new vm #{vm_name} in folder #{dest_dir}"

    vm.CloneVM_Task(
      :folder => vm_newfolder,
      :name => vm_name,
      :spec => spec
    ).wait_for_completion

    log.info "[Vsphere#setup] Cloning complete"

    newvm = vm_newfolder.find(vm_name)
    guest_info = newvm.guest

    timeout(60) do
      while(newvm.guest.guestState != 'running') do
        sleep 2
        puts "#{k}> Waiting for vm to run ..."
      end
    end

    timeout(60) do
      while(newvm.guest.ipAddress == nil) do
        sleep 2
        puts "#{k}> Waiting for ip address ..."
      end
    end

    ipaddress = newvm.guest.ipAddress

    log.info "[Vsphere#setup] establishing Net::SSH channel with #{k}"
    chan = Net::SSH.start(ipaddress, 'vagrant', :password => 'vagrant')

    RSpec.configuration.rspec_storage[:nodes][k] = {
      :ipaddress => ipaddress,
      :ssh => chan,
      :vm => newvm
    }
    log.info "[Vsphere#setup] Node launched: #{k}"
  end

  log.info("[Vsphere#setup] setup complete")

  nil
end

#teardownvoid

This method returns an undefined value.

Shutdown the NodeSet by shutting down all nodes.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rspec-system/node_set/vsphere.rb', line 123

def teardown
  nodes.each do |k,v|
    storage = RSpec.configuration.rspec_storage[:nodes][k]

    if storage.nil?
      log.info "[Vsphere#teardown] No entry for node #{k}, no teardown necessary"
      next
    end

    log.info "[Vsphere#teardown] closing ssh channel to #{k}"
    ssh = storage[:ssh]
    ssh.close unless ssh.closed?

    log.info "[Vsphere#teardown] destroy instance #{k}"
    vm = storage[:vm]
    if vm == nil
      puts "No vm object"
      next
    end
    vm.PowerOffVM_Task.wait_for_completion
    vm.Destroy_Task.wait_for_completion
  end

  nil
end