1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-23 17:33:56 +03:00

feature #789: Moved Configuration.rb to OCA. Added support to print configuration files in JSON. Updated OZones client to use Configuration.rb parser

This commit is contained in:
Ruben S. Montero 2011-10-07 15:02:31 +02:00
parent d6defb6bb8
commit d6f1f5b1fc
6 changed files with 129 additions and 135 deletions

View File

@ -20,7 +20,7 @@
# Install program for OpenNebula. It will install it relative to
# $ONE_LOCATION if defined with the -d option, otherwise it'll be installed
# under /. In this case you may specified the oneadmin user/group, so you do
# not need run the OpenNebula daemon with root priviledges
# not need run the OpenNebula daemon with root privileges
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
@ -300,7 +300,7 @@ elif [ "$SUNSTONE" = "yes" ]; then
elif [ "$OZONES" = "yes" ]; then
MAKE_DIRS="$MAKE_DIRS $OZONES_DIRS $OZONES_CLIENT_DIRS $LIB_OCA_CLIENT_DIRS"
else
MAKE_DIRS="$MAKE_DIRS $SHARE_DIRS $ETC_DIRS $LIB_DIRS $VAR_DIRS \
MAKE_DIRS="$MAKE_DIRS $SHARE_DIRS $ETC_DIRS $LIB_DIRS $VAR_DIRS \
$OZONES_DIRS $OZONES_CLIENT_DIRS $SUNSTONE_DIRS"
fi
@ -655,8 +655,7 @@ ONEDB_MIGRATOR_FILES="src/onedb/2.0_to_2.9.80.rb \
src/onedb/2.9.85_to_2.9.90.rb \
src/onedb/2.9.90_to_3.0.rb \
src/onedb/onedb.rb \
src/onedb/onedb_backend.rb \
src/onedb/Configuration.rb"
src/onedb/onedb_backend.rb"
#-------------------------------------------------------------------------------
# Configuration files for OpenNebula, to be installed under $ETC_LOCATION
@ -786,7 +785,8 @@ RUBY_OPENNEBULA_LIB_FILES="src/oca/ruby/OpenNebula/Host.rb \
src/oca/ruby/OpenNebula/GroupPool.rb \
src/oca/ruby/OpenNebula/Acl.rb \
src/oca/ruby/OpenNebula/AclPool.rb \
src/oca/ruby/OpenNebula/XMLUtils.rb"
src/oca/ruby/OpenNebula/XMLUtils.rb \
src/oca/ruby/OpenNebula/Configuration.rb"
#-------------------------------------------------------------------------------
# Common Cloud Files

View File

@ -0,0 +1,116 @@
# -------------------------------------------------------------------------- #
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
require 'json'
module OpenNebula
############################################################################
# The Configuration Class represents a simple configuration file using
# OpenNebula syntax. It does not check syntax.
############################################################################
class Configuration
attr_reader :conf
########################################################################
# Patterns to parse the Configuration File
########################################################################
NAME_REG =/[\w\d_-]+/
VARIABLE_REG =/\s*(#{NAME_REG})\s*=\s*/
SIMPLE_VARIABLE_REG =/#{VARIABLE_REG}([^\[]+?)(#.*)?/
SINGLE_VARIABLE_REG =/^#{SIMPLE_VARIABLE_REG}$/
ARRAY_VARIABLE_REG =/^#{VARIABLE_REG}\[(.*?)\]/m
########################################################################
########################################################################
def initialize(str)
parse_conf(str)
end
def self.new_from_file(file)
conf_file = File.read(file)
self.new(conf_file)
end
def add_value(key, value)
if @conf[key]
if !@conf[key].kind_of?(Array)
@conf[key]=[@conf[key]]
end
@conf[key]<<value
else
@conf[key]=value
end
end
def [](key)
@conf[key.to_s.upcase]
end
def to_json
JSON::generate(@conf) if @conf
end
########################################################################
########################################################################
private
#
#
#
def parse_conf(conf_file)
@conf=Hash.new
conf_file.scan(SINGLE_VARIABLE_REG) {|m|
key=m[0].strip.upcase
value=m[1].strip
# hack to skip multiline VM_TYPE values
next if %w{NAME TEMPLATE}.include? key.upcase
add_value(key, value)
}
conf_file.scan(ARRAY_VARIABLE_REG) {|m|
master_key=m[0].strip.upcase
pieces=m[1].split(',')
vars=Hash.new
pieces.each {|p|
key, value=p.split('=')
vars[key.strip.upcase]=value.strip
}
add_value(master_key, vars)
}
end
end
#
# Test program for the Configuration Parser
#
if $0 == __FILE__
require 'pp'
conf=Configuration.new_from_file('cloud.conf')
pp conf
end
end

View File

@ -127,8 +127,8 @@ module OpenNebula
# Returns whether or not the user with id 'uid' is part of this group
def contains(uid)
# This doesn't work in ruby 1.8.5
# return self["USERS/ID[.=#{uid}]"] != nil
#This doesn't work in ruby 1.8.5
#return self["USERS/ID[.=#{uid}]"] != nil
id_array = retrieve_elements('USERS/ID')
return id_array != nil && id_array.include?(uid.to_s)

View File

@ -1,115 +0,0 @@
# -------------------------------------------------------------------------- #
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
###############################################################################
# The Configuration Class represents a simple configuration file for the
# Cloud servers. It does not check syntax.
###############################################################################
class Configuration
###########################################################################
# Patterns to parse the Configuration File
###########################################################################
NAME_REG =/[\w\d_-]+/
VARIABLE_REG =/\s*(#{NAME_REG})\s*=\s*/
SIMPLE_VARIABLE_REG =/#{VARIABLE_REG}([^\[]+?)(#.*)?/
SINGLE_VARIABLE_REG =/^#{SIMPLE_VARIABLE_REG}$/
ARRAY_VARIABLE_REG =/^#{VARIABLE_REG}\[(.*?)\]/m
###########################################################################
###########################################################################
def initialize(file)
@conf=parse_conf(file)
end
def add_configuration_value(key,value)
add_value(@conf,key,value)
end
def [](key)
@conf[key.to_s.upcase]
end
###########################################################################
###########################################################################
private
#
#
#
def add_value(conf, key, value)
if conf[key]
if !conf[key].kind_of?(Array)
conf[key]=[conf[key]]
end
conf[key]<<value
else
conf[key]=value
end
end
#
#
#
def parse_conf(file)
conf_file=File.read(file)
conf=Hash.new
conf_file.scan(SINGLE_VARIABLE_REG) {|m|
key=m[0].strip.upcase
value=m[1].strip
# hack to skip multiline VM_TYPE values
next if %w{NAME TEMPLATE}.include? key.upcase
add_value(conf, key, value)
}
conf_file.scan(ARRAY_VARIABLE_REG) {|m|
master_key=m[0].strip.upcase
pieces=m[1].split(',')
vars=Hash.new
pieces.each {|p|
key, value=p.split('=')
vars[key.strip.upcase]=value.strip
}
add_value(conf, master_key, vars)
}
conf
end
end
#
# Test program for the Configuration Parser
#
if $0 == __FILE__
require 'pp'
conf=Configuration.new('cloud.conf')
pp conf
end

View File

@ -14,7 +14,7 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
require 'Configuration'
require 'OpenNebula/Configuration'
require 'onedb_backend'
class OneDB
@ -152,7 +152,7 @@ class OneDB
private
def from_onedconf()
config = Configuration.new("#{ETC_LOCATION}/oned.conf")
config = OpenNebula::Configuration.new_from_file("#{ETC_LOCATION}/oned.conf")
if config[:db] == nil
raise "No DB defined."

View File

@ -18,6 +18,7 @@ require 'rubygems'
require 'uri'
require 'net/https'
require 'json'
require 'OpenNebula/Configuration'
module OZonesClient
class Client
@ -249,18 +250,10 @@ EOT
##########################################################################
def self.to_body(kind, tmpl_str)
body_str = "{\n"
body_str << " \"#{kind}\": {"
tmpl_str.strip.each_line{|line|
line.strip!
key,value = line.split("=")
body_str << "\n \"#{key}\": \"#{value}\","
}
body_str.chop!
tmpl = OpenNebula::Configuration.new(tmpl_str)
res = { "#{kind}" => tmpl.conf }
body_str << "\n }\n"
body_str << "}\n"
return JSON::generate(res)
end
def self.parse_json(json_str, root_element)