functions.docker.inc.php 7.8 KB

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