2016-12-25 10:03:37 +01:00
|
|
|
<?php
|
2017-12-09 13:17:15 +01:00
|
|
|
function docker($service_name, $action, $attr1 = null, $attr2 = null, $extra_headers = null) {
|
2017-10-04 13:04:58 +02:00
|
|
|
$curl = curl_init();
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER,array( 'Content-Type: application/json' ));
|
|
|
|
switch($action) {
|
|
|
|
case 'get_id':
|
2017-10-06 10:20:40 +02:00
|
|
|
curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/json');
|
2017-10-04 13:04:58 +02:00
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_POST, 0);
|
2018-04-26 14:06:10 +02:00
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
|
2017-10-04 13:04:58 +02:00
|
|
|
$response = curl_exec($curl);
|
|
|
|
if ($response === false) {
|
|
|
|
$err = curl_error($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
curl_close($curl);
|
|
|
|
$containers = json_decode($response, true);
|
|
|
|
if (!empty($containers)) {
|
|
|
|
foreach ($containers as $container) {
|
2017-10-06 10:20:40 +02:00
|
|
|
if ($container['Config']['Labels']['com.docker.compose.service'] == $service_name) {
|
2017-10-04 13:04:58 +02:00
|
|
|
return trim($container['Id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2018-05-20 23:28:03 +02:00
|
|
|
case 'states':
|
|
|
|
curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/json');
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_POST, 0);
|
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
|
|
|
|
$response = curl_exec($curl);
|
|
|
|
if ($response === false) {
|
|
|
|
$err = curl_error($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
curl_close($curl);
|
|
|
|
$containers = json_decode($response, true);
|
|
|
|
if (!empty($containers)) {
|
|
|
|
foreach ($containers as $container) {
|
|
|
|
$out[$container['Config']['Labels']['com.docker.compose.service']] = $container['State'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (!empty($out)) ? $out : false;
|
|
|
|
}
|
|
|
|
return false;
|
2017-10-04 13:04:58 +02:00
|
|
|
break;
|
|
|
|
case 'info':
|
|
|
|
$container_id = docker($service_name, 'get_id');
|
|
|
|
if (ctype_xdigit($container_id)) {
|
2017-10-06 10:20:40 +02:00
|
|
|
curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/json');
|
2017-10-04 13:04:58 +02:00
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
2017-10-05 23:38:33 +02:00
|
|
|
curl_setopt($curl, CURLOPT_POST, 0);
|
2018-04-26 14:06:10 +02:00
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
|
2017-10-04 13:04:58 +02:00
|
|
|
$response = curl_exec($curl);
|
|
|
|
if ($response === false) {
|
|
|
|
$err = curl_error($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
curl_close($curl);
|
|
|
|
if (empty($response)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return json_decode($response, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'post':
|
2017-12-09 13:17:15 +01:00
|
|
|
if (!empty($attr1)) {
|
2017-10-04 13:04:58 +02:00
|
|
|
$container_id = docker($service_name, 'get_id');
|
2017-12-09 13:17:15 +01:00
|
|
|
if (ctype_xdigit($container_id) && ctype_alnum($attr1)) {
|
|
|
|
curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/' . $attr1);
|
2017-10-04 13:04:58 +02:00
|
|
|
curl_setopt($curl, CURLOPT_POST, 1);
|
2018-04-26 14:06:10 +02:00
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
|
2017-12-09 13:17:15 +01:00
|
|
|
if (!empty($attr2)) {
|
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($attr2));
|
|
|
|
}
|
|
|
|
if (!empty($extra_headers) && is_array($extra_headers)) {
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $extra_headers);
|
2017-10-04 13:04:58 +02:00
|
|
|
}
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
$response = curl_exec($curl);
|
|
|
|
if ($response === false) {
|
|
|
|
$err = curl_error($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
curl_close($curl);
|
|
|
|
if (empty($response)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-04-26 14:06:10 +02:00
|
|
|
}
|