Class: Fission::VM

Inherits:
Object show all
Defined in:
lib/fission.old/vm.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ VM

Returns a new instance of VM


9
10
11
# File 'lib/fission.old/vm.rb', line 9

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


7
8
9
# File 'lib/fission.old/vm.rb', line 7

def name
  @name
end

Class Method Details

.allObject

Returns an array of vm objects


127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/fission.old/vm.rb', line 127

def self.all
  vm_dirs = Dir[File.join Fission.config.attributes['vm_dir'], '*.vmwarevm'].select do |d|
    File.directory? d
  end

  vm_names=vm_dirs.map { |d| File.basename d, '.vmwarevm' }
  vms=[]
  vm_names.each do |vmname|
    vm=Fission::VM.new vmname
    vms << vm
  end

  return vms
end

.all_runningObject

Returns an array of vms that are running


143
144
145
146
147
148
# File 'lib/fission.old/vm.rb', line 143

def self.all_running
  running_vms=self.all.select do |vm|
    vm.state=="running"
  end
  return running_vms
end

.clone(source_vm, target_vm) ⇒ Object

VM Class Actions

Raises:


158
159
160
161
162
163
164
165
166
167
168
# File 'lib/fission.old/vm.rb', line 158

def self.clone(source_vm, target_vm)
  raise Fission::Error,"VM #{source_vm} does not exist" unless Fission::VM.new(source_vm).exists?
  raise Fission::Error,"VM #{target_vm} already exists" if Fission::VM.new(target_vm).exists?

  FileUtils.cp_r Fission::VM.new(source_vm).path, Fission::VM.new(target_vm).path

  rename_vm_files source_vm, target_vm
  update_config source_vm, target_vm

  response = Response.new :code => 0
end

.delete(vm_name) ⇒ Object

Raises:


170
171
172
173
174
175
176
177
178
# File 'lib/fission.old/vm.rb', line 170

def self.delete(vm_name)
  raise Fission::Error,"VM #{vm_name} does not exist" unless Fission::VM.new(vm_name).exists?

  vm=Fission::VM.new(vm_name)
  FileUtils.rm_rf vm.path
  Fission::Metadata.delete_vm_info(vm.path)

  Response.new :code => 0
end

.get(name) ⇒ Object

Returns an existing vm


151
152
153
# File 'lib/fission.old/vm.rb', line 151

def self.get(name)
  return Fission::VM.new(name)
end

Instance Method Details

#create_snapshot(name) ⇒ Object

VM Instance Actions

Raises:


184
185
186
187
188
189
190
191
192
193
194
# File 'lib/fission.old/vm.rb', line 184

def create_snapshot(name)
  raise Fission::Error,"VM #{@name} does not exist" unless self.exists?

  command = "#{vmrun_cmd} snapshot #{vmx_path.shellescape} \"#{name}\" 2>&1"
  output = `#{command}`

  response = Fission::Response.new :code => $?.exitstatus
  response.output = output unless response.successful?

  response
end

#exists?Boolean

Checks to see if a vm exists

Returns:

  • (Boolean)

57
58
59
# File 'lib/fission.old/vm.rb', line 57

def exists?
  File.exists? vmx_path
end

#haltObject

Raises:


230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/fission.old/vm.rb', line 230

def halt
  raise Fission::Error,"VM #{@name} does not exist" unless self.exists?
  raise Fission::Error,"VM #{@name} is not running" unless self.running?

  command = "#{vmrun_cmd} stop #{vmx_path.shellescape} hard 2>&1"
  output = `#{command}`

  response = Fission::Response.new :code => $?.exitstatus
  response.output = output unless response.successful?

  response
end

#ip_addressObject

Retrieve the ip address for a vm. This will only look for dynamically assigned ip address via vmware dhcp

Raises:


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fission.old/vm.rb', line 106

def ip_address
  raise ::Fission::Error,"VM #{@name} does not exist" unless self.exists?

  unless mac_address.nil?
    lease=LeasesFile.new("/var/db/vmware/vmnet-dhcpd-vmnet8.leases").find_lease_by_mac(mac_address)
    if lease.nil?
      return nil
    else
      return lease.ip
    end
  else
    # No mac address was found for this machine so we can't calculate the ip-address
    return nil
  end
end

#mac_addressObject

Retrieve the first mac address for a vm This will only retrieve the first auto generate mac address

Raises:


92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fission.old/vm.rb', line 92

def mac_address
  raise ::Fission::Error,"VM #{@name} does not exist" unless self.exists?

  line=File.new(vmx_path).grep(/^ethernet0.generatedAddress =/)
  if line.nil?
    #Fission.ui.output "Hmm, the vmx file #{vmx_path} does not contain a generated mac address "
    return nil
  end
  address=line.first.split("=")[1].strip.split(/\"/)[1]
  return address
end

#pathObject

Path Helpers

Returns the topdir of the vm


17
18
19
# File 'lib/fission.old/vm.rb', line 17

def path
  File.join Fission.config.attributes['vm_dir'], "#{@name}.vmwarevm"
end

#resumeObject

Raises:


243
244
245
246
247
248
249
# File 'lib/fission.old/vm.rb', line 243

def resume
  raise Fission::Error,"VM #{@name} does not exist" unless self.exists?
  raise Fission::Error,"VM #{@name} is already running" if self.running?
  if self.suspended?
    self.start
  end
end

#revert_to_snapshot(name) ⇒ Object

Action to revert to a snapshot Returns a response object

Raises:


265
266
267
268
269
270
271
272
273
274
275
# File 'lib/fission.old/vm.rb', line 265

def revert_to_snapshot(name)
  raise Fission::Error,"VM #{@name} does not exist" unless self.exists?

  command = "#{vmrun_cmd} revertToSnapshot #{vmx_path.shellescape} \"#{name}\" 2>&1"
  output = `#{command}`

  response = Fission::Response.new :code => $?.exitstatus
  response.output = output unless response.successful?

  response
end

#running?Boolean

State information

Returns:

  • (Boolean)

Raises:


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fission.old/vm.rb', line 31

def running?
  raise Fission::Error,"VM #{@name} does not exist" unless self.exists?

  command = "#{vmrun_cmd} list"
  output = `#{command}`

  response = Fission::Response.new :code => $?.exitstatus

  if response.successful?
    vms = output.split("\n").select do |vm|
      vm.include?('.vmx') && File.exists?(vm) && File.extname(vm) == '.vmx'
    end
    return vms.include?(self.vmx_path)
  else
    raise Fission::Error,"Error listing the state of vm #{@name}:\n#{output}"
  end
end

#snapshotsObject

Returns an Array of snapshot names

Raises:


77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fission.old/vm.rb', line 77

def snapshots
  raise Fission::Error,"VM #{@name} does not exist" unless self.exists?

  command = "#{vmrun_cmd} listSnapshots #{vmx_path.shellescape} 2>&1"
  output = `#{command}`

  raise "There was an error listing the snapshots of #{@name} :\n #{output}" unless  $?.exitstatus==0

  snaps_unfiltered = output.split("\n").select { |s| !s.include? 'Total snapshots:' }
  snaps=snaps_unfiltered.map { |s| s.strip }
  return snaps
end

#start(args = {}) ⇒ Object

Raises:


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/fission.old/vm.rb', line 196

def start(args={})
  raise Fission::Error,"VM #{@name} does not exist" unless self.exists?
  raise Fission::Error,"VM #{@name} is already started" if self.running?


  command = "#{vmrun_cmd} start #{vmx_path.shellescape}"

  if !args[:headless].blank? && args[:headless]
    command << " nogui 2>&1"
  else
    command << " gui 2>&1"
  end

  output = `#{command}`

  response = Fission::Response.new :code => $?.exitstatus
  response.output = output unless response.successful?

  response
end

#stateObject

Returns the state of a vm


62
63
64
65
66
67
68
69
70
# File 'lib/fission.old/vm.rb', line 62

def state
  return "not created" unless self.exists?

  return "suspend" if self.suspended?

  return "running" if self.running?

  return "not running"
end

#stopObject

Raises:


217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/fission.old/vm.rb', line 217

def stop
  raise Fission::Error,"VM #{@name} does not exist" unless self.exists?
  raise Fission::Error,"VM #{@name} is not running" unless self.running?

  command = "#{vmrun_cmd} stop #{vmx_path.shellescape} 2>&1"
  output = `#{command}`

  response = Fission::Response.new :code => $?.exitstatus
  response.output = output unless response.successful?

  response
end

#suspendObject

Raises:


251
252
253
254
255
256
257
258
259
260
261
# File 'lib/fission.old/vm.rb', line 251

def suspend
  raise Fission::Error,"VM #{@name} does not exist" unless self.exists?
  raise Fission::Error,"VM #{@name} is not running" unless self.running?

  command = "#{vmrun_cmd} suspend #{vmx_path.shellescape} hard 2>&1"
  output = `#{command}`

  response = Fission::Response.new :code => $?.exitstatus
  response.output = output unless response.successful?
  response
end

#suspended?Boolean

Returns:

  • (Boolean)

Raises:


49
50
51
52
53
54
# File 'lib/fission.old/vm.rb', line 49

def suspended?
  raise Fission::Error,"VM #{@name} does not exist" unless self.exists?

  suspend_filename=File.join(File.dirname(vmx_path), File.basename(vmx_path,".vmx")+".vmem")
  return File.exists?(suspend_filename)
end

#vmx_pathObject

Returns a string to the path of the config file There is no guarantee it exists


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

def vmx_path
  return File.join(path, "#{@name}.vmx")
end