Class: Fission::Command::Clone

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

Returns a new instance of Clone


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

def initialize(args=[])
  super
  @options.start = 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
50
51
52
# File 'lib/fission.old/command/clone.rb', line 10

def execute
  option_parser.parse! @args

  unless @args.count > 1
    Fission.ui.output self.class.help
    Fission.ui.output ""
    Fission.ui.output_and_exit "Incorrect arguments for clone command", 1
  end

  source_vm_name = @args.first
  target_vm_name = @args[1]
  source_vm=Fission::VM.new(source_vm_name)
  target_vm=Fission::VM.new(target_vm_name)

  unless source_vm.exists?
      Fission.ui.output_and_exit "Unable to find the source vm #{source_vm_name} (#{source_vm.path})", 1
  end

  if target_vm.exists?
      Fission::ui.output_and_exit "The target vm #{target_vm_name} already exists", 1
  end

  clone_task = Fission::VM.clone source_vm_name, target_vm_name

  if clone_task.successful?
    Fission.ui.output ''
    Fission.ui.output 'Clone complete!'

    if @options.start
      Fission.ui.output "Starting '#{target_vm_name}'"

      start_task = target_vm.start

      if start_task.successful?
        Fission.ui.output "VM '#{target_vm_name}' started"
      else
        Fission.ui.output_and_exit "There was an error starting the VM.  The error was:\n#{start_task.output}", start_task.code
      end
    end
  else
    Fission.ui.output_and_exit "There was an error cloning the VM.  The error was:\n#{clone_task.output}", clone_task.code
  end
end

#option_parserObject


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

def option_parser
  optparse = OptionParser.new do |opts|
    opts.banner = "\nclone usage: fission clone source_vm target_vm [options]"

    opts.on '--start', 'Start the VM after cloning' do
      @options.start = true
    end
  end

  optparse
end