class Fission::Action::VM::Lister

def all

If there is an error, an unsuccessful Response will be returned.
empty Array.
objects. If no VMs are found, the Response's data attribute will be an
If successful, the Response's data attribute will be an Array of VM
Returns a Response with the result.

]
# => [,
Fission::VM.all.data

Examples

Public: Provides all of the VMs which are located in the VM directory.
def all
  vm_dirs = Dir[File.join Fission.config['vm_dir'], '*.vmwarevm'].select do |d|
    File.directory? d
  end
  response = Response.new :code => 0
  response.data = vm_dirs.collect do |dir|
    Fission::VM.new(File.basename dir, '.vmwarevm')
  end
  response
end

def all_running

If there is an error, an unsuccessful Response will be returned.
attribute will be an empty Array.
objects which are running. If no VMs are running, the Response's data
If successful, the Response's data attribute will be an Array of VM
Returns a Response with the result.

]
# => [,
Fission::VM.all_running.data

Examples

Public: Provides all of the VMs which are currently running.
def all_running
  command = "#{Fission.config['vmrun_cmd']} list"
  command_executor = Fission::Action::ShellExecutor.new command
  result = command_executor.execute
  response = Response.new :code => result['process_status'].exitstatus
  if response.successful?
    response.data = get_vm_objects_from_list_output result['output']
  else
    response.message = result['output']
  end
  response
end

def all_with_status

If there is an error, an unsuccessful Repsonse will be returned.
names as keys and their status as the values.
If successful, the Response's data attribute will be a Hash of with the VM
Returns a Response with the result.

# => { 'vm1' => 'running', 'vm2' => 'suspended', 'vm3' => 'not running'}
Fission::VM.all_with_status.data

Examples

Public: Provides a list of all of the VMs and their associated status
def all_with_status
  all_response = all
  return all_response unless all_response.successful?
  all_vms = all_response.data
  running_response = all_running
  return running_response unless running_response.successful?
  response = Response.new :code => 0
  @all_running_vm_names = running_response.data.collect { |v| v.name }
  response.data = all_vms.inject({}) do |result, vm|
    result[vm.name] = determine_status vm
    result
  end
  response
end

def determine_status(vm)

Returns a String of the status.

# => 'suspended'
@lister.determine_status my_vm

Examples:

vm - The VM object.

Internal: Helper to determines the status of a VM.
def determine_status(vm)
  return 'running' if @all_running_vm_names.include? vm.name
  return 'suspended' if vm.suspend_file_exists?
  return 'not running'
end

def get_vm_objects_from_list_output(output)

Returns an Array of VM objects.

]
# => [,
@lister.get_vm_objects_from_list_output my_output

Examples:

output - output from the list command.

Internal: Helper to get VM objects from the output of running VMs.
def get_vm_objects_from_list_output(output)
  vms = output.split("\n").select do |vm|
    vm.include?('.vmx') &&
    File.exists?(vm) &&
    File.extname(vm) == '.vmx'
  end
  vms.collect do |vm|
    Fission::VM.new File.basename(File.dirname(vm), '.vmwarevm')
  end
end