class VMTools::VM

Public Class Methods

new() click to toggle source
# File lib/vmtools.rb, line 81
def initialize
  # Get vmtools gem path
  spec = Gem::Specification.find_by_name("vmtools")
  @gem_root = spec.gem_dir
  # Read config.
  # First searching for config in current dir and then in ruby gems dir
  begin
    VMConfig.from_file('vmtconfig.rb')
  rescue Errno::ENOENT
    puts "Warring: There's no vmtconfig.rb in current dir. Using default vmtconfig.rb"
    VMConfig.from_file("#{@gem_root}/config/vmtconfig.rb")
  end

  # Now only vagrant type of VMs is supported.
  if VMConfig.vm_type != 'vagrant'
    puts "Well, this's embarassing, but only vagrant VMs are supported now"
    puts "Your specified #{VMConfig.vm_type} VM in config."
    exit(0)
  end
  # Assign class instance variables
  ## Chef stuff
  @orgname = VMConfig.orgname
  @knife_config = VMConfig.knife_config

  ## Common stuff
  @testbox_name = VMConfig.testbox_name

  ## Vagrant stuff
  @vtemplate = VMConfig.vtemplate
  @vconfig = VMConfig.vconfig
  @homedir = VMConfig.homedir
  @vmemory = VMConfig.vmemory
  @vforward_ports = VMConfig.vforward_ports
  @vbox_name = VMConfig.vbox_name
  @vbox_url = VMConfig.vbox_url
end

Public Instance Methods

create() click to toggle source
# File lib/vmtools.rb, line 163
def create
  # Read template content
  # First searching for config in current dir and then in ruby gems dir
  begin
    template_content = IO.read(@vtemplate)
  rescue Errno::ENOENT
    puts "Warring: There's no Vagrantfile.erb in current dir. Using default Vagrantfile.erb"
    template_content = IO.read("#{@gem_root}/templates/Vagrantfile.erb")
  end

  erb = ERB.new(template_content)

  env = Vagrant::Environment.new
  # Clean out old vagrant config before writing a new one
  begin
    File.delete(@vconfig)
  rescue Errno::ENOENT
    puts "Warring: There's no #{@vconfig} in this dir. Nothing to delete"
  end
  # Generate new config from erb template
  IO.write(@vconfig,erb.result(self.get_binding))
  env.cli("up")
end
delete() click to toggle source
# File lib/vmtools.rb, line 187
def delete
  # Delete chef client/node
  ENV['ORGNAME'] = "#{@orgname}"
  system("knife node delete #{@testbox} -c #{@knife_config} -y")
  system("knife client delete #{@testbox} -c #{@knife_config} -y")
  # Delete VM
  env = Vagrant::Environment.new
  begin
    File.delete(@vconfig)
  rescue Errno::ENOENT
    puts "Warring: There's no #{@vconfig} in this dir. Nothing to delete"
  end
  env.cli("destroy","--force")
end
get_binding() click to toggle source
# File lib/vmtools.rb, line 77
def get_binding # this is only a helper method to access the objects
# binding method
  binding
end
run() click to toggle source
# File lib/vmtools.rb, line 118
  def run
    # Proccess command line parameters
    ## If no arguments - then let's print help
    if ARGV.empty?
      puts "Well, this's embarassing, but you need to specify an option! Please read help:"
      ARGV << "-h"
    end

    cli = VMCLI.new
    banner = <<-eos
Vmtools is util for create/delete/recreate test VM
VM configuration stored in vmtconfig.rb file

Usage:
       vmtools --create|--delete|--recreate

Options:
eos
    cli.banner=banner
    ## Parse options
    begin
      cli.parse_options
    rescue OptionParser::InvalidOption
      # if we have wrong option, print help.
      puts "Well, this's embarassing, but wrong option! Please read help:"
      ARGV.clear << "-h"
      retry
    end
    cli.config.each do |opt,value|
      if value
        case opt
          when :create_vm
            self.create
          when :delete_vm
            self.delete
          when :recreate_vm
            self.delete
            self.create
          when :version
          puts "vmtools #{VMTools::VERSION} (c) 2012 Atalanta Systems"
        end
      end
    end
  end