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

Added base class for VMM drivers, added some constants to base classes

and INIT action


git-svn-id: http://svn.opennebula.org/one/trunk@337 3034c82b-c49b-4eb3-8279-a7acafdc01c0
This commit is contained in:
Rubén S. Montero 2009-01-30 23:08:08 +00:00
parent 39a6460cb4
commit 5269d55770
2 changed files with 187 additions and 7 deletions

View File

@ -29,14 +29,22 @@ require "ActionManager"
# with the action name through the register_action func
class OpenNebulaDriver < ActionManager
RESULT = {
:success => "SUCCESS",
:failure => "FAILURE"
}
def initialize(concurrency=10, threaded=true)
super(concurrency,threaded)
register_action("INIT", method("init"))
@send_mutex=Mutex.new
end
def send_message(*args)
def send_message(action="-", result=RESULT[:failure], id="-", info="-")
@send_mutex.synchronize {
STDOUT.puts args.join(' ')
STDOUT.puts "#{action} #{result} #{id} #{info}"
STDOUT.flush
}
end
@ -58,6 +66,10 @@ class OpenNebulaDriver < ActionManager
private
def init
send_message("INIT",RESULT[:success])
end
def loop
while true
exit(-1) if STDIN.eof?
@ -84,16 +96,12 @@ if __FILE__ == $0
register_action("SLEEP",method("my_sleep"))
end
def response(action,result,info)
send_message(action,result,info)
end
def my_sleep(timeout, num)
log(num,"Sleeping #{timeout} seconds")
sleep(timeout.to_i)
log(num,"Done with #{num}")
response("SLEEP","SUCCESS",num.to_s)
send_message("SLEEP",RESULT[:success],num.to_s)
end
end

View File

@ -0,0 +1,172 @@
# -------------------------------------------------------------------------- */
# Copyright 2002-2009, 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. */
# -------------------------------------------------------------------------- */
require "OpenNebulaDriver"
# Author:: dsa-research.org
# Copyright:: (c) 2009 Universidad Computense de Madrid
# License:: Apache License
# This class provides basic messaging and logging functionality
# to implement OpenNebula Drivers. A driver is a program that
# specialize the OpenNebula behavior by interfacing with specific
# infrastructure functionalities.
#
# A Driver inherits this class and only has to provide methods
# for each action it wants to receive. The method must be associated
# with the action name through the register_action func
class VirtualMachineDriver < OpenNebulaDriver
ACTION = {
:deploy => "DEPLOY",
:shutdown => "SHUTDOWN",
:cancel => "CANCEL",
:save => "SAVE",
:restore => "RESTORE",
:migrate => "MIGRATE",
:poll => "POLL"
}
POLL_ATTRIBUTE = {
:usedmemory => "USEDMEMORY",
:usedcpu => "USEDCPU",
:nettx => "NETTX",
:netrx => "NETRX",
:state => "STATE"
}
VM_STATE = {
:active => 'a',
:paused => 'p',
:error => 'e',
:deleted => 'd'
}
def initialize(concurrency=10, threaded=true)
super(concurrency,threaded)
register_action(ACTION[:deploy], method("deploy"))
register_action(ACTION[:shutdown], method("shutdown"))
register_action(ACTION[:cancel], method("cancel"))
register_action(ACTION[:save], method("save"))
register_action(ACTION[:restore], method("restore"))
register_action(ACTION[:migrate], method("migrate"))
register_action(ACTION[:poll], method("poll"))
end
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 deploy(id, host, remote_dfile, not_used)
error = "Action not implemented by driver #{self.class}"
send_message(ACTION[:deploy],RESULT[:failure],id,error)
end
def shutdown(id, host, deploy_id, not_used)
error = "Action not implemented by driver #{self.class}"
send_message(ACTION[:shutdown],RESULT[:failure],id,error)
end
def cancel(id, host, deploy_id, not_used)
error = "Action not implemented by driver #{self.class}"
send_message(ACTION[:cancel],RESULT[:failure],id,error)
end
def save(id, host, deploy_id, file)
error = "Action not implemented by driver #{self.class}"
send_message(ACTION[:save],RESULT[:failure],id,error)
end
def restore(id, host, file, not_used)
error = "Action not implemented by driver #{self.class}"
send_message(ACTION[:restore],RESULT[:failure],id,error)
end
def migrate(id, host, deploy_id, dest_host)
error = "Action not implemented by driver #{self.class}"
send_message(ACTION[:migrate],RESULT[:failure],id,error)
end
def poll(id, host, deploy_id, not_used)
error = "Action not implemented by driver #{self.class}"
send_message(ACTION[:poll],RESULT[:failure],id,error)
end
end
if __FILE__ == $0
class TemplateDriver < VirtualMachineDriver
def initialize
super(15,true)
end
def deploy(id, host, remote_dfile, not_used)
#MUST return deploy_id if deployment was successfull
deploy_id = "-"
send_message(ACTION[:deploy],RESULT[:success],id,deploy_id)
end
def shutdown(id, host, deploy_id, not_used)
send_message(ACTION[:shutdown],RESULT[:success],id)
end
def cancel(id, host, deploy_id, not_used)
send_message(ACTION[:cancel],RESULT[:success],id)
end
def save(id, host, deploy_id, file)
send_message(ACTION[:save],RESULT[:success],id)
end
def restore(id, host, file, not_used)
send_message(ACTION[:restore],RESULT[:success],id)
end
def migrate(id, host, deploy_id, dest_host)
send_message(ACTION[:migrate],RESULT[:success],id)
end
def poll(id, host, deploy_id, not_used)
# monitor_info: string in the form "VAR=VAL VAR=VAL ... VAR=VAL"
# known VAR are in POLL_ATTRIBUTES. VM states VM_STATES
monitor_info = "#{POLL_ATTRIBUTE[:state]}=#{VM_STATE[:active]} \
#{POLL_ATTRIBUTE[:nettx]}=12345"
send_message(ACTION[:poll],RESULT[:success],id,monitor_info)
end
end
sd = TemplateDriver.new
sd.start_driver
end