class Foodtaster::Vm

Attributes

name[R]

Public Class Methods

find_by_name(vm_name) click to toggle source
# File lib/foodtaster/vm.rb, line 38
def find_by_name(vm_name)
  @@vms.find { |vm| vm.name == vm_name }
end
get(vm_name) click to toggle source
# File lib/foodtaster/vm.rb, line 42
def get(vm_name)
  find_by_name(vm_name) ||
    self.new(vm_name, Foodtaster::RSpecRun.current.client)
end
new(name, client) click to toggle source
# File lib/foodtaster/vm.rb, line 48
def initialize(name, client)
  @name = name
  @client = client

  unless @client.vm_defined?(name)
    raise ArgumentError, "No machine defined with name #{name}"
  end

  self.class.register_vm(self)
end
register_vm(vm) click to toggle source
# File lib/foodtaster/vm.rb, line 28
def register_vm(vm)
  @@vms << vm
end
shutdown_running_vms() click to toggle source
# File lib/foodtaster/vm.rb, line 32
def shutdown_running_vms
  @@vms.each do |vm|
    vm.shutdown if vm.prepared?
  end
end

Public Instance Methods

execute(command) click to toggle source
# File lib/foodtaster/vm.rb, line 115
def execute(command)
  Foodtaster.logger.debug "#{name}: Executing #{command}"
  exec_result_hash = @client.execute_command_on_vm(name, command)
  exec_result = ExecResult.new(exec_result_hash)

  Foodtaster.logger.debug "#{name}: Finished with #{exec_result.exit_status}"
  Foodtaster.logger.debug "#{name}: STDOUT: #{exec_result.stdout}"
  Foodtaster.logger.debug "#{name}: STDERR: #{exec_result.stderr}"

  ExecResult.new(exec_result_hash)
end
execute_as(user, command) click to toggle source
# File lib/foodtaster/vm.rb, line 127
def execute_as(user, command)
  cmd = %Q[sudo su -l #{user} -c "#{command}"]
  self.execute cmd
end
get_file(vm_fn, local_fn) click to toggle source
# File lib/foodtaster/vm.rb, line 101
def get_file(vm_fn, local_fn)
  @client.get_file_from_vm(name, vm_fn, local_fn)
end
initial_snapshot_made?() click to toggle source
# File lib/foodtaster/vm.rb, line 63
def initial_snapshot_made?
  @client.initial_snapshot_made_on_vm?(self.name)
end
ip() click to toggle source
# File lib/foodtaster/vm.rb, line 93
def ip
  @client.vm_ip(name)
end
make_initial_snapshot!() click to toggle source
# File lib/foodtaster/vm.rb, line 72
def make_initial_snapshot!
  Foodtaster.logger.info "#{name}: Creating initial snapshot"
  @client.make_initial_snapshot_on_vm(self.name)
end
prepare() click to toggle source
# File lib/foodtaster/vm.rb, line 77
def prepare
  Foodtaster.logger.info "#{name}: Preparing VM"

  unless running?
    self.start!
  end

  unless initial_snapshot_made?
    self.make_initial_snapshot!
  end
end
prepared?() click to toggle source
# File lib/foodtaster/vm.rb, line 89
def prepared?
  self.running? && self.initial_snapshot_made?
end
put_file(local_fn, vm_fn) click to toggle source
# File lib/foodtaster/vm.rb, line 97
def put_file(local_fn, vm_fn)
  @client.put_file_to_vm(name, local_fn, vm_fn)
end
rollback() click to toggle source
# File lib/foodtaster/vm.rb, line 110
def rollback
  Foodtaster.logger.info "#{name}: Rolling back VM"
  @client.rollback_vm(name)
end
run_chef(config) click to toggle source
# File lib/foodtaster/vm.rb, line 132
def run_chef(config)
  fail ArgumentError, "#{config.inspect} should have :run_list." unless config[:run_list]

  Foodtaster.logger.info "#{name}: Running Chef with Run List #{config[:run_list].join(', ')}"
  Foodtaster.logger.debug "#{name}: with JSON: #{config[:json].inspect}"
  @client.run_chef_on_vm(name, config)
  Foodtaster.logger.debug "#{name}: Chef Run finished"
end
running?() click to toggle source
# File lib/foodtaster/vm.rb, line 59
def running?
  @client.vm_running?(self.name)
end
shutdown() click to toggle source
# File lib/foodtaster/vm.rb, line 105
def shutdown
  Foodtaster.logger.debug "#{name}: Shutting down VM"
  @client.shutdown_vm(name)
end
start!() click to toggle source
# File lib/foodtaster/vm.rb, line 67
def start!
  Foodtaster.logger.info "#{name}: Power on machine"
  @client.start_vm(self.name)
end