forked from jedi4ever/veewee
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,458 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
module Fission | ||
class CLI | ||
def self.execute(args=ARGV) | ||
optparse = OptionParser.new do |opts| | ||
opts.banner = "\nUsage: fission [options] COMMAND [arguments]" | ||
|
||
opts.on_head('-v', '--version', 'Output the version of fission') do | ||
Fission.ui.output Fission::VERSION | ||
exit(0) | ||
end | ||
|
||
opts.on_head('-h', '--help', 'Displays this message') do | ||
show_all_help(optparse) | ||
exit(0) | ||
end | ||
|
||
opts.define_tail do | ||
commands_banner | ||
end | ||
|
||
end | ||
|
||
begin | ||
optparse.order! args | ||
rescue OptionParser::InvalidOption => e | ||
Fission.ui.output e | ||
show_all_help(optparse) | ||
exit(1) | ||
end | ||
|
||
if commands.include?(args.first) | ||
@cmd = Fission::Command.const_get(args.first.capitalize).new args.drop 1 | ||
elsif is_snapshot_command?(args) | ||
klass = args.take(2).map {|c| c.capitalize}.join('') | ||
@cmd = Fission::Command.const_get(klass).new args.drop 2 | ||
else | ||
show_all_help(optparse) | ||
exit(1) | ||
end | ||
|
||
begin | ||
@cmd.execute | ||
rescue Error => e | ||
puts "Error: #{e}" | ||
end | ||
end | ||
|
||
def self.commands | ||
cmds = Dir.entries(File.join(File.dirname(__FILE__), 'command')).select do |file| | ||
!File.directory? file | ||
end | ||
|
||
cmds.map { |cmd| File.basename(cmd, '.rb').gsub '_', ' ' } | ||
end | ||
|
||
private | ||
def self.is_snapshot_command?(args) | ||
args.first == 'snapshot' && args.count > 1 && commands.include?(args.take(2).join(' ')) | ||
end | ||
|
||
def self.commands_banner | ||
text = "\nCommands:\n" | ||
Fission::Command.descendants.each do |command_klass| | ||
text << (command_klass.send :help) | ||
end | ||
|
||
text | ||
end | ||
|
||
def self.show_all_help(options) | ||
Fission.ui.output options | ||
Fission.ui.output commands_banner | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Fission | ||
class Command | ||
attr_reader :options, :args | ||
|
||
def initialize(args=[]) | ||
@options = OpenStruct.new | ||
@args = args | ||
end | ||
|
||
def self.help | ||
self.new.option_parser.to_s | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
module Fission | ||
class Command | ||
class Clone < Command | ||
|
||
def initialize(args=[]) | ||
super | ||
@options.start = false | ||
end | ||
|
||
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 | ||
|
||
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 | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
module Fission | ||
class Command | ||
class Delete < Command | ||
|
||
def initialize(args=[]) | ||
super | ||
@options.force = false | ||
end | ||
|
||
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_name = @args.first | ||
target_vm=Fission::VM.new(target_vm_name) | ||
unless target_vm.exists? | ||
Fission.ui.output_and_exit "Vm #{target_vm_name} does not exist at (#{target_vm.path})", 1 | ||
end | ||
|
||
if target_vm.running? | ||
Fission.ui.output 'VM is currently running' | ||
if @options.force | ||
Fission.ui.output 'Going to stop it' | ||
Fission::Command::Stop.new([target_vm_name]).execute | ||
else | ||
Fission.ui.output_and_exit "Either stop/suspend the VM or use '--force' and try again.", 1 | ||
end | ||
end | ||
|
||
|
||
if Fission::Fusion.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 | ||
|
||
delete_task = Fission::VM.delete target_vm_name | ||
|
||
if delete_task.successful? | ||
Fission.ui.output '' | ||
Fission.ui.output "Deletion complete!" | ||
else | ||
Fission.ui.output_and_exit "There was an error deleting the VM. The error was:\n#{delete_task.output}", delete_task.code | ||
end | ||
end | ||
|
||
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 | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module Fission | ||
class Command | ||
class SnapshotCreate < Command | ||
|
||
def initialize(args=[]) | ||
super | ||
end | ||
|
||
def execute | ||
unless @args.count == 2 | ||
Fission.ui.output self.class.help | ||
Fission.ui.output "" | ||
Fission.ui.output_and_exit "Incorrect arguments for snapshot create command", 1 | ||
end | ||
|
||
vm_name, snap_name = @args.take 2 | ||
|
||
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 | ||
|
||
unless vm.running? | ||
Fission.ui.output "VM '#{vm_name}' is not running" | ||
Fission.ui.output_and_exit 'A snapshot cannot be created unless the VM is running', 1 | ||
end | ||
|
||
if vm.snapshots.include? snap_name | ||
Fission.ui.output_and_exit "VM '#{vm_name}' already has a snapshot named '#{snap_name}'", 1 | ||
end | ||
|
||
Fission.ui.output "Creating snapshot" | ||
task = vm.create_snapshot(snap_name) | ||
|
||
if task.successful? | ||
Fission.ui.output "Snapshot '#{snap_name}' created" | ||
else | ||
Fission.ui.output_and_exit "There was an error creating the snapshot. The error was:\n#{task.output}", task.code | ||
end | ||
end | ||
|
||
def option_parser | ||
optparse = OptionParser.new do |opts| | ||
opts.banner = "\nsnapshot create: fission snapshot create my_vm snapshot_1" | ||
end | ||
|
||
optparse | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module Fission | ||
class Command | ||
class SnapshotList < Command | ||
|
||
def initialize(args=[]) | ||
super | ||
end | ||
|
||
def execute | ||
unless @args.count == 1 | ||
Fission.ui.output self.class.help | ||
Fission.ui.output "" | ||
Fission.ui.output_and_exit "Incorrect arguments for snapshot list 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} (#{vm.path})", 1 | ||
end | ||
|
||
snaps=vm.snapshots | ||
unless snaps.empty? | ||
Fission.ui.output snaps.join("\n") | ||
else | ||
Fission.ui.output "No snapshots found for VM '#{vm_name}'" | ||
end | ||
|
||
# TODO | ||
Fission.ui.output_and_exit "There was an error listing the snapshots. The error was:\n#{task.output}", task.code | ||
end | ||
|
||
def option_parser | ||
optparse = OptionParser.new do |opts| | ||
opts.banner = "\nsnapshot list: fission snapshot list my_vm" | ||
end | ||
|
||
optparse | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module Fission | ||
class Command | ||
class SnapshotRevert < Command | ||
|
||
def initialize(args=[]) | ||
super | ||
end | ||
|
||
def execute | ||
unless @args.count == 2 | ||
Fission.ui.output self.class.help | ||
Fission.ui.output '' | ||
Fission.ui.output_and_exit 'Incorrect arguments for snapshot revert command', 1 | ||
end | ||
|
||
vm_name, snap_name = @args.take 2 | ||
vm = Fission::VM.new vm_name | ||
|
||
unless vm.exists? vm_name | ||
Fission.ui.output_and_exit "Unable to find the VM #{vm_name} (#{Fission::VM.path(vm_name)})", 1 | ||
end | ||
|
||
if Fission::Fusion.running? | ||
Fission.ui.output 'It looks like the Fusion GUI is currently running' | ||
Fission.ui.output_and_exit 'Please exit the Fusion GUI and try again', 1 | ||
end | ||
|
||
snaps = vm.snapshots | ||
|
||
unless snaps.include? snap_name | ||
Fission.ui.output_and_exit "Unable to find the snapshot '#{snap_name}'", 1 | ||
end | ||
|
||
Fission.ui.output "Reverting to snapshot '#{snap_name}'" | ||
task = vm.revert_to_snapshot snap_name | ||
|
||
if task.successful? | ||
Fission.ui.output "Reverted to snapshot '#{snap_name}'" | ||
else | ||
Fission.ui.output_and_exit "There was an error reverting to the snapshot. The error was:\n#{task.output}", task.code | ||
end | ||
end | ||
|
||
def option_parser | ||
optparse = OptionParser.new do |opts| | ||
opts.banner = "\nsnapshot revert: fission snapshot revert my_vm snapshot_1" | ||
end | ||
|
||
optparse | ||
end | ||
|
||
end | ||
end | ||
end |
Oops, something went wrong.