From dc69fb722f697eda88013389a4e89a075e6ad1f2 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Thu, 10 May 2012 23:47:21 +0200 Subject: [PATCH] Cleaned up some code, now making more use of exceptions in container reinstall code, rather than if statements --- frontend/classes/class.container.php | 18 ++++++++- frontend/classes/class.controller.php | 2 - frontend/classes/class.node.php | 2 - frontend/module.vps.reinstall.php | 56 +++++++++++++-------------- 4 files changed, 44 insertions(+), 34 deletions(-) diff --git a/frontend/classes/class.container.php b/frontend/classes/class.container.php index 8d7d2b8..79f1977 100644 --- a/frontend/classes/class.container.php +++ b/frontend/classes/class.container.php @@ -232,6 +232,22 @@ class Container extends CPHPDatabaseRecordClass ); } + function CheckAllowed() + { + if($this->sStatus == CVM_STATUS_SUSPENDED) + { + throw new ContainerSuspendedException("No operations can be performed on this container beacuse it is suspended.", 1, $this->sInternalId); + } + elseif($this->sStatus == CVM_STATUS_TERMINATED) + { + throw new ContainerSuspendedException("No operations can be performed on this container beacuse it is terminated.", 1, $this->sInternalId); + } + else + { + return true; + } + } + public function RunCommand($command, $throw_exception = false) { return $this->sNode->ssh->RunCommand("vzctl exec {$this->sInternalId} $command", $throw_exception); @@ -598,5 +614,3 @@ class Container extends CPHPDatabaseRecordClass $command = "vzctl set {$this->sInternalId} --devnodes net/tun:rw --save"; } } - -?> diff --git a/frontend/classes/class.controller.php b/frontend/classes/class.controller.php index 40ba5e3..4ff293b 100644 --- a/frontend/classes/class.controller.php +++ b/frontend/classes/class.controller.php @@ -17,5 +17,3 @@ class Controller extends CPHPBaseClass { public $connection = null; } - -?> diff --git a/frontend/classes/class.node.php b/frontend/classes/class.node.php index 68421ac..f6a8094 100644 --- a/frontend/classes/class.node.php +++ b/frontend/classes/class.node.php @@ -137,5 +137,3 @@ class Node extends CPHPDatabaseRecordClass ); } } - -?> diff --git a/frontend/module.vps.reinstall.php b/frontend/module.vps.reinstall.php index 4542ace..092acc9 100644 --- a/frontend/module.vps.reinstall.php +++ b/frontend/module.vps.reinstall.php @@ -21,39 +21,19 @@ if(isset($_POST['submit'])) { try { + $sContainer->CheckAllowed(); $sTemplate = new Template($_POST['template']); $sTemplate->CheckAvailable(); if(isset($_POST['confirm'])) { - if($sContainer->sStatus != CVM_STATUS_SUSPENDED) - { - try - { - $sContainer->uTemplateId = $sTemplate->sId; - $sContainer->InsertIntoDatabase(); - $sContainer->Reinstall(); - $sContainer->Start(); - - $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_SUCCESS, "Reinstallation succeeded!", "Your VPS was successfully reinstalled."); - $sPageContents .= $err->Render(); - } - catch (ContainerReinstallException $e) - { - $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_ERROR, "Reinstallation failed", "Something went wrong during the reinstallation of your VPS. Please try again. If the reinstallation fails again, please contact support."); - $sPageContents .= $err->Render(); - } - catch (ContainerStartException $e) - { - $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_WARNING, "Failed to start", "The VPS was successfully reinstalled, but it could not be started. If the issue persists, please contact support."); - $sPageContents .= $err->Render(); - } - } - else - { - $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_ERROR, "Reinstallation aborted", "You can not reinstall this VPS, because it is suspended. If you believe this is in error, please contact support."); - $sPageContents .= $err->Render(); - } + $sContainer->uTemplateId = $sTemplate->sId; + $sContainer->InsertIntoDatabase(); + $sContainer->Reinstall(); + $sContainer->Start(); + + $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_SUCCESS, "Reinstallation succeeded!", "Your VPS was successfully reinstalled."); + $sPageContents .= $err->Render(); } else { @@ -71,6 +51,26 @@ if(isset($_POST['submit'])) $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_ERROR, "Reinstallation aborted", "The template you selected is not available. Please select a different template."); $sPageContents .= $err->Render(); } + catch (ContainerReinstallException $e) + { + $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_ERROR, "Reinstallation failed", "Something went wrong during the reinstallation of your VPS. Please try again. If the reinstallation fails again, please contact support."); + $sPageContents .= $err->Render(); + } + catch (ContainerStartException $e) + { + $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_WARNING, "Failed to start", "The VPS was successfully reinstalled, but it could not be started. If the issue persists, please contact support."); + $sPageContents .= $err->Render(); + } + catch (ContainerSuspendedException $e) + { + $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_ERROR, "Reinstallation aborted", "You can not reinstall this VPS, because it is suspended. If you believe this is in error, please contact support."); + $sPageContents .= $err->Render(); + } + catch (ContainerTerminatedException $e) + { + $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_ERROR, "Reinstallation aborted", "You can not reinstall this VPS, because it is suspended. If you believe this is in error, please contact support."); + $sPageContents .= $err->Render(); + } } else {