-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from clue-labs/attach
Add `containerAttach()` and `containerAttachStream()` API methods
- Loading branch information
Showing
8 changed files
with
322 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
// this example shows how the containerAttachStream() call can be used to get the output of the given container. | ||
// demonstrates the streaming attach API, which can be used to dump the container output as it arrives | ||
// | ||
// $ docker run -it --rm --name=foo busybox sh | ||
// $ php examples/attach-stream.php foo | ||
|
||
use Clue\CaretNotation\Encoder; | ||
use Clue\React\Docker\Client; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$container = isset($argv[1]) ? $argv[1] : 'foo'; | ||
echo 'Dumping output of container "' . $container . '" (pass as argument to this example)' . PHP_EOL; | ||
|
||
$loop = React\EventLoop\Factory::create(); | ||
$client = new Client($loop); | ||
|
||
// use caret notation for any control characters except \t, \r and \n | ||
$caret = new Encoder("\t\r\n"); | ||
|
||
$stream = $client->containerAttachStream($container, true, true); | ||
$stream->on('data', function ($data) use ($caret) { | ||
echo $caret->encode($data); | ||
}); | ||
|
||
$stream->on('error', function (Exception $e) { | ||
// will be called if either parameter is invalid | ||
echo 'ERROR: ' . $e->getMessage() . PHP_EOL; | ||
}); | ||
|
||
$stream->on('close', function () { | ||
echo 'CLOSED' . PHP_EOL; | ||
}); | ||
|
||
$loop->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
// This example executes a command within a new container and displays how fast | ||
// it can receive its output. | ||
// | ||
// $ php examples/benchmark-attach.php | ||
// $ php examples/benchmark-attach.php busybox echo -n hello | ||
// | ||
// Expect this to be noticeably faster than the (totally unfair) equivalent: | ||
// | ||
// $ docker run -i --rm --log-driver=none busybox dd if=/dev/zero bs=1M count=1000 status=none | dd of=/dev/null | ||
|
||
use Clue\React\Docker\Client; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
if (extension_loaded('xdebug')) { | ||
echo 'NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL; | ||
} | ||
|
||
$image = 'busybox'; | ||
$cmd = array('dd', 'if=/dev/zero', 'bs=1M', 'count=1000', 'status=none'); | ||
|
||
if (isset($argv[1])) { | ||
$image = $argv[1]; | ||
$cmd = array_slice($argv, 2); | ||
} | ||
|
||
$loop = React\EventLoop\Factory::create(); | ||
$client = new Client($loop); | ||
|
||
$client->containerCreate(array( | ||
'Image' => $image, | ||
'Cmd' => $cmd, | ||
'Tty' => false, | ||
'HostConfig' => array( | ||
'LogConfig' => array( | ||
'Type' => 'none' | ||
) | ||
) | ||
))->then(function ($container) use ($client, $loop) { | ||
$stream = $client->containerAttachStream($container['Id'], false, true); | ||
|
||
// we're creating the container without a log, so first wait for attach stream before starting | ||
$loop->addTimer(0.1, function () use ($client, $container) { | ||
$client->containerStart($container['Id'])->then(null, 'printf'); | ||
}); | ||
|
||
$start = microtime(true); | ||
$bytes = 0; | ||
$stream->on('data', function ($chunk) use (&$bytes) { | ||
$bytes += strlen($chunk); | ||
}); | ||
|
||
$stream->on('error', 'printf'); | ||
|
||
// show stats when stream ends | ||
$stream->on('close', function () use ($client, &$bytes, $start, $container) { | ||
$time = microtime(true) - $start; | ||
$client->containerRemove($container['Id'])->then(null, 'printf'); | ||
|
||
echo 'Received ' . $bytes . ' bytes in ' . round($time, 1) . 's => ' . round($bytes / $time / 1000000, 1) . ' MB/s' . PHP_EOL; | ||
}); | ||
}, 'printf'); | ||
|
||
$loop->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.