mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
vmfusion provider , requires the fission gem (pull request pending)
This commit is contained in:
parent
34f44a36a2
commit
1a576820ae
10 changed files with 360 additions and 3 deletions
|
@ -73,6 +73,7 @@ require 'fog/bin/stormondemand'
|
|||
require 'fog/bin/terremark'
|
||||
require 'fog/bin/vcloud'
|
||||
require 'fog/bin/virtual_box'
|
||||
require 'fog/bin/vmfusion'
|
||||
require 'fog/bin/vsphere'
|
||||
require 'fog/bin/voxel'
|
||||
require 'fog/bin/zerigo'
|
||||
|
|
|
@ -30,8 +30,8 @@ module Libvirt # deviates from other bin stuff to accomodate gem
|
|||
availability=false
|
||||
rescue
|
||||
availability_gem=Gem.available?("ruby-libvirt")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
if availability
|
||||
for service in services
|
||||
for collection in self.class_for(service).collections
|
||||
|
|
60
lib/fog/bin/vmfusion.rb
Normal file
60
lib/fog/bin/vmfusion.rb
Normal file
|
@ -0,0 +1,60 @@
|
|||
module Vmfusion # deviates from other bin stuff to accomodate gem
|
||||
class << self
|
||||
|
||||
def class_for(key)
|
||||
case key
|
||||
when :compute
|
||||
Fog::Compute::Vmfusion
|
||||
else
|
||||
raise ArgumentError, "Unrecognized service: #{key}"
|
||||
end
|
||||
end
|
||||
|
||||
def [](service)
|
||||
@@connections ||= Hash.new do |hash, key|
|
||||
hash[key] = case key
|
||||
when :compute
|
||||
Fog::Logger.warning("Vmfusion[:compute] is deprecated, use Compute[:Vmfusion] instead")
|
||||
Fog::Compute.new(:provider => 'Vmfusion')
|
||||
else
|
||||
raise ArgumentError, "Unrecognized service: #{key.inspect}"
|
||||
end
|
||||
end
|
||||
@@connections[service]
|
||||
end
|
||||
|
||||
def available?
|
||||
begin
|
||||
availability=true unless Gem::Specification::find_by_name("fission").nil?
|
||||
rescue Gem::LoadError
|
||||
availability=false
|
||||
rescue
|
||||
availability_gem=Gem.available?("fission")
|
||||
end
|
||||
|
||||
if availability
|
||||
for service in services
|
||||
for collection in self.class_for(service).collections
|
||||
unless self.respond_to?(collection)
|
||||
self.class_eval <<-EOS, __FILE__, __LINE__
|
||||
def self.#{collection}
|
||||
self[:#{service}].#{collection}
|
||||
end
|
||||
EOS
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
availability
|
||||
end
|
||||
|
||||
def collections
|
||||
services.map {|service| self[service].collections}.flatten.sort_by {|service| service.to_s}
|
||||
end
|
||||
|
||||
def services
|
||||
Fog::Vmfusion.services
|
||||
end
|
||||
|
||||
end
|
||||
end
|
|
@ -53,6 +53,9 @@ module Fog
|
|||
when :virtualbox
|
||||
require 'fog/virtual_box/compute'
|
||||
Fog::Compute::VirtualBox.new(attributes)
|
||||
when :vmfusion
|
||||
require 'fog/vmfusion/compute'
|
||||
Fog::Compute::Vmfusion.new(attributes)
|
||||
when :voxel
|
||||
require 'fog/voxel/compute'
|
||||
Fog::Compute::Voxel.new(attributes)
|
||||
|
|
|
@ -208,7 +208,6 @@ module Fog
|
|||
@raw.undefine
|
||||
end
|
||||
|
||||
|
||||
def reboot
|
||||
requires :raw
|
||||
@raw.reboot
|
||||
|
|
|
@ -18,6 +18,7 @@ require 'fog/slicehost'
|
|||
require 'fog/storm_on_demand'
|
||||
require 'fog/vcloud'
|
||||
require 'fog/virtual_box'
|
||||
require 'fog/vmfusion'
|
||||
require 'fog/vsphere'
|
||||
require 'fog/voxel'
|
||||
require 'fog/zerigo'
|
||||
|
|
11
lib/fog/vmfusion.rb
Normal file
11
lib/fog/vmfusion.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
require(File.expand_path(File.join(File.dirname(__FILE__), 'core')))
|
||||
|
||||
module Fog
|
||||
module Vmfusion
|
||||
|
||||
extend Fog::Provider
|
||||
|
||||
service(:compute, 'vmfusion/compute')
|
||||
|
||||
end
|
||||
end
|
29
lib/fog/vmfusion/compute.rb
Normal file
29
lib/fog/vmfusion/compute.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'vmfusion'))
|
||||
require 'fog/compute'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class Vmfusion < Fog::Service
|
||||
|
||||
model_path 'fog/vmfusion/models/compute'
|
||||
model :server
|
||||
collection :servers
|
||||
|
||||
class Mock
|
||||
|
||||
def initialize(options={})
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Real
|
||||
|
||||
def initialize(options={})
|
||||
require 'fission'
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
216
lib/fog/vmfusion/models/compute/server.rb
Normal file
216
lib/fog/vmfusion/models/compute/server.rb
Normal file
|
@ -0,0 +1,216 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class Vmfusion
|
||||
|
||||
class Server < Fog::Model
|
||||
|
||||
identity :name
|
||||
|
||||
attribute :name
|
||||
attribute :state
|
||||
|
||||
attr_accessor :password
|
||||
attr_writer :private_key, :private_key_path, :public_key, :public_key_path, :username
|
||||
|
||||
def initalize(attributes={})
|
||||
end
|
||||
|
||||
def save
|
||||
raise Fog::Errors::Error.new('Creating a new vm is not yet supported')
|
||||
end
|
||||
|
||||
def destroy(options={ :force => false})
|
||||
requires :raw
|
||||
|
||||
if state=="running"
|
||||
if options[:force]
|
||||
@raw.stop
|
||||
end
|
||||
end
|
||||
|
||||
::Fission::VM.delete target_vm
|
||||
end
|
||||
|
||||
def start
|
||||
requires :raw
|
||||
|
||||
unless state=="running"
|
||||
@raw.start
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def stop
|
||||
requires :raw
|
||||
|
||||
if state=="running"
|
||||
@raw.stop
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def reboot
|
||||
requires :raw
|
||||
if state=="running"
|
||||
@raw.stop
|
||||
wait_for { state!="running"}
|
||||
@raw.start
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def halt
|
||||
requires :raw
|
||||
if state=="running"
|
||||
@raw.halt
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def poweroff
|
||||
requires :raw
|
||||
halt
|
||||
end
|
||||
|
||||
def shutdown
|
||||
requires :raw
|
||||
stop
|
||||
end
|
||||
|
||||
def resume
|
||||
requires :raw
|
||||
@raw.resume
|
||||
end
|
||||
|
||||
def suspend
|
||||
requires :raw
|
||||
@raw.suspend
|
||||
end
|
||||
|
||||
def state
|
||||
requires :raw
|
||||
@raw.state
|
||||
end
|
||||
|
||||
def ready?
|
||||
state == "running"
|
||||
end
|
||||
|
||||
def private_ip_address
|
||||
ip_address(:private)
|
||||
end
|
||||
|
||||
def public_ip_address
|
||||
ip_address(:public)
|
||||
end
|
||||
|
||||
|
||||
def username
|
||||
@username ||= 'root'
|
||||
end
|
||||
|
||||
def ssh(commands)
|
||||
requires :public_ip_address, :username
|
||||
|
||||
#requires :password, :private_key
|
||||
ssh_options={}
|
||||
ssh_options[:password] = password unless password.nil?
|
||||
ssh_options[:key_data] = [private_key] if private_key
|
||||
|
||||
Fog::SSH.new(public_ip_address, @username, ssh_options).run(commands)
|
||||
|
||||
end
|
||||
|
||||
def scp(local_path, remote_path, upload_options = {})
|
||||
requires :public_ip_address, :username
|
||||
|
||||
scp_options = {}
|
||||
scp_options[:password] = password unless self.password.nil?
|
||||
scp_options[:key_data] = [private_key] if self.private_key
|
||||
|
||||
Fog::SCP.new(public_ip_address, username, scp_options).upload(local_path, remote_path, upload_options)
|
||||
end
|
||||
|
||||
# Sets up a new key
|
||||
def setup(credentials = {})
|
||||
requires :public_key, :public_ip_address, :username
|
||||
|
||||
credentials[:password] = password unless self.password.nil?
|
||||
credentails[:key_data] = [private_key] if self.private_key
|
||||
|
||||
commands = [
|
||||
%{mkdir .ssh},
|
||||
]
|
||||
if public_key
|
||||
commands << %{echo "#{public_key}" >> ~/.ssh/authorized_keys}
|
||||
end
|
||||
|
||||
# wait for domain to be ready
|
||||
Timeout::timeout(360) do
|
||||
begin
|
||||
Timeout::timeout(8) do
|
||||
Fog::SSH.new(public_ip_address, username, credentials.merge(:timeout => 4)).run('pwd')
|
||||
end
|
||||
rescue Errno::ECONNREFUSED
|
||||
sleep(2)
|
||||
retry
|
||||
rescue Net::SSH::AuthenticationFailed, Timeout::Error
|
||||
retry
|
||||
end
|
||||
end
|
||||
Fog::SSH.new(public_ip_address, username, credentials).run(commands)
|
||||
end
|
||||
|
||||
def private_key_path
|
||||
@private_key_path ||= Fog.credentials[:private_key_path]
|
||||
@private_key_path &&= File.expand_path(@private_key_path)
|
||||
end
|
||||
|
||||
def private_key
|
||||
@private_key ||= private_key_path && File.read(private_key_path)
|
||||
end
|
||||
|
||||
def public_key_path
|
||||
@public_key_path ||= Fog.credentials[:public_key_path]
|
||||
@public_key_path &&= File.expand_path(@public_key_path)
|
||||
end
|
||||
|
||||
def public_key
|
||||
@public_key ||= public_key_path && File.read(public_key_path)
|
||||
end
|
||||
|
||||
private
|
||||
def ip_address(key)
|
||||
@raw.ip_address
|
||||
end
|
||||
|
||||
def raw
|
||||
@raw
|
||||
end
|
||||
|
||||
def raw=(new_raw)
|
||||
@raw = new_raw
|
||||
|
||||
raw_attributes = {
|
||||
:name => new_raw.name,
|
||||
:state => new_raw.state
|
||||
}
|
||||
|
||||
merge_attributes(raw_attributes)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
37
lib/fog/vmfusion/models/compute/servers.rb
Normal file
37
lib/fog/vmfusion/models/compute/servers.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/vmfusion/models/compute/server'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class Vmfusion
|
||||
|
||||
class Servers < Fog::Collection
|
||||
|
||||
model Fog::Compute::Vmfusion::Server
|
||||
|
||||
def all(filter=nil)
|
||||
|
||||
data=[]
|
||||
|
||||
filter={} if filter.nil?
|
||||
unless filter.has_key?(:name)
|
||||
vm_names=::Fission::VM.all
|
||||
vm_names.each do |vm_name|
|
||||
data << { :raw => ::Fission::VM.new(vm_name)}
|
||||
end
|
||||
else
|
||||
data << { :raw => ::Fission::VM.new(filter[:name])}
|
||||
end
|
||||
|
||||
load(data)
|
||||
|
||||
end
|
||||
|
||||
def get(name)
|
||||
self.all(:name =>name).first
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue