lib/rspec-system/node_set/vagrant.rb in rspec-system-2.3.0 vs lib/rspec-system/node_set/vagrant.rb in rspec-system-2.4.0

- old
+ new

@@ -1,16 +1,19 @@ require 'fileutils' require 'systemu' require 'net/ssh' +require 'net/scp' +require 'rspec-system/node_set/base' module RSpecSystem # A NodeSet implementation for Vagrant. class NodeSet::Vagrant < RSpecSystem::NodeSet::Base include RSpecSystem::Log include RSpecSystem::Util ENV_TYPE = 'vagrant' + VALID_VM_OPTIONS = ['ip'] # Creates a new instance of RSpecSystem::NodeSet::Vagrant # # @param setname [String] name of the set to instantiate # @param config [Hash] nodeset configuration hash @@ -117,24 +120,68 @@ 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['vagrant'] + 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 'virtualbox' do |vbox|\n" - node_config << " vbox.customize ['modifyvm',:id,'--macaddress1','#{randmac}']\n" + node_config << customize_virtualbox(k,options) node_config << " end\n" node_config << " end\n" f.write(node_config) end f.write("end\n") end nil + end + + # Adds virtualbox customization to the Vagrantfile + # + # @api private + # @param name [String] name of the node + # @param options [Hash] customization options + # @return [String] a series of vbox.customize lines + def customize_virtualbox(name,options) + custom_config = "" + options.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 + + # Adds VM customization to the Vagrantfile + # + # @api private + # @param name [String] name of the node + # @param options [Hash] customization options + # @return [String] a series of v.vm lines + 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 # 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.