Skip to content

Commit 4c2b232

Browse files
committed
Merge pull request #34 from clue-labs/inspect
Add execInspect() API endpoint
2 parents fd6dc81 + 54df0bc commit 4c2b232

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

examples/exec.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
// this simple example executes a "sleep 2" within the given running container
3+
4+
require __DIR__ . '/../vendor/autoload.php';
5+
6+
use React\EventLoop\Factory as LoopFactory;
7+
use Clue\React\Docker\Factory;
8+
use Clue\React\Docker\ExecHelper;
9+
use React\Stream\Stream;
10+
use Clue\React\Buzz\Message\ResponseException;
11+
12+
$container = isset($argv[1]) ? $argv[1] : 'asd';
13+
14+
$loop = LoopFactory::create();
15+
16+
$factory = new Factory($loop);
17+
$client = $factory->createClient();
18+
19+
$client->execCreate($container, array('Cmd' => array('sleep', '2'), 'AttachStdout' => true))->then(function ($info) use ($client) {
20+
echo 'Created with info: ' . json_encode($info) . PHP_EOL;
21+
22+
return $client->execInspect($info['Id']);
23+
})->then(function ($info) use ($client) {
24+
echo 'Inspected after creation: ' . json_encode($info, JSON_PRETTY_PRINT) . PHP_EOL;
25+
26+
return $client->execStart($info['ID'], array())->then(function ($out) use ($client, $info) {
27+
echo 'Starting returned: ';
28+
var_dump($out);
29+
30+
return $client->execInspect($info['ID']);
31+
});
32+
})->then(function ($info) {
33+
echo 'Inspected after execution: ' . json_encode($info, JSON_PRETTY_PRINT) . PHP_EOL;
34+
}, function (Exception $e) {
35+
echo 'ERROR: ' . $e->getMessage() . PHP_EOL;
36+
37+
if ($e instanceof ResponseException) {
38+
echo 'Response: ' . $e->getResponse()->getBody() . PHP_EOL;
39+
}
40+
});
41+
42+
$loop->run();

src/Client.php

+21
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,27 @@ public function execResize($exec, $w, $h)
949949
)->then(array($this->parser, 'expectEmpty'));
950950
}
951951

952+
/**
953+
* Returns low-level information about the exec command id.
954+
*
955+
* Requires API v1.16+ / Docker v1.4+
956+
*
957+
* @param string $exec exec ID
958+
* @return PromiseInterface Promise<array>
959+
* @link https://docs.docker.com/engine/reference/api/docker_remote_api_v1.16/#exec-inspect
960+
*/
961+
public function execInspect($exec)
962+
{
963+
return $this->browser->get(
964+
$this->uri->expand(
965+
'/exec/{exec}/json',
966+
array(
967+
'exec' => $exec
968+
)
969+
)
970+
)->then(array($this->parser, 'expectJson'));
971+
}
972+
952973
private function postJson($url, $data)
953974
{
954975
$body = $this->json($data);

tests/ClientTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,14 @@ public function testExecResize()
400400
$this->expectPromiseResolveWith('', $this->client->execResize(123, 800, 600));
401401
}
402402

403+
public function testExecInspect()
404+
{
405+
$json = array();
406+
$this->expectRequestFlow('get', '/exec/123/json', $this->createResponseJson($json), 'expectJson');
407+
408+
$this->expectPromiseResolveWith($json, $this->client->execInspect(123));
409+
}
410+
403411
private function expectRequestFlow($method, $url, ResponseInterface $response, $parser)
404412
{
405413
$return = (string)$response->getBody();

0 commit comments

Comments
 (0)