Skip to content

Commit e7449f7

Browse files
committed
Add containerArchive() and containerArchiveStream() methods
1 parent 6b2228d commit e7449f7

3 files changed

Lines changed: 102 additions & 8 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
// this example shows how the containerCopy() call returns a TAR stream,
2+
// this example shows how the containerArchiveStream() method returns a TAR stream,
33
// how it can be passed to a TAR decoder and how we can then pipe each
44
// individual file to the console output.
55

@@ -20,7 +20,7 @@
2020
$factory = new Factory($loop);
2121
$client = $factory->createClient();
2222

23-
$stream = $client->containerCopyStream($container, $path);
23+
$stream = $client->containerArchiveStream($container, $path);
2424

2525
$tar = new Decoder();
2626

src/Client.php

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ public function containerRemove($container, $v = false, $force = false)
557557
}
558558

559559
/**
560-
* Copy files or folders of container id
560+
* [deprecated] Copy files or folders of container id
561561
*
562562
* This resolves with a string in the TAR file format containing all files
563563
* specified in the given $path.
@@ -575,6 +575,8 @@ public function containerRemove($container, $v = false, $force = false)
575575
* @return PromiseInterface Promise<string> tar stream
576576
* @link https://docs.docker.com/engine/api/v1.22/#copy-files-or-folders-from-a-container
577577
* @link https://github.com/clue/reactphp-tar
578+
* @deprecated 0.3.0 Deprecated in Docker Engine API v1.20 (Docker v1.8) and removed in Docker Engine API v1.24 (Docker v1.12), use `containerArchive()` instead
579+
* @see self::containerArchive()
578580
* @see self::containerCopyStream()
579581
*/
580582
public function containerCopy($container, $path)
@@ -593,7 +595,7 @@ public function containerCopy($container, $path)
593595
}
594596

595597
/**
596-
* Copy files or folders of container id
598+
* [Deprecated] Copy files or folders of container id
597599
*
598600
* This returns a stream in the TAR file format containing all files
599601
* specified in the given $path.
@@ -613,6 +615,8 @@ public function containerCopy($container, $path)
613615
* @return ReadableStreamInterface tar stream
614616
* @link https://docs.docker.com/engine/api/v1.22/#copy-files-or-folders-from-a-container
615617
* @link https://github.com/clue/reactphp-tar
618+
* @deprecated 0.3.0 Deprecated in Docker Engine API v1.20 (Docker v1.8) and removed in Docker Engine API v1.24 (Docker v1.12), use `containerArchiveStream()` instead
619+
* @see self::containerArchiveStream()
616620
* @see self::containerCopy()
617621
*/
618622
public function containerCopyStream($container, $path)
@@ -635,6 +639,80 @@ public function containerCopyStream($container, $path)
635639
);
636640
}
637641

642+
/**
643+
* Get a tar archive of a resource in the filesystem of container id.
644+
*
645+
* This resolves with a string in the TAR file format containing all files
646+
* specified in the given $path.
647+
*
648+
* Keep in mind that this means the whole string has to be kept in memory.
649+
* For bigger containers it's usually a better idea to use a streaming approach,
650+
* see containerArchiveStream() for more details.
651+
*
652+
* Accessing individual files in the TAR file format string is out of scope
653+
* for this library. Several libraries are available, one that is known to
654+
* work is clue/reactphp-tar (see links).
655+
*
656+
* @param string $container container ID
657+
* @param string $resource path to file or directory to archive
658+
* @return PromiseInterface Promise<string> tar stream
659+
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerArchive
660+
* @link https://github.com/clue/reactphp-tar
661+
* @since 0.3.0 Available as of Docker Engine API v1.20 (Docker v1.8), use deprecated `containerCopy()` on legacy versions
662+
* @see self::containerArchiveStream()
663+
*/
664+
public function containerArchive($container, $path)
665+
{
666+
return $this->browser->get(
667+
$this->uri->expand(
668+
'/containers/{container}/archive{?path}',
669+
array(
670+
'container' => $container,
671+
'path' => $path
672+
)
673+
)
674+
)->then(array($this->parser, 'expectPlain'));
675+
}
676+
677+
/**
678+
* Get a tar archive of a resource in the filesystem of container id.
679+
*
680+
* This returns a stream in the TAR file format containing all files
681+
* specified in the given $path.
682+
*
683+
* This works for (any number of) files of arbitrary sizes as only small chunks have to
684+
* be kept in memory.
685+
*
686+
* Accessing individual files in the TAR file format stream is out of scope
687+
* for this library. Several libraries are available, one that is known to
688+
* work is clue/reactphp-tar (see links).
689+
*
690+
* The resulting stream is a well-behaving readable stream that will emit
691+
* the normal stream events.
692+
*
693+
* @param string $container container ID
694+
* @param string $path path to file or directory to archive
695+
* @return ReadableStreamInterface tar stream
696+
* @link https://docs.docker.com/engine/api/v1.40/#operation/ContainerArchive
697+
* @link https://github.com/clue/reactphp-tar
698+
* @since 0.3.0 Available as of Docker Engine API v1.20 (Docker v1.8), use deprecated `containerCopyStream()` on legacy versions
699+
* @see self::containerArchive()
700+
*/
701+
public function containerArchiveStream($container, $path)
702+
{
703+
return $this->streamingParser->parsePlainStream(
704+
$this->browser->withOptions(array('streaming' => true))->get(
705+
$this->uri->expand(
706+
'/containers/{container}/archive{?path}',
707+
array(
708+
'container' => $container,
709+
'path' => $path
710+
)
711+
)
712+
)
713+
);
714+
}
715+
638716
/**
639717
* List images
640718
*

tests/ClientTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ public function testContainerCopy()
244244
$data = 'tar stream';
245245
$this->expectRequestFlow('post', '/containers/123/copy', $this->createResponse($data), 'expectPlain');
246246

247-
$config = array('Resource' => 'file.txt');
248-
$this->expectPromiseResolveWith($data, $this->client->containerCopy('123', $config));
247+
$this->expectPromiseResolveWith($data, $this->client->containerCopy('123', 'file.txt'));
249248
}
250249

251250
public function testContainerCopyStream()
@@ -255,8 +254,25 @@ public function testContainerCopyStream()
255254
$this->expectRequest('post', '/containers/123/copy', $this->createResponse(''));
256255
$this->streamingParser->expects($this->once())->method('parsePlainStream')->will($this->returnValue($stream));
257256

258-
$config = array('Resource' => 'file.txt');
259-
$this->assertSame($stream, $this->client->containerCopyStream('123', $config));
257+
$this->assertSame($stream, $this->client->containerCopyStream('123', 'file.txt'));
258+
}
259+
260+
public function testContainerArchive()
261+
{
262+
$data = 'tar stream';
263+
$this->expectRequestFlow('GET', '/containers/123/archive?path=file.txt', $this->createResponse($data), 'expectPlain');
264+
265+
$this->expectPromiseResolveWith($data, $this->client->containerArchive('123', 'file.txt'));
266+
}
267+
268+
public function testContainerArchiveStream()
269+
{
270+
$stream = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock();
271+
272+
$this->expectRequest('GET', '/containers/123/archive?path=file.txt', $this->createResponse(''));
273+
$this->streamingParser->expects($this->once())->method('parsePlainStream')->will($this->returnValue($stream));
274+
275+
$this->assertSame($stream, $this->client->containerArchiveStream('123', 'file.txt'));
260276
}
261277

262278
public function testImageList()

0 commit comments

Comments
 (0)