/
start.rb
69 lines (53 loc) · 1.82 KB
/
start.rb
1
2
3
4
5
6
7
8
9
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module Fission
class Command
class Start < Command
def initialize(args=[])
super
@options.headless = false
end
def execute
option_parser.parse! @args
if @args.empty?
Fission.ui.output self.class.help
Fission.ui.output ""
Fission.ui.output_and_exit "Incorrect arguments for start command", 1
end
vm_name = @args.first
vm = Fission::VM.new(vm_name)
unless vm.exists?
Fission.ui.output_and_exit "Unable to find the VM #{vm_name} (#{Fission::VM.path(vm_name)})", 1
end
if vm.running?
Fission.ui.output ''
Fission.ui.output_and_exit "VM '#{vm_name}' is already running", 0
end
Fission.ui.output "Starting '#{vm_name}'"
start_args = {}
if @options.headless
if Fission::Fusion.running?
Fission.ui.output 'It looks like the Fusion GUI is currently running'
Fission.ui.output 'A VM cannot be started in headless mode when the Fusion GUI is running'
Fission.ui.output_and_exit "Exit the Fusion GUI and try again", 1
else
start_args[:headless] = true
end
end
task = vm.start(start_args)
if task.successful?
Fission.ui.output "VM '#{vm_name}' started"
else
Fission.ui.output_and_exit "There was a problem starting the VM. The error was:\n#{task.output}", task.code
end
end
def option_parser
optparse = OptionParser.new do |opts|
opts.banner = "\nstart usage: fission start vm [options]"
opts.on '--headless', 'Start the VM in headless mode (i.e. no Fusion GUI console)' do
@options.headless = true
end
end
optparse
end
end
end
end