1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Coming along nicely

Noticed that we could avoid implementing our own attribute get functions
These interfered with a destroy operation

Also reload of an object should now work better

A server can be created and will get a random name + annex disk (default = 10G)
This commit is contained in:
Patrick Debois 2011-08-03 14:28:40 +02:00
parent 1ed3ee3379
commit d07d3144d6
8 changed files with 53 additions and 89 deletions

View file

@ -31,6 +31,9 @@ module Fog
attr_reader :connection
attr_reader :uri
# f=Fog::Compute.new(:provider => "Libvirt", :libvirt_uri => "qemu+ssh://patrick.debois@juno/system")
def initialize(options={})
@uri = ::Fog::Compute::LibvirtUtil::URI.new(enhance_uri(options[:libvirt_uri]))

View file

@ -39,6 +39,7 @@ module Fog
self.all(:name => name).first
end
#private # Making these private, screws up realod
# Retrieve the interface by name
def get_by_name(name)
interface=connection.lookup_interface_by_name(name)

View file

@ -38,6 +38,7 @@ module Fog
self.all(:uuid => uuid).first
end
#private # Making these private, screws up realod
# Retrieve the network by uuid
def get_by_uuid(uuid)
network=connection.lookup_network_by_uuid(uuid)

View file

@ -39,7 +39,7 @@ module Fog
self.all(:uuid => uuid).first
end
private
#private # Making these private, screws up realod
# Retrieve the pool by uuid
def get_by_uuid(uuid)
pool=connection.lookup_storage_pool_by_uuid(uuid)

View file

@ -14,7 +14,12 @@ module Fog
include Fog::Compute::LibvirtUtil
identity :id , :aliases => 'uuid'
identity :uuid
attribute :cpus
attribute :os_type
attribute :memory_size
attribute :name
attribute :poolname
attribute :xml
@ -40,7 +45,7 @@ module Fog
def save
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if id
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if uuid
# first check if we have either xml or template_options
if xml.nil? && template_options.nil?
@ -84,11 +89,18 @@ module Fog
template_options2=template_defaults.merge(template_options)
template_options={ :disk_path => "#{template_options2[:name]}.#{template_options2[:disk_extension]}"}.merge(template_options2)
template_options={
:disk_name => "#{template_options2[:name]}.#{template_options2[:disk_extension]}"
}.merge(template_options2)
validate_template_options(template_options)
if !template_options[:disk_template_name].nil?
# Clone the volume
volume=connection.volumes.allocate(:name => template_options[:disk_template_name]).clone("#{template_options[:disk_path]}")
# Clone the volume
volume=connection.volumes.allocate(:name => template_options[:disk_template_name]).clone("#{template_options[:disk_name]}")
# This gets passed to the domain to know the path of the disk
template_options[:disk_path]=volume.path
else
# If no template volume was given, let's create our own volume
@ -101,10 +113,10 @@ module Fog
:allocate => "#{template_options[:disk_allocate]}",
:size_unit => "#{template_options[:disk_size_unit]}" })
# This gets passed to the domain to know the path of the disk
template_options[:disk_path]=volume.path
end
validate_template_options(template_options)
xml=xml_from_template(template_options)
@ -126,6 +138,9 @@ module Fog
#if template_options[:disk_template_name].nil?
# raise Fog::Errors::Error.new('In order to make the disk boot, we require a template volume we can clone')
#end
unless template_options[:interface_type].nil?
raise Fog::Errors::Error.new("#{template_options[:interface_type]} is not a supported interface type") unless ["nat", "bridge"].include?(template_options[:interface_type])
end
end
@ -217,43 +232,6 @@ module Fog
@raw.shutdown
end
def mac
mac = document("domain/devices/interface/mac", "address")
return mac
end
def vnc_port
port = document("domain/devices/graphics[@type='vnc']", "port")
return port
end
def name
requires :raw
raw.name
end
def uuid
requires :raw
raw.uuid
end
def memory_size
requires :raw
raw.memory_size
end
def cpus
requires :raw
raw.cpus
end
def os_type
requires :raw
raw.os_type
end
def xml_desc
requires :raw
raw.xml_desc
@ -415,7 +393,19 @@ module Fog
Fog::SSH.new(public_ip_address, username, credentials).run(commands)
end
def mac
mac = document("domain/devices/interface/mac", "address")
return mac
end
def vnc_port
port = document("domain/devices/graphics[@type='vnc']", "port")
return port
end
private
def raw
@ -426,7 +416,11 @@ module Fog
@raw = new_raw
raw_attributes = {
:id => new_raw.uuid,
:uuid => new_raw.uuid,
:name => new_raw.name,
:memory_size => new_raw.info.max_mem,
:cpus => new_raw.info.nr_virt_cpu,
:os_type => new_raw.os_type
}
merge_attributes(raw_attributes)

View file

@ -12,7 +12,7 @@ module Fog
def all(filter=nil)
data=[]
if filter.nil?
connection.list_defined_domains.map do |server|
connection.list_defined_domains.map do |domain|
data << { :raw => connection.lookup_domain_by_name(domain) }
end
@ -30,12 +30,12 @@ module Fog
end
data << { :raw => domain }
end
load(data)
end
def get(key)
self.all(:key => key).first
def get(uuid)
self.all(:uuid => uuid).first
end
def bootstrap(new_attributes = {})
@ -46,8 +46,9 @@ module Fog
# server.setup(:password => server.password)
# server
end
private
# private #making these internals private screws up reload
# Retrieve the server by uuid
def get_by_uuid(uuid)
server=connection.lookup_domain_by_uuid(uuid)

View file

@ -1,5 +1,5 @@
<volume>
<name><%= "#{name}.#{extension}" %></name>
<name><%= "#{name}" %></name>
<allocation unit="<%= allocate_unit %>"><%= allocate %></allocation>
<capacity unit="<%= size_unit %>"><%= size %></capacity>
<target>

View file

@ -67,21 +67,17 @@ module Fog
template_defaults={
:type => "raw",
:extension => "img",
:name => "fog-#{SecureRandom.random_number*10E14.to_i.round}",
:size => 10,
:allocate_unit => "G",
:size_unit => "G",
:allocate => 1,
}
template_options2=template_defaults.merge(template_options)
template_options={ :path => "#{template_options2[:name]}.#{template_options2[:extension]}"}.merge(template_options2)
template_options={ :name => "fog-#{SecureRandom.random_number*10E14.to_i.round}.#{template_options2[:extension]}"}.merge(template_options2)
validate_template_options(template_options)
xml=xml_from_template(template_options)
# require 'pp'
# pp xml
# exit
end
@ -145,43 +141,11 @@ module Fog
return connection.volumes.all(:name => name)
end
def key
requires :raw
raw.key
end
def path
requires :raw
raw.path
end
def name
requires :raw
raw.name
end
def xml_desc
requires :raw
raw.xml_desc
end
def allocation
requires :raw
raw.info.allocation
end
def capacity
requires :raw
@capacity=raw.info.capacity
end
def type
requires :raw
@type=raw.info.type
end
private
def raw
@raw