functions.docker.inc.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. function docker($service_name, $action, $attr1 = null, $attr2 = null, $extra_headers = null) {
  3. if ($_SESSION['mailcow_cc_role'] != "admin") {
  4. $_SESSION['return'] = array(
  5. 'type' => 'danger',
  6. 'msg' => sprintf($lang['danger']['access_denied'])
  7. );
  8. return false;
  9. }
  10. $curl = curl_init();
  11. curl_setopt($curl, CURLOPT_HTTPHEADER,array( 'Content-Type: application/json' ));
  12. switch($action) {
  13. case 'get_id':
  14. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/json');
  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['Config']['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. $container_id = docker($service_name, 'get_id');
  38. if (ctype_xdigit($container_id)) {
  39. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/json');
  40. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  41. curl_setopt($curl, CURLOPT_POST, 0);
  42. $response = curl_exec($curl);
  43. if ($response === false) {
  44. $err = curl_error($curl);
  45. curl_close($curl);
  46. return $err;
  47. }
  48. else {
  49. curl_close($curl);
  50. if (empty($response)) {
  51. return true;
  52. }
  53. else {
  54. return json_decode($response, true);
  55. }
  56. }
  57. }
  58. else {
  59. return false;
  60. }
  61. break;
  62. case 'logs':
  63. $container_id = docker($service_name, 'get_id');
  64. if (ctype_xdigit($container_id)) {
  65. $lines = (empty($attr1) || !is_numeric($attr1)) ? 100 : $attr1;
  66. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/logs/' . $lines);
  67. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  68. curl_setopt($curl, CURLOPT_POST, 0);
  69. $response = curl_exec($curl);
  70. if ($response === false) {
  71. $err = curl_error($curl);
  72. curl_close($curl);
  73. return $err;
  74. }
  75. else {
  76. curl_close($curl);
  77. if (empty($response)) {
  78. return true;
  79. }
  80. else {
  81. return json_decode($response, true);
  82. }
  83. }
  84. }
  85. else {
  86. return false;
  87. }
  88. break;
  89. case 'post':
  90. if (!empty($attr1)) {
  91. $container_id = docker($service_name, 'get_id');
  92. if (ctype_xdigit($container_id) && ctype_alnum($attr1)) {
  93. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/' . $attr1);
  94. curl_setopt($curl, CURLOPT_POST, 1);
  95. if (!empty($attr2)) {
  96. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($attr2));
  97. }
  98. if (!empty($extra_headers) && is_array($extra_headers)) {
  99. curl_setopt($curl, CURLOPT_HTTPHEADER, $extra_headers);
  100. }
  101. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  102. $response = curl_exec($curl);
  103. if ($response === false) {
  104. $err = curl_error($curl);
  105. curl_close($curl);
  106. return $err;
  107. }
  108. else {
  109. curl_close($curl);
  110. if (empty($response)) {
  111. return true;
  112. }
  113. else {
  114. return $response;
  115. }
  116. }
  117. }
  118. }
  119. break;
  120. }
  121. }