Class: Fission::Action::VM::Stopper

Inherits:
Object
  • Object
show all
Defined in:
lib/fission/action/vm/stopper.rb

Instance Method Summary collapse

Constructor Details

#initialize(vm) ⇒ Stopper

Internal: Creates a new VMStopper object. This accepts a VM object.

vm - An instance of VM

Examples:

Fission::Action::VMStopper.new @my_vm

Returns a new VMStopper object



16
17
18
# File 'lib/fission/action/vm/stopper.rb', line 16

def initialize(vm)
  @vm = vm
end

Instance Method Details

#stop(options = {}) ⇒ Object

Public: Stops a VM. The VM must be running in order to stop it.

options - Hash of options:

:hard - Boolean which specifies to power off the VM (instead
        of attempting to initiate a graceful shutdown). This
        is the equivalent of passing 'hard' to the vmrun
        stop command.
        (default: false)

Examples

@stopper.stop

@stopper.stop :hard => true

Returns a Response with the result. If successful, the Response’s data attribute will be nil. If there is an error, an unsuccessful Response will be returned.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fission/action/vm/stopper.rb', line 38

def stop(options={})
  unless @vm.exists?
    return Response.new :code => 1, :message => 'VM does not exist'
  end

  running_response = @vm.running?
  return running_response unless running_response.successful?

  unless running_response.data
    return Response.new :code => 1, :message => 'VM is not running'
  end

  conf_file_response = @vm.conf_file
  return conf_file_response unless conf_file_response.successful?

  command = "#{vmrun_cmd} stop "
  command << "'#{conf_file_response.data}' "
  command << 'hard ' unless options[:hard].blank?
  command << '2>&1'

  command_exec = Fission::Action::ShellExecutor.new command
  Response.from_shell_executor command_exec.execute
end