container_ctrl.php 2.5 KB

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