Skip to content
This repository was archived by the owner on Oct 26, 2019. It is now read-only.

ContainerManager::copy(): Copy files or folders from a container to a tar stream - #89

Closed
Boran wants to merge 9 commits into
docker-php:masterfrom
Boran:file-copy
Closed

ContainerManager::copy(): Copy files or folders from a container to a tar stream#89
Boran wants to merge 9 commits into
docker-php:masterfrom
Boran:file-copy

Conversation

@Boran

@Boran Boran commented Jan 28, 2015

Copy link
Copy Markdown
Contributor

Api for http://docs.docker.com/reference/api/docker_remote_api_v1.16/#copy-files-or-folders-from-a-container

Example usage

$lookfor = 'vanilla2';
$container = $manager->find($lookfor);
$resource = '/etc/default';      # copy this whole folder
$exportStream = $manager->copy($container, $resource);
$tarFileName  = "/tmp/resource.tar";
$tarFile      = fopen($tarFileName, 'w+');
stream_copy_to_stream($exportStream->detach(), $tarFile);
fclose($tarFile);
echo "$tarFileName written\n";

@hacfi

hacfi commented Jan 28, 2015

Copy link
Copy Markdown
Contributor

Sweet feature..will try this soon!

Comment thread src/Docker/Manager/ContainerManager.php Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can skip the double quotes around $resource here

@ubermuda

Copy link
Copy Markdown
Contributor

Sweet indeed! I have a couple questions/remarks before merging though:

  1. What's in the return stream exactly?
  2. Please write some tests for this method
  3. Can you add a small documentation for this too? Preferably something that's easy to integrate with Docs: Improve installation, add image pull, small changes #87

Comment thread src/Docker/Manager/ContainerManager.php Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing variable type here, I assume it would be string? Maybe you could rename the argument too, because $resource is misleading if it can't be an actual resource. What about $source?

@hacfi

hacfi commented Jan 29, 2015

Copy link
Copy Markdown
Contributor

@ubermuda about 1.: It’s a Guzzle\Stream\Stream instance of a tcp_socket/ssl stream containing the tar file contents.

Comment thread src/Docker/Manager/ContainerManager.php Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@return \Guzzle\Stream\Stream Tarfile stream would be more accurate and allows for autocompletion.

@ubermuda

Copy link
Copy Markdown
Contributor

Ok I misread the title, I was thinking it was meant to copy files to the container, not from. It's all clear now thanks :)

@hacfi

hacfi commented Jan 29, 2015

Copy link
Copy Markdown
Contributor

@Boran I think it makes sense to add an additional helper method containing the example code..something like

    public function copyToDisk(Container $container, $source, $destination)
    {
        $stream = $this->copy($container, $source);

        $output = fopen($destination, 'w+');
        stream_copy_to_stream($stream->detach(), $output);
        fclose($output);

        return $this;
    }

@ubermuda

Copy link
Copy Markdown
Contributor

@Boran I think it makes sense to add an additional helper method containing the example code..something like

+1

@Boran

Boran commented Jan 29, 2015

Copy link
Copy Markdown
Contributor Author

Made the comments and code changes.
One can now do
$manager->copyToDisk($container, '/etc/default', '/tmp/resource.tar')

Docs: my master /stage1/docker-php/doc does not have all the stuff from #87, I must check.
Tests: yes ....

@ubermuda

Copy link
Copy Markdown
Contributor

Great thanks! #87 has been merged btw :)

@Boran

Boran commented Jan 30, 2015

Copy link
Copy Markdown
Contributor Author

Is there doc explaining how to run tests?

@ubermuda

Copy link
Copy Markdown
Contributor

Running bin/phpunit should be enough? Do you have a specific problem?

@Boran

Boran commented Jan 30, 2015

Copy link
Copy Markdown
Contributor Author

Well I'm not used to phpunit. I installed it and then tried:

cd /var/www/html/sites/all/libraries/composer/stage1/docker-php
phpunit src/Docker/Tests/ContainerTest.php
PHPUnit 3.7.28 by Sebastian Bergmann.
Cannot open file "/var/www/html/sites/all/libraries/composer/stage1/docker-php/vendor/autoload.php".

Then tried in the directory where the autoload.php is...
cd /var/www/html/sites/all/libraries/composer

phpunit stage1/docker-php/src/Docker/Tests/ContainerTest.php
PHP Fatal error: Class 'Docker\Container' not found in /var/www/html/sites/all/libraries/composer/stage1/docker-php/src/Docker/Tests/ContainerTest.php on line 12
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:176
PHP 5. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/TextUI/TestRunner.php:349
PHP 6. PHPUnit_Framework_TestSuite->runTest() /usr/share/php/PHPUnit/Framework/TestSuite.php:745
PHP 7. PHPUnit_Framework_TestCase->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:775
PHP 8. PHPUnit_Framework_TestResult->run() /usr/share/php/PHPUnit/Framework/TestCase.php:783
PHP 9. PHPUnit_Framework_TestCase->runBare() /usr/share/php/PHPUnit/Framework/TestResult.php:648
PHP 10. PHPUnit_Framework_TestCase->runTest() /usr/share/php/PHPUnit/Framework/TestCase.php:838
PHP 11. ReflectionMethod->invokeArgs() /usr/share/php/PHPUnit/Framework/TestCase.php:983
PHP 12. Docker\Tests\ContainerTest->testAddEnvWithNoExistingEnv() /usr/share/php/PHPUnit/Framework/TestCase.php:983

Any tips?

@hacfi

hacfi commented Jan 31, 2015

Copy link
Copy Markdown
Contributor

@Boran Go to your docker-php dir (cd /var/www/html/sites/all/libraries/composer/stage1/docker-php) and run composer install. This should solve the issue. (Explanation: the tests are run on the repository outside of the context that docker-php is part of a project which already installed the required libs.)

@hacfi

hacfi commented Jan 31, 2015

Copy link
Copy Markdown
Contributor

Tested it one more time..works like a charm! Thanks @Boran ...that’s a very important feature!

@Boran

Boran commented Jan 31, 2015

Copy link
Copy Markdown
Contributor Author

So test written and working:

Starting test 'Docker\Tests\Manager\ContainerManagerTest::testcopyToDisk'.
.

A quick note on how to run tests:
@hacfi your method works, but also found the "--bootstrap" which is more elegant (composer won't install a second set of files). i.e.:

cd /var/www/html/sites/all/libraries/composer/stage1/docker-php
phpunit --debug --bootstrap /var/www/html/sites/all/libraries/composer/autoload.php src/Docker/Tests/Manager/ContainerManagerTest

Also: didn't find a way of running just one test function, e.g. testcopyToDisk(), which makes developing the test a bit more time consuming, is there a way? (Tried --filter 'Docker\Tests\Manager\ContainerManagerTest::testCreate', but that is ignored)

@hacfi

hacfi commented Feb 1, 2015

Copy link
Copy Markdown
Contributor

@Boran Had the same problem last week :)

Try:
phpunit --filter testCreate ContainerManagerTest src/Docker/Tests/Manager/ContainerManagerTest.php

@Boran

Boran commented Feb 1, 2015

Copy link
Copy Markdown
Contributor Author

Thanks, '--filter' works nicely:

phpunit --debug --bootstrap /var/www/html/sites/all/libraries/composer/autoload.php --filter testCopyToDisk ContainerManagerTest src/Docker/Tests/Manager/ContainerManagerTest.php
......
Starting test 'Docker\Tests\Manager\ContainerManagerTest::testCopyToDisk'.
Time: 1.26 seconds, Memory: 4.50Mb
OK (1 test, 1 assertion)

Comment thread docs/container.md Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Function, file and directory names should be enclosed with backticks (`)
  2. Add an s to the first File (first word in paragraph)

@Boran

Boran commented Feb 5, 2015

Copy link
Copy Markdown
Contributor Author

I think I got all your suggestions in?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo :)

@joelwurtz

Copy link
Copy Markdown
Member

@Boran Looks good, can you rebase the PR into a single commit before merging ? Thanks !

@Boran

Boran commented Mar 3, 2015

Copy link
Copy Markdown
Contributor Author

After trying to rebase and solve merge conflicts with master, am giving up on this branch. Created a new file-copy2 branch and PR #109.

@Boran Boran closed this Mar 3, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants