mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-21 14:50:08 +03:00
feature #575: Some refactor of opennebula drivers code. Command execution wrappers moved to base class
This commit is contained in:
parent
5a3408a64b
commit
e246af0fe5
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
# -------------------------------------------------------------------------
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user