Class: RSpecSystem::NodeSet::Vagrant
- Defined in:
- lib/rspec-system/node_set/vagrant.rb
Overview
A NodeSet implementation for Vagrant.
Constant Summary collapse
- ENV_TYPE =
'vagrant'- VALID_VM_OPTIONS =
['ip']
Instance Attribute Summary
Attributes inherited from Base
#config, #custom_prefabs_path, #destroy, #nodes, #setname
Instance Method Summary collapse
-
#create_vagrantfile ⇒ Object
private
Create the Vagrantfile for the NodeSet.
-
#customize_virtualbox(name, options) ⇒ String
private
Adds virtualbox customization to the Vagrantfile.
-
#customize_vm(name, options) ⇒ String
private
Adds VM customization to the Vagrantfile.
-
#initialize(setname, config, custom_prefabs_path, options) ⇒ Vagrant
constructor
Creates a new instance of RSpecSystem::NodeSet::Vagrant.
-
#rcp(opts) ⇒ Boolean
Transfer files to a host in the NodeSet.
-
#run(opts) ⇒ Hash
Run a command on a host in the NodeSet.
-
#setup ⇒ void
Setup the NodeSet by starting all 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
#default_node, #env_type, #randmac, #random_string, #ssh_exec!, #tmppath
Constructor Details
#initialize(setname, config, custom_prefabs_path, options) ⇒ Vagrant
Creates a new instance of RSpecSystem::NodeSet::Vagrant
22 23 24 25 26 27 |
# File 'lib/rspec-system/node_set/vagrant.rb', line 22 def initialize(setname, config, custom_prefabs_path, ) super @vagrant_path = File.(File.join(RSpec.configuration.system_tmp, 'vagrant_projects', setname)) RSpec.configuration.rspec_storage[:nodes] ||= {} end |
Instance Method Details
#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.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/rspec-system/node_set/vagrant.rb', line 118 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['vagrant'] = { '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 'virtualbox' do |vbox|\n" node_config << customize_virtualbox(k,) node_config << " end\n" node_config << " end\n" f.write(node_config) end f.write("end\n") end nil end |
#customize_virtualbox(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 virtualbox customization to the Vagrantfile
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/rspec-system/node_set/vagrant.rb', line 151 def customize_virtualbox(name,) custom_config = "" .each_pair do |key,value| next if VALID_VM_OPTIONS.include?(key) case key when 'cpus','memory' custom_config << " vbox.customize ['modifyvm', :id, '--#{key}','#{value}']\n" when 'mac' custom_config << " vbox.customize ['modifyvm', :id, '--macaddress1','#{value}']\n" else log.warn("Skipped invalid custom option for node #{name}: #{key}=#{value}") end end custom_config 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
173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/rspec-system/node_set/vagrant.rb', line 173 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 |
#rcp(opts) ⇒ Boolean
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.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rspec-system/node_set/vagrant.rb', line 92 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 cmd = "scp -r '#{source}' #{dest}:#{tmpdest}" output << bold(color("localhost$", :green)) << " #{cmd}\n" 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 = shell(: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.
77 78 79 80 81 82 83 |
# File 'lib/rspec-system/node_set/vagrant.rb', line 77 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 #{shellescape(cmd)}") end |
#setup ⇒ void
This method returns an undefined value.
Setup the NodeSet by starting all nodes.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rspec-system/node_set/vagrant.rb', line 32 def setup create_vagrantfile() teardown() output << bold(color("localhost$", :green)) << " vagrant up\n" vagrant("up") # Establish ssh connectivity nodes.each do |k,v| output << bold(color("localhost$", :green)) << " ssh #{k}\n" chan = Net::SSH.start(k, 'vagrant', :config => ssh_config) RSpec.configuration.rspec_storage[:nodes][k] = { :ssh => chan, } 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.
192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/rspec-system/node_set/vagrant.rb', line 192 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.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rspec-system/node_set/vagrant.rb', line 56 def teardown nodes.each do |k,v| storage = RSpec.configuration.rspec_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.
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
213 214 215 216 217 218 |
# File 'lib/rspec-system/node_set/vagrant.rb', line 213 def vagrant(args) Dir.chdir(@vagrant_path) do system("vagrant #{args}") end nil end |