From e246af0fe5be045a7acc9b8f18c93fae32fbe9b2 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 14 Apr 2011 01:10:20 +0200 Subject: [PATCH] feature #575: Some refactor of opennebula drivers code. Command execution wrappers moved to base class --- src/authm_mad/{spec => test}/client_mock.rb | 0 src/authm_mad/{spec => test}/db_helpers.rb | 0 src/authm_mad/{spec => test}/oca_vms.yaml | 0 .../{spec => test}/one_usage_spec.rb | 0 src/authm_mad/{spec => test}/quota_spec.rb | 0 .../{spec => test}/simple_permissions_spec.rb | 0 src/authm_mad/{spec => test}/spec_common.rb | 0 src/hm/HookManagerDriver.cc | 4 +- src/hm_mad/one_hm.rb | 12 ++-- src/im_mad/im_sh/one_im_sh.rb | 46 ++++++++----- src/im_mad/im_ssh/one_im_ssh.rb | 27 ++------ src/image_mad/one_image.rb | 22 +----- src/mad/ruby/OpenNebulaDriver.rb | 67 +++++++++++++++++-- src/mad/ruby/VirtualMachineDriver.rb | 49 +++----------- 14 files changed, 114 insertions(+), 113 deletions(-) rename src/authm_mad/{spec => test}/client_mock.rb (100%) rename src/authm_mad/{spec => test}/db_helpers.rb (100%) rename src/authm_mad/{spec => test}/oca_vms.yaml (100%) rename src/authm_mad/{spec => test}/one_usage_spec.rb (100%) rename src/authm_mad/{spec => test}/quota_spec.rb (100%) rename src/authm_mad/{spec => test}/simple_permissions_spec.rb (100%) rename src/authm_mad/{spec => test}/spec_common.rb (100%) diff --git a/src/authm_mad/spec/client_mock.rb b/src/authm_mad/test/client_mock.rb similarity index 100% rename from src/authm_mad/spec/client_mock.rb rename to src/authm_mad/test/client_mock.rb diff --git a/src/authm_mad/spec/db_helpers.rb b/src/authm_mad/test/db_helpers.rb similarity index 100% rename from src/authm_mad/spec/db_helpers.rb rename to src/authm_mad/test/db_helpers.rb diff --git a/src/authm_mad/spec/oca_vms.yaml b/src/authm_mad/test/oca_vms.yaml similarity index 100% rename from src/authm_mad/spec/oca_vms.yaml rename to src/authm_mad/test/oca_vms.yaml diff --git a/src/authm_mad/spec/one_usage_spec.rb b/src/authm_mad/test/one_usage_spec.rb similarity index 100% rename from src/authm_mad/spec/one_usage_spec.rb rename to src/authm_mad/test/one_usage_spec.rb diff --git a/src/authm_mad/spec/quota_spec.rb b/src/authm_mad/test/quota_spec.rb similarity index 100% rename from src/authm_mad/spec/quota_spec.rb rename to src/authm_mad/test/quota_spec.rb diff --git a/src/authm_mad/spec/simple_permissions_spec.rb b/src/authm_mad/test/simple_permissions_spec.rb similarity index 100% rename from src/authm_mad/spec/simple_permissions_spec.rb rename to src/authm_mad/test/simple_permissions_spec.rb diff --git a/src/authm_mad/spec/spec_common.rb b/src/authm_mad/test/spec_common.rb similarity index 100% rename from src/authm_mad/spec/spec_common.rb rename to src/authm_mad/test/spec_common.rb diff --git a/src/hm/HookManagerDriver.cc b/src/hm/HookManagerDriver.cc index 90813ddc29..ed6a294e6d 100644 --- a/src/hm/HookManagerDriver.cc +++ b/src/hm/HookManagerDriver.cc @@ -156,14 +156,14 @@ void HookManagerDriver::protocol( if ( is.good() ) { - is >> hook_name >> ws; + getline(is,hook_name); } getline (is,info); if (result == "SUCCESS") { - oss << "Hook " << hook_name << " successfully executed. " << info; + oss << "Success executing Hook: " << hook_name << ". " << info; vm->log("HKM",Log::INFO,oss); } else diff --git a/src/hm_mad/one_hm.rb b/src/hm_mad/one_hm.rb index 403ed6ac33..d1d5948b5b 100755 --- a/src/hm_mad/one_hm.rb +++ b/src/hm_mad/one_hm.rb @@ -28,14 +28,11 @@ end $: << RUBY_LIB_LOCATION -require 'pp' require 'OpenNebulaDriver' -require 'CommandManager' - class HookManagerDriver < OpenNebulaDriver def initialize(num) - super(num, true) + super(num, true, 0) register_action(:EXECUTE, method("action_execute")) end @@ -51,13 +48,14 @@ class HookManagerDriver < OpenNebulaDriver end if cmd.code==0 - send_message("EXECUTE", RESULT[:success], number, hook_name) + message = "#{hook_name}: #{cmd.stdout}" + send_message("EXECUTE", RESULT[:success], number, message) else - send_message("EXECUTE", RESULT[:failure], number, hook_name) + message = "#{hook_name}: #{cmd.get_error_message}" + send_message("EXECUTE", RESULT[:failure], number, message) end end end hm=HookManagerDriver.new(15) hm.start_driver - diff --git a/src/im_mad/im_sh/one_im_sh.rb b/src/im_mad/im_sh/one_im_sh.rb index 8522b69ddb..6188643260 100755 --- a/src/im_mad/im_sh/one_im_sh.rb +++ b/src/im_mad/im_sh/one_im_sh.rb @@ -31,18 +31,18 @@ end $: << RUBY_LIB_LOCATION require 'OpenNebulaDriver' -require 'CommandManager' +require 'getoptlong' #------------------------------------------------------------------------------- # The Local Information Manager Driver #------------------------------------------------------------------------------- -class InformationManager < OpenNebulaDriver +class InformationManagerDriverSH < OpenNebulaDriver #--------------------------------------------------------------------------- # Init the driver #--------------------------------------------------------------------------- def initialize(hypervisor, num) - super(num, true) + super(num, true, 0) @config = read_configuration @hypervisor = hypervisor @@ -57,30 +57,40 @@ class InformationManager < OpenNebulaDriver # Execute the run_probes in the remote host #--------------------------------------------------------------------------- def action_monitor(number, host, unused) - log_lambda=lambda do |message| - log(number, message) - end - cmd_string = "#{@cmd_path}/run_probes #{@hypervisor} #{host}" - monitor_exe = LocalCommand.run(cmd_string, log_lambda) - if monitor_exe.code == 0 - send_message("MONITOR", RESULT[:success], number, monitor_exe.stdout) - else - send_message("MONITOR", RESULT[:failure], number, - "Could not monitor host #{host}. " + - "#{monitor_exe.get_error_message}") - end + local_action(cmd_string, number, "MONITOR") end end #------------------------------------------------------------------------------- #------------------------------------------------------------------------------- -# Information Manager main program +# IM Driver main program #------------------------------------------------------------------------------- #------------------------------------------------------------------------------- -hypervisor = ARGV[0] -im = InformationManager.new(hypervisor, 15) +opts = GetoptLong.new( + [ '--threads', '-t', GetoptLong::OPTIONAL_ARGUMENT ] +) + +hypervisor = '' +threads = 15 + +begin + opts.each do |opt, arg| + case opt + when '--threads' + threads = arg.to_i + end + end +rescue Exception => e + exit(-1) +end + +if ARGV.length >= 1 + hypervisor = ARGV.shift +end + +im = InformationManagerDriverSH.new(hypervisor,threads) im.start_driver diff --git a/src/im_mad/im_ssh/one_im_ssh.rb b/src/im_mad/im_ssh/one_im_ssh.rb index 8c4a36fcc2..f0966e647f 100755 --- a/src/im_mad/im_ssh/one_im_ssh.rb +++ b/src/im_mad/im_ssh/one_im_ssh.rb @@ -31,25 +31,23 @@ end $: << RUBY_LIB_LOCATION require 'OpenNebulaDriver' -require 'CommandManager' require 'getoptlong' #------------------------------------------------------------------------------- # The SSH Information Manager Driver #------------------------------------------------------------------------------- -class InformationManager < OpenNebulaDriver +class InformationManagerDriverSSH < OpenNebulaDriver #--------------------------------------------------------------------------- # Init the driver #--------------------------------------------------------------------------- def initialize(hypervisor, threads, retries) - super(threads, true) + super(threads, true, retries) @config = read_configuration @hypervisor = hypervisor @remote_dir = @config['SCRIPTS_REMOTE_DIR'] - @retries = retries # register actions register_action(:MONITOR, method("action_monitor")) @@ -59,34 +57,19 @@ class InformationManager < OpenNebulaDriver # Execute the run_probes in the remote host #--------------------------------------------------------------------------- def action_monitor(number, host, do_update) - log_lambda=lambda do |message| - log(number, message) - end - if do_update == "1" # Use SCP to sync: sync_cmd = "scp -r #{REMOTES_LOCATION}/. #{host}:#{@remote_dir}" # Use rsync to sync: # sync_cmd = "rsync -Laz #{REMOTES_LOCATION} #{host}:#{@remote_dir}" - LocalCommand.run(sync_cmd, log_lambda) + LocalCommand.run(sync_cmd, log_method(number)) end cmd_string = "#{@remote_dir}/im/run_probes #{@hypervisor} #{host}" - cmd = RemotesCommand.run(cmd_string, - host, - @remote_dir, - log_lambda, - @retries) - if cmd.code == 0 - send_message("MONITOR", RESULT[:success], number, cmd.stdout) - else - send_message("MONITOR", RESULT[:failure], number, - "Could not monitor host #{host}. #{cmd.get_error_message}") - end + remotes_action(cmd_string, number, host, "MONITOR", @remote_dir) end - end #------------------------------------------------------------------------------- @@ -121,5 +104,5 @@ if ARGV.length >= 1 hypervisor = ARGV.shift end -im = InformationManager.new(hypervisor, threads, retries) +im = InformationManagerDriverSSH.new(hypervisor, threads, retries) im.start_driver diff --git a/src/image_mad/one_image.rb b/src/image_mad/one_image.rb index 1d2c2faa8b..2b2dd8dec6 100755 --- a/src/image_mad/one_image.rb +++ b/src/image_mad/one_image.rb @@ -32,7 +32,6 @@ end $: << RUBY_LIB_LOCATION require "OpenNebulaDriver" -require "CommandManager" require 'getoptlong' # This class provides basic messaging and logging functionality @@ -56,7 +55,7 @@ class ImageDriver < OpenNebulaDriver # Register default actions for the protocol # ------------------------------------------------------------------------- def initialize(fs_type, concurrency=10, threaded=true) - super(concurrency,threaded) + super(concurrency,threaded,0) @actions_path = "#{VAR_LOCATION}/remotes/image/#{fs_type}" @@ -66,25 +65,6 @@ class ImageDriver < OpenNebulaDriver register_action(ACTION[:mkfs].to_sym, method("mkfs")) end - # ------------------------------------------------------------------------- - # Execute a command associated to an action and id on localhost - # ------------------------------------------------------------------------- - def local_action(command, id, action) - command_exe = LocalCommand.run(command) - - if command_exe.code == 0 - result = :success - info = "-" - else - result = :failure - info = command_exe.stderr - end - - info = "-" if info == nil || info.empty? - - send_message(ACTION[action],RESULT[result],id,info) - end - # ------------------------------------------------------------------------- # Image Manager Protocol Actions (generic implementation # ------------------------------------------------------------------------- diff --git a/src/mad/ruby/OpenNebulaDriver.rb b/src/mad/ruby/OpenNebulaDriver.rb index 958df72696..00fd0c8bb8 100644 --- a/src/mad/ruby/OpenNebulaDriver.rb +++ b/src/mad/ruby/OpenNebulaDriver.rb @@ -12,10 +12,12 @@ # See the License for the specific language governing permissions and */ # limitations under the License. */ # -------------------------------------------------------------------------- */ + require "ActionManager" +require "CommandManager" # Author:: dsa-research.org -# Copyright:: (c) 2009 Universidad Computense de Madrid +# Copyright:: (c) OpenNebula Project Leads (OpenNebula.org) # License:: Apache License # This class provides basic messaging and logging functionality @@ -34,14 +36,18 @@ class OpenNebulaDriver < ActionManager :failure => "FAILURE" } - def initialize(concurrency=10, threaded=true) + def initialize(concurrency=10, threaded=true, retries=0) super(concurrency,threaded) - register_action(:INIT, method("init")) - + @retries = retries @send_mutex=Mutex.new + + register_action(:INIT, method("init")) end + # ------------------------------------------------------------------------- + # Sends a message to the OpenNebula core through stdout + # ------------------------------------------------------------------------- def send_message(action="-", result=RESULT[:failure], id="-", info="-") @send_mutex.synchronize { STDOUT.puts "#{action} #{result} #{id} #{info}" @@ -49,8 +55,53 @@ class OpenNebulaDriver < ActionManager } end + # ------------------------------------------------------------------------- + # Execute a command associated to an action and id in a remote host. + # ------------------------------------------------------------------------- + def remotes_action(command, id, host, aname, remote_dir, std_in=nil) + + command_exe = RemotesCommand.run(command, + host, + remote_dir, + log_method(id), + std_in, + @retries) + if command_exe.code == 0 + result = RESULT[:success] + info = command_exe.stdout + else + result = RESULT[:failure] + info = command_exe.get_error_message + end + + info = "-" if info == nil || info.empty? + + send_message(aname,result,id,info) + end + + # ------------------------------------------------------------------------- + # Execute a command associated to an action and id on localhost + # ------------------------------------------------------------------------- + def local_action(command, id, action) + command_exe = LocalCommand.run(command, log_method(id)) + + if command_exe.code == 0 + result = RESULT[:success] + info = command_exe.stdout + else + result = RESULT[:failure] + info = command_exe.get_error_message + end + + info = "-" if info == nil || info.empty? + + send_message(aname,result,id,info) + end + + # ------------------------------------------------------------------------- # Sends a log message to ONE. The +message+ can be multiline, it will # be automatically splitted by lines. + # ------------------------------------------------------------------------- def log(number, message) msg=message.strip msg.each_line {|line| @@ -58,16 +109,20 @@ class OpenNebulaDriver < ActionManager } end + # ------------------------------------------------------------------------- # Generates a proc with that calls log with a hardcoded number. It will # be used to add loging to command actions + # ------------------------------------------------------------------------- def log_method(num) lambda {|message| log(num, message) } end + # ------------------------------------------------------------------------- # Start the driver. Reads from STDIN and executes methods associated with # the messages + # ------------------------------------------------------------------------- def start_driver loop_thread = Thread.new { loop } start_listener @@ -108,6 +163,10 @@ private end end +# ------------------------------------------------------------------------- +# ------------------------------------------------------------------------- +# ------------------------------------------------------------------------- +# ------------------------------------------------------------------------- if __FILE__ == $0 class SampleDriver < OpenNebulaDriver diff --git a/src/mad/ruby/VirtualMachineDriver.rb b/src/mad/ruby/VirtualMachineDriver.rb index 2a32245dae..eeb6a6b394 100644 --- a/src/mad/ruby/VirtualMachineDriver.rb +++ b/src/mad/ruby/VirtualMachineDriver.rb @@ -66,10 +66,9 @@ class VirtualMachineDriver < OpenNebulaDriver # Register default actions for the protocol # ------------------------------------------------------------------------- def initialize(concurrency=10, threaded=true, retries=0) - super(concurrency,threaded) + super(concurrency,threaded,retries) @hosts = Array.new - @retries = retries register_action(ACTION[:deploy].to_sym, method("deploy")) register_action(ACTION[:shutdown].to_sym, method("shutdown")) @@ -109,43 +108,14 @@ class VirtualMachineDriver < OpenNebulaDriver # Execute a command associated to an action and id in a remote host. # ------------------------------------------------------------------------- def remotes_action(command, id, host, action, remote_dir, std_in=nil) - - command_exe = RemotesCommand.run(command, - host, - remote_dir, - log_method(id), - std_in, - @retries) - if command_exe.code == 0 - result = :success - info = command_exe.stdout - else - result = :failure - info = command_exe.get_error_message - end - - info = "-" if info == nil || info.empty? - - send_message(ACTION[action],RESULT[result],id,info) + super(command,id,host,ACTION[action],remote_dir,std_in) end # ------------------------------------------------------------------------- # Execute a command associated to an action and id on localhost # ------------------------------------------------------------------------- def local_action(command, id, action) - command_exe = LocalCommand.run(command) - - if command_exe.code == 0 - result = :success - info = command_exe.stdout - else - result = :failure - info = command_exe.stderr - end - - info = "-" if info == nil || info.empty? - - send_message(ACTION[action],RESULT[result],id,info) + super(command,id,ACTION[action]) end # ------------------------------------------------------------------------- @@ -187,7 +157,9 @@ class VirtualMachineDriver < OpenNebulaDriver end private - + # ------------------------------------------------------------------------- + # Interface to handle the pending events from the ActionManager Interface + # ------------------------------------------------------------------------- def delete_running_action(action_id) action=@action_running[action_id] if action @@ -227,11 +199,6 @@ private @action_queue.delete_at(action_index) end - STDERR.puts "action: #{action.inspect}" - STDERR.puts "queue: #{@action_queue.inspect}" - STDERR.puts "hosts: #{@hosts.inspect}" - STDERR.flush - return action end @@ -240,6 +207,10 @@ private end end +# ------------------------------------------------------------------------- +# ------------------------------------------------------------------------- +# ------------------------------------------------------------------------- +# ------------------------------------------------------------------------- if __FILE__ == $0 class TemplateDriver < VirtualMachineDriver