diff --git a/src/mad/ruby/DriverExecHelper.rb b/src/mad/ruby/DriverExecHelper.rb new file mode 100644 index 0000000000..ff43bdbfcd --- /dev/null +++ b/src/mad/ruby/DriverExecHelper.rb @@ -0,0 +1,129 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, 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. # +#--------------------------------------------------------------------------- # + + +# This module provides an abstraction to generate an execution context for +# OpenNebula Drivers +module DriverExecHelper + #Initialize module variables + def initialize_helper(directory, options) + @config = read_configuration + @remote_scripts_base_path = @config['SCRIPTS_REMOTE_DIR'] + + @local_actions = options[:local_actions] + + if ENV['ONE_LOCATION'] == nil + @local_scripts_base_path = "/var/lib/one/remotes" + else + @local_scripts_base_path = "#{ENV['ONE_LOCATION']}/var/remotes" + end + + # dummy paths + @remote_scripts_path = File.join(@remote_scripts_base_path, directory) + @local_scripts_path = File.join(@local_scripts_base_path, directory) + end + + # Given the action name and the parameter returns full path of the script + # and appends its parameters. It uses @local_actions hash to know if the + # actions is remote or local. If the local actions has defined an special + # script name this is used, otherwise the action name in downcase is + # used as the script name. + # + # @param [String, Symbol] action name of the action + # @param [String] parameters arguments for the script + # @param [String, nil] default_name alternative name for the script + # @return [String] command line needed to execute the action + def action_command_line(action, parameters, default_name=nil) + if action_is_local? action + script_path=@local_scripts_path + else + script_path=@remote_scripts_path + end + + File.join(script_path, action_script_name(action, default_name))+ + " "+parameters + end + + # True if the action is meant to be executed locally + # + # @param [String, Symbol] action name of the action + def action_is_local?(action) + @local_actions.include? action.to_s.upcase + end + + # Name of the script file for the given action + # + # @param [String, Symbol] action name of the action + # @param [String, nil] default_name alternative name for the script + def action_script_name(action, default_name=nil) + name=@local_actions[action.to_s.upcase] + + if name + name + else + default_name || action.to_s.downcase + end + end + + def get_info_from_execution(command_exe) + 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? + + [result, info] + end + + # Simple parser for the config file generated by OpenNebula + def read_configuration + one_config=nil + + if ENV['ONE_LOCATION'] + one_config=ENV['ONE_LOCATION']+'/var/config' + else + one_config='/var/lib/one/config' + end + + config=Hash.new + cfg='' + + begin + open(one_config) do |file| + cfg=file.read + end + + cfg.split(/\n/).each do |line| + m=line.match(/^([^=]+)=(.*)$/) + + if m + name=m[1].strip.upcase + value=m[2].strip + config[name]=value + end + end + rescue Exception => e + STDERR.puts "Error reading config: #{e.inspect}" + STDERR.flush + end + + config + end +end \ No newline at end of file diff --git a/src/mad/ruby/StreamDriver.rb b/src/mad/ruby/StreamDriver.rb new file mode 100644 index 0000000000..e07b8ef428 --- /dev/null +++ b/src/mad/ruby/StreamDriver.rb @@ -0,0 +1,46 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2011, 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. # +#--------------------------------------------------------------------------- # + + +# This module provides an abstraction to generate an execution context for +# OpenNebula Drivers + +class VirtualNetworkDriver + include DriverExecHelper + + def initialize(directory, options={}) + + @options = options + @ssh_stream = options[:ssh_stream] + + + initialize_helper("vnet/#{directory}", options) + end + + def do_action(parameters, aname) + vm_encoded = Base64.encode64(message.elements['VM'].to_s).delete("\n") + + command = action_command_line(aname, @vm_encoded, options[:script_name]) + + if action_is_local? aname + execution = LocalCommand.run(command, log_method(id)) + else + execution = @ssh_stream.run(command) + end + + result, info = get_info_from_execution(command_exe) + end +end