-
Notifications
You must be signed in to change notification settings - Fork 3
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 #319 from factorial-io/feature/docker-copy
Feature/docker copy
- Loading branch information
Showing
5 changed files
with
135 additions
and
10 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,103 @@ | ||
<?php | ||
|
||
|
||
namespace Phabalicious\Artifact\Actions\Base; | ||
|
||
use Phabalicious\Artifact\Actions\ActionBase; | ||
use Phabalicious\Configuration\HostConfig; | ||
use Phabalicious\Method\TaskContextInterface; | ||
use Phabalicious\ShellProvider\ShellProviderInterface; | ||
use Phabalicious\Utilities\Utilities; | ||
use Phabalicious\Validation\ValidationService; | ||
|
||
class DockerCopyAction extends ActionBase | ||
{ | ||
protected $dockerImageName = false; | ||
|
||
protected function validateArgumentsConfig(array $action_arguments, ValidationService $validation) | ||
{ | ||
$validation->hasKey('imageRootPath', 'action needs an image root path which is inside the images file system'); | ||
$validation->hasKey('image', 'action needs a docker image argument'); | ||
$validation->hasKey('to', 'action needs a to argument'); | ||
$validation->hasKey('from', 'action needs a from argument'); | ||
} | ||
|
||
public function run(HostConfig $host_config, TaskContextInterface $context) | ||
{ | ||
$this->getDockerImage($host_config, $context); | ||
parent::run($host_config, $context); // TODO: Change the autogenerated stub | ||
} | ||
|
||
protected function getDockerImage(HostConfig $host_config, TaskContextInterface $context) | ||
{ | ||
$variables = Utilities::buildVariablesFrom($host_config, $context); | ||
$replacements = Utilities::expandVariables($variables); | ||
|
||
$this->dockerImageName = Utilities::expandString($this->getArgument('image'), $replacements); | ||
} | ||
|
||
protected function getDirectoryContents(ShellProviderInterface $shell, string $path) | ||
{ | ||
$result = $shell->run(sprintf( | ||
'docker run --entrypoint /bin/ls --rm %s -1a %s', | ||
$this->dockerImageName, | ||
$path | ||
), true); | ||
|
||
if ($result->failed()) { | ||
$result->throwException('Could not get directory contents from docker image!'); | ||
} | ||
return $result->getOutput(); | ||
} | ||
|
||
protected function runImplementation( | ||
HostConfig $host_config, | ||
TaskContextInterface $context, | ||
ShellProviderInterface $shell, | ||
string $install_dir, | ||
string $target_dir | ||
) { | ||
$shell->pushWorkingDir($install_dir); | ||
|
||
if ($this->getArgument('imagePull', true)) { | ||
$shell->run(sprintf('docker pull -q %s', $this->dockerImageName)); | ||
} | ||
|
||
$image_root_path = $this->getArgument('imageRootPath', $install_dir); | ||
$files_to_copy = $this->getArgument('from'); | ||
|
||
if (!is_array($files_to_copy)) { | ||
if ($files_to_copy === '*') { | ||
$files_to_copy = array_filter( | ||
$this->getDirectoryContents($shell, $image_root_path), | ||
static function ($e) { | ||
return !in_array($e, ['.', '..']); | ||
} | ||
); | ||
} else { | ||
$files_to_copy = [$files_to_copy]; | ||
} | ||
} | ||
|
||
$files_to_skip = $context->getConfigurationService()->getSetting('excludeFiles.gitSync', []); | ||
|
||
// Make sure that git-related files are skipped. | ||
$files_to_skip[] = ".git"; | ||
$to = $target_dir . '/' . $this->getArgument('to'); | ||
|
||
// Make sure the target directory exists before copying. | ||
$shell->run(sprintf('mkdir -p %s', $to)); | ||
|
||
$docker_container = Utilities::getTempNamePrefixFromString('phab-docker-copy'); | ||
$shell->run(sprintf('docker create --name %s %s', $docker_container, $this->dockerImageName), false, true); | ||
foreach ($files_to_copy as $file) { | ||
if (!in_array($file, $files_to_skip)) { | ||
$shell->run(sprintf('rm -rf %s', $to . '/' . basename($file))); | ||
$shell->run(sprintf('docker cp -a %s:%s/%s %s', $docker_container, $image_root_path, $file, $to)); | ||
} | ||
} | ||
$shell->run(sprintf('docker rm %s', $docker_container), false, true); | ||
|
||
$shell->popWorkingDir(); | ||
} | ||
} |
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