functions.docker.inc.php 6.7 KB

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