class Tupperware::VagrantVirtualBox

Attributes

kitchen_config[R]
kitchen_root[R]
options[R]

Public Class Methods

new(options={}, kitchen_config=Kitchen::Config.new) click to toggle source
# File lib/tupperware.rb, line 122
def initialize(options={}, kitchen_config=Kitchen::Config.new)
  @options = options
  @kitchen_config = kitchen_config
  @kitchen_root = @kitchen_config.kitchen_root
end

Public Instance Methods

get_id(instance_name) click to toggle source
# File lib/tupperware.rb, line 135
def get_id(instance_name)
  v_root = vagrant_root(instance_name)
  id_file = "#{v_root}/.vagrant/machines/default/virtualbox/id"
  Utils.check_path(id_file)
  IO.read(id_file)
end
halt(instance_name) click to toggle source
# File lib/tupperware.rb, line 188
def halt(instance_name)
  setup_vagrant_env(instance_name) do 
    Utils.run_cmd('vagrant halt')
  end
end
package(instance_name) click to toggle source
# File lib/tupperware.rb, line 194
def package(instance_name)
  setup_vagrant_env(instance_name) do
    if @options[:pre_package]
      Utils.check_path()
      load(@options[:pre_package])
    end
    id = get_id(instance_name)
    puts "\n*** Packaging #{instance_name} ***\n"
    Utils.run_cmd("vagrant package --base #{id} --output #{instance_name}.box")
    current_path = File.absolute_path("#{instance_name}.box")
    desired_path = File.join(@kitchen_root, "#{instance_name}.box")
    FileUtils.mv(current_path, desired_path)
  end
end
provision_local_box(instance) click to toggle source
# File lib/tupperware.rb, line 142
    def provision_local_box(instance)
      if @options[:provision]
        Utils.check_path(@options[:provision])
        script = IO.read(@options[:provision])
      else
        script = <<-SCRIPT
        #!/bin/bash
        echo -n "\n*** Deploying the pulibic key ***\n"
        echo 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key' | sudo tee -a /home/vagrant/.ssh/authorized_keys
        echo "\n*** Setting up passwordless SSH for the vagrant user ***\n"
        echo 'vagrant ALL=(ALL) NOPASSWD: ALL' | sudo tee -a /etc/sudoers.conf
        echo "\n*** Removing locale files ***\n"
        sudo find /usr/share/locale/ -mindepth 1 ! -iregex "/.*en/.*" -type d -exec rm -rf {} +
        echo "\n*** Cleaning logs ***\n"
        sudo find /var/log -type f | while read f; do echo -n '' > $f; done;
        echo "\n*** Cleaning history ***\n"
        sudo rm -f /root/.bash_history
        sudo rm -f /home/vagrant/.bash_history
        SCRIPT
      end
      bin = File.join(vagrant_root(instance.name.to_s), 'kitchen-provision')
      File.open(bin, 'w') {|f| f.write(script)}
      state = Kitchen::StateFile.new(@kitchen_root, instance.name.to_s)
      state_hash = state.read
      options = {
        keys: [state_hash[:ssh_key]], 
        port: state_hash[:port], 
        username: state_hash[:username], 
        hostname: state_hash[:hostname]
      }
      Kitchen::Transport::Ssh::Connection.new(options) do |c|
        c.upload(bin, '/tmp/kitchen-provision')
      end
      instance.remote_exec('sudo chmod +x /tmp/kitchen-provision')
      instance.remote_exec('sudo /tmp/kitchen-provision')
      instance.remote_exec('sudo rm -f /tmp/kitchen-provision')
      File.delete(bin)
    end
setup_vagrant_env(instance_name) { || ... } click to toggle source
# File lib/tupperware.rb, line 181
def setup_vagrant_env(instance_name)
  current_dir = ENV['PWD']
  Dir.chdir(vagrant_root(instance_name))
  yield
  Dir.chdir(current_dir)
end
vagrant_root(instance_name) click to toggle source
# File lib/tupperware.rb, line 128
def vagrant_root(instance_name)
  File.join(
    @kitchen_root, %w[.kitchen kitchen-vagrant],
    "kitchen-#{File.basename(@kitchen_root)}-#{instance_name}"
  )
end