mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Adds Supprt for oVirt (http://ovirt.org).
Signed-off-by: Ohad Levy <ohadlevy@gmail.com>
This commit is contained in:
parent
4af8b390f4
commit
ae29d968c6
15 changed files with 420 additions and 0 deletions
|
@ -47,6 +47,9 @@ module Fog
|
|||
when :openstack
|
||||
require 'fog/openstack/compute'
|
||||
Fog::Compute::OpenStack.new(attributes)
|
||||
when :ovirt
|
||||
require 'fog/ovirt/compute'
|
||||
Fog::Compute::Ovirt.new(attributes)
|
||||
when :rackspace
|
||||
require 'fog/rackspace/compute'
|
||||
Fog::Compute::Rackspace.new(attributes)
|
||||
|
|
17
lib/fog/ovirt.rb
Normal file
17
lib/fog/ovirt.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
require 'fog/core'
|
||||
|
||||
module Fog
|
||||
module Ovirt
|
||||
|
||||
extend Fog::Provider
|
||||
|
||||
module Errors
|
||||
class ServiceError < Fog::Errors::Error; end
|
||||
class SecurityError < ServiceError; end
|
||||
class NotFound < ServiceError; end
|
||||
end
|
||||
|
||||
service(:compute, 'ovirt/compute', 'Compute')
|
||||
|
||||
end
|
||||
end
|
52
lib/fog/ovirt/compute.rb
Normal file
52
lib/fog/ovirt/compute.rb
Normal file
|
@ -0,0 +1,52 @@
|
|||
|
||||
module Fog
|
||||
module Compute
|
||||
class Ovirt < Fog::Service
|
||||
|
||||
requires :ovirt_username, :ovirt_password
|
||||
recognizes :ovirt_url, :ovirt_server, :ovirt_port, :ovirt_api_path, :ovirt_datacenter
|
||||
|
||||
model_path 'fog/ovirt/models/compute'
|
||||
model :server
|
||||
collection :servers
|
||||
model :template
|
||||
collection :templates
|
||||
model :cluster
|
||||
collection :clusters
|
||||
|
||||
request_path 'fog/ovirt/requests/compute'
|
||||
|
||||
request :vm_action
|
||||
request :destroy_vm
|
||||
request :datacenters
|
||||
request :storage_domains
|
||||
|
||||
class Mock
|
||||
|
||||
def initialize(options={})
|
||||
username = options[:ovirt_username]
|
||||
password = options[:password]
|
||||
url = options[:ovirt_url]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Real
|
||||
attr_reader :client
|
||||
|
||||
def initialize(options={})
|
||||
require 'rbovirt'
|
||||
username = options[:ovirt_username]
|
||||
password = options[:ovirt_password]
|
||||
server = options[:ovirt_server]
|
||||
port = options[:ovirt_port] || 8080
|
||||
api_path = options[:ovirt_api_path] || '/api'
|
||||
url = options[:ovirt_url] || "#{@scheme}://#{@ovirt_server}:#{@ovirt_port}#{@ovirt_api_path}"
|
||||
datacenter = options[:ovirt_datacenter]
|
||||
|
||||
@client = OVIRT::Client.new(username, password, url, datacenter)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
26
lib/fog/ovirt/models/compute/cluster.rb
Normal file
26
lib/fog/ovirt/models/compute/cluster.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
require 'fog/compute/models/server'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class Ovirt
|
||||
|
||||
class Cluster < Fog::Model
|
||||
|
||||
identity :id
|
||||
|
||||
attribute :name
|
||||
attribute :raw
|
||||
|
||||
def networks
|
||||
connection.client.networks(:cluster_id => id)
|
||||
end
|
||||
|
||||
def to_s
|
||||
name
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
26
lib/fog/ovirt/models/compute/clusters.rb
Normal file
26
lib/fog/ovirt/models/compute/clusters.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/ovirt/models/compute/cluster'
|
||||
require 'fog/ovirt/models/compute/helpers/collection_helper'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class Ovirt
|
||||
|
||||
class Clusters < Fog::Collection
|
||||
|
||||
include Fog::Compute::Ovirt::Helpers::CollectionHelper
|
||||
model Fog::Compute::Ovirt::Cluster
|
||||
|
||||
def all(filters = {})
|
||||
attrs = connection.client.clusters(filters).map { |cluster| ovirt_attrs(cluster) }
|
||||
load attrs
|
||||
end
|
||||
|
||||
def get(id)
|
||||
new ovirt_attrs(connection.client.cluster(id))
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
32
lib/fog/ovirt/models/compute/helpers/collection_helper.rb
Normal file
32
lib/fog/ovirt/models/compute/helpers/collection_helper.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Ovirt
|
||||
module Helpers
|
||||
module CollectionHelper
|
||||
|
||||
# converts an OVIRT object into an hash for fog to consume.
|
||||
def ovirt_attrs obj
|
||||
opts = {:raw => obj}
|
||||
obj.instance_variables.each do |v|
|
||||
key = v.gsub("@","").to_sym
|
||||
value = obj.instance_variable_get(v)
|
||||
#ignore nil values
|
||||
next if value.nil?
|
||||
|
||||
opts[key] = case value.class
|
||||
when OVIRT::Link
|
||||
value.id
|
||||
when Hash
|
||||
value
|
||||
else
|
||||
value.to_s.strip
|
||||
end
|
||||
end
|
||||
opts
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
76
lib/fog/ovirt/models/compute/server.rb
Normal file
76
lib/fog/ovirt/models/compute/server.rb
Normal file
|
@ -0,0 +1,76 @@
|
|||
require 'fog/compute/models/server'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class Ovirt
|
||||
|
||||
class Server < Fog::Compute::Server
|
||||
|
||||
# This will be the instance uuid which is globally unique across
|
||||
# a oVirt deployment.
|
||||
identity :id
|
||||
|
||||
attribute :name
|
||||
attribute :description
|
||||
attribute :profile
|
||||
attribute :display
|
||||
attribute :storage, :aliases => 'disk_size'
|
||||
attribute :creation_time
|
||||
attribute :os
|
||||
attribute :ip
|
||||
attribute :status
|
||||
attribute :cores, :aliases => 'cpus'
|
||||
attribute :memory
|
||||
attribute :host
|
||||
attribute :cluster
|
||||
attribute :template
|
||||
attribute :raw
|
||||
|
||||
def ready?
|
||||
!(status =~ /down/i)
|
||||
end
|
||||
|
||||
def stopped?
|
||||
!!(status =~ /down/i)
|
||||
end
|
||||
|
||||
def mac
|
||||
raw.interfaces.first.mac if raw.interfaces
|
||||
end
|
||||
|
||||
def start(options = {})
|
||||
connection.client.vm_action(id, :start)
|
||||
reload
|
||||
end
|
||||
|
||||
def stop(options = {})
|
||||
connection.client.vm_action(id, :stop)
|
||||
reload
|
||||
end
|
||||
|
||||
def reboot(options = {})
|
||||
connection.client.vm_action(id, :reboot)
|
||||
reload
|
||||
end
|
||||
|
||||
def destroy(options = {})
|
||||
stop unless stopped?
|
||||
wait_for { stopped? }
|
||||
connection.client.destroy_vm(id)
|
||||
end
|
||||
|
||||
def save
|
||||
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
|
||||
self.id = connection.client.create_vm(attributes).id
|
||||
reload
|
||||
end
|
||||
|
||||
def to_s
|
||||
name
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
33
lib/fog/ovirt/models/compute/servers.rb
Normal file
33
lib/fog/ovirt/models/compute/servers.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/ovirt/models/compute/server'
|
||||
require 'fog/ovirt/models/compute/helpers/collection_helper'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class Ovirt
|
||||
|
||||
class Servers < Fog::Collection
|
||||
|
||||
include Fog::Compute::Ovirt::Helpers::CollectionHelper
|
||||
model Fog::Compute::Ovirt::Server
|
||||
|
||||
def all(filters = {})
|
||||
attrs = connection.client.vms(filters).map { |server| ovirt_attrs(server) }
|
||||
load attrs
|
||||
end
|
||||
|
||||
def get(id)
|
||||
new ovirt_attrs(connection.client.vm(id))
|
||||
end
|
||||
|
||||
def bootstrap(new_attributes = {})
|
||||
server = create(new_attributes)
|
||||
server.wait_for { stopped? }
|
||||
server.start
|
||||
server
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
42
lib/fog/ovirt/models/compute/template.rb
Normal file
42
lib/fog/ovirt/models/compute/template.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Ovirt
|
||||
|
||||
class Template < Fog::Model
|
||||
|
||||
identity :id
|
||||
|
||||
attribute :name
|
||||
attribute :description
|
||||
attribute :profile
|
||||
attribute :display
|
||||
attribute :storage, :aliases => 'disk_size'
|
||||
attribute :creation_time
|
||||
attribute :os
|
||||
attribute :status
|
||||
attribute :cores, :aliases => 'cpus'
|
||||
attribute :memory
|
||||
attribute :cluster
|
||||
|
||||
def ready?
|
||||
!(status =~ /down/i)
|
||||
end
|
||||
|
||||
def destroy(options = {})
|
||||
connection.client.destroy_template(id)
|
||||
end
|
||||
|
||||
def save
|
||||
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
|
||||
connection.client.create_template(attributes)
|
||||
end
|
||||
|
||||
def to_s
|
||||
name
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
26
lib/fog/ovirt/models/compute/templates.rb
Normal file
26
lib/fog/ovirt/models/compute/templates.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/ovirt/models/compute/template'
|
||||
require 'fog/ovirt/models/compute/helpers/collection_helper'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class Ovirt
|
||||
|
||||
class Templates < Fog::Collection
|
||||
|
||||
include Fog::Compute::Ovirt::Helpers::CollectionHelper
|
||||
model Fog::Compute::Ovirt::Template
|
||||
|
||||
def all(filters = {})
|
||||
attrs = connection.client.templates(filters).map { |template| ovirt_attrs(template) }
|
||||
load attrs
|
||||
end
|
||||
|
||||
def get(id)
|
||||
new ovirt_attrs(connection.client.template(id))
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
19
lib/fog/ovirt/requests/compute/datacenters.rb
Normal file
19
lib/fog/ovirt/requests/compute/datacenters.rb
Normal file
|
@ -0,0 +1,19 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Ovirt
|
||||
class Real
|
||||
|
||||
def datacenters filter={}
|
||||
client.datacenters(filter)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
def datacenters filter={}
|
||||
[ "Solutions", "Solutions2", "Solutions3" ]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
22
lib/fog/ovirt/requests/compute/destroy_vm.rb
Normal file
22
lib/fog/ovirt/requests/compute/destroy_vm.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Ovirt
|
||||
class Real
|
||||
|
||||
def destroy_vm(options = {})
|
||||
raise ArgumentError, "instance id is a required parameter" unless options.has_key? :id
|
||||
client.destroy_vm(options[:id])
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
def destroy_vm(options = {})
|
||||
raise ArgumentError, "instance id is a required parameter" unless options.has_key? :id
|
||||
true
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
19
lib/fog/ovirt/requests/compute/storage_domains.rb
Normal file
19
lib/fog/ovirt/requests/compute/storage_domains.rb
Normal file
|
@ -0,0 +1,19 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Ovirt
|
||||
class Real
|
||||
|
||||
def storage_domains filter={}
|
||||
client.storage_domains(filter)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
def storage_domains filter={}
|
||||
[ "Storage", "Storage2", "Storage3" ]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
26
lib/fog/ovirt/requests/compute/vm_action.rb
Normal file
26
lib/fog/ovirt/requests/compute/vm_action.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class Ovirt
|
||||
class Real
|
||||
|
||||
def vm_action(options = {})
|
||||
raise ArgumentError, "instance id is a required parameter" unless options.has_key? :id
|
||||
raise ArgumentError, "action is a required parameter" unless options.has_key? :action
|
||||
|
||||
client.vm_action options[:id], options[:action]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def vm_action(options = {})
|
||||
raise ArgumentError, "id is a required parameter" unless options.has_key? :id
|
||||
raise ArgumentError, "action is a required parameter" unless options.has_key? :action
|
||||
true
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -17,6 +17,7 @@ require 'fog/new_servers'
|
|||
require 'fog/ninefold'
|
||||
require 'fog/rackspace'
|
||||
require 'fog/openstack'
|
||||
require 'fog/ovirt'
|
||||
require 'fog/slicehost'
|
||||
require 'fog/storm_on_demand'
|
||||
require 'fog/vcloud'
|
||||
|
|
Loading…
Reference in a new issue