From cd163f2a54474ad7b20dee852f9dcfabaa5f1b5d Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 15 Apr 2011 00:34:22 +0200 Subject: [PATCH] feature #575: Add error messages to VM templates (VMM part) --- include/VirtualMachine.h | 11 ++ src/vm/VirtualMachine.cc | 29 ++++++ src/vmm/VirtualMachineManagerDriver.cc | 133 +++++++++++++------------ 3 files changed, 107 insertions(+), 66 deletions(-) diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index 9c510d831f..234cd9e218 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -541,6 +541,13 @@ public: */ int parse_template_attribute(const string& attribute, string& parsed); + /** + * Sets an error message for the VM in the template + * @param message + * @return 0 on success + */ + void set_error_message(const string& message); + // ------------------------------------------------------------------------ // States // ------------------------------------------------------------------------ @@ -823,6 +830,10 @@ private: // ------------------------------------------------------------------------- // Attribute Parser // ------------------------------------------------------------------------- + /** + * Name for the error messages attribute + */ + static const char * error_attribute_name; /** * Mutex to perform just one attribute parse at a time diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index 83b0af12d2..bf6895cad6 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -102,6 +102,8 @@ const char * VirtualMachine::db_bootstrap = "CREATE TABLE IF NOT EXISTS " "vm_pool (oid INTEGER PRIMARY KEY, name TEXT, body TEXT, uid INTEGER, " "last_poll INTEGER, state INTEGER, lcm_state INTEGER)"; +const char * VirtualMachine::error_attribute_name = "ERROR_MESSAGE"; + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -1012,6 +1014,33 @@ error_common: return -1; } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ +void VirtualMachine::set_error_message(const string& message) +{ + SingleAttribute * attr; + ostringstream error; + + char str[26]; + time_t the_time; + + the_time = time(NULL); + +#ifdef SOLARIS + ctime_r(&(the_time),str,sizeof(char)*26); +#else + ctime_r(&(the_time),str); +#endif + // Get rid of final enter character + str[24] = '\0'; + + error << str << ": " << message; + + attr = new SingleAttribute(error_attribute_name,error.str()); + + obj_template->set(attr); +} + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/src/vmm/VirtualMachineManagerDriver.cc b/src/vmm/VirtualMachineManagerDriver.cc index 3d278bd6fe..cb7d2707d2 100644 --- a/src/vmm/VirtualMachineManagerDriver.cc +++ b/src/vmm/VirtualMachineManagerDriver.cc @@ -227,17 +227,68 @@ void VirtualMachineManagerDriver::poll ( /* MAD Interface */ /* ************************************************************************** */ +/* -------------------------------------------------------------------------- */ +/* Helpers for the protocol function */ +/* -------------------------------------------------------------------------- */ + +static void log_error(VirtualMachine* vm, + ostringstream& os, + istringstream& is, + const char * msg) +{ + string info; + + getline(is,info); + + os.str(""); + os << msg; + + if (info[0] != '-') + { + os << ": " << info; + vm->set_error_message(os.str()); + } + + vm->log("VMM",Log::ERROR,os); +} + +static Log::MessageType log_type(const char r) +{ + Log::MessageType lt; + + switch (r) + { + case 'E': + lt = Log::ERROR; + break; + case 'I': + lt = Log::INFO; + break; + case 'D': + lt = Log::DEBUG; + break; + default: + lt = Log::INFO; + } + + return lt; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void VirtualMachineManagerDriver::protocol( string& message) { - istringstream is(message); - ostringstream os; + istringstream is(message); + ostringstream os; - string action; - string result; + string action; + string result; + + int id; + VirtualMachine * vm; - int id; - VirtualMachine * vm; os << "Message received: " << message; NebulaLog::log("VMM", Log::DEBUG, os); @@ -265,7 +316,7 @@ void VirtualMachineManagerDriver::protocol( is.clear(); getline(is,info); - NebulaLog::log("VMM",Log::INFO, info.c_str()); + NebulaLog::log("VMM", log_type(result[0]), info.c_str()); } return; @@ -316,17 +367,7 @@ void VirtualMachineManagerDriver::protocol( } else { - string info; - - getline(is,info); - - os.str(""); - os << "Error deploying virtual machine"; - - if (info[0] != '-') - os << ": " << info; - - vm->log("VMM",Log::ERROR,os); + log_error(vm,os,is,"Error deploying virtual machine"); lcm->trigger(LifeCycleManager::DEPLOY_FAILURE, id); } @@ -342,14 +383,7 @@ void VirtualMachineManagerDriver::protocol( } else { - string info; - - getline(is,info); - - os.str(""); - os << "Error shuting down VM, " << info; - - vm->log("VMM",Log::ERROR,os); + log_error(vm,os,is,"Error shuting down VM"); lcm->trigger(LifeCycleManager::SHUTDOWN_FAILURE, id); } @@ -365,14 +399,7 @@ void VirtualMachineManagerDriver::protocol( } else { - string info; - - getline(is,info); - - os.str(""); - os << "Error canceling VM, " << info; - - vm->log("VMM",Log::ERROR,os); + log_error(vm,os,is,"Error canceling VM"); lcm->trigger(LifeCycleManager::CANCEL_FAILURE, id); } @@ -388,14 +415,7 @@ void VirtualMachineManagerDriver::protocol( } else { - string info; - - getline(is,info); - - os.str(""); - os << "Error saving VM state, " << info; - - vm->log("VMM",Log::ERROR,os); + log_error(vm,os,is,"Error saving VM state"); lcm->trigger(LifeCycleManager::SAVE_FAILURE, id); } @@ -411,14 +431,7 @@ void VirtualMachineManagerDriver::protocol( } else { - string info; - - getline(is,info); - - os.str(""); - os << "Error restoring VM, " << info; - - vm->log("VMM",Log::ERROR,os); + log_error(vm,os,is,"Error restoring VM"); lcm->trigger(LifeCycleManager::DEPLOY_FAILURE, id); } @@ -434,14 +447,7 @@ void VirtualMachineManagerDriver::protocol( } else { - string info; - - getline(is,info); - - os.str(""); - os << "Error live-migrating VM, " << info; - - vm->log("VMM",Log::ERROR,os); + log_error(vm,os,is,"Error live migrating VM"); lcm->trigger(LifeCycleManager::DEPLOY_FAILURE, id); } @@ -507,7 +513,7 @@ void VirtualMachineManagerDriver::protocol( { tiss >> state; } - else + else if (!var.empty()) { string val; @@ -580,12 +586,7 @@ void VirtualMachineManagerDriver::protocol( } else { - string info; - - getline(is,info); - - os.str(""); - os << "Error monitoring VM, " << info; + log_error(vm,os,is,"Error monitoring VM"); vm->log("VMM",Log::ERROR,os); } @@ -595,7 +596,7 @@ void VirtualMachineManagerDriver::protocol( string info; getline(is,info); - vm->log("VMM",Log::INFO,info.c_str()); + vm->log("VMM",log_type(result[0]),info.c_str()); } vm->unlock();