diff --git a/lib/fog/compute/models/libvirt/interface.rb b/lib/fog/compute/models/libvirt/interface.rb index 1d718020b..260d35b15 100644 --- a/lib/fog/compute/models/libvirt/interface.rb +++ b/lib/fog/compute/models/libvirt/interface.rb @@ -7,20 +7,20 @@ module Fog class Interface < Fog::Model identity :name - + attribute :mac attribute :xml_desc def save raise Fog::Errors::Error.new('Creating a new interface is not yet implemented. Contributions welcome!') end - + def destroy requires :raw raw.delete true end - + private def raw @@ -30,7 +30,7 @@ module Fog def raw=(new_raw) @raw = new_raw - raw_attributes = { + raw_attributes = { :name => new_raw.name, :mac => new_raw.mac, :xml_desc => new_raw.xml_desc, @@ -39,7 +39,7 @@ module Fog merge_attributes(raw_attributes) end - + end end diff --git a/lib/fog/compute/models/libvirt/interfaces.rb b/lib/fog/compute/models/libvirt/interfaces.rb index 037cb4777..fbc4b6f03 100644 --- a/lib/fog/compute/models/libvirt/interfaces.rb +++ b/lib/fog/compute/models/libvirt/interfaces.rb @@ -10,14 +10,14 @@ module Fog model Fog::Compute::Libvirt::Interface def all(filter=nil) - data=[] + data=[] if filter.nil? connection.list_interfaces.each do |ifname| - interface=connection.lookup_interface_by_name(ifname) + interface=connection.lookup_interface_by_name(ifname) data << { :raw => interface } end connection.list_defined_interfaces.each do |ifname| - interface=connection.lookup_interface_by_name(ifname) + interface=connection.lookup_interface_by_name(ifname) data << { :raw => interface } end @@ -34,10 +34,10 @@ module Fog load(data) end - + def get(key) self.all(:name => name).first - end + end #private # Making these private, screws up realod # Retrieve the interface by name diff --git a/lib/fog/compute/models/libvirt/network.rb b/lib/fog/compute/models/libvirt/network.rb index aadd0c2de..cc0d7c5d0 100644 --- a/lib/fog/compute/models/libvirt/network.rb +++ b/lib/fog/compute/models/libvirt/network.rb @@ -14,29 +14,29 @@ module Fog attribute :name attribute :bridge_name attribute :xml_desc - + ##https://www.redhat.com/archives/libvirt-users/2011-May/msg00091.html # Bridged VLAN - + # https://www.redhat.com/archives/libvirt-users/2011-April/msg00006.html # Routed network without IP - + # http://wiki.libvirt.org/page/Networking #http://wiki.libvirt.org/page/VirtualNetworking#Virtual_network_switches def initialize(attributes = {}) super end - + def save raise Fog::Errors::Error.new('Creating a new network is not yet implemented. Contributions welcome!') end - + def destroy() requires :raw raw.destroy true end - + private def raw @@ -46,7 +46,7 @@ module Fog def raw=(new_raw) @raw = new_raw - raw_attributes = { + raw_attributes = { :uuid => new_raw.uuid, :name => new_raw.name, :bridge_name => new_raw.bridge_name, @@ -56,7 +56,7 @@ module Fog merge_attributes(raw_attributes) end - + end end diff --git a/lib/fog/compute/models/libvirt/networks.rb b/lib/fog/compute/models/libvirt/networks.rb index 799c8ccc1..2b8bb09bd 100644 --- a/lib/fog/compute/models/libvirt/networks.rb +++ b/lib/fog/compute/models/libvirt/networks.rb @@ -10,12 +10,12 @@ module Fog model Fog::Compute::Libvirt::Network def all(filter=nil) - data=[] + data=[] if filter.nil? connection.list_networks.each do |networkname| - network=connection.lookup_network_by_name(networkname) + network=connection.lookup_network_by_name(networkname) data << { :raw => network } - end + end connection.list_defined_networks.each do |networkname| network=connection.lookup_network_by_name(networkname) data << { :raw => network} @@ -37,7 +37,7 @@ module Fog def get(uuid) self.all(:uuid => uuid).first end - + #private # Making these private, screws up realod # Retrieve the network by uuid def get_by_uuid(uuid) diff --git a/lib/fog/compute/models/libvirt/pool.rb b/lib/fog/compute/models/libvirt/pool.rb index ea192f5d3..5a8726a72 100644 --- a/lib/fog/compute/models/libvirt/pool.rb +++ b/lib/fog/compute/models/libvirt/pool.rb @@ -7,20 +7,20 @@ module Fog class Pool < Fog::Model identity :uuid - + # These attributes are only used for creation attribute :xml attribute :create_persistent attribute :create_auto_build - - # Can be created by passing in XML + + # Can be created by passing in XML def initialize(attributes={} ) self.xml ||= nil unless attributes[:xml] self.create_persistent ||= true unless attributes[:create_persistent] self.create_auto_build ||= true unless attributes[:create_auto_build] super end - + def save requires :xml unless xml.nil? @@ -29,96 +29,96 @@ module Fog pool=connection.connection.define_storage_pool_xml(xml) else pool=connection.connection.create_storage_pool_xml(xml) - end + end self.raw=pool true else - raise Fog::Errors::Error.new('Creating a new pool requires proper xml') + raise Fog::Errors::Error.new('Creating a new pool requires proper xml') false end end - - + + # Start the pool = make it active # Performs a libvirt create (= start) def start requires :raw - + @raw.create end - + # Stop the pool = make it non-active # Performs a libvirt destroy (= stop) def stop requires :raw - + @raw.destroy end - + # Shuts down the pool def shutdown requires :raw - + @raw.destroy true end - + # Build this storage pool def build requires :raw - + @raw.build unless @raw.nil? end - + # Destroys the storage pool def destroy( destroy_options={}) requires :raw - + # Shutdown pool if active @raw.destroy if @raw.active? - + # Delete corresponding data in this pool # @raw.delete # If this is a persistent domain we need to undefine it @raw.undefine if @raw.persistent? - + end - + # Set autostart value of the storage pool (true|false) def auto_start=(flag) requires :raw - + @raw.auto_start(flag) end - + # Is the pool active or not? def active? requires :raw @raw.active? end - + # Will the pool autostart or not? def auto_start? requires :raw @raw.autostart? end - + # Is the pool persistent or not? def persistent? requires :raw @raw.persistent? end - + # Returns the xml description of the current pool def xml_desc requires :raw - + @raw.xml_desc unless @raw.nil? end - + # Retrieves the name of the pool def name @@ -131,25 +131,25 @@ module Fog requires :raw @raw.uuid end - + # Retrieves the allocated disk space of the pool def allocation requires :raw @raw.info.allocation end - + # Retrieves the capacity of disk space of the pool def capacity requires :raw @raw.info.capacity end - + # Retrieves the number of volumes available in this pool def num_of_volumes requires :raw @raw.num_of_volumes end - + def state requires :raw @@ -161,12 +161,12 @@ module Fog states=[:inactive, :building,:running,:degrated,:inaccessible] return states[@raw.info.state] - + end - + # Retrieves the volumes of this pool def volumes - + volumes=Array.new @raw.list_volumes.each do |volume| fog_volume=connection.volumes.all(:name => volume).first @@ -174,7 +174,7 @@ module Fog end return volumes end - + private def raw @@ -184,14 +184,14 @@ module Fog def raw=(new_raw) @raw = new_raw - raw_attributes = { + raw_attributes = { :uuid => new_raw.uuid, - + } - + merge_attributes(raw_attributes) end - + end end diff --git a/lib/fog/compute/models/libvirt/pools.rb b/lib/fog/compute/models/libvirt/pools.rb index 90276f586..9511ff84f 100644 --- a/lib/fog/compute/models/libvirt/pools.rb +++ b/lib/fog/compute/models/libvirt/pools.rb @@ -10,12 +10,12 @@ module Fog model Fog::Compute::Libvirt::Pool def all(filter=nil) - data=[] + data=[] if filter.nil? connection.list_storage_pools.each do |poolname| - pool=connection.lookup_storage_pool_by_name(poolname) + pool=connection.lookup_storage_pool_by_name(poolname) data << { :raw => pool } - end + end connection.list_defined_storage_pools.each do |poolname| data << { :raw => connection.lookup_storage_pool_by_name(poolname) @@ -31,16 +31,16 @@ module Fog end data << { :raw => pool} end - + load(data) end def get(uuid) self.all(:uuid => uuid).first end - + #private # Making these private, screws up realod - # Retrieve the pool by uuid + # Retrieve the pool by uuid def get_by_uuid(uuid) pool=connection.lookup_storage_pool_by_uuid(uuid) return pool diff --git a/lib/fog/compute/models/libvirt/server.rb b/lib/fog/compute/models/libvirt/server.rb index 838518694..521179965 100644 --- a/lib/fog/compute/models/libvirt/server.rb +++ b/lib/fog/compute/models/libvirt/server.rb @@ -15,7 +15,7 @@ module Fog include Fog::Compute::LibvirtUtil identity :uuid - + attribute :cpus attribute :os_type attribute :memory_size @@ -30,8 +30,8 @@ module Fog attr_accessor :password attr_writer :private_key, :private_key_path, :public_key, :public_key_path, :username - # Can be created by passing in :xml => "" - # or by providing :template_options => { + # Can be created by passing in :xml => "" + # or by providing :template_options => { # :name => "", :cpus => 1, :memory_size => 256 , :volume_template # :} # @@ -41,28 +41,28 @@ module Fog self.create_persistent ||=true unless attributes[:create_persistent] self.template_options ||=nil unless attributes[:template_options] super - end + end 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 object may create a duplicate') if uuid + # first check if we have either xml or 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') + raise Fog::Errors::Error.new('Creating a new domain/server requires either xml or passing template_options') end if !xml.nil? && !template_options.nil? - raise Fog::Errors::Error.new('Creating a new domain/server requires either xml or passing template_options,not both') + 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 + # We have a template, let's generate some xml for it if !template_options.nil? template_defaults={ - :cpus => 1, - :memory_size => 256, - :arch => "x86_64", + :cpus => 1, + :memory_size => 256, + :arch => "x86_64", :os => "hvm", :domain_type => "kvm", :name => "fog-#{SecureRandom.random_number*10E14.to_i.round}", @@ -89,7 +89,7 @@ module Fog template_options2=template_defaults.merge(template_options) - template_options={ + template_options={ :disk_name => "#{template_options2[:name]}.#{template_options2[:disk_extension]}" }.merge(template_options2) @@ -97,16 +97,16 @@ module Fog if !template_options[:disk_template_name].nil? - # Clone the volume + # 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]}", + 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]}", @@ -129,14 +129,14 @@ module Fog domain=connection.define_domain_xml(xml) else domain=connection.create_domain_xml(xml) - end + end self.raw=domain - end + end end def validate_template_options(template_options) #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') + # 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]) @@ -144,7 +144,7 @@ module Fog end - def xml_from_template(template_options) + def xml_from_template(template_options) # We only want specific variables for ERB vars = ErbBinding.new(template_options) @@ -167,11 +167,11 @@ module Fog begin @raw.create true - rescue + rescue false end end - end + end def destroy(options={ :destroy_volumes => false}) @@ -179,7 +179,7 @@ module Fog requires :raw if @raw.active? @raw.destroy - end + end @raw.undefine end @@ -261,10 +261,10 @@ module Fog command="grep #{mac} /var/log/arpwatch.log |cut -d ':' -f 4-| cut -d ' ' -f 4" # command="grep #{mac} /var/lib/arpwatch/arp.dat|cut -f 2|tail -1" - # TODO: we need to take the time into account, when IP's are re-allocated, we might be executing + # TODO: we need to take the time into account, when IP's are re-allocated, we might be executing # On the wrong host - # We can get the host, the + # We can get the host, the user=connection.uri.user #could be nil host=connection.uri.host keyfile=connection.uri.keyfile @@ -284,9 +284,9 @@ module Fog # We couldn't retrieve any IP information return { :public => nil , :private => nil} end - else + else # TODO for locat execute - #No ssh just do it locally + #No ssh just do it locally #cat /var/log/daemon.log|grep "52:54:00:52:f6:22"| # or local execute arp -an to get the ip (as a last resort) @@ -301,7 +301,7 @@ module Fog def public_ip_address ip_address(:public) end - + def ip_address(key) ips=addresses[key] unless ips.nil? @@ -328,7 +328,7 @@ module Fog def public_key @public_key ||= public_key_path && File.read(public_key_path) end - + def ssh(commands) requires :public_ip_address, :username @@ -338,7 +338,7 @@ module Fog ssh_options[:key_data] = [private_key] if private_key ssh_options[:proxy]= ssh_proxy unless ssh_proxy.nil? - Fog::SSH.new(public_ip_address, @username, ssh_options).run(commands) + Fog::SSH.new(public_ip_address, @username, ssh_options).run(commands) end @@ -356,8 +356,8 @@ module Fog # This is a direct connection, so we don't need a proxy to be set end end - - # Transfers a file + + # Transfers a file def scp(local_path, remote_path, upload_options = {}) requires :public_ip_address, :username @@ -378,7 +378,7 @@ module Fog credentials[:proxy]= ssh_proxy unless ssh_proxy.nil? credentials[:password] = password unless self.password.nil? credentails[:key_data] = [private_key] if self.private_key - + commands = [ %{mkdir .ssh}, # %{passwd -l #{username}}, #Not sure if we need this here @@ -415,8 +415,8 @@ module Fog port = document("domain/devices/graphics[@type='vnc']", "port") return port end - - + + private def raw @@ -426,13 +426,13 @@ module Fog def raw=(new_raw) @raw = new_raw - raw_attributes = { - :uuid => new_raw.uuid, + raw_attributes = { + :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) end diff --git a/lib/fog/compute/models/libvirt/uri.rb b/lib/fog/compute/models/libvirt/uri.rb index f6a799949..aa28479b6 100644 --- a/lib/fog/compute/models/libvirt/uri.rb +++ b/lib/fog/compute/models/libvirt/uri.rb @@ -1,50 +1,50 @@ require 'uri' -require 'cgi' +require 'cgi' module Fog module Compute module LibvirtUtil - class URI + class URI attr_reader :uri - + def initialize(uri) @parsed_uri=::URI.parse(uri) @uri=uri return self end - + # Transport will be part of the scheme # The part after the plus sign # f.i. qemu+ssh def transport scheme=@parsed_uri.scheme return nil if scheme.nil? - + return scheme.split(/\+/)[1] end - + def scheme return @parsed_uri.scheme end - + def driver scheme=@parsed_uri.scheme return nil if scheme.nil? - + return scheme.split(/\+/).first end - + def ssh_enabled? transport.include?("ssh") end - + def remote? return !transport.nil? end - + def user @parsed_uri.user end @@ -56,7 +56,7 @@ module Fog def port @parsed_uri.port end - + def password @parsed_uri.password end @@ -64,19 +64,19 @@ module Fog def name value("name") end - + def command value("command") end - + def socket value("socket") end - + def keyfile value("command") end - + def netcat value("netcat") end @@ -84,23 +84,23 @@ module Fog def no_verify? no_verify=value("no_verify") return false if no_verify.nil? - + if no_verify.to_s=="0" return false else return true end end - + def verify? return !no_verify? end def no_tty? no_tty=value("no_tty") - + return false if no_tty.nil? - + if no_tty=="0" return false else @@ -108,16 +108,16 @@ module Fog end end - + def tty? return !no_tty? end - + def pkipath value("pkipath") end - - + + # A libvirt URI allows you to specify extra params # http://libvirt.org/remote.html private @@ -129,7 +129,7 @@ module Fog return nil end end - + end end diff --git a/lib/fog/compute/models/libvirt/util.rb b/lib/fog/compute/models/libvirt/util.rb index 11d56ad26..06ef19c11 100644 --- a/lib/fog/compute/models/libvirt/util.rb +++ b/lib/fog/compute/models/libvirt/util.rb @@ -12,14 +12,14 @@ module Fog xml = REXML::Document.new(@xml_desc) attribute.nil? ? xml.elements[path].text : xml.elements[path].attributes[attribute] end - + class ErbBinding < OpenStruct def get_binding return binding() end end - - + + end end end diff --git a/lib/fog/compute/models/libvirt/volume.rb b/lib/fog/compute/models/libvirt/volume.rb index 46874fb53..24ff50ad9 100644 --- a/lib/fog/compute/models/libvirt/volume.rb +++ b/lib/fog/compute/models/libvirt/volume.rb @@ -15,10 +15,10 @@ module Fog identity :key attribute :poolname - + attribute :xml attribute :template_options - + # attribute :key attribute :path attribute :name @@ -26,15 +26,15 @@ module Fog attribute :allocation attribute :type - # Can be created by passing in :xml => "" + # Can be created by passing in :xml => "" # A volume always belongs to a pool, :poolname => "" # # @returns volume created def initialize(attributes={} ) self.xml ||= nil unless attributes[:xml] - + super - + # Try to guess the default/first pool of no poolname was specificed default_pool_name="default" default_pool=connection.pools.all(:name => "default") @@ -42,14 +42,14 @@ module Fog first_pool=connection.pools.first if first_pool.nil? raise Fog::Errors::Error.new('We could not find a pool called "default" and there was no other pool defined') - else + else default_pool_name=first_pool.name end - + end - + self.poolname ||= default_pool_name unless attributes[:poolname] - + end # Takes a pool and either uses :xml or :template_options->xml to create the volume @@ -61,10 +61,10 @@ module Fog # :disk_type => "raw", # :disk_extension => "img", # :disk_size => "10000", - # We have a template, let's generate some xml for it + # We have a template, let's generate some xml for it if !template_options.nil? - template_defaults={ + template_defaults={ :type => "raw", :extension => "img", :size => 10, @@ -99,19 +99,19 @@ module Fog end end - + def validate_template_options(template_options) # Here we can validate the template_options end - def xml_from_template(template_options) + def xml_from_template(template_options) # We only want specific variables for ERB - + # we can't use an option of name type # This clashes with ruby 1.8 Erb.type which is equivalent of .class new_template_options={:vol_type => template_options[:type]}.merge(template_options) - + vars = ErbBinding.new(template_options) template_path=File.join(File.dirname(__FILE__),"templates","volume.xml.erb") template=File.open(template_path).readlines.join @@ -135,7 +135,7 @@ module Fog true end - # Clones this volume to the name provided + # Clones this volume to the name provided def clone(name) pool=@raw.pool xml = REXML::Document.new(xml_desc) @@ -159,7 +159,7 @@ module Fog def raw=(new_raw) @raw = new_raw - raw_attributes = { + raw_attributes = { :key => new_raw.key, :path => new_raw.path, :name => new_raw.name,