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, #custom_prefabs_path, #destroy, #nodes, #setname

NodeSet Methods collapse

Instance Method Summary collapse

Methods included from Log

#bold, #color, #formatter, #log, #output

Methods inherited from Base

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

Constructor Details

#initialize(setname, config, custom_prefabs_path, options) ⇒ Vsphere

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

Parameters:

  • setname (String)

    name of the set to instantiate

  • config (Hash)

    nodeset configuration hash

  • custom_prefabs_path (String)

    path of custom prefabs yaml file

  • options (Hash)

    options Hash



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

def initialize(setname, config, custom_prefabs_path, options)
  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



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rspec-system/node_set/vsphere.rb', line 168

def rcp(opts)
  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
# 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]
  ssh_exec!(ssh, "cd /tmp && sudo sh -c '#{cmd}'")
end

#setupvoid

This method returns an undefined value.

Setup the NodeSet by starting all nodes.



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
# File 'lib/rspec-system/node_set/vsphere.rb', line 39

def setup
  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 "Launching VSphere 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 "Launching VSphere 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 "Cloning new VSphere vm #{vm_name} in folder #{dest_dir}"

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

    log.info "Cloning complete"

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

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

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

    ipaddress = newvm.guest.ipAddress

    output << bold(color("localhost$", :green)) << " ssh #{k}"
    chan = Net::SSH.start(ipaddress, 'vagrant', :password => 'vagrant')

    RSpec.configuration.rspec_storage[:nodes][k] = {
      :ipaddress => ipaddress,
      :ssh => chan,
      :vm => newvm
    }
  end

  nil
end

#teardownvoid

This method returns an undefined value.

Shutdown the NodeSet by shutting down all nodes.



120
121
122
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 120

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

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

    ssh = storage[:ssh]
    ssh.close unless ssh.closed?

    if destroy
      log.info "Destroying instance #{k}"
      vm = storage[:vm]
      if vm == nil
        log.error "No vm object for #{k}"
        next
      end
      vm.PowerOffVM_Task.wait_for_completion
      vm.Destroy_Task.wait_for_completion
    else
      next
    end
  end

  nil
end