functions.docker.inc.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 'post':
  63. if (!empty($attr1)) {
  64. $container_id = docker($service_name, 'get_id');
  65. if (ctype_xdigit($container_id) && ctype_alnum($attr1)) {
  66. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/' . $attr1);
  67. curl_setopt($curl, CURLOPT_POST, 1);
  68. if (!empty($attr2)) {
  69. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($attr2));
  70. }
  71. if (!empty($extra_headers) && is_array($extra_headers)) {
  72. curl_setopt($curl, CURLOPT_HTTPHEADER, $extra_headers);
  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. }