Permalink
Browse files

working vmfusion 0.4 support, 1 monkey patch but now relying on plain…

… fission 0.4.0
  • Loading branch information...
1 parent 88a79f5 commit ac7964c530ff0d59d166c238655cf3e8182b98a3 @jedi4ever committed Aug 5, 2012
View
2 Gemfile
@@ -6,7 +6,7 @@
source "http://rubygems.org"
#gem "veewee", :path => "."
-gem "fission", :path => '/Users/patrick/dev/fission'
+#gem "fission", :path => '/Users/patrick/dev/fission'
group :test do
gem "rake"
View
4 doc/definition.md
@@ -7,6 +7,10 @@ To create a definition you use the 'define' subcommand:
veewee vbox define 'myubuntu' 'ubuntu-10.10-server-amd64'
+If you want to use an external repo for the definition you can specify a git-url
+
+ veewee vbox define 'myubuntu' 'git://github.com/jedi4ever/myubuntu'
+
## Modifying a definition
Definitions are stored under a directory 'definitions' relative to the current directory.
View
8 lib/fission.rb
@@ -5,9 +5,10 @@
$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
-require 'fission/error'
+#require 'fission/error'
require 'fission/cli'
require 'fission/command'
+require 'fission/command_helpers'
require 'fission/command/clone'
require 'fission/command/snapshot_create'
require 'fission/command/snapshot_list'
@@ -16,12 +17,12 @@
require 'fission/command/status'
require 'fission/command/stop'
require 'fission/command/suspend'
-require 'fission/command/delete'
require 'fission/config'
require 'fission/core_ext/class'
require 'fission/core_ext/file'
require 'fission/core_ext/object'
require 'fission/fusion'
+require 'fission/lease'
require 'fission/metadata'
require 'fission/response'
require 'fission/ui'
@@ -35,7 +36,4 @@ def config
@config ||= Fission::Config.new
end
- def ui
- @ui ||= Fission::UI.new
- end
end
View
76 lib/fission/cli.rb
@@ -1,76 +0,0 @@
-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
View
15 lib/fission/command.rb
@@ -1,15 +0,0 @@
-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
View
68 lib/fission/command/clone.rb
@@ -1,68 +0,0 @@
-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
View
71 lib/fission/command/delete.rb
@@ -1,71 +0,0 @@
-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
View
52 lib/fission/command/snapshot_create.rb
@@ -1,52 +0,0 @@
-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
View
45 lib/fission/command/snapshot_list.rb
@@ -1,45 +0,0 @@
-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
View
54 lib/fission/command/snapshot_revert.rb
@@ -1,54 +0,0 @@
-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
View
69 lib/fission/command/start.rb
@@ -1,69 +0,0 @@
-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
View
31 lib/fission/command/status.rb
@@ -1,31 +0,0 @@
-module Fission
- class Command
- class Status < Command
-
- def initialize(args=[])
- super
- end
-
- def execute
-
- all_vms=Fission::VM.all
- vm_with_longest_name = all_vms.max { |a,b| a.name.length <=> b.name.length }
- max_name_length=vm_with_longest_name.name.length
- all_vms.each do |vm|
- status = vm.state
- Fission.ui.output_printf "%-#{max_name_length}s %s\n", vm.name, "["+status+"]"
- end
-
- end
-
- def option_parser
- optparse = OptionParser.new do |opts|
- opts.banner = "\nstatus usage: fission status"
- end
-
- optparse
- end
-
- end
- end
-end
View
49 lib/fission/command/stop.rb
@@ -1,49 +0,0 @@
-module Fission
- class Command
- class Stop < 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 stop command", 1
- end
-
- vm_name = @args.first
- vm = Fission::VM.new vm_name
-
- unless vm.exists?
- Fission.ui.output_and_exit "VM #{vm_name} does not exist at (#{vm.path})", 1
- end
-
-
- unless vm.running?
- Fission.ui.output ''
- Fission.ui.output_and_exit "VM '#{vm_name}' is not running", 0
- end
-
- Fission.ui.output "Stopping '#{vm_name}'"
- task = vm.stop
-
- if task.successful?
- Fission.ui.output "VM '#{vm_name}' stopped"
- else
- Fission.ui.output_and_exit "There was an error stopping the VM. The error was:\n#{response.output}", response.code
- end
- end
-
- def option_parser
- optparse = OptionParser.new do |opts|
- opts.banner = "\nstop usage: fission stop vm"
- end
-
- optparse
- end
-
- end
- end
-end
View
67 lib/fission/command/suspend.rb
@@ -1,67 +0,0 @@
-module Fission
- class Command
- class Suspend < Command
-
- def initialize(args=[])
- super
- @options.all = false
- end
-
- 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
-
- 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
-
- 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
-
- end
- end
-end
View
57 lib/fission/config.rb
@@ -1,24 +1,71 @@
module Fission
class Config
+
+ # Public: Gets/Sets the Hash of attributes.
attr_accessor :attributes
+ # Public: Path to the Fission conf file (default: ~/.fissionrc).
CONF_FILE = File.expand_path '~/.fissionrc'
+ # Public: Initializes a Config object. This also sets the default config
+ # attributes for 'vmrun_bin', 'vmrun_cmd', 'vm_dir', 'plist_file', and
+ # 'gui_bin'.
+ #
+ # Examples
+ #
+ # Fission::Config.new
+ #
+ # Returns a new Config instance.
def initialize
@attributes = {}
- load_from_file
- if @attributes['vm_dir'].blank?
- @attributes['vm_dir'] = File.expand_path('~/Documents/Virtual Machines.localized/')
+ @attributes['vm_dir'] = File.expand_path('~/Documents/Virtual Machines.localized/')
+ @attributes['lease_file'] = '/var/db/vmware/vmnet-dhcpd-vmnet8.leases'
+
+ fusion_version = :unknown
+
+ if File.exists?("/Library/Application Support/VMware Fusion/vmrun")
+ @attributes['vmrun_bin'] = '/Library/Application Support/VMware Fusion/vmrun'
+ end
+
+ if File.exists?("/Applications/VMware Fusion.app/Contents/Library/vmrun")
+ @attributes['vmrun_bin'] = "/Applications/VMware Fusion.app/Contents/Library/vmrun"
+ end
+
+ if fusion_version == :unknown
end
- @attributes['vmrun_bin'] = '/Library/Application Support/VMware Fusion/vmrun'
- @attributes['vmrun_cmd'] = "#{@attributes['vmrun_bin'].gsub(' ', '\ ')} -T fusion"
@attributes['plist_file'] = File.expand_path('~/Library/Preferences/com.vmware.fusion.plist')
@attributes['gui_bin'] = File.expand_path('/Applications/VMware Fusion.app/Contents/MacOS/vmware')
+
+ load_from_file
+
+ @attributes['vmrun_cmd'] = "#{@attributes['vmrun_bin'].gsub(' ', '\ ')} -T fusion"
+ end
+
+ # Public: Helper method to access config atributes. This is a shortcut for
+ # querying the config attributes.
+ #
+ # item - The config item to query.
+ #
+ # Examples
+ #
+ # Fission.config['vmrun_bin']
+ # # => '/foo/bar/vmrun'
+ #
+ # Returns the value of the specified config item.
+ def [](item)
+ @attributes[item]
end
private
+ # Internal: Loads config values from the Fission conf file into attributes.
+ #
+ # Examples
+ #
+ # load_from_file
+ #
+ # Returns nothing.
def load_from_file
if File.file?(CONF_FILE)
@attributes.merge!(YAML.load_file(CONF_FILE))
View
5 lib/fission/core_ext/class.rb
@@ -1,5 +0,0 @@
-class Class
- def descendants
- ObjectSpace.each_object(Class).select { |klass| klass < self }
- end
-end
View
7 lib/fission/core_ext/file.rb
@@ -1,7 +0,0 @@
-class File
- # from ptools
- def self.binary?(file)
- s = (File.read(file, File.stat(file).blksize) || "").split(//)
- ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
- end
-end
View
112 lib/fission/core_ext/object.rb
@@ -1,112 +0,0 @@
-# this is from active_support
-# github.com/rails/rails/activesupport
-#
-class Object
- # An object is blank if it's false, empty, or a whitespace string.
- # For example, "", " ", +nil+, [], and {} are blank.
- #
- # This simplifies:
- #
- # if !address.nil? && !address.empty?
- #
- # ...to:
- #
- # if !address.blank?
- def blank?
- respond_to?(:empty?) ? empty? : !self
- end
-
- # An object is present if it's not <tt>blank?</tt>.
- def present?
- !blank?
- end
-
- # Returns object if it's <tt>present?</tt> otherwise returns +nil+.
- # <tt>object.presence</tt> is equivalent to <tt>object.present? ? object : nil</tt>.
- #
- # This is handy for any representation of objects where blank is the same
- # as not present at all. For example, this simplifies a common check for
- # HTTP POST/query parameters:
- #
- # state = params[:state] if params[:state].present?
- # country = params[:country] if params[:country].present?
- # region = state || country || 'US'
- #
- # ...becomes:
- #
- # region = params[:state].presence || params[:country].presence || 'US'
- def presence
- self if present?
- end
-end
-
-class NilClass
- # +nil+ is blank:
- #
- # nil.blank? # => true
- #
- def blank?
- true
- end
-end
-
-class FalseClass
- # +false+ is blank:
- #
- # false.blank? # => true
- #
- def blank?
- true
- end
-end
-
-class TrueClass
- # +true+ is not blank:
- #
- # true.blank? # => false
- #
- def blank?
- false
- end
-end
-
-class Array
- # An array is blank if it's empty:
- #
- # [].blank? # => true
- # [1,2,3].blank? # => false
- #
- alias_method :blank?, :empty?
-end
-
-class Hash
- # A hash is blank if it's empty:
- #
- # {}.blank? # => true
- # {:key => 'value'}.blank? # => false
- #
- alias_method :blank?, :empty?
-end
-
-class String
- # A string is blank if it's empty or contains whitespaces only:
- #
- # "".blank? # => true
- # " ".blank? # => true
- # " something here ".blank? # => false
- #
- def blank?
- self !~ /\S/
- end
-end
-
-class Numeric #:nodoc:
- # No number is blank:
- #
- # 1.blank? # => false
- # 0.blank? # => false
- #
- def blank?
- false
- end
-end
View
9 lib/fission/error.rb
@@ -1,9 +0,0 @@
-module Fission
- class Error < StandardError
- attr_reader :orginal
- def initialize(msg, original=$!)
- super(msg)
- @original = original; end
- end
-end
-
View
17 lib/fission/fusion.rb
@@ -1,17 +0,0 @@
-module Fission
- class Fusion
-
- def self.running?
- command = "ps -ef | grep -v grep | grep -c "
- command << "#{Fission.config.attributes['gui_bin'].gsub(' ', '\ ')} 2>&1"
- output = `#{command}`
-
- response = Fission::Response.new :code => 0
-
- output.strip.to_i > 0 ? response.data = true : response.data = false
-
- response
- end
-
- end
-end
View
74 lib/fission/leasesfile.rb
@@ -1,74 +0,0 @@
-require 'date'
-
-class Lease
- attr_accessor :name,:mac,:start,:end,:ip
-
- def initialize(name)
- @name=name
- @ip=name
- end
-
- def expired?
- @end < DateTime.now
- end
-end
-
-class LeasesFile
-
- attr_reader :leases
-
- def initialize(filename)
- @filename=filename
- @leases=Array.new
- load
- end
-
- def load
- @leases=Array.new
- File.open(@filename,"r") do |leasefile|
- lease=nil
- while (line = leasefile.gets)
-
- line=line.lstrip.gsub(';','')
- case line
- when /^lease/
- @leases << lease unless lease.nil?
- name=line.split(' ')[1]
- lease=Lease.new(name)
- when /^hardware/
- lease.mac=line.split(" ")[2]
- when /^starts/
- lease.start=DateTime.parse(line.split(" ")[2..3].join(" "))
- when /^ends/
- lease.end=DateTime.parse(line.split(" ")[2..3].join(" "))
- end
-
- end
- @leases << lease unless lease.nil?
- end
- return @leases
- end
-
- def all_leases
- return @leases
- end
-
- def current_leases
- hash_list=Hash.new
- @leases.each do |lease|
- hash_list[lease.name]=lease
- end
- collapsed_list=Array.new
- hash_list.each do |key,value|
- collapsed_list << value
- end
- return collapsed_list
- end
-
- def find_lease_by_mac(mac)
- matches=current_leases.select{|l| l.mac==mac}
- return nil if matches.nil?
- return matches[0]
- end
-
-end
View
39 lib/fission/metadata.rb
@@ -1,39 +0,0 @@
-module Fission
- class Metadata
-
- require 'cfpropertylist'
-
- attr_accessor :content
-
- def self.delete_vm_info(vm_path)
- metadata = new
- metadata.load
- metadata.delete_vm_restart_document(vm_path)
- metadata.delete_vm_favorite_entry(vm_path)
- metadata.save
- end
-
- def load
- raw_data = CFPropertyList::List.new :file => Fission.config.attributes['plist_file']
- @content = CFPropertyList.native_types raw_data.value
- end
-
- def save
- new_content = CFPropertyList::List.new
- new_content.value = CFPropertyList.guess @content
- new_content.save Fission.config.attributes['plist_file'],
- CFPropertyList::List::FORMAT_BINARY
- end
-
- def delete_vm_restart_document(vm_path)
- if @content.has_key?('PLRestartDocumentPaths')
- @content['PLRestartDocumentPaths'].delete_if { |p| p == vm_path }
- end
- end
-
- def delete_vm_favorite_entry(vm_path)
- @content['VMFavoritesListDefaults2'].delete_if { |vm| vm['path'] == vm_path }
- end
-
- end
-end
View
16 lib/fission/response.rb
@@ -1,16 +0,0 @@
-module Fission
- class Response
- attr_accessor :code, :output, :data
-
- def initialize(args={})
- @code = args.fetch :code, 1
- @output = args.fetch :output, ''
- @data = args.fetch :data, nil
- end
-
- def successful?
- @code == 0
- end
-
- end
-end
View
22 lib/fission/ui.rb
@@ -1,22 +0,0 @@
-module Fission
- class UI
- attr_reader :stdout
-
- def initialize(stdout=$stdout)
- @stdout = stdout
- end
-
- def output(s)
- @stdout.puts s
- end
-
- def output_printf(string, key, value)
- @stdout.send :printf, string, key, value
- end
-
- def output_and_exit(s, exit_code)
- output s
- exit exit_code
- end
- end
-end
View
3 lib/fission/version.rb
@@ -1,3 +0,0 @@
-module Fission
- VERSION = "0.4.0a"
-end
View
365 lib/fission/vm.rb
@@ -1,365 +0,0 @@
-require 'fission/leasesfile'
-require 'shellwords'
-require 'fission/error'
-
-module Fission
- class VM
- attr_reader :name
-
- def initialize(name)
- @name = name
- end
-
- #####################################################
- # Path Helpers
- #####################################################
- # Returns the topdir of the vm
- def path
- File.join Fission.config.attributes['vm_dir'], "#{@name}.vmwarevm"
- end
-
- # Returns a string to the path of the config file
- # There is no guarantee it exists
- def vmx_path
- return File.join(path, "#{@name}.vmx")
- end
-
-
- ####################################################################
- # State information
- ####################################################################
- def running?
- raise Fission::Error,"VM #{@name} does not exist" unless self.exists?
-
- command = "#{vmrun_cmd} list"
- output = `#{command}`
-
- response = Fission::Response.new :code => $?.exitstatus
-
- if response.successful?
- vms = output.split("\n").select do |vm|
- vm.include?('.vmx') && File.exists?(vm) && File.extname(vm) == '.vmx'
- end
- return vms.include?(self.vmx_path)
- else
- raise Fission::Error,"Error listing the state of vm #{@name}:\n#{output}"
- end
- end
-
- def suspended?
- raise Fission::Error,"VM #{@name} does not exist" unless self.exists?
-
- suspend_filename=File.join(File.dirname(vmx_path), File.basename(vmx_path,".vmx")+".vmem")
- return File.exists?(suspend_filename)
- end
-
- # Checks to see if a vm exists
- def exists?
- File.exists? vmx_path
- end
-
- # Returns the state of a vm
- def state
- return "not created" unless self.exists?
-
- return "suspend" if self.suspended?
-
- return "running" if self.running?
-
- return "not running"
- end
-
- ####################################################################
- # VM information
- ####################################################################
-
- # Returns an Array of snapshot names
- def snapshots
- raise Fission::Error,"VM #{@name} does not exist" unless self.exists?
-
- command = "#{vmrun_cmd} listSnapshots #{vmx_path.shellescape} 2>&1"
- output = `#{command}`
-
- raise "There was an error listing the snapshots of #{@name} :\n #{output}" unless $?.exitstatus==0
-
- snaps_unfiltered = output.split("\n").select { |s| !s.include? 'Total snapshots:' }
- snaps=snaps_unfiltered.map { |s| s.strip }
- return snaps
- end
-
- # Retrieve the first mac address for a vm
- # This will only retrieve the first auto generate mac address
- def mac_address
- raise ::Fission::Error,"VM #{@name} does not exist" unless self.exists?
-
- line=File.new(vmx_path).grep(/^ethernet0.generatedAddress =/)
- if line.nil?
- #Fission.ui.output "Hmm, the vmx file #{vmx_path} does not contain a generated mac address "
- return nil
- end
- address=line.first.split("=")[1].strip.split(/\"/)[1]
- return address
- end
-
- # Retrieve the ip address for a vm.
- # This will only look for dynamically assigned ip address via vmware dhcp
- def ip_address
- raise ::Fission::Error,"VM #{@name} does not exist" unless self.exists?
-
- unless mac_address.nil?
- lease=LeasesFile.new("/var/db/vmware/vmnet-dhcpd-vmnet8.leases").find_lease_by_mac(mac_address)
- if lease.nil?
- return nil
- else
- return lease.ip
- end
- else
- # No mac address was found for this machine so we can't calculate the ip-address
- return nil
- end
- end
-
- ####################################################################
- # VMS information
- ####################################################################
-
- # Returns an array of vm objects
- def self.all
- vm_dirs = Dir[File.join Fission.config.attributes['vm_dir'], '*.vmwarevm'].select do |d|
- File.directory? d
- end
-
- vm_names=vm_dirs.map { |d| File.basename d, '.vmwarevm' }
- vms=[]
- vm_names.each do |vmname|
- vm=Fission::VM.new vmname
- vms << vm
- end
-
- return vms
- end
-
- # Returns an array of vms that are running
- def self.all_running
- running_vms=self.all.select do |vm|
- vm.state=="running"
- end
- return running_vms
- end
-
- # Returns an existing vm
- def self.get(name)
- return Fission::VM.new(name)
- end
-
- #####################################################
- # VM Class Actions
- #####################################################
- def self.clone(source_vm, target_vm)
- raise Fission::Error,"VM #{source_vm} does not exist" unless Fission::VM.new(source_vm).exists?
- raise Fission::Error,"VM #{target_vm} already exists" if Fission::VM.new(target_vm).exists?
-
- FileUtils.cp_r Fission::VM.new(source_vm).path, Fission::VM.new(target_vm).path
-
- rename_vm_files source_vm, target_vm
- update_config source_vm, target_vm
-
- response = Response.new :code => 0
- end
-
- def self.delete(vm_name)
- raise Fission::Error,"VM #{vm_name} does not exist" unless Fission::VM.new(vm_name).exists?
-
- vm=Fission::VM.new(vm_name)
- FileUtils.rm_rf vm.path
- Fission::Metadata.delete_vm_info(vm.path)
-
- Response.new :code => 0
- end
-
-
- #####################################################
- # VM Instance Actions
- #####################################################
- def create_snapshot(name)
- raise Fission::Error,"VM #{@name} does not exist" unless self.exists?
-
- command = "#{vmrun_cmd} snapshot #{vmx_path.shellescape} \"#{name}\" 2>&1"
- output = `#{command}`
-
- response = Fission::Response.new :code => $?.exitstatus
- response.output = output unless response.successful?
-
- response
- end
-
- def start(args={})
- raise Fission::Error,"VM #{@name} does not exist" unless self.exists?
- raise Fission::Error,"VM #{@name} is already started" if self.running?
-
-
- command = "#{vmrun_cmd} start #{vmx_path.shellescape}"
-
- if !args[:headless].blank? && args[:headless]
- command << " nogui 2>&1"
- else
- command << " gui 2>&1"
- end
-
- output = `#{command}`
-
- response = Fission::Response.new :code => $?.exitstatus
- response.output = output unless response.successful?
-
- response
- end
-
- def stop
- raise Fission::Error,"VM #{@name} does not exist" unless self.exists?
- raise Fission::Error,"VM #{@name} is not running" unless self.running?
-
- command = "#{vmrun_cmd} stop #{vmx_path.shellescape} 2>&1"
- output = `#{command}`
-
- response = Fission::Response.new :code => $?.exitstatus
- response.output = output unless response.successful?
-
- response
- end
-
- def halt
- raise Fission::Error,"VM #{@name} does not exist" unless self.exists?
- raise Fission::Error,"VM #{@name} is not running" unless self.running?
-
- command = "#{vmrun_cmd} stop #{vmx_path.shellescape} hard 2>&1"
- output = `#{command}`
-
- response = Fission::Response.new :code => $?.exitstatus
- response.output = output unless response.successful?
-
- response
- end
-
- def resume
- raise Fission::Error,"VM #{@name} does not exist" unless self.exists?
- raise Fission::Error,"VM #{@name} is already running" if self.running?
- if self.suspended?
- self.start
- end
- end
-
- def suspend
- raise Fission::Error,"VM #{@name} does not exist" unless self.exists?
- raise Fission::Error,"VM #{@name} is not running" unless self.running?
-
- command = "#{vmrun_cmd} suspend #{vmx_path.shellescape} hard 2>&1"
- output = `#{command}`
-
- response = Fission::Response.new :code => $?.exitstatus
- response.output = output unless response.successful?
- response
- end
-
- # Action to revert to a snapshot
- # Returns a response object
- def revert_to_snapshot(name)
- raise Fission::Error,"VM #{@name} does not exist" unless self.exists?
-
- command = "#{vmrun_cmd} revertToSnapshot #{vmx_path.shellescape} \"#{name}\" 2>&1"
- output = `#{command}`
-
- response = Fission::Response.new :code => $?.exitstatus
- response.output = output unless response.successful?
-
- response
- end
-
- #####################################################
- # Helpers
- #####################################################
- private
- def self.rename_vm_files(from, to)
- to_vm=Fission::VM.new(to)
-
- files_to_rename(from, to).each do |filename|
- text_to_replace = File.basename(filename, File.extname(filename))
-
- if File.extname(filename) == '.vmdk'
- if filename.match /\-s\d+\.vmdk/
- text_to_replace = filename.partition(/\-s\d+.vmdk/).first
- end
- end
-
- unless File.exists?(File.join(to_vm.path, filename.gsub(text_to_replace, to)))
- FileUtils.mv File.join(to_vm.path, filename),
- File.join(to_vm.path, filename.gsub(text_to_replace, to))
- end
- end
- end
-
- def self.files_to_rename(from, to)
- files_which_match_source_vm = []
- other_files = []
-
- from_vm=Fission::VM.new(from)
-
- Dir.entries(from_vm.path).each do |f|
- unless f == '.' || f == '..'
- f.include?(from) ? files_which_match_source_vm << f : other_files << f
- end
- end
-
- files_which_match_source_vm + other_files
- end
-
- def self.vm_file_extensions
- ['.nvram', '.vmdk', '.vmem', '.vmsd', '.vmss', '.vmx', '.vmxf']
- end
-
- # This is done after a clone has been done
- # All files are already at the to location
- # The content of the text files will be substituted with strings from => to
- def self.update_config(from, to)
- to_vm=Fission::VM.new(to)
-
- ['.vmx', '.vmxf', '.vmdk'].each do |ext|
- file = File.join to_vm.path, "#{to}#{ext}"
-
- unless File.binary?(file)
- text = (File.read file).gsub from, to
- # Force binary mode to prevent windows from putting CR-LF end line style
- # http://www.ruby-forum.com/topic/60697#58748
- File.open(file, 'wb'){ |f| f.print text }
- end
-
- end
-
- # Rewrite vmx file to avoid messages
- new_vmx_file=File.open(File.join(to_vm.vmx_path),'r')
-
- content=new_vmx_file.read
-
- # Filter out other values
- content=content.gsub(/^tools.remindInstall.*\n/, "")
- content=content.gsub(/^uuid.action.*\n/,"").strip
-
- # Remove generate mac addresses
- content=content.gsub(/^ethernet.+generatedAddress.*\n/,"").strip
-
- # Add the correct values
- content=content+"\ntools.remindInstall = \"FALSE\"\n"
- content=content+"uuid.action = \"create\"\n"
-
- # Now rewrite the vmx file
- # Force binary mode to prevent windows from putting CR-LF end line style
- # http://www.ruby-forum.com/topic/60697#58748
- File.open(new_vmx_file,'wb'){ |f| f.print content}
-
- end
-
- def vmrun_cmd
- return Fission.config.attributes['vmrun_cmd']
- end
-
- end
-end
View
7 lib/veewee/definitions.rb
@@ -67,6 +67,7 @@ def define(definition_name,template_name,options = {})
if template_name.start_with?("git://")
git_template=true
end
+
# Check if template exists
template=env.templates[template_name]
if template.nil? and ! git_template
@@ -89,21 +90,21 @@ def define(definition_name,template_name,options = {})
end
end
- env.logger.debug("Creating definition #{definition_name} in directory '#{env.definition_dir}' ")
+ env.logger.info("Creating definition #{definition_name} in directory '#{env.definition_dir}' ")
dst_dir="#{File.join(env.definition_dir,definition_name)}"
FileUtils.mkdir(dst_dir)
env.logger.debug("Definition Directory '#{File.join(env.definition_dir,definition_name)}' succesfuly created")
# Start copying/cloning the directory of the template to the definition directory
if (git_template)
begin
- env.logger.debug("Starting git clone #{template_name} #{dst_dir}")
+ env.logger.info("Starting git clone #{template_name} #{dst_dir}")
g = Grit::Git.new(dst_dir)
g.clone({ :timeout => false }, template_name, dst_dir)
rescue Exception => ex
err = "git clone #{template_name} #{dst_dir} failed: #{ex}"
env.logger.fatal(err)
- raise Veewee::Error, err
+ raise Veewee::DefinitionError, err
end
else
begin
View
14 lib/veewee/provider/vmfusion/box.rb
@@ -33,23 +33,15 @@ class Box < Veewee::Provider::Core::Box
def initialize(name,env)
require 'fission'
-
super(name,env)
end
- def determine_vmrun_cmd
- return "#{fusion_path}/vmrun"
+ def vmrun_cmd
+ return ::Fission.config['vmrun_bin']
end
def vm_path
- home=ENV['HOME']
- dir="#{home}/Documents/Virtual Machines.localized/#{name}.vmwarevm"
- return dir
- end
-
- def fusion_path
- dir="/Library/Application Support/VMware Fusion/"
- return dir
+ return File.join(::Fission.config['vm_dir'], "#{name}.vmwarevm")
end
def vmx_file_path
View
2 lib/veewee/provider/vmfusion/box/create.rb
@@ -27,7 +27,7 @@ def create_disk
current_dir=FileUtils.pwd
FileUtils.chdir(vm_path)
env.ui.info "Creating disk"
- command="#{fusion_path.shellescape}/vmware-vdiskmanager -c -s #{definition.disk_size}M -a lsilogic -t #{disk_type} #{name}.vmdk"
+ command="#{File.dirname(vmrun_cmd).shellescape}/vmware-vdiskmanager -c -s #{definition.disk_size}M -a lsilogic -t #{disk_type} #{name}.vmdk"
shell_results=shell_exec("#{command}",{:mute => true})
FileUtils.chdir(current_dir)
end
View
2 lib/veewee/provider/vmfusion/box/destroy.rb
@@ -9,7 +9,7 @@ def destroy(options={})
end
raw.halt if raw.state=="running"
- ::Fission::VM.delete(name)
+ ::Fission::VM.new(name).delete
# remove it from memory
@raw=nil
end
View
2 lib/veewee/provider/vmfusion/box/export_ova.rb
@@ -41,7 +41,7 @@ def export_ova(options)
# before exporting the system needs to be shut down
# otherwise the debug log will show - The specified virtual disk needs repair
- shell_exec("#{fusion_path.shellescape}/ovftool/ovftool.bin #{debug} #{flags} #{vmx_file_path.shellescape} #{name}.ova")
+ shell_exec("#{File.dirname(vmrun_cmd).shellescape}/ovftool/ovftool.bin #{debug} #{flags} #{vmx_file_path.shellescape} #{name}.ova")
end
end
end
View
24 lib/veewee/provider/vmfusion/box/helper/buildinfo.rb
@@ -5,22 +5,30 @@ module BoxCommand
def build_info
info=super
- command="/Library/Application Support/VMware Fusion/vmrun"
- output=IO.popen("#{command.shellescape}").readlines
+ output=IO.popen("#{vmrun_cmd.shellescape}").readlines
info << {:filename => ".vmfusion_version",:content => output[1].split(/ /)[2..3].join.strip}
+ end
+
+
+ def guest_iso_directory
+ # use vmware fusion 3.x as default path
+ iso_images_dir="/Library/Application Support/VMware Fusion/isoimages"
+ # if path doesn't exist check for vmware fusion 4.x path
+ if( ! File.exists?(iso_images_dir) )
+ iso_images_dir="/Applications/VMware Fusion.app/Contents/Library/isoimages"
+ end
+ return iso_images_dir
end
# Determine the iso of the guest additions
def guest_iso_path
# So we begin by transferring the ISO file of the vmware tools
-
- iso_image="/Library/Application Support/VMware Fusion/isoimages/linux.iso"
- iso_image="/Library/Application Support/VMware Fusion/isoimages/darwin.iso" if definition.os_type_id=~/^Darwin/
- iso_image="/Library/Application Support/VMware Fusion/isoimages/freebsd.iso" if definition.os_type_id=~/^Free/
- iso_image="/Library/Application Support/VMware Fusion/isoimages/windows.iso" if definition.os_type_id=~/^Win/
+ iso_image=File.join(guest_iso_directory, "linux.iso")
+ iso_image=File.join(guest_iso_directory, "darwin.iso") if definition.os_type_id=~/^Darwin/
+ iso_image=File.join(guest_iso_directory, "freebsd.iso") if definition.os_type_id=~/^Free/
+ iso_image=File.join(guest_iso_directory, "windows.iso") if definition.os_type_id=~/^Win/
return iso_image
-
end
# Transfer information provide by the provider to the box
View
51 lib/veewee/provider/vmfusion/box/helper/ip.rb
@@ -3,14 +3,59 @@ module Provider
module Vmfusion
module BoxCommand
- # Get the IP address of the box
+ # Retrieve the first mac address for a vm
+ # This will only retrieve the first auto generate mac address
+ def mac_address
+ raise ::Fission::Error,"VM #{name} does not exist" unless self.exists?
+
+ line=File.new(vmx_file_path).grep(/^ethernet0.generatedAddress =/)
+ if line.nil?
+ #Fission.ui.output "Hmm, the vmx file #{vmx_path} does not contain a generated mac address "
+ return nil
+ end
+ address=line.first.split("=")[1].strip.split(/\"/)[1]
+ return address
+ end
+
+ # Retrieve the ip address for a vm.
+ # This will only look for dynamically assigned ip address via vmware dhcp
def ip_address
- return raw.ip_address
+ # Does not work for now as the vmx path is not escape correctly by fission 0.4.0
+ #return raw.network_info.data.first['ip_address']
+ raise ::Fission::Error,"VM #{name} does not exist" unless self.exists?
+
+ unless mac_address.nil?
+ lease = Fission::Lease.all.data.find { |l| l.mac_address=mac_address}
+ return lease.ip_address unless lease.nil?
+ return nil
+ else
+ # No mac address was found for this machine so we can't calculate the ip-address
+ return nil
+ end
end
# http://www.thirdbit.net/articles/2008/03/04/dhcp-on-vmware-fusion/
def host_ip_as_seen_by_guest
- File.open("/Library/Application Support/VMware Fusion/vmnet8/nat.conf").readlines.grep(/ip = /).first.split(" ")[2]
+
+ # if File.exists?("/Library/Application Support/VMware Fusion/vmnet8/nat.conf")
+ # file = "/Library/Application Support/VMware Fusion/vmnet8/nat.conf"
+ # end
+
+ # if File.exists?("/Library/Preferences/VMware Fusion/vmnet8/nat.conf")
+ # file = "/Library/Preferences/VMware Fusion/vmnet8/nat.conf"
+ # end
+ # File.open(file).readlines.grep(/ip = /).first.split(" ")[2]
+
+ # The above is not always correct
+ # There seems also an entry for vmnet8 in the dhcpd.conf
+ # /Library/Preferences/VMware Fusion/vmnet8/dhcpd.conf
+ # host vmnet8 {
+ # fixed-address
+
+ # The above is fancy but doesn't always agree, we need to do is ifconfig vmnet8
+ # Ifconfig never lies
+ shell_results = shell_exec("ifconfig vmnet8", { :mute => true})
+ shell_results.stdout.split(/\n/).grep(/inet /)[0].strip.split(/ /)[1]
end
end
View
4 lib/veewee/provider/vmfusion/box/helper/vnc.rb
@@ -4,7 +4,7 @@ module Vmfusion
module BoxCommand
def vnc_port
- lines=File.readlines(raw.vmx_path)
+ lines=File.readlines(vmx_file_path)
matches=lines.grep(/^RemoteDisplay.vnc.port/)
if matches.length==0
raise Veewee::Error,"No VNC port found, maybe it is not enabled?"
@@ -42,7 +42,7 @@ def vnc_display_port
end
def vnc_enabled?
- lines=File.readlines(raw.vmx_path)
+ lines=File.readlines(vmx_file_path)
matches=lines.grep(/^RemoteDisplay.vnc.enabled/)
if matches.length==0
return false
View
3 lib/veewee/provider/vmfusion/provider.rb
@@ -10,12 +10,15 @@ class Provider < Veewee::Provider::Core::Provider
def check_requirements
fusion_version = :unknown
+ require 'fission'
if File.exists?("/Library/Application Support/VMware Fusion/vmrun")
fusion_version = "3.x"
+ ::Fission.config.attributes["vmrun_bin"] = "/Library/Application Support/VMware Fusion/vmrun"
end
if File.exists?("/Applications/VMware Fusion.app/Contents/Library/vmrun")
fusion_version = "4.x"
+ ::Fission.config.attributes["vmrun_bin"] = "/Applications/VMware Fusion.app/Contents/Library/vmrun"
end
if fusion_version == :unknown
View
1 veewee.gemspec
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
s.add_dependency "fog", "~> 1.4"
s.add_dependency "childprocess"
s.add_dependency "grit"
+ s.add_dependency "fission", "0.4.0"
# Modified dependency version, as libxml-ruby dependency has been removed in version 2.1.1
# See : https://github.com/ckruse/CFPropertyList/issues/14

0 comments on commit ac7964c

Please sign in to comment.