functions.docker.inc.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. function docker($service_name, $action, $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. return $err;
  17. }
  18. else {
  19. curl_close($curl);
  20. $containers = json_decode($response, true);
  21. if (!empty($containers)) {
  22. foreach ($containers as $container) {
  23. if ($container['Config']['Labels']['com.docker.compose.service'] == $service_name) {
  24. return trim($container['Id']);
  25. }
  26. }
  27. }
  28. }
  29. return false;
  30. case 'states':
  31. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/json');
  32. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  33. curl_setopt($curl, CURLOPT_POST, 0);
  34. curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
  35. $response = curl_exec($curl);
  36. if ($response === false) {
  37. $err = curl_error($curl);
  38. curl_close($curl);
  39. return $err;
  40. }
  41. else {
  42. curl_close($curl);
  43. $containers = json_decode($response, true);
  44. if (!empty($containers)) {
  45. foreach ($containers as $container) {
  46. $out[$container['Config']['Labels']['com.docker.compose.service']] = $container['State'];
  47. }
  48. }
  49. return (!empty($out)) ? $out : false;
  50. }
  51. return false;
  52. break;
  53. case 'info':
  54. $container_id = docker($service_name, 'get_id');
  55. if (ctype_xdigit($container_id)) {
  56. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/json');
  57. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  58. curl_setopt($curl, CURLOPT_POST, 0);
  59. curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
  60. $response = curl_exec($curl);
  61. if ($response === false) {
  62. $err = curl_error($curl);
  63. curl_close($curl);
  64. return $err;
  65. }
  66. else {
  67. curl_close($curl);
  68. if (empty($response)) {
  69. return true;
  70. }
  71. else {
  72. return json_decode($response, true);
  73. }
  74. }
  75. }
  76. else {
  77. return false;
  78. }
  79. break;
  80. case 'post':
  81. if (!empty($attr1)) {
  82. $container_id = docker($service_name, 'get_id');
  83. if (ctype_xdigit($container_id) && ctype_alnum($attr1)) {
  84. curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/' . $attr1);
  85. curl_setopt($curl, CURLOPT_POST, 1);
  86. curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
  87. if (!empty($attr2)) {
  88. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($attr2));
  89. }
  90. if (!empty($extra_headers) && is_array($extra_headers)) {
  91. curl_setopt($curl, CURLOPT_HTTPHEADER, $extra_headers);
  92. }
  93. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  94. $response = curl_exec($curl);
  95. if ($response === false) {
  96. $err = curl_error($curl);
  97. curl_close($curl);
  98. return $err;
  99. }
  100. else {
  101. curl_close($curl);
  102. if (empty($response)) {
  103. return true;
  104. }
  105. else {
  106. return $response;
  107. }
  108. }
  109. }
  110. }
  111. break;
  112. }
  113. }