protected function execute($arguments = array(), $options = array()) { // Context $context = sfContext::createInstance(sfProjectConfiguration::getApplicationConfiguration('app', $options['env'], true)); // initialize the database connection $databaseManager = new sfDatabaseManager($this->configuration); $connection = $databaseManager->getDatabase($options['connection'])->getConnection(); // add your code here // server id $server = $arguments['server']; $this->log("[INFO] Shutdown server with '{$server}'"); $etva_server = EtvaServerPeer::retrieveByPK($server); if (!$etva_server) { $etva_server = EtvaServerPeer::retrieveByUuid($server); } if (!$etva_server) { $etva_server = EtvaServerPeer::retrieveByName($server); } if (!$etva_server) { $msg_i18n = $context->getI18N()->__(EtvaServerPeer::_ERR_NOTFOUND_, array('name' => $server)); $error = array('success' => false, 'agent' => sfConfig::get('config_acronym'), 'error' => $msg_i18n, 'info' => $msg_i18n); $this->log("[ERROR] " . $error['error']); return $error; } else { if ($nid = $options['node']) { $etva_node = EtvaNodePeer::retrieveByPK($nid); } else { $etva_node = $etva_server->getEtvaNode(); } if (!$etva_node) { //notify event log $msg_i18n = Etva::makeNotifyLogMessage(sfConfig::get('config_acronym'), EtvaNodePeer::_ERR_NOTFOUND_ID_, array('id' => $nid), EtvaServerPeer::_ERR_STOP_, array('name' => $server)); $error = array('success' => false, 'agent' => sfConfig::get('config_acronym'), 'error' => $msg_i18n, 'info' => $msg_i18n); $this->log("[ERROR] " . $error['error']); return $error; } $destroy = $options['destroy'] ? 1 : 0; $force = $options['force'] ? 1 : 0; $extra = array('destroy' => $destroy, 'force' => $force); $server_va = new EtvaServer_VA($etva_server); $response = $server_va->send_stop($etva_node, $extra); if ($response['success']) { $this->log("[INFO] " . $response['response']); } else { $this->log("[ERROR] " . $response['error']); } return $response; } }
protected function execute($arguments = array(), $options = array()) { // Context $context = sfContext::createInstance(sfProjectConfiguration::getApplicationConfiguration('app', $options['env'], true)); // initialize the database connection $databaseManager = new sfDatabaseManager($this->configuration); $connection = $databaseManager->getDatabase($options['connection'])->getConnection(); // add your code here // server id $server = $arguments['server']; $this->log("[INFO] Start server with '{$server}'"); $etva_server = EtvaServerPeer::retrieveByPK($server); if (!$etva_server) { $etva_server = EtvaServerPeer::retrieveByUuid($server); } if (!$etva_server) { $etva_server = EtvaServerPeer::retrieveByName($server); } if (!$etva_server) { $msg_i18n = $context->getI18N()->__(EtvaServerPeer::_ERR_NOTFOUND_, array('name' => $server)); $error = array('success' => false, 'agent' => sfConfig::get('config_acronym'), 'error' => $msg_i18n, 'info' => $msg_i18n); $this->log("[ERROR] " . $error['error']); return $error; } else { if ($nid = $options['node']) { if (!($etva_node = EtvaNodePeer::retrieveByPK($nid))) { //notify event log $msg_i18n = Etva::makeNotifyLogMessage(sfConfig::get('config_acronym'), EtvaNodePeer::_ERR_NOTFOUND_ID_, array('id' => $nid), EtvaServerPeer::_ERR_START_, array('name' => $server)); $error = array('success' => false, 'agent' => sfConfig::get('config_acronym'), 'error' => $msg_i18n, 'info' => $msg_i18n); $this->log("[ERROR] " . $error['error']); return $error; } } else { // get list of nodes that server can assign $nodes_toassign = $etva_server->listNodesAssignTo(); if (count($nodes_toassign)) { $etva_node = $nodes_toassign[0]; // get first } else { //notify event log $msg_i18n = Etva::makeNotifyLogMessage(sfConfig::get('config_acronym'), EtvaServerPeer::_ERR_NO_NODE_TO_ASSIGN_, array(), EtvaServerPeer::_ERR_START_, array('name' => $server)); $error = array('success' => false, 'agent' => sfConfig::get('config_acronym'), 'error' => $msg_i18n, 'info' => $msg_i18n); $this->log("[ERROR] " . $error['error']); return $error; } } $server_va = new EtvaServer_VA($etva_server); if ($options['assign'] || $etva_server->getUnassigned()) { // Assign server to node $response = $server_va->send_assign($etva_node); if (!$response['success']) { $this->log("[ERROR] " . $response['error']); return $response; } } // start server $response = $server_va->send_start($etva_node); if ($response['success']) { // update GA Info $response_ga = $server_va->getGAInfo($etva_node); $this->log("[INFO] " . $response['response']); } else { $this->log("[ERROR] " . $response['error']); } return $response; } }
public function migrateAllServers(EtvaNode $etva_sparenode = null, $off = false, $ignoreAdmissionGate = false) { // migrate all servers // order by priority HA $querysrvs = EtvaServerQuery::create(); $querysrvs->orderByPriorityHa('desc'); $etva_servers = $this->etva_node->getEtvaServers($querysrvs); foreach ($etva_servers as $etva_server) { $server_va = new EtvaServer_VA($etva_server); $response = array(); $etva_tonode = $etva_sparenode; if (!$etva_tonode) { $list_nodes_toassign = $etva_server->listNodesAssignTo(true); if (count($list_nodes_toassign)) { $etva_tonode = $list_nodes_toassign[0]; } // get first } if ($etva_tonode) { if (!$off && $etva_server->getVmState() == 'running') { error_log("migrate server=" . $etva_server->getName() . " to node=" . $etva_tonode->getName()); $response = $server_va->send_migrate($this->etva_node, $etva_tonode); } else { error_log("move server=" . $etva_server->getName() . " to node=" . $etva_tonode->getName()); $response = $server_va->send_move($this->etva_node, $etva_tonode); // start it server is running or has autostart or has HA or has priority HA if ($off && ($etva_server->getVmState() == 'running' || $etva_server->getAutostart())) { // send start server $start_res = $server_va->send_start($etva_tonode, null, $ignoreAdmissionGate); } } if ($response['success']) { Etva::makeNotifyLogMessage($this->etva_node->getName(), sprintf('Server %s migrate ok', $etva_server->getName()), array(), null, array(), EtvaEventLogger::INFO); error_log(sprintf('Server %s migrate ok', $etva_server->getName())); } else { Etva::makeNotifyLogMessage($this->etva_node->getName(), sprintf('Server %s migrate nok', $etva_server->getName())); error_log(sprintf('Server %s migrate nok', $etva_server->getName())); } } else { Etva::makeNotifyLogMessage($this->etva_node->getName(), sprintf('Can\'t migrate server %s. No node free available.', $etva_server->getName())); error_log(sprintf('Can\'t migrate server %s. No node free available.', $etva_server->getName())); } } }
protected function execute($arguments = array(), $options = array()) { $context = sfContext::createInstance(sfProjectConfiguration::getApplicationConfiguration('app', $options['env'], true)); parent::execute($arguments, $options); // initialize the database connection $databaseManager = new sfDatabaseManager($this->configuration); $con = $databaseManager->getDatabase($options['connection'])->getConnection(); // add your code here $this->log('[INFO] Checking Servers heartbeat...' . "\n"); $servers_timeout = EtvaServerQuery::create()->filterByVmState(EtvaServer::RUNNING)->filterByGaState(EtvaServerPeer::_GA_RUNNING_)->filterByHasha(1)->where('hbtimeout>0 AND UNIX_TIMESTAMP(heartbeat) < (UNIX_TIMESTAMP(now()) - hbtimeout)')->useEtvaServerAssignQuery('ServerAssign', 'RIGHT JOIN')->useEtvaNodeQuery()->filterByState(EtvaNode::NODE_ACTIVE)->endUse()->endUse()->find(); if (count($servers_timeout) > 0) { foreach ($servers_timeout as $etva_server) { $message = sprintf(' The server %s with id %s get heartbeat timed out (last heartbeat at %s and heartbeat timeout %s secods)', $etva_server->getName(), $etva_server->getId(), $etva_server->getHeartbeat(), $etva_server->getHbtimeout()); $this->log($message); // add log message Etva::makeNotifyLogMessage($this->name, $message); $etva_node = $etva_server->getEtvaNode(); $server_va = new EtvaServer_VA($etva_server); $response_ga = $etva_server->getGAInfo($etva_node); if (!$response_ga['success']) { $msg = sprintf(' Something wrong with node %s agent, can\'t get GA state of the server %s with id %s run.', $etva_node->getName(), $etva_server->getName(), $etva_server->getId()); $this->log($msg); // add log message Etva::makeNotifyLogMessage($this->name, $msg); $etva_server->setGaState(EtvaServerPeer::_GA_STOPPED_); // mark GA as stopped $etva_server->save(); } else { if ($reponse_ga['ga_state'] == EtvaServerPeer::_GA_RUNNING_) { $message_ga = sprintf(' But the explicit check GA state of the server %s with id %s run with success.', $etva_server->getName(), $etva_server->getId()); $this->log($message_ga); // add log message Etva::makeNotifyLogMessage($this->name, $message_ga, array(), null, array(), EtvaEventLogger::INFO); } else { // go restart $starttime = sfConfig::get('app_server_heartbeat_starttime'); $starttime_date = date("c", time() - $starttime); if ($etva_server->getHblaststart() && $etva_server->getHblaststart() > $starttime_date) { $msg = sprintf(' the server %s with id %s is in starttime.', $etva_server->getName(), $etva_server->getId()); $this->log($msg); // add log message Etva::makeNotifyLogMessage($this->name, $msg, array(), null, array(), EtvaEventLogger::INFO); } else { $last_nrestarts = $etva_server->getHbnrestarts(); if ($last_nrestarts >= sfConfig::get('app_server_heartbeat_number_of_restart')) { $msg = sprintf(' the server %s with id %s exceed number of restart.', $etva_server->getName(), $etva_server->getId()); $this->log($msg); // add log message Etva::makeNotifyLogMessage($this->name, $msg); } else { $msg = sprintf(' the server %s with id %s is heartbeat out of date and will be restart.', $etva_server->getName(), $etva_server->getId()); $this->log($msg); // add log message Etva::makeNotifyLogMessage($this->name, $msg, array(), null, array(), EtvaEventLogger::INFO); // force to stop $response_stop = $server_va->send_stop($etva_node, array('force' => 1, 'destroy' => 1)); sleep(5); // wait a few seconds $response_start = $server_va->send_start($etva_node); if (!$response_start['success']) { // start fail... sleep(10); // wait a few seconds $response_start = $server_va->send_start($etva_node); // start again } $etva_server->resetHeartbeat(EtvaServerPeer::_GA_STOPPED_); // reset heartbeat and mark GA as stopped $etva_server->setHbnrestarts($last_nrestarts + 1); // inc number of restart $etva_server->save(); } } } } } } else { $this->log("[INFO] No servers with heartbeat timed out."); } // log the message! $this->log("[INFO] The check servers heartbeat task ran!"); }
protected function _execute($arguments = array(), $options = array()) { // get server id $sid = $arguments['serverid']; $etva_server = EtvaServerPeer::retrieveByPK($sid); // try by id if (!$etva_server) { $etva_server = EtvaServerPeer::retrieveByUuid($sid); } // try by uuid if (!$etva_server) { $etva_server = EtvaServerPeer::retrieveByName($sid); } // try by name if (!$etva_server) { $msg_i18n = sfContext::getInstance()->getI18N()->__(EtvaServerPeer::_ERR_NOTFOUND_ID_, array('%id%' => $sid)); // Error $err_m = "[ERROR] {$msg_i18n}"; $this->report .= $err_m . "\r\n"; $this->log($err_m); array_push($this->errors, array('message' => $err_m)); return -1; } else { $etva_node = $etva_server->getEtvaNode(); if ($etva_node) { $info_m = '[INFO] Backup of the server ' . $etva_server->getName() . ' assign to node ' . $etva_node->getName() . '...'; $this->log($info_m . "\n"); $params = array('name' => $etva_server->getName(), 'uuid' => $etva_server->getUuid()); if ($options['location']) { // location set $params['location'] = $options['location']; if ($options['do_not_generate_tar'] && $options['do_not_generate_tar'] != 'false') { // do not generate tar $params['do_not_generate_tar'] = true; } } if ($options['shutdown']) { // set shutdown $params['shutdown'] = true; } if ($options['delete_backups_n_days']) { $params['clean_old_backups'] = true; $n_days = intval($options['delete_backups_n_days']); if ($n_days) { $params['n_days'] = $n_days; } } if (!$etva_server->getHasSnapshots() && !$options['shutdown'] && !$options['snapshot'] && !$options['newsnapshot'] && $etva_server->getVmState() != 'stop' && $etva_server->getVmState() != 'notrunning') { // Error is running $err_m = "[ERROR] Server '" . $etva_server->getName() . "' can't create backup file of running server without snapshots"; $this->report .= $err_m . "\r\n"; $this->log($err_m); array_push($this->errors, array('message' => $err_m)); return -101; } $server_va = new EtvaServer_VA($etva_server); // use snapshot for backup if ($options['snapshot']) { // set snapshot $params['snapshot'] = $options['snapshot']; } if ($options['newsnapshot'] || !$options['shutdown']) { // create new snapshot $newsnapshot = $options['newsnapshot']; if (!$etva_server->getHasSnapshots() || $newsnapshot) { $response = $server_va->create_snapshot($etva_node, $newsnapshot); if (!$response['success']) { $msg_i18n = $response['info']; $err_m = "[ERROR] Server '" . $etva_server->getName() . "' can't create snapshot: {$msg_i18n}"; $this->report .= $err_m . "\r\n"; $this->log($err_m); array_push($this->errors, array('message' => $err_m, 'error' => $response)); // Error return -110; } $params['snapshot'] = $newsnapshot; } } // call backup $res = $this->call_backup($etva_node, $params, $options['filepath']); if ($res < 0) { return $res; } if ($options['deletesnapshot'] && $options['deletesnapshot'] != 'false') { // delete snapshot after if ($newsnapshot) { $server_va->remove_snapshot($etva_node, $newsnapshot); } else { if ($snapshot) { $server_va->remove_snapshot($etva_node, $snapshot); } } } return 0; } else { $warn_m = '[WARN] ' . $etva_server->getName() . ' VM is not assigned and will be ignored.'; $this->report .= $warn_m . "\r\n"; $this->log($warn_m . "\n"); return -1010; } } }
public function executeJsonRemoveSnapshot(sfWebRequest $request) { $sid = $request->getParameter('id'); if (!($etva_server = EtvaServerPeer::retrieveByPK($sid))) { $msg_i18n = $this->getContext()->getI18N()->__(EtvaServerPeer::_ERR_NOTFOUND_ID_, array('%id%' => $sid)); $error = array('agent' => sfConfig::get('config_acronym'), 'success' => false, 'error' => $msg_i18n); //notify event log $server_log = Etva::getLogMessage(array('id' => $sid), EtvaServerPeer::_ERR_NOTFOUND_ID_); $message = Etva::getLogMessage(array('info' => $server_log), EtvaServerPeer::_ERR_MIGRATE_UNKNOWN_); $this->dispatcher->notify(new sfEvent($error['agent'], 'event.log', array('message' => $message, 'priority' => EtvaEventLogger::ERR))); // if is browser request return text renderer $error = $this->setJsonError($error); return $this->renderText($error); } $etva_node = $etva_server->getEtvaNode(); $snapshot = $request->getParameter('snapshot'); $server_va = new EtvaServer_VA($etva_server); $response = $server_va->remove_snapshot($etva_node, $snapshot); if ($response['success']) { $return = json_encode($response); // if the request is made throught soap request... if (sfConfig::get('sf_environment') == 'soap') { return $return; } // if is browser request return text renderer $this->getResponse()->setHttpHeader('Content-type', 'application/json'); return $this->renderText($return); } else { if (sfConfig::get('sf_environment') == 'soap') { return json_encode($response); } $return = $this->setJsonError($response); return $this->renderText($return); } }
protected function execute($arguments = array(), $options = array()) { // Context $context = sfContext::createInstance(sfProjectConfiguration::getApplicationConfiguration('app', $options['env'], true)); // initialize the database connection $databaseManager = new sfDatabaseManager($this->configuration); $connection = $databaseManager->getDatabase($options['connection'])->getConnection(); // add your code here $node = $options['node']; $etva_node = EtvaNodePeer::retrieveByPK($node); if (!$etva_node) { $msg_i18n = $context->getI18N()->__(EtvaNodePeer::_ERR_NOTFOUND_ID_, array('%id%' => $node)); $error = array('success' => false, 'agent' => sfConfig::get('config_acronym'), 'error' => $msg_i18n, 'info' => $msg_i18n); return $error; } if ($arguments['boot'] == 'location') { $valid = $this->validateLocationUrl($options['location']); if ($valid == false) { $msg_i18n = $context->getI18N()->__('Could not validate the location URL!'); $error = array('agent' => sfConfig::get('config_acronym'), 'success' => false, 'error' => $msg_i18n, 'info' => $msg_i18n); return $error; } } $server = array_merge($arguments, $options); // decode networks if ($server['networks']) { $server['networks'] = $this->process_array_of_arguments($server['networks']); } //$this->log("[DEBUG] server networks ".print_r($server['networks'],true)); // decode disks if ($server['disks']) { $server_disks = $this->process_array_of_arguments($server['disks']); $server['disks'] = $this->prepare_disks($server_disks); } //$this->log("[DEBUG] server disks ".print_r($server['disks'],true)); $etva_server = new EtvaServer(); $server_va = new EtvaServer_VA($etva_server); $response = $server_va->send_create($etva_node, $server); if ($response['success']) { $this->log("[INFO] " . $response['response']); } else { $this->log("[ERROR] " . $response['error']); } return $response; }