From df7751ce807b55520f2ed9636bd980530e8ee370 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Mon, 30 Apr 2012 02:27:32 +0200 Subject: [PATCH] Status display and some actual functionality --- frontend/classes/class.container.php | 47 ++++++++++++++++++ frontend/css/cvm.css | 55 ++++++++++++++++++++- frontend/interface.php | 73 ++++++++++++++++++++++++++++ frontend/templates/main.tpl | 5 +- 4 files changed, 178 insertions(+), 2 deletions(-) diff --git a/frontend/classes/class.container.php b/frontend/classes/class.container.php index e05b8eb..47ca88e 100644 --- a/frontend/classes/class.container.php +++ b/frontend/classes/class.container.php @@ -63,6 +63,12 @@ class Container extends CPHPDatabaseRecordClass case "sBandwidthUsed": return $this->GetBandwidthUsed(); break; + case "sStatus": + return (int)$this->GetStatus(); + break; + case "sStatusText": + return $this->GetStatusText(); + break; default: return null; break; @@ -74,6 +80,47 @@ class Container extends CPHPDatabaseRecordClass return ($this->sOutgoingTrafficUsed + $this->IncomingTrafficUsed) / (1024 * 1024); } + public function GetStatus() + { + $command = "vzctl status {$this->sInternalId}"; + + $result = $this->sNode->ssh->RunCommandCached($command, false); + + if($result->returncode == 0) + { + $values = split_whitespace($result->stdout); + + if($values[4] == "running") + { + return CVM_STATUS_STARTED; + } + else + { + return CVM_STATUS_STOPPED; + } + } + } + + public function GetStatusText() + { + if($this->GetStatus() == CVM_STATUS_STARTED) + { + return "running"; + } + elseif($this->GetStatus() == CVM_STATUS_STOPPED) + { + return "stopped"; + } + elseif($this->GetStatus() == CVM_STATUS_SUSPENDED) + { + return "suspended"; + } + else + { + return "unknown"; + } + } + public function Deploy($conf = array()) { $sRootPassword = random_string(20); diff --git a/frontend/css/cvm.css b/frontend/css/cvm.css index 61d6b5c..b2d96bb 100644 --- a/frontend/css/cvm.css +++ b/frontend/css/cvm.css @@ -255,8 +255,61 @@ table.vpsinfo td font-weight: bold; } -.suspended +.suspended, .unknown { color: #5B5F6B; font-weight: bold; } + + +/* CPHPErrorHandler styles */ + +.errorhandler +{ + margin: 10px 0px; + padding: 8px 10px; + padding-left: 46px; + background-position: 8px 6px; + background-repeat: no-repeat; + min-height: 24px; +} + +.errorhandler .error-title +{ + font-weight: bold; + font-size: 120%; +} + +.errorhandler.error-error +{ + background-color: #FCCBC9; + border: 1px solid red; + background-image: url(../images/errorhandler_error.png); +} + +.errorhandler.error-info +{ + background-color: #C9F6FC; + border: 1px solid blue; + background-image: url(../images/errorhandler_info.png); +} + +.errorhandler.error-warning +{ + background-color: #FBFCC9; + border: 1px solid #C5BA00; + background-image: url(../images/errorhandler_warning.png); +} + +.errorhandler.error-success +{ + background-color: #DEFCC9; + border: 1px solid #4CC500; + background-image: url(../images/errorhandler_success.png); +} + +.errorhandler ul +{ + margin: 0px; + padding-left: 24px; +} diff --git a/frontend/interface.php b/frontend/interface.php index 8242b2f..e715bfd 100644 --- a/frontend/interface.php +++ b/frontend/interface.php @@ -7,8 +7,81 @@ $settings['master_pubkey'] = "/etc/cvm/key.pub"; $settings['salt'] = "kAU0qM"; $sContainer = new Container(1); +$sError = ""; + +if($_GET['action'] == "start") +{ + if($sContainer->GetStatus() != CVM_STATUS_STARTED) + { + try + { + $sContainer->Start(); + + $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_SUCCESS, "Container started", "Your container was successfully started."); + $sError .= $err->Render(); + } + catch(ContainerStartException $e) + { + $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_ERROR, "Container failed to start", "Your container could not be started. If this error persists, please file a support ticket."); + $sError .= $err->Render(); + } + } + else + { + $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_ERROR, "Container can't be started", "Your container cannot be started because it is already running."); + $sError .= $err->Render(); + } +} +elseif($_GET['action'] == "stop") +{ + if($sContainer->GetStatus() != CVM_STATUS_STOPPED) + { + try + { + $sContainer->Stop(); + + $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_SUCCESS, "Container stopped", "Your container was successfully stopped."); + $sError .= $err->Render(); + } + catch(ContainerStartException $e) + { + $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_ERROR, "Container failed to stop", "Your container could not be stopped. If this error persists, please file a support ticket."); + $sError .= $err->Render(); + } + } + else + { + $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_ERROR, "Container can't be stopped", "Your container cannot be stopped because it is not running."); + $sError .= $err->Render(); + } +} +elseif($_GET['action'] == "restart") +{ + try + { + try + { + $sContainer->Stop(); + } + catch(ContainerStopException $e) + { + // we can make this silently fail, as the only important thing is that it starts again + } + + $sContainer->Start(); + + $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_SUCCESS, "Container restarted", "Your container was successfully restarted."); + $sError .= $err->Render(); + } + catch(ContainerStartException $e) + { + $err = new CPHPErrorHandler(CPHP_ERRORHANDLER_TYPE_ERROR, "Container failed to start", "Your container could not be started. If this error persists, please file a support ticket."); + $sError .= $err->Render(); + } +} echo(Templater::InlineRender("main", $locale->strings, array( + 'error' => $sError, 'server-location' => $sContainer->sNode->sPhysicalLocation, 'operating-system' => $sContainer->sTemplate->sName, 'guaranteed-ram' => "{$sContainer->sGuaranteedRam}MB", diff --git a/frontend/templates/main.tpl b/frontend/templates/main.tpl index 5394855..e16e36f 100644 --- a/frontend/templates/main.tpl +++ b/frontend/templates/main.tpl @@ -23,6 +23,9 @@ Alerts API + + <%?error> +

Overview

@@ -72,7 +75,7 @@ - +
StatusRunning Stopped Suspended <%?status>
Server location