1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-11 05:17:41 +03:00

Removed unused files

This commit is contained in:
Ruben S. Montero 2010-08-31 17:02:13 +02:00
parent 14efd8bb3c
commit 3462215fa9
3 changed files with 0 additions and 509 deletions

View File

@ -1,131 +0,0 @@
# -------------------------------------------------------------------------- #
# Copyright 2002-2010, 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. #
#--------------------------------------------------------------------------- #
require 'thread'
=begin rdoc
Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org)
This class manages a pool of threads with a maximun number of concurrent running jobs.
== Example
Creates 1000 threads that sleep 1 second and executes them with a concurrency of 100.
th=ThreadScheduler.new(100)
1000.times {
th.new_thread {
sleep 1
}
}
=end
class ThreadScheduler
# Creates a new thread pool
#
# +concurrent_number+ is the maximun number of threads that can run
# at the same time
def initialize(concurrent_number=10)
@concurrent_number=concurrent_number
@thread_queue=Array.new
@running_threads=0
@threads_mutex=Mutex.new
@threads_cond=ConditionVariable.new
start_thread_runner
end
# Creates a new job that will be placed on the queue. It will be scheduled
# when there is room at the selected concurrency level. Job is a block.
def new_thread(&block)
@threads_mutex.synchronize {
@thread_queue<<block
if @running_threads < @concurrent_number
# Awakes thread runner only if there is room for a new thread
@threads_cond.signal
end
}
end
# Kills the thread that manages job queues. Should be called before
# exiting
def shutdown
@thread_runner.kill!
end
private
# Selects new jobs to be run as threads
#
# NOTE: should be called inside a syncronize block
def run_new_thread
thread = @thread_queue.shift
if thread
@running_threads += 1
Thread.new {
thread.call
@threads_mutex.synchronize {
# Tell thread runner that the thread has finished
@running_threads -= 1
@threads_cond.signal
}
}
end
end
def start_thread_runner
@thread_runner=Thread.new {
while true
@threads_mutex.synchronize {
while ((@concurrent_number-@running_threads)==0) ||
@thread_queue.size==0
@threads_cond.wait(@threads_mutex)
end
run_new_thread
}
end
}
end
end
if __FILE__ == $0
th=ThreadScheduler.new(20)
100.times {|n|
100.times {|m|
th.new_thread {
puts "Starting #{m+n*100}"
sleep 1
#puts "Finishing #{m+n*100}"
}
}
}
th.new_thread {
sleep 4
th.shutdown
exit(0)
}
sleep 3600
end

View File

@ -1,179 +0,0 @@
# --------------------------------------------------------------------------
# Copyright 2002-2010, 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.
# --------------------------------------------------------------------------
require "thread"
# Author:: dsa-research.org
# Copyright:: (c) 2007 Universidad Computense de Madrid
# License:: Apache License
# This class provides basic messaging and logging functionality.
#
# A MAD inherits this class and only has to provide methods
# for each action it wants to receive. This methods will be
# named action_<name_of_the_action>. For example a method that
# handles INIT message should be like this:
#
# def action_init(args)
# call_some_function(args[2], args[3])
# send_message("INIT", "SUCCESS")
# end
class ONEMad
##
# Debug constants
##
ERROR, DEBUG=[0,1]
# * +num_params_in+: number of parameters that mensages will have
# * +num_params_out+: number of parameters that mensages sent back
# will have
def initialize(num_params_in, num_params_out=nil)
@num_params=num_params_in
if !num_params_out
@num_params_out=num_params_in
else
@num_params_out=num_params_out
end
@debug_level = -1
@send_mutex=Mutex.new
end
# Sends a message to the logger
def log(str, level)
if level == ERROR
str = "------- ERROR ---------------\n" +
str + "\n-----------------------------"
end
if @debug_level != -1 and level <= @debug_level
str.split("\n").each{|line|
@logger.puts(Time.now.ctime + ": " + line.strip)
@logger.flush
}
end
end
# Sets the logger file, this can be an open file
def set_logger(logger, level)
@debug_level = level.to_i
@logger=logger
end
# Main loop that will get messages from STDIN and call any
# action associated to the message.
def loop
while true
exit(-1) if STDIN.eof?
str=STDIN.gets
next if !str
line=str.split(/\s+/)
log(str,DEBUG)
args=Array.new
args+=line[0..(@num_params-2)]
args<<line[(@num_params-1)..-1].join(' ') if line.length>=@num_params
process args
end
end
# Sends a message to ONE, this takes an array with the message and arguments.
# If the message is shorter than the number of parameters specified in the
# initialization more marameters will be added containing '-'.
def send_message(*args)
@send_mutex.synchronize {
to_send=args
if args.length<@num_params_out
(@num_params_out-args.length).times{ to_send<<'-' }
end
STDOUT.puts to_send.join(' ')
STDOUT.flush
log(to_send.join(' '),DEBUG)
}
end
# Sends a log message to ONE. The +message+ can be multiline, it will
# be automatically splitted by lines.
def mad_log(command, number, message)
msg=message.strip
msg.each_line {|line|
send_message("LOG", "-", number, line.strip)
}
end
# Proceses each message received, called by +loop+.
def process(args)
return nil if !args or !args[0]
action=args[0].downcase
if self.respond_to?("action_#{action}")
self.action_debug(args)
self.send("action_#{action}", args)
else
self.action_debug(args)
end
end
# Action called when there is not a handler for a message. By default it
# logs the message, but can be redefined to do any other thing.
def action_debug(args)
#@logger.puts(args.join(';'))
end
# Default FINALIZE action. Exists the program.
def action_finalize(args)
exit(0)
end
# Functions for VM deployment
# TODO: Move elsewhere
def get_local_deployment_file(remote_deployment_file)
local_deployment_file=nil
one_location=ENV["ONE_LOCATION"]
if one_location == nil
var_location = "/var/lib/one/"
else
var_location = one_location + "/var/"
end
m=remote_deployment_file.match(/.*?\/(\d+)\/images\/(deployment.\d+)$/)
local_deployment_file="#{var_location}#{m[1]}/#{m[2]}" if m
return local_deployment_file
end
def execute_local_command(cmd_string)
command=SSHCommand.new(cmd_string)
stdout, stderr=command.exec_local_command(cmd_string)
exit_code=command.get_exit_code(stderr)
if exit_code!=0
return stderr
else
nil
end
end
end

View File

@ -1,199 +0,0 @@
# -------------------------------------------------------------------------- #
# Copyright 2002-2010, 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. #
#--------------------------------------------------------------------------- #
=begin rdoc
Here will be a description of SSH library and an example on
how to use it.
=end
require 'pp'
require 'open3'
require 'thread'
require 'ThreadScheduler'
# This class holds a command that will be executed on a remote host
# using ssh. Commands can have an associated callback that will be
# after they finish.
class SSHCommand
attr_accessor :value, :code, :stdout, :stderr, :command
attr_accessor :callback
# Creates a new command
def initialize(command)
@command=command
@callback=nil
end
# Runs the command on the specified host
def run(host)
(@stdout, @stderr)=execute(host, @command)
@code=get_exit_code(@stderr)
end
# Executes callback associated to this command
def exec_callback(num)
if @callback
@callback.call(self, num)
end
end
#private
# Gets exit code from STDERR
def get_exit_code(str)
tmp=str.scan(/^ExitCode: (\d*)$/)
return nil if !tmp[0]
tmp[0][0].to_i
end
# Executes a command in a remote machine
def execute(host, command)
std=exec_remote_command(host, command)
[std[1].read, std[2].read]
end
# Low level local command execution
def exec_local_command(command)
std=Open3.popen3(
"#{command} ;"+
" echo ExitCode: $? 1>&2")
[std[1].read, std[2].read]
end
# Low level remote command execution
def exec_remote_command(host, command)
Open3.popen3(
"ssh -n #{host} #{command} ;"+
" echo ExitCode: $? 1>&2")
end
end
class SSHCommandList < Array
=begin
def clone_actions
new_array=Array.new
self.each {|s|
new_array << s.clone
}
new_array
end
=end
end
# An action is composed by one or more SSHCommands that will be executed in an
# specific host. It holds a number that is the command identifier for a MAD.
class SSHAction
attr_accessor :callback
def initialize(number, host, actions)
@number=number
@host=host
if actions.is_a?(SSHCommandList) || actions.is_a?(Array)
@actions=clone_actions(actions)
else
@actions=SSHCommandList.new
@actions<<actions
# Really needed
@actions=clone_actions(@actions)
end
@finished=false
@callback=nil
end
def finished
@finished
end
def run
run_actions
@finished=true
end
def run_actions
@actions.each {|a|
a.run(@host)
}
end
def exec_callbacks
@actions.each {|a|
a.exec_callback(@number)
}
if @callback
@callback.call(@actions, @number)
end
end
private
# Clone an array of sensors
def clone_actions(actions)
actions.collect{|a| a.clone }
end
end
module SSHActionController
def init_actions(num=10)
@thread_scheduler=ThreadScheduler.new(num)
end
def send_ssh_action(action)
@thread_scheduler.new_thread {
action.run
action.exec_callbacks
}
end
def action_finalize(args)
@thread_scheduler.shutdown
super(args)
end
end
=begin
EXAMPLE
def action_poll(args)
action_name=args[0]
action_number=args[1]
action_host=args[2]
action_dom=args[3]
poll=SSHCommand.new("xm poll #{action_dom}")
poll.callback = lambda {|a,num|
if a.code==0
STDOUT.puts("POLL #{num} SUCCESS #{a.stdout}")
else
STDOUT.puts("POLL #{num} FAILURE #{a.stderr}")
end
STDOUT.flush
}
cmd_list=SSHCommandList.new
cmd_list<<poll
poll_action=SSHAction.new(action_number, action_host, cmd_list)
send_ssh_action(poll_action)
end
=end