2
0

functions.docker.inc.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // logger(array('return' => array(
  17. // 'type' => 'danger',
  18. // 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
  19. // 'msg' => $err,
  20. // )));
  21. return $err;
  22. }
  23. else {
  24. curl_close($curl);
  25. // logger(array('return' => array(
  26. // 'type' => 'success',
  27. // 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
  28. // )));
  29. $containers = json_decode($response, true);
  30. if (!empty($containers)) {
  31. foreach ($containers as $container) {
  32. if ($container['Config']['Labels']['com.docker.compose.service'] == $service_name
  33. && $container['Config']['Labels']['com.docker.compose.project'] == getenv('COMPOSE_PROJECT_NAME')) {
  34. return trim($container['Id']);
  35. }
  36. }
  37. }
  38. }
  39. return false;
  40. case 'containers':
  41. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/json');
  42. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  43. curl_setopt($curl, CURLOPT_POST, 0);
  44. curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
  45. $response = curl_exec($curl);
  46. if ($response === false) {
  47. $err = curl_error($curl);
  48. curl_close($curl);
  49. // logger(array('return' => array(
  50. // 'type' => 'danger',
  51. // 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
  52. // 'msg' => $err,
  53. // )));
  54. return $err;
  55. }
  56. else {
  57. curl_close($curl);
  58. // logger(array('return' => array(
  59. // 'type' => 'success',
  60. // 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
  61. // )));
  62. $containers = json_decode($response, true);
  63. if (!empty($containers)) {
  64. foreach ($containers as $container) {
  65. if ($container['Config']['Labels']['com.docker.compose.project'] == getenv('COMPOSE_PROJECT_NAME')) {
  66. $out[$container['Config']['Labels']['com.docker.compose.service']]['State'] = $container['State'];
  67. $out[$container['Config']['Labels']['com.docker.compose.service']]['Config'] = $container['Config'];
  68. }
  69. }
  70. }
  71. return (!empty($out)) ? $out : false;
  72. }
  73. return false;
  74. break;
  75. case 'info':
  76. if (empty($service_name)) {
  77. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/json');
  78. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  79. curl_setopt($curl, CURLOPT_POST, 0);
  80. curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
  81. }
  82. else {
  83. $container_id = docker('get_id', $service_name);
  84. if (ctype_xdigit($container_id)) {
  85. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/json');
  86. }
  87. else {
  88. // logger(array('return' => array(
  89. // 'type' => 'danger',
  90. // 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
  91. // 'msg' => 'invalid_container_id'
  92. // )));
  93. return false;
  94. }
  95. }
  96. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  97. curl_setopt($curl, CURLOPT_POST, 0);
  98. curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
  99. $response = curl_exec($curl);
  100. if ($response === false) {
  101. $err = curl_error($curl);
  102. curl_close($curl);
  103. // logger(array('return' => array(
  104. // 'type' => 'danger',
  105. // 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
  106. // 'msg' => $err,
  107. // )));
  108. return $err;
  109. }
  110. else {
  111. curl_close($curl);
  112. // logger(array('return' => array(
  113. // 'type' => 'success',
  114. // 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
  115. // )));
  116. $decoded_response = json_decode($response, true);
  117. if (!empty($decoded_response)) {
  118. if (empty($service_name)) {
  119. foreach ($decoded_response as $container) {
  120. if ($container['Config']['Labels']['com.docker.compose.project'] == getenv('COMPOSE_PROJECT_NAME')) {
  121. unset($container['Config']['Env']);
  122. $out[$container['Config']['Labels']['com.docker.compose.service']]['State'] = $container['State'];
  123. $out[$container['Config']['Labels']['com.docker.compose.service']]['Config'] = $container['Config'];
  124. }
  125. }
  126. }
  127. else {
  128. if ($decoded_response['Config']['Labels']['com.docker.compose.project'] == getenv('COMPOSE_PROJECT_NAME')) {
  129. unset($container['Config']['Env']);
  130. $out[$decoded_response['Config']['Labels']['com.docker.compose.service']]['State'] = $decoded_response['State'];
  131. $out[$decoded_response['Config']['Labels']['com.docker.compose.service']]['Config'] = $decoded_response['Config'];
  132. }
  133. }
  134. }
  135. if (empty($response)) {
  136. return true;
  137. }
  138. else {
  139. return (!empty($out)) ? $out : false;
  140. }
  141. }
  142. break;
  143. case 'post':
  144. if (!empty($attr1)) {
  145. $container_id = docker('get_id', $service_name);
  146. if (ctype_xdigit($container_id) && ctype_alnum($attr1)) {
  147. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/' . $attr1);
  148. curl_setopt($curl, CURLOPT_POST, 1);
  149. curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
  150. if (!empty($attr2)) {
  151. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($attr2));
  152. }
  153. if (!empty($extra_headers) && is_array($extra_headers)) {
  154. curl_setopt($curl, CURLOPT_HTTPHEADER, $extra_headers);
  155. }
  156. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  157. $response = curl_exec($curl);
  158. if ($response === false) {
  159. $err = curl_error($curl);
  160. curl_close($curl);
  161. logger(array('return' => array(array(
  162. 'type' => 'danger',
  163. 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
  164. 'msg' => $err,
  165. ))));
  166. return $err;
  167. }
  168. else {
  169. curl_close($curl);
  170. logger(array('return' => array(array(
  171. 'type' => 'success',
  172. 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
  173. ))));
  174. if (empty($response)) {
  175. return true;
  176. }
  177. else {
  178. return $response;
  179. }
  180. }
  181. }
  182. }
  183. break;
  184. }
  185. }