functions.docker.inc.php 8.0 KB

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