Class: Fission::VM

Inherits:
Object show all
Defined in:
lib/fission/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.



5
6
7
# File 'lib/fission/vm.rb', line 5

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/fission/vm.rb', line 3

def name
  @name
end

Class Method Details

.allObject



107
108
109
110
111
112
113
# File 'lib/fission/vm.rb', line 107

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

  vm_dirs.map { |d| File.basename d, '.vmwarevm' }
end

.all_runningObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/fission/vm.rb', line 115

def self.all_running
  command = "#{Fission.config.attributes['vmrun_cmd']} list"

  output = `#{command}`

  if $?.exitstatus == 0
    vms = output.split("\n").select do |vm|
      vm.include?('.vmx') && File.exists?(vm) && File.extname(vm) == '.vmx'
    end

    vms.map { |vm| File.basename(File.dirname(vm), '.vmwarevm') }
  else
    Fission.ui.output_and_exit "Unable to determine the list of running VMs", 1
  end
end

.clone(source_vm, target_vm) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/fission/vm.rb', line 139

def self.clone(source_vm, target_vm)
  Fission.ui.output "Cloning #{source_vm} to #{target_vm}"
  FileUtils.cp_r path(source_vm), path(target_vm)

  Fission.ui.output "Configuring #{target_vm}"
  rename_vm_files source_vm, target_vm
  update_config source_vm, target_vm
end

.delete(vm_name) ⇒ Object



148
149
150
151
152
# File 'lib/fission/vm.rb', line 148

def self.delete(vm_name)
  Fission.ui.output "Deleting vm #{vm_name}"
  FileUtils.rm_rf path(vm_name)
  Fission::Metadata.delete_vm_info(path(vm_name))
end

.exists?(vm_name) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/fission/vm.rb', line 131

def self.exists?(vm_name)
  File.directory? path(vm_name)
end

.path(vm_name) ⇒ Object



135
136
137
# File 'lib/fission/vm.rb', line 135

def self.path(vm_name)
  File.join Fission.config.attributes['vm_dir'], "#{vm_name}.vmwarevm"
end

Instance Method Details

#conf_fileObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fission/vm.rb', line 86

def conf_file
  vmx_path = File.join(self.class.path(@name), "*.vmx")
  conf_files = Dir.glob(vmx_path)

  case conf_files.count
  when 0
    Fission.ui.output_and_exit "Unable to find a config file for VM '#{@name}' (in '#{vmx_path}')", 1
  when 1
    conf_files.first
  else
    if conf_files.include?(File.join(File.dirname(vmx_path), "#{@name}.vmx"))
      File.join(File.dirname(vmx_path), "#{@name}.vmx")
    else
      output = "Multiple config files found for VM '#{@name}' ("
      output << conf_files.sort.map { |f| "'#{File.basename(f)}'" }.join(', ')
      output << " in '#{File.dirname(vmx_path)}')"
      Fission.ui.output_and_exit output, 1
    end
  end
end

#create_snapshot(name) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fission/vm.rb', line 9

def create_snapshot(name)
  command = "#{Fission.config.attributes['vmrun_cmd']} snapshot #{conf_file.gsub ' ', '\ '} \"#{name}\" 2>&1"
  output = `#{command}`

  if $?.exitstatus == 0
    Fission.ui.output "Snapshot '#{name}' created"
  else
    Fission.ui.output "There was an error creating the snapshot."
    Fission.ui.output_and_exit "The error was:\n#{output}", 1
  end
end

#revert_to_snapshot(name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fission/vm.rb', line 21

def revert_to_snapshot(name)
  command = "#{Fission.config.attributes['vmrun_cmd']} revertToSnapshot #{conf_file.gsub ' ', '\ '} \"#{name}\" 2>&1"
  output = `#{command}`

  if $?.exitstatus == 0
    Fission.ui.output "Reverted to snapshot '#{name}'"
  else
    Fission.ui.output "There was an error reverting to the snapshot."
    Fission.ui.output_and_exit "The error was:\n#{output}", 1
  end
end

#snapshotsObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fission/vm.rb', line 33

def snapshots
  command = "#{Fission.config.attributes['vmrun_cmd']} listSnapshots #{conf_file.gsub ' ', '\ '} 2>&1"
  output = `#{command}`

  if $?.exitstatus == 0
    snaps = output.split("\n").select { |s| !s.include? 'Total snapshots:' }
    snaps.map { |s| s.strip }
  else
    Fission.ui.output "There was an error getting the list of snapshots."
    Fission.ui.output_and_exit "The error was:\n#{output}", 1
  end
end

#start(args = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fission/vm.rb', line 46

def start(args={})
  command = "#{Fission.config.attributes['vmrun_cmd']} start #{conf_file.gsub ' ', '\ '} "

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

  output = `#{command}`

  if $?.exitstatus == 0
    Fission.ui.output "VM started"
  else
    Fission.ui.output "There was a problem starting the VM.  The error was:\n#{output}"
  end
end

#stopObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/fission/vm.rb', line 64

def stop
  command = "#{Fission.config.attributes['vmrun_cmd']} stop #{conf_file.gsub ' ', '\ '} 2>&1"
  output = `#{command}`

  if $?.exitstatus == 0
    Fission.ui.output "VM stopped"
  else
    Fission.ui.output "There was a problem stopping the VM.  The error was:\n#{output}"
  end
end

#suspendObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/fission/vm.rb', line 75

def suspend
  command = "#{Fission.config.attributes['vmrun_cmd']} suspend #{conf_file.gsub ' ', '\ '} 2>&1"
  output = `#{command}`

  if $?.exitstatus == 0
    Fission.ui.output "VM suspended"
  else
    Fission.ui.output "There was a problem suspending the VM.  The error was:\n#{output}"
  end
end