2016-12-25 10:03:37 +01:00
|
|
|
<?php
|
|
|
|
session_start();
|
|
|
|
$AuthUsers = array("admin");
|
|
|
|
if (!isset($_SESSION['mailcow_cc_role']) OR !in_array($_SESSION['mailcow_cc_role'], $AuthUsers)) {
|
|
|
|
echo "Not allowed." . PHP_EOL;
|
|
|
|
exit();
|
|
|
|
}
|
2017-10-04 13:04:58 +02:00
|
|
|
|
|
|
|
function docker($service_name, $action, $post_action = null, $post_fields = null) {
|
|
|
|
$curl = curl_init();
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER,array( 'Content-Type: application/json' ));
|
|
|
|
switch($action) {
|
|
|
|
case 'get_id':
|
2017-10-05 23:38:33 +02:00
|
|
|
curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/info/container/all');
|
2017-10-04 13:04:58 +02:00
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_POST, 0);
|
|
|
|
$response = curl_exec($curl);
|
|
|
|
if ($response === false) {
|
|
|
|
$err = curl_error($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
curl_close($curl);
|
|
|
|
$containers = json_decode($response, true);
|
|
|
|
if (!empty($containers)) {
|
|
|
|
foreach ($containers as $container) {
|
|
|
|
if ($container['Labels']['com.docker.compose.service'] == $service_name) {
|
|
|
|
return trim($container['Id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case 'info':
|
|
|
|
$container_id = docker($service_name, 'get_id');
|
|
|
|
if (ctype_xdigit($container_id)) {
|
2017-10-05 23:38:33 +02:00
|
|
|
curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/info/container/' . $container_id);
|
2017-10-04 13:04:58 +02:00
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
2017-10-05 23:38:33 +02:00
|
|
|
curl_setopt($curl, CURLOPT_POST, 0);
|
2017-10-04 13:04:58 +02:00
|
|
|
$response = curl_exec($curl);
|
|
|
|
if ($response === false) {
|
|
|
|
$err = curl_error($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
curl_close($curl);
|
|
|
|
if (empty($response)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return json_decode($response, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'post':
|
|
|
|
if (!empty($post_action)) {
|
|
|
|
$container_id = docker($service_name, 'get_id');
|
2017-10-05 23:38:33 +02:00
|
|
|
if (ctype_xdigit($container_id) && ctype_alnum($post_action)) {
|
|
|
|
curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/' . $post_action . '/container/' . $container_id);
|
2017-10-04 13:04:58 +02:00
|
|
|
curl_setopt($curl, CURLOPT_POST, 1);
|
|
|
|
if (!empty($post_fields)) {
|
|
|
|
curl_setopt( $curl, CURLOPT_POSTFIELDS, json_encode($post_fields));
|
|
|
|
}
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
$response = curl_exec($curl);
|
|
|
|
if ($response === false) {
|
|
|
|
$err = curl_error($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
curl_close($curl);
|
|
|
|
if (empty($response)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-25 10:03:37 +01:00
|
|
|
if ($_GET['ACTION'] == "start") {
|
2017-10-04 13:04:58 +02:00
|
|
|
$retry = 0;
|
2017-10-05 23:38:33 +02:00
|
|
|
while (docker('sogo-mailcow', 'info')[0]['State'] != "running" && $retry <= 3) {
|
2017-10-04 13:04:58 +02:00
|
|
|
$response = docker('sogo-mailcow', 'post', 'start');
|
2017-10-05 23:38:33 +02:00
|
|
|
$last_response = (trim($response) == "\"OK\"") ? '<b><span class="pull-right text-success">OK</span></b>' : '<b><span class="pull-right text-danger">Error: ' . $response . '</span></b>';
|
|
|
|
if (trim($response) == "\"OK\"") {
|
2017-10-04 13:04:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
usleep(1500000);
|
|
|
|
$retry++;
|
2017-06-19 10:30:11 +02:00
|
|
|
}
|
2017-10-05 23:38:33 +02:00
|
|
|
echo (!isset($last_response)) ? '<b><span class="pull-right text-warning">Not running</span></b>' : $last_response;
|
2016-12-25 10:03:37 +01:00
|
|
|
}
|
2017-10-04 13:04:58 +02:00
|
|
|
|
|
|
|
if ($_GET['ACTION'] == "stop") {
|
|
|
|
$retry = 0;
|
2017-10-05 23:38:33 +02:00
|
|
|
while (docker('sogo-mailcow', 'info')[0]['State'] == "running" && $retry <= 3) {
|
2017-10-04 13:04:58 +02:00
|
|
|
$response = docker('sogo-mailcow', 'post', 'stop');
|
2017-10-05 23:38:33 +02:00
|
|
|
$last_response = (trim($response) == "\"OK\"") ? '<b><span class="pull-right text-success">OK</span></b>' : '<b><span class="pull-right text-danger">Error: ' . $response . '</span></b>';
|
|
|
|
if (trim($response) == "\"OK\"") {
|
2017-10-04 13:04:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
usleep(1500000);
|
|
|
|
$retry++;
|
|
|
|
}
|
2017-10-05 23:38:33 +02:00
|
|
|
echo (!isset($last_response)) ? '<b><span class="pull-right text-warning">Not running</span></b>' : $last_response;
|
2016-12-25 10:03:37 +01:00
|
|
|
}
|
2017-10-04 13:04:58 +02:00
|
|
|
|
2017-07-24 23:25:04 +02:00
|
|
|
?>
|