Class: Fission::Command::Delete

Inherits:
Fission::Command show all
Defined in:
lib/fission/command/delete.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 = []) ⇒ Delete

Returns a new instance of Delete.



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

def initialize(args=[])
  super
  @options.force = 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fission/command/delete.rb', line 10

def execute
  option_parser.parse! @args

  if @args.count < 1
    Fission.ui.output self.class.help
    Fission.ui.output ""
    Fission.ui.output_and_exit "Incorrect arguments for delete command", 1
  end

  target_vm = @args.first

  unless Fission::VM.exists? target_vm
    Fission.ui.output_and_exit "Unable to find target vm #{target_vm} (#{Fission::VM.path(target_vm)})", 1
  end

  if Fission::VM.all_running.include? target_vm
    Fission.ui.output 'VM is currently running'
    if @options.force
      Fission.ui.output 'Going to stop it'
      Fission::Command::Stop.new([target_vm]).execute
    else
      Fission.ui.output_and_exit "Either stop/suspend the VM or use '--force' and try again.", 1
    end
  end

  if Fission::Fusion.is_running?
    Fission.ui.output 'It looks like the Fusion GUI is currently running'
    if @options.force
      Fission.ui.output 'The Fusion metadata for the VM may not be removed completely'
    else
      Fission.ui.output "Either exit the Fusion GUI or use '--force' and try again"
      Fission.ui.output_and_exit "NOTE: Forcing a VM deletion with the Fusion GUI running may not clean up all of the VM metadata", 1
    end
  end

  Fission::VM.delete target_vm

  Fission.ui.output ''
  Fission.ui.output "Deletion complete!"
end

#option_parserObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fission/command/delete.rb', line 51

def option_parser
  optparse = OptionParser.new do |opts|
    opts.banner = "\ndelete usage: fission delete target_vm [--force]"

    opts.on '--force', "Stop the VM if it's running and then delete it" do
      @options.force = true
    end
  end

  optparse
end