1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

feature #1112: Fix issues in VDC. Restores unique functionality. CloudServer has a static/class logger

This commit is contained in:
Ruben S. Montero 2012-03-16 17:22:29 +01:00
parent 2b78758892
commit 25460dd995
6 changed files with 105 additions and 31 deletions

View File

@ -67,6 +67,11 @@ class OneImageHelper < OpenNebulaHelper::OneHelper
OneImageHelper.type_to_str(d["TYPE"])
end
column :REGTIME, "Registration time of the Image",
:size=>20 do |d|
OpenNebulaHelper.time_to_str(d["REGTIME"])
end
column :PERSISTENT, "Whether the Image is persistent or not",
:size=>3 do |d|
OpenNebulaHelper.boolean_to_str(d["PERSISTENT"])

View File

@ -110,15 +110,27 @@ module CloudLogger
DATE_FORMAT = "%a %b %d %H:%M:%S %Y"
# Patch logger class to be compatible with Rack::CommonLogger
class ::Logger
class CloudLogger < Logger
def initialize(path)
super(path)
end
def write(msg)
info msg.chop
end
def add(severity, message = nil, progname = nil, &block)
rc = super(severity, message, progname, &block)
@logdev.dev.flush
rc
end
end
def enable_logging(path=nil, debug_level=3)
path ||= $stdout
logger = ::Logger.new(path)
logger = CloudLogger.new(path)
logger.level = DEBUG_LEVEL[debug_level]
logger.formatter = proc do |severity, datetime, progname, msg|
MSG_FORMAT % [

View File

@ -43,6 +43,13 @@ cmd=CommandParser::CmdParser.new(ARGV) do
########################################################################
set :option, CommandParser::OPTIONS
FORCE={
:name => "force",
:short => "-f",
:large => "--force",
:description => "Force the usage of Hosts in more than one VDC"
}
begin
helper = VDCHelper.new "vdc"
rescue Exception => e
@ -50,7 +57,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
exit -1
end
command :create, 'Create a new VDC', :file do
command :create, 'Create a new VDC', :file, :options=>[FORCE] do
helper.create_resource(args[0], options)
end

View File

@ -49,6 +49,9 @@ class VDCHelper < OZonesHelper::OZHelper
tmpl_hash["RESOURCES"] = { "HOSTS" => eval("[#{hosts}]"),
"DATASTORES" => eval("[#{ds}]"),
"NETWORKS" => eval("[#{nets}]") }
if options[:force]
tmpl_hash["FORCE"] = "YES"
end
vdc = { "#{@vdc_str.upcase}" => tmpl_hash }

View File

@ -270,26 +270,22 @@ EOT
def self.parse_error(value, kind)
if Zona.is_error?(value)
return value
else
if Zona.is_http_error?(value)
str = "Operating with #{kind} failed with HTTP error"
str += " code: #{value.code}\n"
if value.body
# Try to extract error message
begin
str << "Body: " <<
OZonesJSON.parse_json(value.body,
"error")["message"]
rescue
str.gsub!("\nBody:","")
end
end
return Error.new(str)
end
end
if Zona.is_http_error?(value)
str = "Operating with #{kind} failed with HTTP error"
str += " code: #{value.code}\n"
if value.body
ehash = OZonesJSON.parse_json(value.body,"error")
str << ehash[:message] if !ehash.nil?
end
return Error.new(str)
end
value # If it is not an error, return it as-is
end
end
# Parses a OpenNebula template string and turns it into a JSON string

View File

@ -90,8 +90,11 @@ class OzonesServer < CloudServer
"Reason: #{data.message}.").to_json]
end
#-----------------------------------------------------------------------
#Get the Zone that will host the VDC. And check resouces
#-----------------------------------------------------------------------
zoneid = vdc_data.delete(:ZONE_ID)
force = vdc_data.delete(:FORCE)
if !zoneid
return [400, OZones::Error.new("Error: Couldn't create vdc. " \
@ -104,7 +107,14 @@ class OzonesServer < CloudServer
"Zone #{zoneid} not found.").to_json]
end
if (!force or force.upcase != "YES") and !host_unique?(zone, vdc_data)
return [403, OZones::Error.new("Error: Couldn't create vdc. " \
"Hosts are not unique, use force to override").to_json]
end
#-----------------------------------------------------------------------
# Create de VDC
#-----------------------------------------------------------------------
vdc = OZones::OpenNebulaVdc.new(-1,zone)
rc = vdc.create(vdc_data)
@ -113,7 +123,9 @@ class OzonesServer < CloudServer
"Reason: #{rc.message}").to_json]
end
#-----------------------------------------------------------------------
#Update the zone and save the vdc
#-----------------------------------------------------------------------
zone.raise_on_save_failure = true
zone.vdcs << vdc.vdc
@ -160,17 +172,16 @@ class OzonesServer < CloudServer
if OpenNebula.is_error?(vdc_data)
return [400, OZones::Error.new("Error: Couldn't update vdc. " \
"Reason: #{data.message}.").to_json]
end
rsrc = vdc_data.delete(:RESOURCES)
# Check parameters
if !rsrc
end
#-----------------------------------------------------------------------
# Check parameters & VDC
#-----------------------------------------------------------------------
if !vdc_data[:RESOURCES]
return [400, OZones::Error.new("Error: Couldn't update vdc. " \
"Missing RESOURCES.").to_json]
end
# Check if the referenced Vdc exists
begin
vdc = OZones::OpenNebulaVdc.new(vdc_id)
rescue => e
@ -178,7 +189,17 @@ class OzonesServer < CloudServer
"#{e.message}").to_json]
end
rc = vdc.update(rsrc)
vdc_data[:CLUSTER_ID] = vdc.CLUSTER_ID
vdc_data[:ID] = vdc.ID
force = vdc_data.delete(:FORCE)
if (!force or force.upcase!="YES") and !host_unique?(vdc.zone, vdc_data)
return [403, OZones::Error.new("Error: Couldn't update vdc. " \
"Hosts are not unique, use force to override").to_json]
end
rc = vdc.update(vdc_data[:RESOURCES])
if !OpenNebula.is_error?(rc)
return [200, rc]
@ -215,9 +236,8 @@ class OzonesServer < CloudServer
if zone
rc = zone.destroy
else
return [404,
OZones::Error.new("Error: Cannot delete " \
"zone. Reason: zone #{id} not found").to_json]
return [404, OZones::Error.new("Error: Cannot delete " \
"zone. Reason: zone #{id} not found").to_json]
end
if !rc
@ -228,4 +248,35 @@ class OzonesServer < CloudServer
return [200, OZones.str_to_json("Zone #{id} successfully deleted")]
end
end
############################################################################
# Misc Helper Functions
############################################################################
private
# Check if hosts are already include in any Vdc of the zone
def host_unique?(zone, vdc_data)
hosts = vdc_data[:RESOURCES][:HOSTS]
c_id = vdc_data[:CLUSTER_ID]
return true if hosts.empty?
all_hosts = Array.new
zone.vdcs.all(:CLUSTER_ID =>c_id).each{ |vdc|
rsrc = vdc.resources
if !rsrc[:HOSTS].empty? and vdc.ID != vdc_data[:ID]
all_hosts.concat(rsrc[:HOSTS])
end
}
hosts.each{|hid|
return false if all_hosts.include?(hid)
}
return true
end
end