Class: Fission::Command::Suspend

Inherits:
Fission::Command show all
Defined in:
lib/fission.old/command/suspend.rb

Instance Attribute Summary

Attributes inherited from Fission::Command

#args, #options

Instance Method Summary collapse

Methods inherited from Fission::Command

help

Constructor Details

#initialize(args = []) ⇒ Suspend

Returns a new instance of Suspend


5
6
7
8
# File 'lib/fission.old/command/suspend.rb', line 5

def initialize(args=[])
  super
  @options.all = false
end

Instance Method Details

#executeObject


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fission.old/command/suspend.rb', line 10

def execute
  option_parser.parse! @args

  if @args.count != 1 && !@options.all
    Fission.ui.output self.class.help
    Fission.ui.output ""
    Fission.ui.output_and_exit "Incorrect arguments for suspend command", 1
  end

  vms_to_suspend.each do |vm|
    Fission.ui.output "Suspending '#{vm.name}'"
    task = vm.suspend

    if task.successful?
      Fission.ui.output "VM '#{vm.name}' suspended"
    else
      Fission.ui.output_and_exit "There was an error suspending the VM.  The error was:\n#{task.output}", task.code
    end
  end
end

#option_parserObject


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fission.old/command/suspend.rb', line 53

def option_parser
  optparse = OptionParser.new do |opts|
    opts.banner = "\nsuspend usage: fission suspend [vm | --all]"

    opts.on '--all', 'Suspend all running VMs' do
      @options.all = true
    end
  end

  optparse
end

#vms_to_suspendObject


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

def vms_to_suspend
  if @options.all
    vms=Fission::VM.all_running
  else
    vm_name = @args.first
    vm=Fission::VM.new(vm_name)

    unless vm.exists?
      Fission.ui.output ''
      Fission.ui.output_and_exit "VM #{vm_name} does not exist (#{Fission::VM.path(vm_name)})", 1
    end

    unless vm.running?
      Fission.ui.output ''
      Fission.ui.output_and_exit "VM '#{vm_name}' is not running", 1
    end

    vms = [vm]
  end
  vms
end