functions.docker.inc.php 3.7 KB

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