call_sogo_ctrl.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. session_start();
  3. $AuthUsers = array("admin");
  4. if (!isset($_SESSION['mailcow_cc_role']) OR !in_array($_SESSION['mailcow_cc_role'], $AuthUsers)) {
  5. echo "Not allowed." . PHP_EOL;
  6. exit();
  7. }
  8. function docker($service_name, $action, $post_action = null, $post_fields = null) {
  9. $curl = curl_init();
  10. curl_setopt($curl, CURLOPT_HTTPHEADER,array( 'Content-Type: application/json' ));
  11. switch($action) {
  12. case 'get_id':
  13. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/json');
  14. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  15. curl_setopt($curl, CURLOPT_POST, 0);
  16. $response = curl_exec($curl);
  17. if ($response === false) {
  18. $err = curl_error($curl);
  19. curl_close($curl);
  20. return $err;
  21. }
  22. else {
  23. curl_close($curl);
  24. $containers = json_decode($response, true);
  25. if (!empty($containers)) {
  26. foreach ($containers as $container) {
  27. if ($container['Config']['Labels']['com.docker.compose.service'] == $service_name) {
  28. return trim($container['Id']);
  29. }
  30. }
  31. }
  32. }
  33. return false;
  34. break;
  35. case 'info':
  36. $container_id = docker($service_name, 'get_id');
  37. if (ctype_xdigit($container_id)) {
  38. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/json');
  39. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  40. curl_setopt($curl, CURLOPT_POST, 0);
  41. $response = curl_exec($curl);
  42. if ($response === false) {
  43. $err = curl_error($curl);
  44. curl_close($curl);
  45. return $err;
  46. }
  47. else {
  48. curl_close($curl);
  49. if (empty($response)) {
  50. return true;
  51. }
  52. else {
  53. return json_decode($response, true);
  54. }
  55. }
  56. }
  57. else {
  58. return false;
  59. }
  60. break;
  61. case 'post':
  62. if (!empty($post_action)) {
  63. $container_id = docker($service_name, 'get_id');
  64. if (ctype_xdigit($container_id) && ctype_alnum($post_action)) {
  65. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/' . $post_action);
  66. curl_setopt($curl, CURLOPT_POST, 1);
  67. if (!empty($post_fields)) {
  68. curl_setopt( $curl, CURLOPT_POSTFIELDS, json_encode($post_fields));
  69. }
  70. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  71. $response = curl_exec($curl);
  72. if ($response === false) {
  73. $err = curl_error($curl);
  74. curl_close($curl);
  75. return $err;
  76. }
  77. else {
  78. curl_close($curl);
  79. if (empty($response)) {
  80. return true;
  81. }
  82. else {
  83. return $response;
  84. }
  85. }
  86. }
  87. }
  88. break;
  89. }
  90. }
  91. if ($_GET['ACTION'] == "start") {
  92. $retry = 0;
  93. while (docker('sogo-mailcow', 'info')['State']['Running'] != 1 && $retry <= 3) {
  94. $response = docker('sogo-mailcow', 'post', 'start');
  95. $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>';
  96. if (trim($response) == "\"OK\"") {
  97. break;
  98. }
  99. usleep(1500000);
  100. $retry++;
  101. }
  102. echo (!isset($last_response)) ? '<b><span class="pull-right text-warning">Already running</span></b>' : $last_response;
  103. }
  104. if ($_GET['ACTION'] == "stop") {
  105. $retry = 0;
  106. while (docker('sogo-mailcow', 'info')['State']['Running'] == 1 && $retry <= 3) {
  107. $response = docker('sogo-mailcow', 'post', 'stop');
  108. $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>';
  109. if (trim($response) == "\"OK\"") {
  110. break;
  111. }
  112. usleep(1500000);
  113. $retry++;
  114. }
  115. echo (!isset($last_response)) ? '<b><span class="pull-right text-warning">Not running</span></b>' : $last_response;
  116. }
  117. ?>