Class: RSpecSystem::NodeSet::Vagrant

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

Overview

A NodeSet implementation for Vagrant.

Constant Summary collapse

ENV_TYPE =
'vagrant'

Instance Attribute Summary

Attributes inherited from Base

#config, #destroy, #nodes, #setname

Instance Method Summary collapse

Methods included from Util

#shellescape

Methods included from Log

#log

Methods inherited from Base

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

Constructor Details

#initialize(setname, config, options) ⇒ Vagrant

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

Parameters:

  • setname (String)

    name of the set to instantiate

  • config (Hash)

    nodeset configuration hash

  • options (Hash)

    options Hash



18
19
20
21
# File 'lib/rspec-system/node_set/vagrant.rb', line 18

def initialize(setname, config, options)
  super
  @vagrant_path = File.expand_path(File.join(RSpec.configuration.system_tmp, 'vagrant_projects', setname))
end

Instance Method Details

#create_vagrantfileObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create the Vagrantfile for the NodeSet.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rspec-system/node_set/vagrant.rb', line 127

def create_vagrantfile
  log.info "[Vagrant#create_vagrantfile] Creating vagrant file here: #{@vagrant_path}"
  FileUtils.mkdir_p(@vagrant_path)
  File.open(File.expand_path(File.join(@vagrant_path, "Vagrantfile")), 'w') do |f|
    f.write("Vagrant::Config.run do |c|\n")
    nodes.each do |k,v|
      log.debug "Filling in content for #{k}"

      ps = v.provider_specifics['vagrant']

      f.write(<<-EOS)
  c.vm.define '#{k}' do |v|
v.vm.host_name = '#{k}'
v.vm.box = '#{ps['box']}'
v.vm.box_url = '#{ps['box_url']}'
v.vm.base_mac = '#{randmac}'
  end
      EOS
    end
    f.write('end')
  end
  log.debug "[Vagrant#create_vagrantfile] Finished creating vagrant file"
  nil
end

#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



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
119
120
121
122
# File 'lib/rspec-system/node_set/vagrant.rb', line 89

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
  cmd = "scp -r -F '#{ssh_config}' '#{source}' #{dest}:#{tmpdest}"
  puts "------------------"
  puts "localhost$ #{cmd}"
  r = systemu cmd

  result = {
    :exit_code => r[0].exitstatus,
    :stdout => r[1],
    :stderr => r[2]
  }

  print "#{result[:stdout]}"
  print "#{result[:stderr]}"
  puts "Exit code: #{result[:exit_code]}"

  # 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



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rspec-system/node_set/vagrant.rb', line 70

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

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

#setupvoid

This method returns an undefined value.

Setup the NodeSet by starting all nodes.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rspec-system/node_set/vagrant.rb', line 26

def setup
  log.info "[Vagrant#setup] Begin setting up vagrant"

  create_vagrantfile()

  teardown()

  log.info "[Vagrant#setup] Running 'vagrant up'"
  vagrant("up")

  # Establish ssh connectivity
  ssh_channels = {}
  nodes.each do |k,v|
    log.info "[Vagrant#setup] establishing Net::SSH channel with #{k}"
    chan = Net::SSH.start(k, 'vagrant', :config => ssh_config)
    ssh_channels[k] = chan
  end
  RSpec.configuration.ssh_channels = ssh_channels

  nil
end

#ssh_configString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Here we get vagrant to drop the ssh_config its using so we can monopolize it for transfers and custom stuff. We drop it into a single file, and since its indexed based on our own node names its quite ideal.

Returns:

  • (String)

    path to ssh_config file



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rspec-system/node_set/vagrant.rb', line 158

def ssh_config
  ssh_config_path = File.expand_path(File.join(@vagrant_path, "ssh_config"))
  begin
    File.unlink(ssh_config_path)
  rescue Errno::ENOENT
  end
  self.nodes.each do |k,v|
    Dir.chdir(@vagrant_path) do
      result = systemu("vagrant ssh-config #{k} >> #{ssh_config_path}")
      puts result.inspect
    end
  end
  ssh_config_path
end

#teardownvoid

This method returns an undefined value.

Shutdown the NodeSet by shutting down or pausing all nodes.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rspec-system/node_set/vagrant.rb', line 51

def teardown
  log.info "[Vagrant#teardown] closing all ssh channels"
  RSpec.configuration.ssh_channels.each do |k,v|
    v.close unless v.closed?
  end

  if destroy
    log.info "[Vagrant#teardown] Running 'vagrant destroy'"
    vagrant("destroy --force")
  else
    log.info "[Vagrant#teardown] Skipping 'vagrant destroy'"
  end
  nil
end

#vagrant(args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO:

This seems a little too specific these days, might want to generalize. It doesn’t use systemu, because we want to see the output immediately, but still - maybe we can make systemu do that.

Execute vagrant command in vagrant_path

Parameters:

  • args (String)

    args to vagrant



180
181
182
183
184
185
# File 'lib/rspec-system/node_set/vagrant.rb', line 180

def vagrant(args)
  Dir.chdir(@vagrant_path) do
    system("vagrant #{args}")
  end
  nil
end