2016-12-25 17:03:37 +08:00
|
|
|
<?php
|
2018-07-29 06:38:22 +08:00
|
|
|
function docker($action, $service_name = null, $attr1 = null, $attr2 = null, $extra_headers = null) {
|
2018-07-24 02:01:01 +08:00
|
|
|
global $DOCKER_TIMEOUT;
|
2017-10-04 19:04:58 +08:00
|
|
|
$curl = curl_init();
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER,array( 'Content-Type: application/json' ));
|
|
|
|
switch($action) {
|
|
|
|
case 'get_id':
|
2017-10-06 16:20:40 +08:00
|
|
|
curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/json');
|
2017-10-04 19:04:58 +08:00
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_POST, 0);
|
2018-07-24 02:01:01 +08:00
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
|
2017-10-04 19:04:58 +08:00
|
|
|
$response = curl_exec($curl);
|
|
|
|
if ($response === false) {
|
|
|
|
$err = curl_error($curl);
|
|
|
|
curl_close($curl);
|
2018-08-04 02:31:33 +08:00
|
|
|
// logger(array('return' => array(
|
|
|
|
// 'type' => 'danger',
|
|
|
|
// 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
|
|
|
|
// 'msg' => $err,
|
|
|
|
// )));
|
2017-10-04 19:04:58 +08:00
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
curl_close($curl);
|
2018-08-04 02:31:33 +08:00
|
|
|
// logger(array('return' => array(
|
|
|
|
// 'type' => 'success',
|
|
|
|
// 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
|
|
|
|
// )));
|
2017-10-04 19:04:58 +08:00
|
|
|
$containers = json_decode($response, true);
|
|
|
|
if (!empty($containers)) {
|
|
|
|
foreach ($containers as $container) {
|
2018-07-29 06:38:22 +08:00
|
|
|
if ($container['Config']['Labels']['com.docker.compose.service'] == $service_name
|
|
|
|
&& $container['Config']['Labels']['com.docker.compose.project'] == getenv('COMPOSE_PROJECT_NAME')) {
|
2017-10-04 19:04:58 +08:00
|
|
|
return trim($container['Id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2018-07-29 06:38:22 +08:00
|
|
|
case 'containers':
|
2018-05-21 05:28:03 +08:00
|
|
|
curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/json');
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_POST, 0);
|
2018-07-24 02:01:01 +08:00
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
|
2018-05-21 05:28:03 +08:00
|
|
|
$response = curl_exec($curl);
|
|
|
|
if ($response === false) {
|
|
|
|
$err = curl_error($curl);
|
|
|
|
curl_close($curl);
|
2018-08-04 02:31:33 +08:00
|
|
|
// logger(array('return' => array(
|
|
|
|
// 'type' => 'danger',
|
|
|
|
// 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
|
|
|
|
// 'msg' => $err,
|
|
|
|
// )));
|
2018-05-21 05:28:03 +08:00
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
curl_close($curl);
|
2018-08-04 02:31:33 +08:00
|
|
|
// logger(array('return' => array(
|
|
|
|
// 'type' => 'success',
|
|
|
|
// 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
|
|
|
|
// )));
|
2018-05-21 05:28:03 +08:00
|
|
|
$containers = json_decode($response, true);
|
|
|
|
if (!empty($containers)) {
|
|
|
|
foreach ($containers as $container) {
|
2018-07-29 06:38:22 +08:00
|
|
|
if ($container['Config']['Labels']['com.docker.compose.project'] == getenv('COMPOSE_PROJECT_NAME')) {
|
|
|
|
$out[$container['Config']['Labels']['com.docker.compose.service']]['State'] = $container['State'];
|
|
|
|
$out[$container['Config']['Labels']['com.docker.compose.service']]['Config'] = $container['Config'];
|
|
|
|
}
|
2018-05-21 05:28:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (!empty($out)) ? $out : false;
|
|
|
|
}
|
|
|
|
return false;
|
2017-10-04 19:04:58 +08:00
|
|
|
break;
|
|
|
|
case 'info':
|
2018-07-29 06:38:22 +08:00
|
|
|
if (empty($service_name)) {
|
|
|
|
curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/json');
|
2017-10-04 19:04:58 +08:00
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
2017-10-06 05:38:33 +08:00
|
|
|
curl_setopt($curl, CURLOPT_POST, 0);
|
2018-07-24 02:01:01 +08:00
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
|
2018-07-29 06:38:22 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$container_id = docker('get_id', $service_name);
|
|
|
|
if (ctype_xdigit($container_id)) {
|
|
|
|
curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/json');
|
2017-10-04 19:04:58 +08:00
|
|
|
}
|
|
|
|
else {
|
2018-08-04 02:31:33 +08:00
|
|
|
// logger(array('return' => array(
|
|
|
|
// 'type' => 'danger',
|
|
|
|
// 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
|
|
|
|
// 'msg' => 'invalid_container_id'
|
|
|
|
// )));
|
2018-07-29 06:38:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_POST, 0);
|
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
|
|
|
|
$response = curl_exec($curl);
|
|
|
|
if ($response === false) {
|
|
|
|
$err = curl_error($curl);
|
|
|
|
curl_close($curl);
|
2018-08-04 02:31:33 +08:00
|
|
|
// logger(array('return' => array(
|
|
|
|
// 'type' => 'danger',
|
|
|
|
// 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
|
|
|
|
// 'msg' => $err,
|
|
|
|
// )));
|
2018-07-29 06:38:22 +08:00
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
curl_close($curl);
|
2018-08-04 02:31:33 +08:00
|
|
|
// logger(array('return' => array(
|
|
|
|
// 'type' => 'success',
|
|
|
|
// 'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
|
|
|
|
// )));
|
2018-07-29 06:38:22 +08:00
|
|
|
$decoded_response = json_decode($response, true);
|
|
|
|
if (!empty($decoded_response)) {
|
|
|
|
if (empty($service_name)) {
|
|
|
|
foreach ($decoded_response as $container) {
|
|
|
|
if ($container['Config']['Labels']['com.docker.compose.project'] == getenv('COMPOSE_PROJECT_NAME')) {
|
|
|
|
unset($container['Config']['Env']);
|
|
|
|
$out[$container['Config']['Labels']['com.docker.compose.service']]['State'] = $container['State'];
|
|
|
|
$out[$container['Config']['Labels']['com.docker.compose.service']]['Config'] = $container['Config'];
|
|
|
|
}
|
|
|
|
}
|
2017-10-04 19:04:58 +08:00
|
|
|
}
|
|
|
|
else {
|
2018-07-29 06:38:22 +08:00
|
|
|
if ($decoded_response['Config']['Labels']['com.docker.compose.project'] == getenv('COMPOSE_PROJECT_NAME')) {
|
|
|
|
unset($container['Config']['Env']);
|
|
|
|
$out[$decoded_response['Config']['Labels']['com.docker.compose.service']]['State'] = $decoded_response['State'];
|
|
|
|
$out[$decoded_response['Config']['Labels']['com.docker.compose.service']]['Config'] = $decoded_response['Config'];
|
|
|
|
}
|
2017-10-04 19:04:58 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-29 06:38:22 +08:00
|
|
|
if (empty($response)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return (!empty($out)) ? $out : false;
|
|
|
|
}
|
2017-10-04 19:04:58 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'post':
|
2017-12-09 20:17:15 +08:00
|
|
|
if (!empty($attr1)) {
|
2018-07-29 06:38:22 +08:00
|
|
|
$container_id = docker('get_id', $service_name);
|
2017-12-09 20:17:15 +08:00
|
|
|
if (ctype_xdigit($container_id) && ctype_alnum($attr1)) {
|
|
|
|
curl_setopt($curl, CURLOPT_URL, 'http://dockerapi:8080/containers/' . $container_id . '/' . $attr1);
|
2017-10-04 19:04:58 +08:00
|
|
|
curl_setopt($curl, CURLOPT_POST, 1);
|
2018-07-24 02:01:01 +08:00
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, $DOCKER_TIMEOUT);
|
2017-12-09 20:17:15 +08: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 19:04:58 +08:00
|
|
|
}
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
$response = curl_exec($curl);
|
|
|
|
if ($response === false) {
|
|
|
|
$err = curl_error($curl);
|
|
|
|
curl_close($curl);
|
2018-08-04 02:31:33 +08:00
|
|
|
logger(array('return' => array(
|
|
|
|
'type' => 'danger',
|
|
|
|
'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
|
|
|
|
'msg' => $err,
|
|
|
|
)));
|
2017-10-04 19:04:58 +08:00
|
|
|
return $err;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
curl_close($curl);
|
2018-08-04 02:31:33 +08:00
|
|
|
logger(array('return' => array(
|
|
|
|
'type' => 'success',
|
|
|
|
'log' => array(__FUNCTION__, $action, $service_name, $attr1, $attr2, $extra_headers),
|
|
|
|
)));
|
2017-10-04 19:04:58 +08:00
|
|
|
if (empty($response)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-04-26 20:06:10 +08:00
|
|
|
}
|