/usr/lib/one/mads/one_tm.rb is in opennebula 3.4.1-3.1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | #!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2002-2012, 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. #
#--------------------------------------------------------------------------- #
ONE_LOCATION=ENV["ONE_LOCATION"]
if !ONE_LOCATION
RUBY_LIB_LOCATION="/usr/lib/one/ruby"
ETC_LOCATION="/etc/one/"
else
RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
ETC_LOCATION=ONE_LOCATION+"/etc/"
end
$: << RUBY_LIB_LOCATION
require 'pp'
require 'OpenNebulaDriver'
require 'CommandManager'
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
# 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={
:concurrency => 15,
:threaded => true,
:retries => 0
}.merge!(options)
super('tm/', @options)
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
# Driver Action: TRANSFER id script_file
# Executes a transfer script
def action_transfer(id, script_file)
script = parse_script(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)
return
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 << " " << args
rc = LocalCommand.run(path, log_method(id))
result, info = get_info_from_execution(rc)
return result, info
end
end
################################################################################
################################################################################
# TransferManager Driver Main program
################################################################################
################################################################################
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_driver = TransferManagerDriver.new(tm_type, :concurrency => threads)
tm_driver.start_driver
|