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

feature #595: changed comments and formating

This commit is contained in:
Javi Fontan 2011-05-27 12:46:37 +02:00
parent e5734fbc74
commit 89ac12ebcd
3 changed files with 90 additions and 129 deletions

View File

@ -1,17 +1,18 @@
# -------------------------------------------------------------------------- */
# -------------------------------------------------------------------------- #
# 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. */
# -------------------------------------------------------------------------- */
# #
# 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 "ActionManager"
require "CommandManager"
@ -90,9 +91,7 @@ class OpenNebulaDriver < ActionManager
register_action(:INIT, method("init"))
end
# -------------------------------------------------------------------------
# Sends a message to the OpenNebula core through stdout
# -------------------------------------------------------------------------
def send_message(action="-", result=RESULT[:failure], id="-", info="-")
@send_mutex.synchronize {
STDOUT.puts "#{action} #{result} #{id} #{info}"
@ -100,9 +99,8 @@ class OpenNebulaDriver < ActionManager
}
end
# -------------------------------------------------------------------------
# Calls remotes or local action checking the action name and @local_actions
# -------------------------------------------------------------------------
# Calls remotes or local action checking the action name and
# @local_actions
def do_action(parameters, id, host, aname, std_in=nil)
command=action_command_line(aname, parameters)
@ -151,9 +149,7 @@ class OpenNebulaDriver < ActionManager
end
end
# -------------------------------------------------------------------------
# Execute a command associated to an action and id in a remote host.
# -------------------------------------------------------------------------
def remotes_action(command, id, host, aname, remote_dir, std_in=nil)
command_exe = RemotesCommand.run(command,
@ -175,9 +171,7 @@ class OpenNebulaDriver < ActionManager
send_message(aname,result,id,info)
end
# -------------------------------------------------------------------------
# Execute a command associated to an action and id on localhost
# -------------------------------------------------------------------------
def local_action(command, id, aname)
command_exe = LocalCommand.run(command, log_method(id))
@ -194,10 +188,8 @@ class OpenNebulaDriver < ActionManager
send_message(aname,result,id,info)
end
# -------------------------------------------------------------------------
# Sends a log message to ONE. The +message+ can be multiline, it will
# be automatically splitted by lines.
# -------------------------------------------------------------------------
def log(number, message)
in_error_message=false
msg=message.strip
@ -234,20 +226,16 @@ class OpenNebulaDriver < ActionManager
}
end
# -------------------------------------------------------------------------
# Generates a proc with that calls log with a hardcoded number. It will
# be used to add loging to command actions
# -------------------------------------------------------------------------
def log_method(num)
lambda {|message|
log(num, message)
}
end
# -------------------------------------------------------------------------
# Start the driver. Reads from STDIN and executes methods associated with
# the messages
# -------------------------------------------------------------------------
def start_driver
loop_thread = Thread.new { loop }
start_listener
@ -288,10 +276,9 @@ private
end
end
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
################################################################
################################################################
if __FILE__ == $0
class SampleDriver < OpenNebulaDriver

View File

@ -1,22 +1,23 @@
# -------------------------------------------------------------------------- */
# -------------------------------------------------------------------------- #
# 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. */
# -------------------------------------------------------------------------- */
# #
# 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"
require "CommandManager"
# Author:: dsa-research.org
# Copyright:: (c) 2009 Universidad Computense de Madrid
# Copyright:: (c) 2011 Universidad Computense de Madrid
# License:: Apache License
# This class provides basic messaging and logging functionality
@ -30,9 +31,7 @@ require "CommandManager"
class VirtualMachineDriver < OpenNebulaDriver
# -------------------------------------------------------------------------
# Virtual Machine Driver Protocol constants
# -------------------------------------------------------------------------
ACTION = {
:deploy => "DEPLOY",
:shutdown => "SHUTDOWN",
@ -62,9 +61,7 @@ class VirtualMachineDriver < OpenNebulaDriver
HOST_ARG = 1
# -------------------------------------------------------------------------
# Register default actions for the protocol
# -------------------------------------------------------------------------
def initialize(concurrency=10, threaded=true, retries=0,
directory='vmm', local_actions={})
super(concurrency, threaded, retries, directory, local_actions)
@ -80,12 +77,9 @@ class VirtualMachineDriver < OpenNebulaDriver
register_action(ACTION[:poll].to_sym, method("poll"))
end
# -------------------------------------------------------------------------
# Converts a deployment file from its remote path to the local (front-end)
# path
# -------------------------------------------------------------------------
def get_local_deployment_file(rfile)
lfile = nil
one_location = ENV["ONE_LOCATION"]
@ -105,23 +99,17 @@ class VirtualMachineDriver < OpenNebulaDriver
return lfile
end
# -------------------------------------------------------------------------
# Execute a command associated to an action and id in a remote host.
# -------------------------------------------------------------------------
def remotes_action(command, id, host, action, remote_dir, std_in=nil)
super(command,id,host,ACTION[action],remote_dir,std_in)
end
# -------------------------------------------------------------------------
# Execute a command associated to an action and id on localhost
# -------------------------------------------------------------------------
def local_action(command, id, action)
super(command,id,ACTION[action])
end
# -------------------------------------------------------------------------
# Virtual Machine Manager Protocol Actions (generic implementation
# -------------------------------------------------------------------------
# Virtual Machine Manager Protocol Actions (generic implementation)
def deploy(id, host, remote_dfile, not_used)
error = "Action not implemented by driver #{self.class}"
send_message(ACTION[:deploy],RESULT[:failure],id,error)
@ -158,9 +146,7 @@ class VirtualMachineDriver < OpenNebulaDriver
end
private
# -------------------------------------------------------------------------
# Interface to handle the pending events from the ActionManager Interface
# -------------------------------------------------------------------------
def delete_running_action(action_id)
action=@action_running[action_id]
if action
@ -208,55 +194,52 @@ private
end
end
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
################################################################
################################################################
if __FILE__ == $0
class TemplateDriver < VirtualMachineDriver
def initialize
super(15,true)
end
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 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 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 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 save(id, host, deploy_id, file)
send_message(ACTION[:save],RESULT[:success],id)
end
def restore(id, host, deploy_id , file)
send_message(ACTION[:restore],RESULT[:success],id)
end
def restore(id, host, deploy_id , file)
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 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
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

View File

@ -1,24 +1,23 @@
#!/usr/bin/env ruby
# -------------------------------------------------------------------------.- #
# 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. #
#---------------------------------------------------------------------------- #
# -------------------------------------------------------------------------- #
# 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. #
#--------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# Set up the environment for the driver #
# ---------------------------------------------------------------------------- #
# Set up the environment for the driver
ONE_LOCATION = ENV["ONE_LOCATION"]
@ -35,22 +34,17 @@ $: << RUBY_LIB_LOCATION
require "VirtualMachineDriver"
require 'getoptlong'
# ----------------------------------------------------------------------------
# The main class for the Sh driver
# ----------------------------------------------------------------------------
class SshDriver < VirtualMachineDriver
# ------------------------------------------------------------------------
# SshDriver constructor
# ------------------------------------------------------------------------
def initialize(hypervisor, threads, retries, local_actions)
super(threads, true, retries, "vmm/#{hypervisor}", local_actions)
@hypervisor = hypervisor
end
# ------------------------------------------------------------------------ #
# DEPLOY action, sends the deployment file to remote host #
# ------------------------------------------------------------------------ #
# DEPLOY action, sends the deployment file to remote host
def deploy(id, host, remote_dfile, not_used)
local_dfile = get_local_deployment_file(remote_dfile)
@ -73,9 +67,8 @@ class SshDriver < VirtualMachineDriver
do_action("#{dfile} #{host}", id, host, :deploy, domain)
end
# ------------------------------------------------------------------------ #
# Basic Domain Management Operations #
# ------------------------------------------------------------------------ #
# Basic Domain Management Operations
def shutdown(id, host, deploy_id, not_used)
do_action("#{deploy_id} #{host}", id, host, :shutdown)
end
@ -101,9 +94,7 @@ class SshDriver < VirtualMachineDriver
end
end
# ---------------------------------------------------------------------------- #
# SshDriver Main program
# ---------------------------------------------------------------------------- #
opts = GetoptLong.new(
[ '--retries', '-r', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--threads', '-t', GetoptLong::OPTIONAL_ARGUMENT ],