From 51db1d2fadb36d7cfa0d3a1c96bbe4c1d3e215ee Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Tue, 14 Jun 2011 12:51:56 +0200 Subject: [PATCH 1/5] feature #573: erb engine for plugins --- src/sunstone/sunstone-server.rb | 26 ++++++++++++++++++- .../{templates/index.html => views/index.erb} | 8 +++++- 2 files changed, 32 insertions(+), 2 deletions(-) rename src/sunstone/{templates/index.html => views/index.erb} (95%) diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index 94e1274f4d..a31716343f 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -43,6 +43,7 @@ $: << File.dirname(__FILE__)+'/share/OneMonitor' ############################################################################## require 'rubygems' require 'sinatra' +require 'erb' require 'cloud/Configuration' require 'SunstoneServer' @@ -96,6 +97,27 @@ helpers do return [204, ""] end + def user_plugins + plugins = Array.new + base_path = File.dirname(__FILE__)+'/public' + static_plugins = [ "/js/plugins/templates-tab.js", + "/js/plugins/dashboard-tab.js", + "/js/plugins/groups-tab.js", + "/js/plugins/hosts-tab.js", + "/js/plugins/vms-tab.js", + "/js/plugins/images-tab.js", + "/js/plugins/vnets-tab.js", + "/js/plugins/users-tab.js" ] + + Dir[base_path+'/js/plugins/*'].each do |p| + m = p.match(/^#{base_path}(.*\.js)$/) + if m and plugin = m[1] + plugins << plugin if !static_plugins.include? plugin + end + end + + plugins + end end before do @@ -132,7 +154,9 @@ get '/' do :value=>"#{session[:user_id]}", :expires=>time) - File.read(File.dirname(__FILE__)+'/templates/index.html') + #File.read(File.dirname(__FILE__)+'/templates/index.html' + @user_plugins = user_plugins + erb :index end get '/login' do diff --git a/src/sunstone/templates/index.html b/src/sunstone/views/index.erb similarity index 95% rename from src/sunstone/templates/index.html rename to src/sunstone/views/index.erb index 883487d9eb..0c280ac345 100644 --- a/src/sunstone/templates/index.html +++ b/src/sunstone/views/index.erb @@ -36,9 +36,15 @@ - +<% if !@user_plugins.empty? %> + +<% @user_plugins.each do |plugin| %> + +<% end %> +<% end %> +
From 50a33cd8f7ba9b70e73e1aed10f34973e1184841 Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Tue, 14 Jun 2011 14:26:55 +0200 Subject: [PATCH 2/5] feature #573: specific dir for plugins --- src/sunstone/sunstone-server.rb | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index a31716343f..15fa9219cf 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -98,21 +98,12 @@ helpers do end def user_plugins - plugins = Array.new base_path = File.dirname(__FILE__)+'/public' - static_plugins = [ "/js/plugins/templates-tab.js", - "/js/plugins/dashboard-tab.js", - "/js/plugins/groups-tab.js", - "/js/plugins/hosts-tab.js", - "/js/plugins/vms-tab.js", - "/js/plugins/images-tab.js", - "/js/plugins/vnets-tab.js", - "/js/plugins/users-tab.js" ] - - Dir[base_path+'/js/plugins/*'].each do |p| + plugins = Array.new + Dir[base_path+'/js/user-plugins/*'].each do |p| m = p.match(/^#{base_path}(.*\.js)$/) if m and plugin = m[1] - plugins << plugin if !static_plugins.include? plugin + plugins << plugin end end From bff8f3011c505d5a000007231b34c65330def90c Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Tue, 14 Jun 2011 14:27:15 +0200 Subject: [PATCH 3/5] feature #573: update install.sh --- install.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/install.sh b/install.sh index 681b8cff95..0281339fcc 100755 --- a/install.sh +++ b/install.sh @@ -205,6 +205,7 @@ SUNSTONE_DIRS="$SUNSTONE_LOCATION/models \ $SUNSTONE_LOCATION/public \ $SUNSTONE_LOCATION/public/js \ $SUNSTONE_LOCATION/public/js/plugins \ + $SUNSTONE_LOCATION/public/js/user-plugins \ $SUNSTONE_LOCATION/public/css \ $SUNSTONE_LOCATION/public/vendor \ $SUNSTONE_LOCATION/public/vendor/jQueryLayout \ @@ -213,7 +214,8 @@ SUNSTONE_DIRS="$SUNSTONE_LOCATION/models \ $SUNSTONE_LOCATION/public/vendor/jQuery \ $SUNSTONE_LOCATION/public/vendor/jGrowl \ $SUNSTONE_LOCATION/public/images \ - $SUNSTONE_LOCATION/templates" + $SUNSTONE_LOCATION/templates \ + $SUNSTONE_LOCATION/views" LIB_ECO_CLIENT_DIRS="$LIB_LOCATION/ruby \ $LIB_LOCATION/ruby/OpenNebula \ @@ -307,6 +309,7 @@ INSTALL_SUNSTONE_FILES=( SUNSTONE_MODELS_FILES:$SUNSTONE_LOCATION/models SUNSTONE_MODELS_JSON_FILES:$SUNSTONE_LOCATION/models/OpenNebulaJSON SUNSTONE_TEMPLATE_FILES:$SUNSTONE_LOCATION/templates + SUNSTONE_VIEWS_FILES:$SUNSTONE_LOCATION/views SUNSTONE_PUBLIC_JS_FILES:$SUNSTONE_LOCATION/public/js SUNSTONE_PUBLIC_JS_PLUGINS_FILES:$SUNSTONE_LOCATION/public/js/plugins SUNSTONE_PUBLIC_CSS_FILES:$SUNSTONE_LOCATION/public/css @@ -753,8 +756,9 @@ SUNSTONE_MODELS_JSON_FILES="src/sunstone/models/OpenNebulaJSON/ClusterJSON.rb \ src/sunstone/models/OpenNebulaJSON/TemplateJSON.rb \ src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb" -SUNSTONE_TEMPLATE_FILES="src/sunstone/templates/index.html \ - src/sunstone/templates/login.html" +SUNSTONE_TEMPLATE_FILES="src/sunstone/templates/login.html" + +SUNSTONE_VIEWS_FILES="src/sunstone/views/index.erb" SUNSTONE_PUBLIC_JS_FILES="src/sunstone/public/js/layout.js \ src/sunstone/public/js/login.js \ From 2ce569960ff551f4e025bd8ca74c20975120bbf7 Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Fri, 17 Jun 2011 11:43:53 +0200 Subject: [PATCH 4/5] feature #573: improve yaml format and update install.sh --- install.sh | 6 +- src/sunstone/config.ru | 5 +- src/sunstone/etc/sunstone-plugins.yaml | 33 ++++++ src/sunstone/models/SunstonePlugins.rb | 102 ++++++++++++++++++ src/sunstone/models/plugins.rb | 100 +++++++++++++++++ .../share/OneMonitor/runOneMonitor.rb | 2 - src/sunstone/sunstone-server.rb | 29 ++--- src/sunstone/views/index.erb | 24 ++--- 8 files changed, 261 insertions(+), 40 deletions(-) create mode 100644 src/sunstone/etc/sunstone-plugins.yaml create mode 100644 src/sunstone/models/SunstonePlugins.rb create mode 100644 src/sunstone/models/plugins.rb diff --git a/install.sh b/install.sh index 0281339fcc..0ffdb22180 100755 --- a/install.sh +++ b/install.sh @@ -741,10 +741,12 @@ SUNSTONE_FILES="src/sunstone/config.ru \ SUNSTONE_BIN_FILES="src/sunstone/bin/sunstone-server" -SUNSTONE_ETC_FILES="src/sunstone/etc/sunstone-server.conf" +SUNSTONE_ETC_FILES="src/sunstone/etc/sunstone-server.conf \ + src/sunstone/etc/sunstone-plugins.yaml" SUNSTONE_MODELS_FILES="src/sunstone/models/OpenNebulaJSON.rb \ - src/sunstone/models/SunstoneServer.rb" + src/sunstone/models/SunstoneServer.rb \ + src/sunstone/models/SunstonePlugins.rb" SUNSTONE_MODELS_JSON_FILES="src/sunstone/models/OpenNebulaJSON/ClusterJSON.rb \ src/sunstone/models/OpenNebulaJSON/HostJSON.rb \ diff --git a/src/sunstone/config.ru b/src/sunstone/config.ru index 8b695e23af..9c0c37947d 100644 --- a/src/sunstone/config.ru +++ b/src/sunstone/config.ru @@ -16,8 +16,9 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -# TBD Change path for intallation tree -$: << File.dirname(__FILE__) +SUNSTONE_ROOT_DIR = File.dirname(__FILE__) + +$: << SUNSTONE_ROOT_DIR require 'sunstone-server.rb' diff --git a/src/sunstone/etc/sunstone-plugins.yaml b/src/sunstone/etc/sunstone-plugins.yaml new file mode 100644 index 0000000000..05b6eb0aed --- /dev/null +++ b/src/sunstone/etc/sunstone-plugins.yaml @@ -0,0 +1,33 @@ +--- +- plugins/dashboard-tab.js: + :ALL: true + :user: + :group: +- plugins/hosts-tab.js: + :ALL: true + :user: + :group: +- plugins/groups-tab.js: + :ALL: true + :user: + :group: +- plugins/templates-tab.js: + :ALL: true + :user: + :group: +- plugins/vms-tab.js: + :ALL: true + :user: + :group: +- plugins/vnets-tab.js: + :ALL: true + :user: + :group: +- plugins/images-tab.js: + :ALL: true + :user: + :group: +- plugins/users-tab.js: + :ALL: true + :user: + :group: diff --git a/src/sunstone/models/SunstonePlugins.rb b/src/sunstone/models/SunstonePlugins.rb new file mode 100644 index 0000000000..e9246437f2 --- /dev/null +++ b/src/sunstone/models/SunstonePlugins.rb @@ -0,0 +1,102 @@ +# -------------------------------------------------------------------------- # +# 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 'yaml' +require 'json' + +class SunstonePlugins + USER_PLUGIN_POLICY = false # or true to enable them by default + + attr_reader :plugins_conf + + def initialize + load_conf + check_plugins + end + + def load_conf + @plugins_conf = YAML.load_file(PLUGIN_CONFIGURATION_FILE) + end + + def check_plugins + base_path = SUNSTONE_ROOT_DIR+'/public/js/' + + @installed_plugins = Array.new + + # read user plugins + modified = false + Dir[base_path+'user-plugins/*.js'].each do |p_path| + m = p_path.match(/^#{base_path}(.*)$/) + if m and plugin = m[1] + @installed_plugins << plugin + if !plugins.include? plugin + @plugins_conf << {plugin=>{:ALL => USER_PLUGIN_POLICY, + :user => nil, + :group => nil}} + modified = true + end + end + end + write_conf if modified + + # read base plugins + Dir[base_path+'plugins/*.js'].each do |p_path| + m = p_path.match(/^#{base_path}(.*)$/) + if m and plugin = m[1] + @installed_plugins << plugin + end + end + end + + def plugins + @plugins_conf.collect{|p| p.keys[0]} + end + + def installed?(plugin) + @installed_plugins.include? plugin + end + + def authorized_plugins(user,group=nil) + auth_plugins = {"user-plugins"=>Array.new, "plugins"=>Array.new} + + @plugins_conf.each do |plugin_conf| + plugin = plugin_conf.keys.first + perms = plugin_conf[plugin] + if installed?(plugin) + p_path, p_name = plugin.split('/') + + if perms[:user] and perms[:user][user] + auth_plugins[p_path] << p_name + elsif perms[:group] and perms[:group][group] + auth_plugins[p_path] << p_name + elsif perms[:ALL] + auth_plugins[p_path] << p_name + end + end + end + auth_plugins + end + + def write_conf + File.open(PLUGIN_CONFIGURATION_FILE,'w') do |f| + f.write(@plugins_conf.to_yaml) + end + end + + def to_json + @plugins_conf.to_json + end +end diff --git a/src/sunstone/models/plugins.rb b/src/sunstone/models/plugins.rb new file mode 100644 index 0000000000..241dc03625 --- /dev/null +++ b/src/sunstone/models/plugins.rb @@ -0,0 +1,100 @@ +# -------------------------------------------------------------------------- # +# 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 'yaml' +require 'json' + +class Plugins # TODO SunstonePlugins + USER_PLUGIN_POLICY = false # or true to enable them by default + + attr_reader :plugins_conf + + def initialize + load_conf + check_plugins + end + + def load_conf + @plugins_conf = YAML.load(File.read(PLUGIN_CONFIGURATION_FILE)) #TODO load file + end + + def check_plugins + base_path = SUNSTONE_ROOT_DIR+'/public/js/' + + @installed_plugins = Array.new + + # read user plugins + modified = false + Dir[base_path+'user-plugins/*.js'].each do |p| #TODO cambiar p + m = p.match(/^#{base_path}(.*)$/) + if m and plugin = m[1] + @installed_plugins << plugin + if !plugins.include? plugin + @plugins_conf << {plugin=>{:ALL => USER_PLUGIN_POLICY, + :user => nil, + :group => nil}} + modified = true + end + end + end + write_conf if modified + + # read base plugins + Dir[base_path+'plugins/*.js'].each do |p| + m = p.match(/^#{base_path}(.*)$/) + if m and plugin = m[1] + @installed_plugins << plugin + end + end + end + + def plugins + @plugins_conf.collect{|p| p.keys[0]} + end + + def installed?(plugin) + @installed_plugins.include? plugin + end + + def authorized_plugins(user,group=nil) + p = {"user-plugins"=>Array.new, "plugins"=>Array.new} #TODO cambiar p + + @plugins_conf.each do |plugin_conf| + plugin = plugin_conf.keys.first + perms = plugin_conf[plugin] + if installed?(plugin) + p_path, p_name = plugin.split('/') + + if perms[:user] and perms[:user][user] + p[p_path] << p_name + elsif perms[:group] and perms[:group][group] + p[p_path] << p_name + elsif perms[:ALL] + p[p_path] << p_name + end + end + end + p + end + + def write_conf + File.open(PLUGIN_CONFIGURATION_FILE,'w') {|f| f.write(@plugins_conf.to_yaml)} + end + + def to_json + @plugins_conf.to_json + end +end diff --git a/src/sunstone/share/OneMonitor/runOneMonitor.rb b/src/sunstone/share/OneMonitor/runOneMonitor.rb index 48818a852c..8ca591235b 100755 --- a/src/sunstone/share/OneMonitor/runOneMonitor.rb +++ b/src/sunstone/share/OneMonitor/runOneMonitor.rb @@ -16,8 +16,6 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -$: << File.dirname(__FILE__) - require 'HostMonitor.rb' require 'VMMonitor.rb' diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index 15fa9219cf..a1f83032b4 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -15,7 +15,7 @@ # 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. # -#--------------------------------------------------------------------------- # +#--------------------------------------------------------------------------- # ONE_LOCATION = ENV["ONE_LOCATION"] @@ -24,19 +24,21 @@ if !ONE_LOCATION VAR_LOCATION = "/var/lib/one" RUBY_LIB_LOCATION = "/usr/lib/one/ruby" CONFIGURATION_FILE = "/etc/one/sunstone-server.conf" + PLUGIN_CONFIGURATION_FILE = "/etc/sunstone-plugins.yaml" else VAR_LOCATION = ONE_LOCATION+"/var" LOG_LOCATION = ONE_LOCATION+"/var" RUBY_LIB_LOCATION = ONE_LOCATION+"/lib/ruby" CONFIGURATION_FILE = ONE_LOCATION+"/etc/sunstone-server.conf" + PLUGIN_CONFIGURATION_FILE = ONE_LOCATION+"/etc/sunstone-plugins.yaml" end HOST_LOG_FOLDER = LOG_LOCATION+"/OneMonitor/host" VM_LOG_FOLDER = LOG_LOCATION+"/OneMonitor/vm" $: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__)+'/models' -$: << File.dirname(__FILE__)+'/share/OneMonitor' +$: << SUNSTONE_ROOT_DIR+'/models' +$: << SUNSTONE_ROOT_DIR+'/share/OneMonitor' ############################################################################## # Required libraries @@ -47,6 +49,7 @@ require 'erb' require 'cloud/Configuration' require 'SunstoneServer' +require 'SunstonePlugins' set :config, Configuration.new(CONFIGURATION_FILE) @@ -96,19 +99,6 @@ helpers do session.clear return [204, ""] end - - def user_plugins - base_path = File.dirname(__FILE__)+'/public' - plugins = Array.new - Dir[base_path+'/js/user-plugins/*'].each do |p| - m = p.match(/^#{base_path}(.*\.js)$/) - if m and plugin = m[1] - plugins << plugin - end - end - - plugins - end end before do @@ -145,13 +135,14 @@ get '/' do :value=>"#{session[:user_id]}", :expires=>time) - #File.read(File.dirname(__FILE__)+'/templates/index.html' - @user_plugins = user_plugins + p = SunstonePlugins.new + @plugins = p.authorized_plugins(session[:user]) + erb :index end get '/login' do - File.read(File.dirname(__FILE__)+'/templates/login.html') + File.read(SUNSTONE_ROOT_DIR+'/templates/login.html') end ############################################################################## diff --git a/src/sunstone/views/index.erb b/src/sunstone/views/index.erb index 0c280ac345..7a4bb97de7 100644 --- a/src/sunstone/views/index.erb +++ b/src/sunstone/views/index.erb @@ -27,23 +27,17 @@ - - - - - - - - - - - -<% if !@user_plugins.empty? %> - -<% @user_plugins.each do |plugin| %> - + +<% @plugins["plugins"].each do |plugin| %> + <% end %> + + + +<% @plugins["user-plugins"].each do |plugin| %> + <% end %> + From 6fe828c0e6ae6454a44fbd68263ec756501915b7 Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Fri, 17 Jun 2011 11:44:23 +0200 Subject: [PATCH 5/5] feature #573: remove plugins.rb --- src/sunstone/models/plugins.rb | 100 --------------------------------- 1 file changed, 100 deletions(-) delete mode 100644 src/sunstone/models/plugins.rb diff --git a/src/sunstone/models/plugins.rb b/src/sunstone/models/plugins.rb deleted file mode 100644 index 241dc03625..0000000000 --- a/src/sunstone/models/plugins.rb +++ /dev/null @@ -1,100 +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. # -#--------------------------------------------------------------------------- # - -require 'yaml' -require 'json' - -class Plugins # TODO SunstonePlugins - USER_PLUGIN_POLICY = false # or true to enable them by default - - attr_reader :plugins_conf - - def initialize - load_conf - check_plugins - end - - def load_conf - @plugins_conf = YAML.load(File.read(PLUGIN_CONFIGURATION_FILE)) #TODO load file - end - - def check_plugins - base_path = SUNSTONE_ROOT_DIR+'/public/js/' - - @installed_plugins = Array.new - - # read user plugins - modified = false - Dir[base_path+'user-plugins/*.js'].each do |p| #TODO cambiar p - m = p.match(/^#{base_path}(.*)$/) - if m and plugin = m[1] - @installed_plugins << plugin - if !plugins.include? plugin - @plugins_conf << {plugin=>{:ALL => USER_PLUGIN_POLICY, - :user => nil, - :group => nil}} - modified = true - end - end - end - write_conf if modified - - # read base plugins - Dir[base_path+'plugins/*.js'].each do |p| - m = p.match(/^#{base_path}(.*)$/) - if m and plugin = m[1] - @installed_plugins << plugin - end - end - end - - def plugins - @plugins_conf.collect{|p| p.keys[0]} - end - - def installed?(plugin) - @installed_plugins.include? plugin - end - - def authorized_plugins(user,group=nil) - p = {"user-plugins"=>Array.new, "plugins"=>Array.new} #TODO cambiar p - - @plugins_conf.each do |plugin_conf| - plugin = plugin_conf.keys.first - perms = plugin_conf[plugin] - if installed?(plugin) - p_path, p_name = plugin.split('/') - - if perms[:user] and perms[:user][user] - p[p_path] << p_name - elsif perms[:group] and perms[:group][group] - p[p_path] << p_name - elsif perms[:ALL] - p[p_path] << p_name - end - end - end - p - end - - def write_conf - File.open(PLUGIN_CONFIGURATION_FILE,'w') {|f| f.write(@plugins_conf.to_yaml)} - end - - def to_json - @plugins_conf.to_json - end -end