Skip to content

Commit

Permalink
master Import/Export:411 ensure to export current branch, php version,
Browse files Browse the repository at this point in the history
repository name and url
  • Loading branch information
daniwe4 committed Feb 14, 2024
1 parent 942673a commit d3fd730
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 13 deletions.
79 changes: 74 additions & 5 deletions app/src/Commands/Pack/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

use Closure;
use RuntimeException;
use CaT\Doil\Lib\Git\Git;
use CaT\Doil\Lib\Posix\Posix;
use CaT\Doil\Lib\Docker\Docker;
use CaT\Doil\Lib\ProjectConfig;
use CaT\Doil\Commands\Repo\Repo;
use CaT\Doil\Lib\ConsoleOutput\Writer;
use CaT\Doil\Lib\FileSystem\Filesystem;
use CaT\Doil\Commands\Repo\RepoManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -28,15 +32,28 @@ class ExportCommand extends Command
protected Posix $posix;
protected Filesystem $filesystem;
protected Writer $writer;

public function __construct(Docker $docker, Posix $posix, Filesystem $filesystem, Writer $writer)
{
protected ProjectConfig $project_config;
protected Git $git;
protected RepoManager $repo_manager;

public function __construct(
Docker $docker,
Posix $posix,
Filesystem $filesystem,
Writer $writer,
ProjectConfig $project_config,
Git $git,
RepoManager $repo_manager
) {
parent::__construct();

$this->docker = $docker;
$this->posix = $posix;
$this->filesystem = $filesystem;
$this->writer = $writer;
$this->project_config = $project_config;
$this->git = $git;
$this->repo_manager = $repo_manager;
}

public function configure() : void
Expand Down Expand Up @@ -75,8 +92,6 @@ public function execute(InputInterface $input, OutputInterface $output) : int
return Command::FAILURE;
}

$this->writer->beginBlock($output, "Building zip file for " . $instance . "_" . $suffix);

if (! $this->docker->isInstanceUp($path)) {
$this->writer->beginBlock($output, "Starting instance $instance");
$this->docker->startContainerByDockerCompose($path);
Expand All @@ -85,6 +100,10 @@ public function execute(InputInterface $input, OutputInterface $output) : int
$this->writer->endBlock();
}

$this->writer->beginBlock($output, "Update project config for " . $instance . "_" . $suffix);
$this->updateProjectConfig($path);
$this->writer->beginBlock($output, "Building zip file for " . $instance . "_" . $suffix);

$this->writer->beginBlock($output, "Exporting database");

if ($input->getOption("cron")) {
Expand Down Expand Up @@ -186,4 +205,54 @@ public function hasDockerComposeFile(string $path, OutputInterface $output) : bo

return false;
}

protected function updateProjectConfig(string $path) : void
{
$project_config = $this->filesystem->readFromJsonFile($path . "/conf/project_config.json");
$project_config = array_shift($project_config);

$branch = $this->git->getCurrentBranch($path . "/volumes/ilias");

$cmd = "php -v | head -n 1 | cut -d ' ' -f2 | cut -d . -f1,2";
$php_version = trim($this->docker->executeDockerCommandWithReturn("cate79_local", $cmd));

$repos = $this->git->getRemotes($path . "/volumes/ilias");

$repo_name = array_key_first($repos);
$repo_url = $repos[$repo_name];

if (count($repos) > 1) {
$repos = array_filter($repos, function ($url) use ($branch, $path) {
return $this->git->isBranchInRepo($path . "/volumes/ilias", $url, $branch);
});
foreach ($repos as $name => $url) {
$repo = new Repo($name, $url);

if ($this->repo_manager->repoUrlExists($repo)) {
$repo_name = $name;
$repo_url = $url;
break;
}

$repo = $repo->withIsGlobal(true);
if ($this->repo_manager->repoUrlExists($repo)) {
$repo_name = $name;
$repo_url = $url;
break;
}

$repo_name = $name;
$repo_url = $url;
}
}

$project_config = $project_config
->withRepositoryBranch($branch)
->withPhpVersion($php_version)
->withRepositoryName($repo_name)
->withRepositoryUrl($repo_url)
;
$this->filesystem->saveToJsonFile($path . "/conf/project_config.json", [$project_config]);
$this->writer->endBlock();
}
}
7 changes: 4 additions & 3 deletions app/src/Commands/Pack/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function execute(InputInterface $input, OutputInterface $output) : int
}

$this->repo_manager->addRepo($repo);
$repo_added = true;
}

$create_command = $this->getApplication()->find('instances:create');
Expand Down Expand Up @@ -245,9 +246,6 @@ public function execute(InputInterface $input, OutputInterface $output) : int
$this->writer->endBlock();

$this->writer->beginBlock($output, "Setting permissions");
$this->docker->stopContainerByDockerCompose($path);
$this->docker->startContainerByDockerCompose($path);
sleep(15);
$this->docker->applyState($instance . "." . $suffix, "access");
$this->writer->endBlock();

Expand All @@ -264,6 +262,9 @@ public function execute(InputInterface $input, OutputInterface $output) : int
"-c",
"rm /var/ilias/data/ilias.sql"
);
if ($repo_added) {
$this->repo_manager->deleteRepo($repo);
}
$this->docker->stopContainerByDockerCompose($path);
$this->writer->endBlock();

Expand Down
1 change: 1 addition & 0 deletions app/src/Lib/Docker/Docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function runContainer(string $name) : void;
public function stop(string $name) : void;
public function removeContainer(string $name) : void;
public function executeDockerCommand(string $name, string $cmd) : void;
public function executeDockerCommandWithReturn(string $name, string $cmd) : string;
public function setGrain(string $name, string $key, string $value) : void;
public function deleteInstances(array $instances) : void;
public function pruneNetworks() : void;
Expand Down
17 changes: 17 additions & 0 deletions app/src/Lib/Docker/DockerShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,23 @@ public function executeDockerCommand(string $name, string $cmd) : void
$this->run($cmd, $logger);
}

public function executeDockerCommandWithReturn(string $name, string $cmd) : string
{
$cmd = [
"docker",
"exec",
"-i",
$name,
"bash",
"-c",
$cmd
];

$logger = $this->logger->getDoilLogger($name);
$logger->info("Execute docker command", $cmd);
return $this->run($cmd, $logger);
}

public function setGrain(string $name, string $key, string $value) : void
{
$cmd = [
Expand Down
3 changes: 3 additions & 0 deletions app/src/Lib/Git/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
interface Git
{
public function getBranches(string $path) : array;
public function getCurrentBranch(string $path) : string;
public function fetchBare(string $path) : void;
public function cloneBare(string $url, string $path) : void;
public function setLocalConfig(string $path, ...$commands) : void;
public function checkoutRemote(string $path, string $branch) : void;
public function getRemotes(string $path) : array;
public function isBranchInRepo(string $path, string $url, string $branch) : bool;
}
55 changes: 55 additions & 0 deletions app/src/Lib/Git/GitShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public function getBranches(string $path) : array
return array_filter(explode("\n", $this->run($cmd, $this->logger)));
}

public function getCurrentBranch(string $path) : string
{
$cmd = [
"git",
"-C",
$path,
"rev-parse",
"--abbrev-ref",
"HEAD"
];
return trim($this->run($cmd, $this->logger));
}

public function fetchBare(string $path) : void
{
$cmd = [
Expand Down Expand Up @@ -90,4 +103,46 @@ public function checkoutRemote(string $path, string $branch) : void
$this->logger->info("Check out remote branch '$branch' for path '$path'");
$this->run($cmd, $this->logger);
}

public function getRemotes(string $path) : array
{
$cmd = [
"git",
"-C",
$path,
"remote",
"-v"
];

$this->logger->info("Get remote repos for $path");
$result = $this->run($cmd, $this->logger);
$result = explode("\n", $result);
array_pop($result);

foreach ($result as $r) {
$r = preg_split('/\s+/', $r);
$arr[$r[0]] = $r[1];
}

return $arr;
}

public function isBranchInRepo(string $path, string $url, string $branch) : bool
{
$cmd = [
"git",
"ls-remote",
"--heads",
$url,
"refs/heads/$branch"
];

$this->logger->info("Get remote repos for $path");
$result = $this->run($cmd, $this->logger);

if ($result == "") {
return false;
}
return true;
}
}
5 changes: 4 additions & 1 deletion app/src/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ function buildContainerForApp() : Container
$c["docker.shell"],
$c["posix.shell"],
$c["filesystem.shell"],
$c["command.writer"]
$c["command.writer"],
$c["project.config"],
$c["git.shell"],
$c["repo.manager"]
);
};

Expand Down
23 changes: 19 additions & 4 deletions app/tests/Commands/Pack/ExportCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace CaT\Doil\Commands\Pack;

use RuntimeException;
use CaT\Doil\Lib\Git\Git;
use CaT\Doil\Lib\Posix\Posix;
use PHPUnit\Framework\TestCase;
use CaT\Doil\Lib\Docker\Docker;
use CaT\Doil\Lib\ProjectConfig;
use CaT\Doil\Lib\FileSystem\Filesystem;
use CaT\Doil\Commands\Repo\RepoManager;
use CaT\Doil\Lib\ConsoleOutput\CommandWriter;
use Symfony\Component\Console\Tester\CommandTester;

Expand All @@ -17,9 +20,12 @@ public function test_execute_without_instance_param() : void
$docker = $this->createMock(Docker::class);
$posix = $this->createMock(Posix::class);
$filesystem = $this->createMock(Filesystem::class);
$project_config = $this->createMock(ProjectConfig::class);
$git = $this->createMock(Git::class);
$repo_manager = $this->createMock(RepoManager::class);
$writer = new CommandWriter();

$command = new ExportCommand($docker, $posix, $filesystem, $writer);
$command = new ExportCommand($docker, $posix, $filesystem, $writer, $project_config, $git, $repo_manager);
$tester = new CommandTester($command);

$this->expectException(RuntimeException::class);
Expand All @@ -32,9 +38,12 @@ public function test_execute_with_empty_instance_param() : void
$docker = $this->createMock(Docker::class);
$posix = $this->createMock(Posix::class);
$filesystem = $this->createMock(Filesystem::class);
$project_config = $this->createMock(ProjectConfig::class);
$git = $this->createMock(Git::class);
$repo_manager = $this->createMock(RepoManager::class);
$writer = new CommandWriter();

$command = new ExportCommand($docker, $posix, $filesystem, $writer);
$command = new ExportCommand($docker, $posix, $filesystem, $writer, $project_config, $git, $repo_manager);
$tester = new CommandTester($command);

$this->expectException(RuntimeException::class);
Expand All @@ -47,9 +56,12 @@ public function test_execute_with_wrong_chars_in_instance_param() : void
$docker = $this->createMock(Docker::class);
$posix = $this->createMock(Posix::class);
$filesystem = $this->createMock(Filesystem::class);
$project_config = $this->createMock(ProjectConfig::class);
$git = $this->createMock(Git::class);
$repo_manager = $this->createMock(RepoManager::class);
$writer = new CommandWriter();

$command = new ExportCommand($docker, $posix, $filesystem, $writer);
$command = new ExportCommand($docker, $posix, $filesystem, $writer, $project_config, $git, $repo_manager);
$tester = new CommandTester($command);

$this->expectException(RuntimeException::class);
Expand All @@ -62,9 +74,12 @@ public function test_execute_with_no_docker_compose_file() : void
$docker = $this->createMock(Docker::class);
$posix = $this->createMock(Posix::class);
$filesystem = $this->createMock(Filesystem::class);
$project_config = $this->createMock(ProjectConfig::class);
$git = $this->createMock(Git::class);
$repo_manager = $this->createMock(RepoManager::class);
$writer = new CommandWriter();

$command = new ExportCommand($docker, $posix, $filesystem, $writer);
$command = new ExportCommand($docker, $posix, $filesystem, $writer, $project_config, $git, $repo_manager);
$tester = new CommandTester($command);

$posix
Expand Down

0 comments on commit d3fd730

Please sign in to comment.