1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-16 22:50:10 +03:00

Ported tm mad to use the new mad libraries

git-svn-id: http://svn.opennebula.org/one/trunk@348 3034c82b-c49b-4eb3-8279-a7acafdc01c0
This commit is contained in:
Javier Fontán Muiños 2009-02-10 17:41:47 +00:00
parent 038a3a0efc
commit 2908d083e3
2 changed files with 46 additions and 71 deletions

View File

@ -19,6 +19,7 @@
require 'pp'
require 'open3'
require 'ftools'
require 'CommandManager'
=begin rdoc
@ -80,15 +81,12 @@ class TMPlugin < Hash
# Executes the command, get its exit code and logs every line that
# comes from stdout. Returns whatever came from stderr.
def exec_local_command(command, logger)
cmd="#{command} ; echo ExitCode: $? 1>&2"
stderr=""
std=Open3.popen3(cmd) {|stdin, stdout, stderr_|
# TODO: this should be sent to ONE and not to STDERR
while !stdout.eof?
log(stdout.readline, logger)
end
stderr_.read
}
cmd=LocalCommand.new(command, logger)
cmd.run
log(cmd.stdout, logger)
cmd
end
# Uses +logger+ to send +message+ to ONE
@ -196,12 +194,11 @@ class TMScript
# Gets exit code and error message (if failed) from
# +stderr+
def parse_output(err)
exit_code=get_exit_code(err)
if exit_code==0
def parse_output(command)
if command.code==0
[true, ""]
else
[false, get_error_message(err)]
[false, get_error_message(command.stderr)]
end
end
@ -222,6 +219,8 @@ end
if $0 == __FILE__
=begin
require 'one_log'
logger=ONELog.new
@ -237,7 +236,12 @@ if $0 == __FILE__
thingy
EOT
=end
log_proc=lambda{|message|
puts message
}
script_text="
CLONE localhost:/tmp/source.img ursa:/tmp/one_jfontan/0/hda.img
@ -249,6 +253,7 @@ if $0 == __FILE__
plugin=TMPlugin.new
plugin["CLONE"]="./tm_clone.sh"
plugin["CLONE"]="echo"
scr=TMScript.new(script_text, log_proc)
pp scr.lines

View File

@ -1,21 +1,4 @@
#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2002-2008, Distributed Systems Architecture Group, Universidad #
# Complutense de Madrid (dsa-research.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. #
#--------------------------------------------------------------------------- #
ONE_LOCATION=ENV["ONE_LOCATION"]
@ -30,60 +13,46 @@ end
$: << RUBY_LIB_LOCATION
require 'pp'
require 'one_mad'
require 'ThreadScheduler'
require 'OpenNebulaDriver'
require 'CommandManager'
require 'TMScript'
class TM < ONEMad
def initialize(plugin)
@thread_scheduler=ThreadScheduler.new(10)
class TransferManager < OpenNebulaDriver
def initialize(plugin, num)
super(num, true)
@plugin=plugin
# Messages with 3 input elements and 4 output elements:
#
# in: TRANSFER 65 /something/var/65/tmscript.0
# out: TRANSFER 65 FAILURE error message
super(3,4)
set_logger(STDERR, 1)
# register actions
register_action(:TRANSFER, method("action_transfer"))
end
def action_init(args)
STDOUT.puts "INIT SUCCESS"
STDOUT.flush
log("INIT SUCCESS",DEBUG)
end
def action_transfer(args)
number=args[1]
script_file=args[2]
def action_transfer(number, script_file)
script_text=""
# Create the lambda that will be used to log
mad_logger=lambda {|message|
mad_log("TRANSFER", number, message)
}
log(number, script_file)
open(script_file) {|f|
script_text=f.read
}
if File.exist?(script_file)
open(script_file) {|f|
script_text=f.read
}
script=TMScript.new(script_text, mad_logger)
@thread_scheduler.new_thread {
script=TMScript.new(script_text, log_method(number))
res=script.execute(@plugin)
if res[0]
send_message("TRANSFER", "SUCCESS", number)
else
send_message("TRANSFER", "FAILURE", number, res[1])
end
}
else
send_message("TRANSFER", "FAILURE", number,
"Transfer file not found: #{script_file}")
end
end
def action_finalize(args)
@thread_scheduler.shutdown
super(args)
end
end
tm_conf=ARGV[0]
@ -97,8 +66,9 @@ tm_conf=ETC_LOCATION+tm_conf if tm_conf[0] != ?/
plugin=TMPlugin.new(tm_conf)
tm=TM.new(plugin)
tm.loop
tm=TransferManager.new(plugin, 15)
tm.start_driver