functions.docker.inc.php 8.1 KB

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