Class: RSpecSystem::NodeSet::VagrantBase
- Defined in:
- lib/rspec-system/node_set/vagrant_base.rb
Overview
An abstract NodeSet implementation for Vagrant.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Base
#config, #custom_prefabs_path, #destroy, #nodes, #setname
Abstract Methods collapse
-
#vagrant_provider_name ⇒ String
abstract
The vagrant specific provider name.
Common Methods collapse
-
#connect ⇒ void
Connect to the nodes.
-
#create_vagrantfile ⇒ Object
private
Create the Vagrantfile for the NodeSet.
-
#customize_provider(name, options) ⇒ String
abstract
private
Add provider specific customization to the Vagrantfile.
-
#customize_vm(name, options) ⇒ String
private
Adds VM customization to the Vagrantfile.
-
#global_vagrant_options ⇒ Array<String>
private
Returns a list of options that apply to all types of vagrant providers.
-
#initialize(setname, config, custom_prefabs_path, options) ⇒ VagrantBase
constructor
Creates a new instance of RSpecSystem::NodeSet::Vagrant.
-
#launch ⇒ void
Launch the nodes.
-
#ssh_config ⇒ String
private
Here we get vagrant to drop the ssh_config its using so we can monopolize it for transfers and custom stuff.
-
#teardown ⇒ void
Shutdown the NodeSet by shutting down or pausing all nodes.
-
#vagrant(args) ⇒ Object
private
Execute vagrant command in vagrant_path.
Methods included from Util
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
31 32 33 34 35 36 |
# File 'lib/rspec-system/node_set/vagrant_base.rb', line 31 def initialize(setname, config, custom_prefabs_path, ) super @vagrant_path = File.(File.join(RSpec.configuration.rs_tmp, 'vagrant_projects', setname)) RSpec.configuration.rs_storage[:nodes] ||= {} end |
Instance Method Details
#connect ⇒ void
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_vagrantfile ⇒ 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.
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.(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] = { 'mac' => randmac } = .merge(v. || {}) 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,) node_config << " v.vm.provider '#{vagrant_provider_name}' do |prov, override|\n" node_config << customize_provider(k,) 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.
Overridet ot provide your own customizations
Add provider specific customization to the Vagrantfile
139 140 141 |
# File 'lib/rspec-system/node_set/vagrant_base.rb', line 139 def customize_provider(name,) '' 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
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,) vm_config = "" .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_options ⇒ Array<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
197 198 199 |
# File 'lib/rspec-system/node_set/vagrant_base.rb', line 197 def ['ip'] end |
#launch ⇒ void
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_config ⇒ 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.
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.
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.(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 |
#teardown ⇒ void
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
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_name ⇒ String
override to return the name of the vagrant provider
The vagrant specific provider name
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 |