call_sogo_ctrl.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_UNIX_SOCKET_PATH, "/var/run/docker.sock");
  14. curl_setopt($curl, CURLOPT_URL, 'http:/v1.26/containers/json?all=1');
  15. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  16. curl_setopt($curl, CURLOPT_POST, 0);
  17. $response = curl_exec($curl);
  18. if ($response === false) {
  19. $err = curl_error($curl);
  20. curl_close($curl);
  21. return $err;
  22. }
  23. else {
  24. curl_close($curl);
  25. $containers = json_decode($response, true);
  26. if (!empty($containers)) {
  27. foreach ($containers as $container) {
  28. if ($container['Labels']['com.docker.compose.service'] == $service_name) {
  29. return trim($container['Id']);
  30. }
  31. }
  32. }
  33. }
  34. return false;
  35. break;
  36. case 'info':
  37. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, "/var/run/docker.sock");
  38. $container_id = docker($service_name, 'get_id');
  39. if (ctype_xdigit($container_id)) {
  40. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, "/var/run/docker.sock");
  41. curl_setopt($curl, CURLOPT_URL, 'http/containers/' . $container_id . '/json');
  42. curl_setopt($curl, CURLOPT_POST, 0);
  43. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  44. $response = curl_exec($curl);
  45. if ($response === false) {
  46. $err = curl_error($curl);
  47. curl_close($curl);
  48. return $err;
  49. }
  50. else {
  51. curl_close($curl);
  52. if (empty($response)) {
  53. return true;
  54. }
  55. else {
  56. return json_decode($response, true);
  57. }
  58. }
  59. }
  60. else {
  61. return false;
  62. }
  63. break;
  64. case 'post':
  65. if (!empty($post_action)) {
  66. $container_id = docker($service_name, 'get_id');
  67. if (ctype_xdigit($container_id)) {
  68. curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, "/var/run/docker.sock");
  69. curl_setopt($curl, CURLOPT_URL, 'http/containers/' . $container_id . '/' . $post_action);
  70. curl_setopt($curl, CURLOPT_POST, 1);
  71. if (!empty($post_fields)) {
  72. curl_setopt( $curl, CURLOPT_POSTFIELDS, json_encode($post_fields));
  73. }
  74. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  75. $response = curl_exec($curl);
  76. if ($response === false) {
  77. $err = curl_error($curl);
  78. curl_close($curl);
  79. return $err;
  80. }
  81. else {
  82. curl_close($curl);
  83. if (empty($response)) {
  84. return true;
  85. }
  86. else {
  87. return $response;
  88. }
  89. }
  90. }
  91. }
  92. break;
  93. }
  94. }
  95. if ($_GET['ACTION'] == "start") {
  96. $retry = 0;
  97. while (!docker('sogo-mailcow', 'info')['State']['Running'] && $retry <= 3) {
  98. $response = docker('sogo-mailcow', 'post', 'start');
  99. $last_response = ($response === true) ? '<b><span class="pull-right text-success">OK</span></b>' : '<b><span class="pull-right text-warning">Error: ' . $response . '</span></b>';
  100. if ($response === true) {
  101. break;
  102. }
  103. usleep(1500000);
  104. $retry++;
  105. }
  106. echo $last_response;
  107. }
  108. if ($_GET['ACTION'] == "stop") {
  109. $retry = 0;
  110. while (docker('sogo-mailcow', 'info')['State']['Running'] && $retry <= 3) {
  111. $response = docker('sogo-mailcow', 'post', 'stop');
  112. $last_response = ($response === true) ? '<b><span class="pull-right text-success">OK</span></b>' : '<b><span class="pull-right text-warning">Error: ' . $response . '</span></b>';
  113. if ($response === true) {
  114. break;
  115. }
  116. usleep(1500000);
  117. $retry++;
  118. }
  119. echo $last_response;
  120. }
  121. ?>