mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Removed the template_options param
Cloning a server is now as simple as: s1=f.servers.create(:name => "mymachine",:disk_template_name => "lucid64.img") Creating a server is now as simple as: s1=f.servers.create(:name => "mymachine")
This commit is contained in:
parent
ffa8dbf55c
commit
4221e22f1a
3 changed files with 146 additions and 116 deletions
|
@ -16,16 +16,31 @@ module Fog
|
||||||
|
|
||||||
identity :id, :aliases => 'uuid'
|
identity :id, :aliases => 'uuid'
|
||||||
|
|
||||||
|
attribute :xml
|
||||||
|
|
||||||
attribute :cpus
|
attribute :cpus
|
||||||
attribute :os_type
|
attribute :os_type
|
||||||
attribute :memory_size
|
attribute :memory_size
|
||||||
attribute :name
|
attribute :name
|
||||||
|
attribute :arch
|
||||||
|
attribute :persistent
|
||||||
|
attribute :domain_type
|
||||||
|
attribute :uuid
|
||||||
|
|
||||||
attribute :poolname
|
attribute :disk_format_type
|
||||||
attribute :xml
|
attribute :disk_allocation
|
||||||
attribute :create_persistent
|
attribute :disk_capacity
|
||||||
attribute :template_options
|
attribute :disk_name
|
||||||
attribute :template_erb
|
attribute :disk_pool_name
|
||||||
|
attribute :disk_template_name
|
||||||
|
attribute :disk_path
|
||||||
|
|
||||||
|
attribute :iso_dir
|
||||||
|
attribute :iso_file
|
||||||
|
|
||||||
|
attribute :network_interface_type
|
||||||
|
attribute :network_nat_network
|
||||||
|
attribute :network_bridge_name
|
||||||
|
|
||||||
attr_accessor :password
|
attr_accessor :password
|
||||||
attr_writer :private_key, :private_key_path, :public_key, :public_key_path, :username
|
attr_writer :private_key, :private_key_path, :public_key, :public_key_path, :username
|
||||||
|
@ -37,116 +52,128 @@ module Fog
|
||||||
#
|
#
|
||||||
# @returns server/domain created
|
# @returns server/domain created
|
||||||
def initialize(attributes={} )
|
def initialize(attributes={} )
|
||||||
|
|
||||||
self.xml ||= nil unless attributes[:xml]
|
self.xml ||= nil unless attributes[:xml]
|
||||||
self.create_persistent ||=true unless attributes[:create_persistent]
|
self.persistent ||=true unless attributes[:persistent]
|
||||||
self.template_options ||=nil unless attributes[:template_options]
|
self.cpus ||=1 unless attributes[:cpus]
|
||||||
|
self.memory_size ||=256 unless attributes[:memory_size]
|
||||||
|
self.name ||="fog-#{SecureRandom.random_number*10E14.to_i.round}" unless attributes[:name]
|
||||||
|
|
||||||
|
self.os_type ||="hvm" unless attributes[:os_type]
|
||||||
|
self.arch ||="x86_64" unless attributes[:arch]
|
||||||
|
|
||||||
|
self.domain_type ||="kvm" unless attributes[:domain_type]
|
||||||
|
|
||||||
|
self.iso_file ||=nil unless attributes[:iso_file]
|
||||||
|
self.iso_dir ||="/var/lib/libvirt/images" unless attributes[:iso_dir]
|
||||||
|
|
||||||
|
self.disk_format_type ||=nil unless attributes[:disk_format_type]
|
||||||
|
self.disk_capacity ||=nil unless attributes[:disk_capacity]
|
||||||
|
self.disk_allocation ||=nil unless attributes[:disk_allocation]
|
||||||
|
|
||||||
|
|
||||||
|
self.disk_name ||=nil unless attributes[:disk_name]
|
||||||
|
self.disk_pool_name ||=nil unless attributes[:disk_pool_name]
|
||||||
|
self.disk_template_name ||=nil unless attributes[:disk_template_name]
|
||||||
|
|
||||||
|
self.network_interface_type ||="nat" unless attributes[:network_interface_type]
|
||||||
|
self.network_nat_network ||="default" unless attributes[:network_nat_network]
|
||||||
|
self.network_bridge_name ||="br0" unless attributes[:network_bridge_name]
|
||||||
|
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def save
|
def save
|
||||||
|
|
||||||
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if uuid
|
raise Fog::Errors::Error.new('Resaving an existing server may create a duplicate') if uuid
|
||||||
|
|
||||||
# first check if we have either xml or template_options
|
validate_template_options
|
||||||
if xml.nil? && template_options.nil?
|
|
||||||
raise Fog::Errors::Error.new('Creating a new domain/server requires either xml or passing template_options')
|
|
||||||
end
|
|
||||||
|
|
||||||
if !xml.nil? && !template_options.nil?
|
xml=xml_from_template if xml.nil?
|
||||||
raise Fog::Errors::Error.new('Creating a new domain/server requires either xml or passing template_options,not both')
|
|
||||||
end
|
|
||||||
|
|
||||||
# We have a template, let's generate some xml for it
|
create_or_clone_volume
|
||||||
if !template_options.nil?
|
|
||||||
|
|
||||||
template_defaults={
|
xml=xml_from_template
|
||||||
:cpus => 1,
|
|
||||||
:memory_size => 256,
|
|
||||||
:arch => "x86_64",
|
|
||||||
:os => "hvm",
|
|
||||||
:domain_type => "kvm",
|
|
||||||
:name => "fog-#{SecureRandom.random_number*10E14.to_i.round}",
|
|
||||||
|
|
||||||
# Network options
|
|
||||||
:interface_type => "nat", #or "bridge"
|
|
||||||
:nat_network_name => "default",
|
|
||||||
:bridge_name => "br0",
|
|
||||||
|
|
||||||
# Disk options
|
|
||||||
:disk_type => "raw",
|
|
||||||
:disk_size => 10,
|
|
||||||
:disk_size_unit => "G",
|
|
||||||
:disk_allocate => 1,
|
|
||||||
:disk_allocate_unit => "G",
|
|
||||||
:disk_extension => "img",
|
|
||||||
:disk_template_name => nil,
|
|
||||||
:poolname => nil,
|
|
||||||
|
|
||||||
# DVD options
|
|
||||||
:iso_file => nil ,
|
|
||||||
:iso_dir => "/var/lib/libvirt/images/"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template_options2=template_defaults.merge(template_options)
|
|
||||||
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.all(:name => template_options[:disk_template_name]).first.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
|
|
||||||
volume=connection.volumes.create(:template_options => {
|
|
||||||
:name => "#{template_options[:disk_name]}",
|
|
||||||
:extension => "#{template_options[:disk_extension]}",
|
|
||||||
:type => "#{template_options[:disk_type]}",
|
|
||||||
:size => "#{template_options[:disk_size]}",
|
|
||||||
:size_unit => "#{template_options[:disk_size_unit]}",
|
|
||||||
:allocate => "#{template_options[:disk_allocate]}",
|
|
||||||
:allocate_unit => "#{template_options[:disk_allocate_unit]}" })
|
|
||||||
|
|
||||||
# This gets passed to the domain to know the path of the disk
|
|
||||||
template_options[:disk_path]=volume.path
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
xml=xml_from_template(template_options)
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
# We either now have xml provided by the user or generated by the template
|
# We either now have xml provided by the user or generated by the template
|
||||||
|
begin
|
||||||
if !xml.nil?
|
if !xml.nil?
|
||||||
domain=nil
|
domain=nil
|
||||||
if create_persistent
|
if self.persistent
|
||||||
domain=connection.define_domain_xml(xml)
|
domain=connection.define_domain_xml(xml)
|
||||||
else
|
else
|
||||||
domain=connection.create_domain_xml(xml)
|
domain=connection.create_domain_xml(xml)
|
||||||
end
|
end
|
||||||
self.raw=domain
|
self.raw=domain
|
||||||
end
|
end
|
||||||
|
rescue
|
||||||
|
raise Fog::Errors::Error.new("Error saving the server: #{$!}")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_template_options(template_options)
|
def create_or_clone_volume
|
||||||
#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')
|
volume_options=Hash.new
|
||||||
#end
|
|
||||||
unless template_options[:interface_type].nil?
|
unless self.disk_name.nil?
|
||||||
raise Fog::Errors::Error.new("#{template_options[:interface_type]} is not a supported interface type") unless ["nat", "bridge"].include?(template_options[:interface_type])
|
volume_options[:name]=self.disk_name
|
||||||
|
else
|
||||||
|
extension = self.disk_format_type.nil? ? "img" : self.disk_format_type
|
||||||
|
volume_name = "#{self.name}.#{extension}"
|
||||||
|
volume_options[:name]=volume_name
|
||||||
|
end
|
||||||
|
|
||||||
|
# Check if a disk template was specified
|
||||||
|
unless self.disk_template_name.nil?
|
||||||
|
|
||||||
|
template_volumes=connection.volumes.all(:name => self.disk_template_name)
|
||||||
|
|
||||||
|
raise Fog::Errors::Error.new("Template #{self.disk_template_name} not found") unless template_volumes.length==1
|
||||||
|
|
||||||
|
volume=template_volumes.first.clone("#{volume_options[:name]}")
|
||||||
|
|
||||||
|
# This gets passed to the domain to know the path of the disk
|
||||||
|
self.disk_path=volume.path
|
||||||
|
|
||||||
|
else
|
||||||
|
# If no template volume was given, let's create our own volume
|
||||||
|
|
||||||
|
volume_options[:format_type]=self.disk_format_type unless self.disk_format_type.nil?
|
||||||
|
volume_options[:capacity]=self.disk_capacity unless self.disk_capacity.nil?
|
||||||
|
volume_options[:allocation]=self.disk_allocation unless self.disk_allocation.nil?
|
||||||
|
|
||||||
|
begin
|
||||||
|
volume=connection.volumes.create(volume_options)
|
||||||
|
self.disk_path=volume.path
|
||||||
|
rescue
|
||||||
|
raise Fog::Errors::Error.new("Error creating the volume : #{$!}")
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def xml_from_template(template_options)
|
def validate_template_options
|
||||||
|
unless self.network_interface_type.nil?
|
||||||
|
raise Fog::Errors::Error.new("#{self.network_interface_type} is not a supported interface type") unless ["nat", "bridge"].include?(self.network_interface_type)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# We only want specific variables for ERB
|
def xml_from_template
|
||||||
|
|
||||||
|
template_options={
|
||||||
|
:cpus => self.cpus,
|
||||||
|
:memory_size => self.memory_size,
|
||||||
|
:domain_type => self.domain_type,
|
||||||
|
:name => self.name,
|
||||||
|
:iso_file => self.iso_file,
|
||||||
|
:iso_dir => self.iso_dir,
|
||||||
|
:os_type => self.os_type,
|
||||||
|
:arch => self.arch,
|
||||||
|
:disk_path => self.disk_path,
|
||||||
|
:network_interface_type => self.network_interface_type,
|
||||||
|
:network_nat_network => self.network_nat_network,
|
||||||
|
:network_bridge_name => self.network_bridge_name
|
||||||
|
}
|
||||||
vars = ErbBinding.new(template_options)
|
vars = ErbBinding.new(template_options)
|
||||||
template_path=File.join(File.dirname(__FILE__),"templates","server.xml.erb")
|
template_path=File.join(File.dirname(__FILE__),"templates","server.xml.erb")
|
||||||
template=File.open(template_path).readlines.join
|
template=File.open(template_path).readlines.join
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
<memory><%= memory_size %></memory>
|
<memory><%= memory_size %></memory>
|
||||||
<vcpu><%= cpus %></vcpu>
|
<vcpu><%= cpus %></vcpu>
|
||||||
<os>
|
<os>
|
||||||
<type arch='<%= arch %>'><%= os %></type>
|
<type arch='<%= arch %>'><%= os_type %></type>
|
||||||
<boot dev='hd'/>
|
<boot dev='hd'/>
|
||||||
<% if !iso_file.nil? %>
|
<% if !iso_file.nil? %>
|
||||||
|
|
||||||
|
@ -31,14 +31,14 @@
|
||||||
<address type='drive' controller='0' bus='1' unit='0'/>
|
<address type='drive' controller='0' bus='1' unit='0'/>
|
||||||
</disk>
|
</disk>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% if interface_type=="bridge" %>
|
<% if network_interface_type=="bridge" %>
|
||||||
<interface type="bridge">
|
<interface type="bridge">
|
||||||
<source bridge='<%= bridge_name %>'/>
|
<source bridge='<%= network_bridge_name %>'/>
|
||||||
<model type='virtio'/>
|
<model type='virtio'/>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% if interface_type=="nat" %>
|
<% if network_interface_type=="nat" %>
|
||||||
<interface type='network'>
|
<interface type='network'>
|
||||||
<source network='<%= nat_network_name %>'/>
|
<source network='<%= interface_nat_network %>'/>
|
||||||
<model type='virtio'/>
|
<model type='virtio'/>
|
||||||
<% end %>
|
<% end %>
|
||||||
</interface>
|
</interface>
|
||||||
|
|
|
@ -12,7 +12,7 @@ module Fog
|
||||||
|
|
||||||
include Fog::Compute::LibvirtUtil
|
include Fog::Compute::LibvirtUtil
|
||||||
|
|
||||||
identity :key
|
identity :id , :aliases => 'key'
|
||||||
|
|
||||||
attribute :pool_name
|
attribute :pool_name
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ module Fog
|
||||||
self.key = nil
|
self.key = nil
|
||||||
self.format_type ||= "raw" unless attributes[:format_type]
|
self.format_type ||= "raw" unless attributes[:format_type]
|
||||||
extension = self.format_type=="raw" ? "img" : self.format_type
|
extension = self.format_type=="raw" ? "img" : self.format_type
|
||||||
self.name ||= "fog-#{SecureRandom.random_number*10E14.to_i.round}.{extension}" unless attributes[:name]
|
self.name ||= "fog-#{SecureRandom.random_number*10E14.to_i.round}.#{extension}" unless attributes[:name]
|
||||||
self.capacity ||= "10G" unless attributes[:capacity]
|
self.capacity ||= "10G" unless attributes[:capacity]
|
||||||
self.allocation ||= "1G" unless attributes[:allocation]
|
self.allocation ||= "1G" unless attributes[:allocation]
|
||||||
super
|
super
|
||||||
|
@ -64,6 +64,8 @@ module Fog
|
||||||
def save
|
def save
|
||||||
requires :pool_name
|
requires :pool_name
|
||||||
|
|
||||||
|
raise Fog::Errors::Error.new('Resaving an existing volume may create a duplicate') if key
|
||||||
|
|
||||||
xml=xml_from_template if xml.nil?
|
xml=xml_from_template if xml.nil?
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
@ -154,6 +156,7 @@ module Fog
|
||||||
|
|
||||||
raw_attributes = {
|
raw_attributes = {
|
||||||
:key => new_raw.key,
|
:key => new_raw.key,
|
||||||
|
:id => new_raw.key,
|
||||||
:path => new_raw.path,
|
:path => new_raw.path,
|
||||||
:name => new_raw.name,
|
:name => new_raw.name,
|
||||||
:format_type => format_type,
|
:format_type => format_type,
|
||||||
|
|
Loading…
Add table
Reference in a new issue