functions.docker.inc.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. function docker($action, $service_name = null, $attr1 = null, $attr2 = null, $extra_headers = null) {
  3. global $DOCKER_TIMEOUT;
  4. $curl = curl_init();
  5. curl_setopt($curl, CURLOPT_HTTPHEADER,array( 'Content-Type: application/json' ));
  6. switch($action) {
  7. case 'get_id':
  8. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/json');
  9. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  10. curl_setopt($curl, CURLOPT_POST, 0);
  11. curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
  12. $response = curl_exec($curl);
  13. if ($response === false) {
  14. $err = curl_error($curl);
  15. curl_close($curl);
  16. return $err;
  17. }
  18. else {
  19. curl_close($curl);
  20. $containers = json_decode($response, true);
  21. if (!empty($containers)) {
  22. foreach ($containers as $container) {
  23. if ($container['Config']['Labels']['com.docker.compose.service'] == $service_name
  24. && $container['Config']['Labels']['com.docker.compose.project'] == getenv('COMPOSE_PROJECT_NAME')) {
  25. return trim($container['Id']);
  26. }
  27. }
  28. }
  29. }
  30. return false;
  31. case 'containers':
  32. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/json');
  33. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  34. curl_setopt($curl, CURLOPT_POST, 0);
  35. curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
  36. $response = curl_exec($curl);
  37. if ($response === false) {
  38. $err = curl_error($curl);
  39. curl_close($curl);
  40. return $err;
  41. }
  42. else {
  43. curl_close($curl);
  44. $containers = json_decode($response, true);
  45. if (!empty($containers)) {
  46. foreach ($containers as $container) {
  47. if ($container['Config']['Labels']['com.docker.compose.project'] == getenv('COMPOSE_PROJECT_NAME')) {
  48. $out[$container['Config']['Labels']['com.docker.compose.service']]['State'] = $container['State'];
  49. $out[$container['Config']['Labels']['com.docker.compose.service']]['Config'] = $container['Config'];
  50. }
  51. }
  52. }
  53. return (!empty($out)) ? $out : false;
  54. }
  55. return false;
  56. break;
  57. case 'info':
  58. if (empty($service_name)) {
  59. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/json');
  60. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  61. curl_setopt($curl, CURLOPT_POST, 0);
  62. curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
  63. }
  64. else {
  65. $container_id = docker('get_id', $service_name);
  66. if (ctype_xdigit($container_id)) {
  67. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/json');
  68. }
  69. else {
  70. return false;
  71. }
  72. }
  73. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  74. curl_setopt($curl, CURLOPT_POST, 0);
  75. curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
  76. $response = curl_exec($curl);
  77. if ($response === false) {
  78. $err = curl_error($curl);
  79. curl_close($curl);
  80. return $err;
  81. }
  82. else {
  83. curl_close($curl);
  84. $decoded_response = json_decode($response, true);
  85. if (!empty($decoded_response)) {
  86. if (empty($service_name)) {
  87. foreach ($decoded_response as $container) {
  88. if ($container['Config']['Labels']['com.docker.compose.project'] == getenv('COMPOSE_PROJECT_NAME')) {
  89. unset($container['Config']['Env']);
  90. $out[$container['Config']['Labels']['com.docker.compose.service']]['State'] = $container['State'];
  91. $out[$container['Config']['Labels']['com.docker.compose.service']]['Config'] = $container['Config'];
  92. }
  93. }
  94. }
  95. else {
  96. if ($decoded_response['Config']['Labels']['com.docker.compose.project'] == getenv('COMPOSE_PROJECT_NAME')) {
  97. unset($container['Config']['Env']);
  98. $out[$decoded_response['Config']['Labels']['com.docker.compose.service']]['State'] = $decoded_response['State'];
  99. $out[$decoded_response['Config']['Labels']['com.docker.compose.service']]['Config'] = $decoded_response['Config'];
  100. }
  101. }
  102. }
  103. if (empty($response)) {
  104. return true;
  105. }
  106. else {
  107. return (!empty($out)) ? $out : false;
  108. }
  109. }
  110. break;
  111. case 'post':
  112. if (!empty($attr1)) {
  113. $container_id = docker('get_id', $service_name);
  114. if (ctype_xdigit($container_id) && ctype_alnum($attr1)) {
  115. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/' . $attr1);
  116. curl_setopt($curl, CURLOPT_POST, 1);
  117. curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
  118. if (!empty($attr2)) {
  119. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($attr2));
  120. }
  121. if (!empty($extra_headers) && is_array($extra_headers)) {
  122. curl_setopt($curl, CURLOPT_HTTPHEADER, $extra_headers);
  123. }
  124. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  125. $response = curl_exec($curl);
  126. if ($response === false) {
  127. $err = curl_error($curl);
  128. curl_close($curl);
  129. return $err;
  130. }
  131. else {
  132. curl_close($curl);
  133. if (empty($response)) {
  134. return true;
  135. }
  136. else {
  137. return $response;
  138. }
  139. }
  140. }
  141. }
  142. break;
  143. }
  144. }