Class: Foodtaster::Vm

Inherits:
Object
  • Object
show all
Defined in:
lib/foodtaster/vm.rb

Defined Under Namespace

Classes: ExecResult

Constant Summary collapse

@@vms =
Set.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, client) ⇒ Vm

Returns a new instance of Vm.



48
49
50
51
52
53
54
55
56
57
# 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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



23
24
25
# File 'lib/foodtaster/vm.rb', line 23

def name
  @name
end

Class Method Details

.find_by_name(vm_name) ⇒ Object



38
39
40
# File 'lib/foodtaster/vm.rb', line 38

def find_by_name(vm_name)
  @@vms.find { |vm| vm.name == vm_name }
end

.get(vm_name) ⇒ Object



42
43
44
45
# 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

.register_vm(vm) ⇒ Object



28
29
30
# File 'lib/foodtaster/vm.rb', line 28

def register_vm(vm)
  @@vms << vm
end

.shutdown_running_vmsObject



32
33
34
35
36
# File 'lib/foodtaster/vm.rb', line 32

def shutdown_running_vms
  @@vms.each do |vm|
    vm.shutdown if vm.prepared?
  end
end

Instance Method Details

#execute(command) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# 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) ⇒ Object



127
128
129
130
# 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) ⇒ Object



101
102
103
# 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?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/foodtaster/vm.rb', line 63

def initial_snapshot_made?
  @client.initial_snapshot_made_on_vm?(self.name)
end

#ipObject



93
94
95
# File 'lib/foodtaster/vm.rb', line 93

def ip
  @client.vm_ip(name)
end

#make_initial_snapshot!Object



72
73
74
75
# 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

#prepareObject



77
78
79
80
81
82
83
84
85
86
87
# 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?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/foodtaster/vm.rb', line 89

def prepared?
  self.running? && self.initial_snapshot_made?
end

#put_file(local_fn, vm_fn) ⇒ Object



97
98
99
# 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

#rollbackObject



110
111
112
113
# File 'lib/foodtaster/vm.rb', line 110

def rollback
  Foodtaster.logger.info "#{name}: Rolling back VM"
  @client.rollback_vm(name)
end

#run_chef(config) ⇒ Object



132
133
134
135
136
137
138
139
# 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?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/foodtaster/vm.rb', line 59

def running?
  @client.vm_running?(self.name)
end

#shutdownObject



105
106
107
108
# File 'lib/foodtaster/vm.rb', line 105

def shutdown
  Foodtaster.logger.debug "#{name}: Shutting down VM"
  @client.shutdown_vm(name)
end

#start!Object



67
68
69
70
# File 'lib/foodtaster/vm.rb', line 67

def start!
  Foodtaster.logger.info "#{name}: Power on machine"
  @client.start_vm(self.name)
end