Class: RSpecSystem::NodeSet::Vagrant

Inherits:
Base
  • Object
show all
Includes:
Log
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, #setname

Instance Method Summary collapse

Methods included from Log

#log

Methods inherited from Base

#env_type, #rollback, #snapshot

Constructor Details

#initialize(setname, config) ⇒ Vagrant

Returns a new instance of Vagrant.



10
11
12
13
# File 'lib/rspec-system/node_set/vagrant.rb', line 10

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rspec-system/node_set/vagrant.rb', line 44

def create_vagrantfile
  log.info "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|')
    @config['nodes'].each do |k,v|
      log.debug "Filling in content for #{k}"
      f.write(<<-EOS)
  c.vm.define '#{k}' do |vmconf|
#{template_prefabs(v["prefab"])}
  end
      EOS
    end
    f.write('end')
  end
  log.debug "Finished creating vagrant file"
end

#run(dest, command) ⇒ Object

Run a command on a host in the NodeSet.



34
35
36
37
38
39
40
# File 'lib/rspec-system/node_set/vagrant.rb', line 34

def run(dest, command)
  result = ""
  Dir.chdir(@vagrant_path) do
    result = `vagrant ssh #{dest} --command 'cd /tmp && #{command}'`
  end
  result
end

#setupObject

Setup the NodeSet by starting all nodes.



16
17
18
19
20
21
22
23
24
25
# File 'lib/rspec-system/node_set/vagrant.rb', line 16

def setup
  log.info "Begin setting up vagrant"
  create_vagrantfile

  log.info "Running 'vagrant destroy'"
  vagrant("destroy", "--force")

  log.info "Running 'vagrant up'"
  vagrant("up")
end

#teardownObject

Shutdown the NodeSet by shutting down or pausing all nodes.



28
29
30
31
# File 'lib/rspec-system/node_set/vagrant.rb', line 28

def teardown
  log.info "Running 'vagrant destroy'"
  vagrant("destroy", "--force")
end

#template_prefabs(prefab) ⇒ 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.

Provide Vagrantfile templates for prefabs.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rspec-system/node_set/vagrant.rb', line 64

def template_prefabs(prefab)
  case prefab
  when 'centos-58-x64'
    <<-EOS
vmconf.vm.box = 'centos-58-x64'
vmconf.vm.box_url = 'http://puppet-vagrant-boxes.puppetlabs.com/centos-58-x64.box'
    EOS
  when 'debian-606-x64'
    <<-EOS
vmconf.vm.box = 'debian-606-x64'
vmconf.vm.box_url = 'http://puppet-vagrant-boxes.puppetlabs.com/debian-606-x64.box'
    EOS
  else
    raise 'Unknown prefab'
  end
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.

Execute vagrant command in vagrant_path



83
84
85
86
87
# File 'lib/rspec-system/node_set/vagrant.rb', line 83

def vagrant(*args)
  Dir.chdir(@vagrant_path) do
    system("vagrant", *args)
  end
end