Class: RSpecSystem::NodeSet::VagrantBase

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

Overview

An abstract NodeSet implementation for Vagrant.

Direct Known Subclasses

VagrantVirtualbox, VagrantVmwareFusion

Instance Attribute Summary

Attributes inherited from Base

#config, #custom_prefabs_path, #destroy, #nodes, #setname

Abstract Methods collapse

Common Methods collapse

Methods included from Util

#deep_merge!, #shellescape

Methods included from Log

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

Methods inherited from Base

#configure, #default_node, #provider_type, #randmac, #random_string, #rcp, #run, #setup, #ssh_connect, #ssh_exec!, #tmppath

Constructor Details

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

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

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



31
32
33
34
35
36
# File 'lib/rspec-system/node_set/vagrant_base.rb', line 31

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

  RSpec.configuration.rs_storage[:nodes] ||= {}
end

Instance Method Details

#connectvoid

This method returns an undefined value.

Connect to the nodes



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rspec-system/node_set/vagrant_base.rb', line 58

def connect
  nodes.each do |k,v|
    RSpec.configuration.rs_storage[:nodes][k] ||= {}

    chan = ssh_connect(:host => k, :user => 'vagrant', :net_ssh_options => {
      :config => ssh_config
    })

    # Copy the authorized keys from vagrant user to root then reconnect
    cmd = 'mkdir /root/.ssh ; cp /home/vagrant/.ssh/authorized_keys /root/.ssh'

    output << bold(color("#{k}$ ", :green)) << cmd << "\n"
    ssh_exec!(chan, "cd /tmp && sudo sh -c #{shellescape(cmd)}")

    chan = ssh_connect(:host => k, :user => 'root', :net_ssh_options => {
      :config => ssh_config
    })
    RSpec.configuration.rs_storage[:nodes][k][:ssh] = chan
  end

  nil
end

#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.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rspec-system/node_set/vagrant_base.rb', line 105

def create_vagrantfile
  output << bold(color("localhost$", :green)) << " cd #{@vagrant_path}\n"
  FileUtils.mkdir_p(@vagrant_path)
  File.open(File.expand_path(File.join(@vagrant_path, "Vagrantfile")), 'w') do |f|
    f.write('Vagrant.configure("2") do |c|' + "\n")
    nodes.each do |k,v|
      ps = v.provider_specifics[provider_type]
      default_options = { 'mac' => randmac }
      options = default_options.merge(v.options || {})

      node_config = "  c.vm.define '#{k}' do |v|\n"
      node_config << "    v.vm.hostname = '#{k}'\n"
      node_config << "    v.vm.box = '#{ps['box']}'\n"
      node_config << "    v.vm.box_url = '#{ps['box_url']}'\n" unless ps['box_url'].nil?
      node_config << customize_vm(k,options)
      node_config << "    v.vm.provider '#{vagrant_provider_name}' do |prov, override|\n"
      node_config << customize_provider(k,options)
      node_config << "    end\n"
      node_config << "  end\n"

      f.write(node_config)
    end
    f.write("end\n")
  end
  nil
end

#customize_provider(name, options) ⇒ String

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.

This method is abstract.

Overridet ot provide your own customizations

Add provider specific customization to the Vagrantfile

Parameters:

  • name (String)

    name of the node

  • options (Hash)

    customization options

Returns:

  • (String)

    a series of prov.customize lines



139
140
141
# File 'lib/rspec-system/node_set/vagrant_base.rb', line 139

def customize_provider(name,options)
  ''
end

#customize_vm(name, options) ⇒ String

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.

Adds VM customization to the Vagrantfile

Parameters:

  • name (String)

    name of the node

  • options (Hash)

    customization options

Returns:

  • (String)

    a series of v.vm lines



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rspec-system/node_set/vagrant_base.rb', line 149

def customize_vm(name,options)
  vm_config = ""
  options.each_pair do |key,value|
    case key
    when 'ip'
      vm_config << "    v.vm.network :private_network, :ip => '#{value}'\n"
    else
      next
    end
  end
  vm_config
end

#global_vagrant_optionsArray<String>

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.

Returns a list of options that apply to all types of vagrant providers

Returns:

  • (Array<String>)

    Array of options



197
198
199
# File 'lib/rspec-system/node_set/vagrant_base.rb', line 197

def global_vagrant_options
  ['ip']
end

#launchvoid

This method returns an undefined value.

Launch the nodes



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rspec-system/node_set/vagrant_base.rb', line 41

def launch
  create_vagrantfile()

  teardown()

  nodes.each do |k,v|
    RSpec.configuration.rs_storage[:nodes][k] ||= {}
    output << bold(color("localhost$", :green)) << " vagrant up #{k}\n"
    vagrant("up #{k} --provider=#{vagrant_provider_name}")
  end

  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



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rspec-system/node_set/vagrant_base.rb', line 168

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}")
    end
  end
  ssh_config_path
end

#teardownvoid

This method returns an undefined value.

Shutdown the NodeSet by shutting down or pausing all nodes.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rspec-system/node_set/vagrant_base.rb', line 84

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

    next if storage.nil?

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

  if destroy
    output << bold(color("localhost$", :green)) << " vagrant destroy --force\n"
    vagrant("destroy --force")
  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.

Execute vagrant command in vagrant_path

Parameters:

  • args (String)

    args to vagrant



186
187
188
189
190
191
# File 'lib/rspec-system/node_set/vagrant_base.rb', line 186

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

#vagrant_provider_nameString

This method is abstract.

override to return the name of the vagrant provider

The vagrant specific provider name

Returns:

  • (String)

    name of the provider as used by ‘vagrant –provider`

Raises:

  • (RuntimeError)


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

def vagrant_provider_name
  raise RuntimeError, "Unimplemented method #vagrant_provider_name"
end