From 2d04e8b3addfd402e80921c00d3871743e02acdf Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Thu, 10 May 2012 22:44:15 +0200 Subject: [PATCH] Verify that a container is not suspended before stopping it --- frontend/classes/class.container.php | 29 +++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/frontend/classes/class.container.php b/frontend/classes/class.container.php index 61325a8..8d7d2b8 100644 --- a/frontend/classes/class.container.php +++ b/frontend/classes/class.container.php @@ -445,19 +445,30 @@ class Container extends CPHPDatabaseRecordClass public function Stop() { - $command = "vzctl stop {$this->sInternalId}"; - $result = $this->sNode->ssh->RunCommand($command, false); - - // vzctl is retarded enough to return exit status 0 when the command fails because the container isn't running, so we'll have to check the stderr for specific error string(s) as well. come on guys, it's 2012. - if($result->returncode == 0 && strpos($result->stderr, "Unable to stop") === false) + if($this->sStatus == CVM_STATUS_SUSPENDED) { - $this->uStatus = CVM_STATUS_STOPPED; - $this->InsertIntoDatabase(); - return true; + throw new ContainerSuspendedException("The container cannot be stopped as it is suspended.", 1, $this->sInternalId); + } + elseif($this->sStatus == CVM_STATUS_TERMINATED) + { + throw new ContainerTerminatedException("The container cannot be stopped as it is terminated.", 1, $this->sInternalId); } else { - throw new ContainerStopException($result->stderr, $result->returncode, $this->sInternalId); + $command = "vzctl stop {$this->sInternalId}"; + $result = $this->sNode->ssh->RunCommand($command, false); + + // vzctl is retarded enough to return exit status 0 when the command fails because the container isn't running, so we'll have to check the stderr for specific error string(s) as well. come on guys, it's 2012. + if($result->returncode == 0 && strpos($result->stderr, "Unable to stop") === false) + { + $this->uStatus = CVM_STATUS_STOPPED; + $this->InsertIntoDatabase(); + return true; + } + else + { + throw new ContainerStopException($result->stderr, $result->returncode, $this->sInternalId); + } } }