From f9e18cb569240b40e9f06a5a74e8f50ce5800a6e Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 28 Feb 2012 00:13:34 +0100 Subject: [PATCH] feature #1112: Adapt TM driver to the new protocol --- src/tm_mad/one_tm | 6 -- src/tm_mad/one_tm.rb | 154 ++++++++++++++++++++++++++++++++----------- 2 files changed, 115 insertions(+), 45 deletions(-) diff --git a/src/tm_mad/one_tm b/src/tm_mad/one_tm index 0144d09498..d1da2965b1 100755 --- a/src/tm_mad/one_tm +++ b/src/tm_mad/one_tm @@ -19,21 +19,15 @@ DRIVER_NAME=`basename $1 | cut -d. -f1` if [ -z "${ONE_LOCATION}" ]; then - DRIVERRC=/etc/one/${DRIVER_NAME}/${DRIVER_NAME}rc MADCOMMON=/usr/lib/one/mads/madcommon.sh VAR_LOCATION=/var/lib/one else - DRIVERRC=$ONE_LOCATION/etc/${DRIVER_NAME}/${DRIVER_NAME}rc MADCOMMON=$ONE_LOCATION/lib/mads/madcommon.sh VAR_LOCATION=$ONE_LOCATION/var fi . $MADCOMMON -# Export the im_mad specific rc - -export_rc_vars $DRIVERRC - # Go to ONE_LOCATION cd $VAR_LOCATION diff --git a/src/tm_mad/one_tm.rb b/src/tm_mad/one_tm.rb index 1791d112d6..a6dafe47a9 100755 --- a/src/tm_mad/one_tm.rb +++ b/src/tm_mad/one_tm.rb @@ -31,64 +31,140 @@ $: << RUBY_LIB_LOCATION require 'pp' require 'OpenNebulaDriver' require 'CommandManager' -require 'TMScript' +require 'getoptlong' +# This class provides basic messaging and logging functionality to implement +# TransferManager Drivers. A TransferManager driver is a program (or a set of) +# that specialize the OpenNebula behavior to distribute disk images in a +# specific datastore to the hosts +class TransferManagerDriver < OpenNebulaDriver -class TransferManager < OpenNebulaDriver - - def initialize(plugin, options={}) + # Register TRANSFER action, and tm drivers available + # @param tm_type [Array] of tm types + # @param options [Hash] basic options for an OpenNebula driver + def initialize(tm_type, options={}) @options={ - :threaded => true + :concurrency => 15, + :threaded => true, + :retries => 0 }.merge!(options) - super('', @options) + super('tm/', @options) - @plugin=plugin + if tm_type == nil + @types = Dir["#{@local_scripts_path}/*/"].map do |d| + d.split('/')[-1] + end + elsif tm_type.class == String + @types = [tm_type] + else + @types = tm_type + end # register actions register_action(:TRANSFER, method("action_transfer")) end - def action_transfer(number, script_file) - script_text="" + # Driver Action: TRANSFER id script_file + # Executes a transfer script + def action_transfer(id, script_file) - if File.exist?(script_file) - open(script_file) {|f| - script_text=f.read - } + script = parse_script(script_file) - script=TMScript.new(script_text, log_method(number)) - res=script.execute(@plugin) - - if res[0] - send_message("TRANSFER", RESULT[:success], number) - else - send_message("TRANSFER", RESULT[:failure], number, res[1]) - end - else - send_message("TRANSFER", RESULT[:failure], number, - "Transfer file not found: #{script_file}") + if script.nil? + send_message("TRANSFER", + RESULT[:failure], + id, + "Transfer file '#{script_file}' does not exist") + return end + + script.each { |command| + result, info = do_transfer_action(id, command) + + if result == RESULT[:failure] + send_message("TRANSFER", result, id, info) + break + end + } + + send_message("TRANSFER", RESULT[:success], id) end + private + + # Parse a script file + # @param sfile [String] path to the transfer script + # @return lines [Array] with the commands of the script. Each command is an + # array itself. + def parse_script(sfile) + return nil if !File.exist?(sfile) + + stext = File.read(sfile) + lines = Array.new + + stext.each_line {|line| + next if line.match(/^\s*#/) # skip if the line is commented + next if line.match(/^\s*$/) # skip if the line is empty + + command = line.split(" ") + + lines << command + } + + return lines + end + + # Executes a single transfer action (command), as returned by the parse + # method + # @param id [String] with the OpenNebula ID for the TRANSFER action + # @param command [Array] + def do_transfer_action(id, command) + cmd = command[0].downcase + tm = command[1] + args = command[2..-1].join(" ") + + if not @types.include?(tm) + return RESULT[:failure], "Transfer Driver '#{tm}' not available" + end + + path = File.join(@local_scripts_path, tm, cmd) + path << " " << arguments + + rc = LocalCommand.run(path, log_method(id)) + + result, info = get_info_from_execution(rc) + + return result, info + end end -tm_conf=ARGV[0] +################################################################################ +################################################################################ +# TransferManager Driver Main program +################################################################################ +################################################################################ -if !tm_conf - puts "You need to specify config file." +opts = GetoptLong.new( + [ '--threads', '-t', GetoptLong::OPTIONAL_ARGUMENT ], + [ '--tm-types', '-d', GetoptLong::OPTIONAL_ARGUMENT ] +) + +tm_type = nil +threads = 15 + +begin + opts.each do |opt, arg| + case opt + when '--threads' + threads = arg.to_i + when '--tm-types' + tm_type = arg.split(',').map {|a| a.strip } + end + end +rescue Exception => e exit(-1) end -tm_conf=ETC_LOCATION+tm_conf if tm_conf[0] != ?/ - -plugin=TMPlugin.new(tm_conf) - -tm=TransferManager.new(plugin, - :concurrency => 15) - -tm.start_driver - - - - +tm_driver = TransferManagerDriver.new(tm_type, :concurrency => threads) +tm_driver.start_driver