-
-
Notifications
You must be signed in to change notification settings - Fork 16
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 #9 from clue-labs/streaming
Add streaming API
- Loading branch information
Showing
13 changed files
with
856 additions
and
12 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
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,49 @@ | ||
<?php | ||
// this example shows how the containerCopy() call returns a TAR stream, | ||
// how it can be passed to a TAR decoder and how we can then pipe each | ||
// individual file to the console output. | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use React\EventLoop\Factory as LoopFactory; | ||
use Clue\React\Docker\Factory; | ||
use Clue\React\Tar\Decoder; | ||
use React\Stream\ReadableStreamInterface; | ||
use Clue\CaretNotation\Encoder; | ||
|
||
$container = isset($argv[1]) ? $argv[1] : 'asd'; | ||
$file = isset($argv[2]) ? $argv[2] : '/etc/passwd'; | ||
echo 'Container "' . $container . '" dumping "' . $file . '" (pass as arguments to this example)' . PHP_EOL; | ||
|
||
$loop = LoopFactory::create(); | ||
|
||
$factory = new Factory($loop); | ||
$client = $factory->createClient(); | ||
|
||
$stream = $client->containerCopyStream($container, array('Resource' => $file)); | ||
|
||
$tar = new Decoder(); | ||
|
||
// use caret notation for any control characters expect \t, \r and \n | ||
$caret = new Encoder("\t\r\n"); | ||
|
||
$tar->on('entry', function ($header, ReadableStreamInterface $file) use ($caret) { | ||
// write each entry to the console output | ||
echo '########## ' . $caret->encode($header['filename']) . ' ##########' . PHP_EOL; | ||
$file->on('data', function ($chunk) use ($caret) { | ||
echo $caret->encode($chunk); | ||
}); | ||
}); | ||
|
||
$tar->on('error', function ($e = null) { | ||
// should not be invoked, unless the stream is somehow interrupted | ||
echo 'ERROR processing tar stream' . PHP_EOL . $e; | ||
}); | ||
$stream->on('error', function ($e = null) { | ||
// will be called if either parameter is invalid | ||
echo 'ERROR requesting stream' . PHP_EOL . $e; | ||
}); | ||
|
||
$stream->pipe($tar); | ||
|
||
$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,30 @@ | ||
<?php | ||
// this example shows how the containerExport() call returns a TAR stream | ||
// and how we it can be piped into a output tar file. | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use React\EventLoop\StreamSelectLoop; | ||
use Clue\React\Docker\Factory; | ||
use React\Stream\Stream; | ||
|
||
$container = isset($argv[1]) ? $argv[1] : 'asd'; | ||
$target = isset($argv[2]) ? $argv[2] : ($container . '.tar'); | ||
echo 'Exporting whole container "' . $container . '" to "' . $target .'" (pass as arguments to this example)' . PHP_EOL; | ||
|
||
$loop = new StreamSelectLoop(); | ||
|
||
$factory = new Factory($loop); | ||
$client = $factory->createClient(); | ||
|
||
$stream = $client->containerExportStream($container); | ||
|
||
$stream->on('error', function ($e = null) { | ||
// will be called if the container is invalid/does not exist | ||
echo 'ERROR requesting stream' . PHP_EOL . $e; | ||
}); | ||
|
||
$out = new Stream(fopen($target, 'w'), $loop); | ||
$stream->pipe($out); | ||
|
||
$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,28 @@ | ||
<?php | ||
// this example shows how the imageCreateStream() call can be used to pull a given image. | ||
// demonstrates the JSON streaming API, individual progress events will be printed as they happen. | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use React\EventLoop\Factory as LoopFactory; | ||
use Clue\React\Docker\Factory; | ||
|
||
$image = isset($argv[1]) ? $argv[1] : 'clue/redis-benchmark'; | ||
echo 'Pulling image "' . $image . '" (pass as argument to this example)' . PHP_EOL; | ||
|
||
$loop = LoopFactory::create(); | ||
|
||
$factory = new Factory($loop); | ||
$client = $factory->createClient(); | ||
|
||
$stream = $client->imageCreateStream($image); | ||
|
||
$stream->on('progress', function ($progress) { | ||
echo 'progress: '. json_encode($progress) . PHP_EOL; | ||
}); | ||
|
||
$stream->on('close', function () { | ||
echo 'stream 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,26 @@ | ||
<?php | ||
// this example shows how the imagePush() call can be used to publish a given image. | ||
// this requires authorization and this example includes some invalid defaults. | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use React\EventLoop\Factory as LoopFactory; | ||
use Clue\React\Docker\Factory; | ||
|
||
$image = isset($argv[1]) ? $argv[1] : 'asd'; | ||
$auth = json_decode('{"username": "string", "password": "string", "email": "string", "serveraddress" : "string", "auth": ""}'); | ||
echo 'Pushing image "' . $image . '" (pass as argument to this example)' . PHP_EOL; | ||
|
||
$loop = LoopFactory::create(); | ||
|
||
$factory = new Factory($loop); | ||
$client = $factory->createClient(); | ||
|
||
$client->imagePush($image, null, null, $auth)->then( | ||
function ($response) { | ||
echo 'response: ' . json_encode($response) . PHP_EOL; | ||
}, | ||
'var_dump' | ||
); | ||
|
||
$loop->run(); |
Oops, something went wrong.