From 26c51cb3a3b48174625e56f08e3219dfd24ae19d Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Thu, 3 May 2012 13:41:04 +0200 Subject: [PATCH] Feature #1125: Move ozones to Sequel instead of Datamapper --- src/ozones/Server/lib/OZones/ApacheWritter.rb | 4 +- src/ozones/Server/lib/OZones/VDC.rb | 79 +++++++++++-------- src/ozones/Server/lib/OZones/Zones.rb | 52 +++++++----- src/ozones/Server/models/Auth.rb | 19 +++-- src/ozones/Server/models/OzonesServer.rb | 13 ++- src/ozones/Server/ozones-server.rb | 21 +++-- 6 files changed, 109 insertions(+), 79 deletions(-) diff --git a/src/ozones/Server/lib/OZones/ApacheWritter.rb b/src/ozones/Server/lib/OZones/ApacheWritter.rb index ca3ae1fdd3..47832c93ba 100644 --- a/src/ozones/Server/lib/OZones/ApacheWritter.rb +++ b/src/ozones/Server/lib/OZones/ApacheWritter.rb @@ -24,8 +24,8 @@ module OZones def update htaccess = "RewriteEngine On\n" - OZones::Zones.all.each{|zone| - zone.vdcs.all.each{|vdc| + OZones::Zones.each{|zone| + zone.vdcs.each{|vdc| htaccess << "RewriteRule ^#{vdc.NAME} " + "#{zone.ENDPOINT} [P]\n" diff --git a/src/ozones/Server/lib/OZones/VDC.rb b/src/ozones/Server/lib/OZones/VDC.rb index 46ec6de56f..263f11eabc 100644 --- a/src/ozones/Server/lib/OZones/VDC.rb +++ b/src/ozones/Server/lib/OZones/VDC.rb @@ -22,28 +22,42 @@ module OZones # ID, NAME, the GROUP backing the VDC, the admin credentials and the VDC # resources. VDC resources are stored in JSON document in the DB and used as # a hash in the VDC. - class Vdc - include DataMapper::Resource + class Vdc < Sequel::Model include OpenNebulaJSON::JSONUtils extend OpenNebulaJSON::JSONUtils - property :ID, Serial - property :NAME, String, :required => true, :unique => true - property :GROUP_ID, Integer - property :VDCADMINNAME, String, :required => true - property :VDCADMIN_ID, Integer - property :CLUSTER_ID, Integer - property :RESOURCES, Text + plugin :schema + plugin :validation_helpers - belongs_to :zones + set_schema do + primary_key :ID + String :NAME, :unique => true + Integer :GROUP_ID + foreign_key :ZONES_ID, :zones, :key => :ID + String :VDCADMINNAME + Integer :VDCADMIN_ID + Integer :CLUSTER_ID + String :RESOURCES, :text => true + + end + + create_table unless table_exists? + + many_to_one :zone, :class => 'OZones::Zones', :key => :ZONES_ID + + def validate + super + validates_presence [:NAME, :VDCADMINNAME] + validates_unique :NAME + end def resources rsrc_json = self.RESOURCES parser = JSON.parser.new(rsrc_json, {:symbolize_names => true}) parser.parse - end - + end + def resources=(rsrc_hash) self.RESOURCES = JSON.generate(rsrc_hash) end @@ -55,11 +69,7 @@ module OZones zonePoolHash["VDC_POOL"]["VDC"] = Array.new unless self.all.empty? self.all.each{ |vdc| - # Hack! zones_ID does not respect the "all capital letters" policy - attrs = vdc.attributes.clone - - attrs[:ZONES_ID] = vdc.attributes[:zones_ID] - attrs.delete(:zones_ID) + attrs = vdc.values.clone rsrc_json = attrs.delete(:RESOURCES) parser = JSON.parser.new(rsrc_json, {:symbolize_names=>true}) @@ -74,11 +84,7 @@ module OZones def to_hash vdc_attributes = Hash.new - # Hack! zones_ID does not respect the "all capital letters" policy - attrs = attributes.clone - - attrs[:ZONES_ID] = attributes[:zones_ID] - attrs.delete(:zones_ID) + attrs = @values.clone rsrc_json = attrs.delete(:RESOURCES) parser = JSON.parser.new(rsrc_json, {:symbolize_names=>true}) @@ -99,9 +105,9 @@ module OZones ####################################################################### # Constants ####################################################################### - VDC_ATTRS = [:VDCADMINNAME, - :VDCADMINPASS, - :NAME, + VDC_ATTRS = [:VDCADMINNAME, + :VDCADMINPASS, + :NAME, :CLUSTER_ID, :RESOURCES] @@ -111,13 +117,13 @@ module OZones #Creates an OpenNebula VDC, using its ID, vdcid and the associated zone def initialize(vdcid, zone = nil) if vdcid != -1 - @vdc = Vdc.get(vdcid) + @vdc = Vdc[vdcid] if !@vdc raise "VDC with id #{vdcid} not found." end - @zone = OZones::Zones.get(@vdc.zones_ID) + @zone = OZones::Zones[@vdc.ZONES_ID] else @zone = zone end @@ -150,9 +156,17 @@ module OZones #------------------------------------------------------------------- # Create a vdc record & check cluster consistency #------------------------------------------------------------------- - @vdc = Vdc.new + begin - @vdc.attributes = vdc_data + + @vdc = Vdc.new + + @vdc.update(vdc_data) + @vdc.ZONES_ID = @zone.ID + + rescue => e + return OpenNebula::Error.new(e.message) + end rc = resources_in_cluster?(rsrc) @@ -198,10 +212,11 @@ module OZones rc, acl_ids = create_acls(rules) return rollback(group, user, acl_ids,rc) if OpenNebula.is_error?(rc) - OzonesServer::logger.debug {"ACLs #{acl_ids} created"} + OzonesServer::logger.debug {"ACLs #{acl_ids} created"} rsrc[:ACLS] = acl_ids @vdc.resources = rsrc + @vdc.save return true end @@ -283,10 +298,8 @@ module OZones #Update the VDC Record # ------------------------------------------------------------------ begin - @vdc.raise_on_save_failure = true @vdc.resources = rsrc_hash - - @vdc.save + @vdc.save(:raise_on_failure => true) rescue => e return OpenNebula::Error.new(e.message) end diff --git a/src/ozones/Server/lib/OZones/Zones.rb b/src/ozones/Server/lib/OZones/Zones.rb index 34b48e655a..25340469ed 100644 --- a/src/ozones/Server/lib/OZones/Zones.rb +++ b/src/ozones/Server/lib/OZones/Zones.rb @@ -16,29 +16,42 @@ module OZones - class Zones - include DataMapper::Resource + class Zones < Sequel::Model include OpenNebulaJSON::JSONUtils extend OpenNebulaJSON::JSONUtils + plugin :schema + plugin :validation_helpers + ####################################################################### # Data Model for the Zone ####################################################################### - property :ID, Serial - property :NAME, String, :required => true, :unique => true - property :ONENAME, String, :required => true - property :ONEPASS, String, :required => true - property :ENDPOINT, String, :required => true - property :SUNSENDPOINT, String - property :SELFENDPOINT, String + set_schema do + primary_key :ID + String :NAME, :unique => true + String :ONENAME + String :ONEPASS + String :ENDPOINT + String :SUNSENDPOINT + String :SELFENDPOINT + end - has n, :vdcs + create_table unless table_exists? + + one_to_many :vdcs, :class => 'OZones::Vdc', :key => :ZONES_ID ####################################################################### # Constants ####################################################################### ZONE_ATTRS = [:ONENAME, :ONEPASS, :ENDPOINT, :NAME] + def validate + super + validates_presence ZONE_ATTRS + validates_unique :NAME + end + + ####################################################################### # JSON Functions ####################################################################### @@ -47,11 +60,11 @@ module OZones zonePoolHash["ZONE_POOL"] = Hash.new zonePoolHash["ZONE_POOL"]["ZONE"] = Array.new unless self.all.empty? - self.all.each{|zone| - zattr = zone.attributes.clone + self.each{|zone| + zattr = zone.values.clone zattr[:ONEPASS] = Zones.encrypt(zattr[:ONEPASS]) - zattr[:NUMBERVDCS] = zone.vdcs.all.size + zattr[:NUMBERVDCS] = zone.vdcs.size zonePoolHash["ZONE_POOL"]["ZONE"] << zattr } @@ -62,12 +75,12 @@ module OZones def to_hash zattr = Hash.new - zattr["ZONE"] = attributes.clone + zattr["ZONE"] = @values.clone zattr["ZONE"][:ONEPASS] = Zones.encrypt(zattr["ZONE"][:ONEPASS]) zattr["ZONE"][:VDCS] = Array.new - self.vdcs.all.each{|vdc| - zattr["ZONE"][:VDCS]<< vdc.attributes.clone + self.vdcs.each{|vdc| + zattr["ZONE"][:VDCS]<< vdc.values.clone } return zattr @@ -111,10 +124,7 @@ module OZones # Create the zone begin zone = Zones.new - zone.raise_on_save_failure = true - - zone.attributes = zone_data - zone.save + zone.update(zone_data) rescue => e return OZones::Error.new(e.message) end @@ -165,7 +175,7 @@ module OZones ########################################################################## class OpenNebulaZone def initialize(zoneid) - @zone = Zones.get(zoneid) + @zone = Zones[zoneid] if !@zone raise "Error: Zone with id #{zoneid} not found" diff --git a/src/ozones/Server/models/Auth.rb b/src/ozones/Server/models/Auth.rb index 220c82a2b1..bda86623c9 100644 --- a/src/ozones/Server/models/Auth.rb +++ b/src/ozones/Server/models/Auth.rb @@ -14,12 +14,21 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -class Auth +class Auth < Sequel::Model + plugin :schema + plugin :validation_helpers - include DataMapper::Resource + set_schema do + primary_key :id + String :name, :unique => true + String :password + end - property :id, Serial - property :name, String, :required => true, :unique => true - property :password, String, :required => true + create_table unless table_exists? + def validate + super + validates_unique(:name) + validates_presence [:name, :password] + end end diff --git a/src/ozones/Server/models/OzonesServer.rb b/src/ozones/Server/models/OzonesServer.rb index 2777fe6532..ca58210ae1 100644 --- a/src/ozones/Server/models/OzonesServer.rb +++ b/src/ozones/Server/models/OzonesServer.rb @@ -36,7 +36,7 @@ class OzonesServer < CloudServer # Gets a VDC def get_vdc(id) - vdc = OZones::Vdc.get(id) + vdc = OZones::Vdc[id] if vdc return [200, vdc.to_json] @@ -53,7 +53,7 @@ class OzonesServer < CloudServer #Gets a zone def get_zone(id) - zone = OZones::Zones.get(id) + zone = OZones::Zones[id] if zone return [200, zone.to_json] @@ -101,7 +101,7 @@ class OzonesServer < CloudServer "Mandatory attribute zoneid missing.").to_json] end - zone = OZones::Zones.get(zoneid) + zone = OZones::Zones[zoneid] if !zone return [404, OZones::Error.new("Error: Couldn't create vdc. " \ "Zone #{zoneid} not found.").to_json] @@ -126,11 +126,10 @@ class OzonesServer < CloudServer #----------------------------------------------------------------------- #Update the zone and save the vdc #----------------------------------------------------------------------- - zone.raise_on_save_failure = true zone.vdcs << vdc.vdc begin - zone.save + zone.save(:raise_on_failture => true) rescue => e #vdc.clean_bootstrap logger.error {"create_vdc: #{e.resource.errors.inspect}"} @@ -231,7 +230,7 @@ class OzonesServer < CloudServer end def delete_zone(id, pr) - zone = OZones::Zones.get(id) + zone = OZones::Zones[id] if zone rc = zone.destroy @@ -264,7 +263,7 @@ class OzonesServer < CloudServer all_hosts = Array.new - zone.vdcs.all(:CLUSTER_ID =>c_id).each{ |vdc| + zone.vdcs(:CLUSTER_ID =>c_id).each{ |vdc| rsrc = vdc.resources if !rsrc[:HOSTS].empty? and vdc.ID != vdc_data[:ID] diff --git a/src/ozones/Server/ozones-server.rb b/src/ozones/Server/ozones-server.rb index 70eb42323a..dd9634a62e 100755 --- a/src/ozones/Server/ozones-server.rb +++ b/src/ozones/Server/ozones-server.rb @@ -51,7 +51,7 @@ require 'sinatra' require 'yaml' require 'rubygems' -require 'data_mapper' +require 'sequel' require 'digest/sha1' require 'OzonesServer' @@ -102,18 +102,16 @@ enable_logging OZONES_LOG, settings.config[:debug_level].to_i ############################################################################## # DB bootstrapping ############################################################################## -if config[:dbdebug] - DataMapper::Logger.new($stdout, :debug) -end -DataMapper.setup(:default, db_url) +DB = Sequel.connect(db_url) + +if config[:dbdebug] + DB.loggers << settings.logger +end require 'OZones' require 'Auth' -DataMapper.finalize -DataMapper.auto_upgrade! - if Auth.all.size == 0 if ENV['OZONES_AUTH'] && File.exist?(ENV['OZONES_AUTH']) credentials = IO.read(ENV['OZONES_AUTH']).strip.split(':') @@ -123,9 +121,10 @@ if Auth.all.size == 0 exit -1 end credentials[1] = Digest::SHA1.hexdigest(credentials[1]) - @auth=Auth.create({:name => credentials[0], - :password => credentials[1]}) - @auth.save + @auth=Auth.new + @auth.name = credentials[0] + @auth.password = credentials[1] + @auth.save(:raise_on_failure => true) else error_m = "oZones admin credentials not set, missing OZONES_AUTH file." settings.logger.error { error_m }