container_ctrl.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. session_start();
  3. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  4. if (!isset($_SESSION['mailcow_cc_role']) || $_SESSION['mailcow_cc_role'] != 'admin') {
  5. exit();
  6. }
  7. if (preg_match('/^[a-z\-]{0,}-mailcow/', $_GET['service'])) {
  8. if ($_GET['action'] == "start") {
  9. header('Content-Type: text/html; charset=utf-8');
  10. $retry = 0;
  11. while (docker('info', $_GET['service'])['State']['Running'] != 1 && $retry <= 3) {
  12. $response = docker('post', $_GET['service'], 'start');
  13. $response = json_decode($response, true);
  14. $last_response = ($response['type'] == "success") ? '<b><span class="pull-right text-success">OK</span></b>' : '<b><span class="pull-right text-danger">Error: ' . $response['msg'] . '</span></b>';
  15. if ($response['type'] == "success") {
  16. break;
  17. }
  18. usleep(1500000);
  19. $retry++;
  20. }
  21. echo (!isset($last_response)) ? '<b><span class="pull-right text-warning">Already running</span></b>' : $last_response;
  22. }
  23. if ($_GET['action'] == "stop") {
  24. header('Content-Type: text/html; charset=utf-8');
  25. $retry = 0;
  26. while (docker('info', $_GET['service'])['State']['Running'] == 1 && $retry <= 3) {
  27. $response = docker('post', $_GET['service'], 'stop');
  28. $response = json_decode($response, true);
  29. $last_response = ($response['type'] == "success") ? '<b><span class="pull-right text-success">OK</span></b>' : '<b><span class="pull-right text-danger">Error: ' . $response['msg'] . '</span></b>';
  30. if ($response['type'] == "success") {
  31. break;
  32. }
  33. usleep(1500000);
  34. $retry++;
  35. }
  36. echo (!isset($last_response)) ? '<b><span class="pull-right text-warning">Not running</span></b>' : $last_response;
  37. }
  38. if ($_GET['action'] == "restart") {
  39. header('Content-Type: text/html; charset=utf-8');
  40. $response = docker('post', $_GET['service'], 'restart');
  41. $response = json_decode($response, true);
  42. $last_response = ($response['type'] == "success") ? '<b><span class="pull-right text-success">OK</span></b>' : '<b><span class="pull-right text-danger">Error: ' . $response['msg'] . '</span></b>';
  43. echo (!isset($last_response)) ? '<b><span class="pull-right text-warning">Cannot restart container</span></b>' : $last_response;
  44. }
  45. if ($_GET['action'] == "logs") {
  46. $lines = (empty($_GET['lines']) || !is_numeric($_GET['lines'])) ? 1000 : $_GET['lines'];
  47. header('Content-Type: text/plain; charset=utf-8');
  48. print_r(preg_split('/\n/', docker('logs', $_GET['service'], $lines)));
  49. }
  50. }
  51. ?>