Skip to content

Commit

Permalink
fmui export: 392 enable doil to start export by cron
Browse files Browse the repository at this point in the history
  • Loading branch information
daniwe4 committed Oct 24, 2023
1 parent d7153bd commit c3da421
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 28 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ build with doil (instances from version >=1.1) are able to be exported.

See `doil pack:<command> --help` for more information

To start `doil:pack export` via cronjob you need two additional parameters.
The first parameter (-T or --no-term) is attached to the **doil** commando.
It ensures that docker can run without a terminal.
The second parameter (-c or --cron) is required for the `pack:export` command.
It ensures that the console command does not open an additional terminal.
The complete call then looks like this:

```bash
doil -T pack:export -c <instance_name> [-g]
```

### Quietmode and Logs

Most of the commands come with a `--quiet` flag to omit the log messages.
Expand Down
65 changes: 53 additions & 12 deletions app/src/Commands/Instances/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,21 @@ public function execute(InputInterface $input, OutputInterface $output) : int
$group_id = (string) $this->posix->getGroupId();
$this->docker->executeDockerCommand($instance_name, "usermod -u $usr_id www-data");
$this->docker->executeDockerCommand($instance_name, "groupmod -g $group_id www-data");
$this->docker->executeDockerCommand($instance_name, "/etc/init.d/mariadb start");
sleep(5);
$this->docker->executeDockerCommand($instance_name, "/etc/init.d/mariadb stop");

$this->docker->copy($instance_name, "/var/log/apache2/", $instance_path . "/volumes/logs/");
$this->docker->copy($instance_name, "/etc/mysql/", $instance_path . "/volumes/etc/");
$this->docker->copy($instance_name, "/var/lib/mysql/", $instance_path . "/volumes/");

$this->docker->commit($instance_name);
$this->docker->stop($instance_name);
$this->docker->removeContainer($instance_name);
sleep(5);
$this->docker->startContainerByDockerCompose($instance_path);
$this->docker->executeCommand($instance_path, $options["name"], "/bin/bash", "-c", "chown -R mysql:mysql var/lib/mysql &>/dev/null");
$this->docker->executeCommand($instance_path, $options["name"], "/bin/bash", "-c", "/etc/init.d/mariadb start &>/dev/null");
sleep(5);
$this->writer->endBlock();

Expand Down Expand Up @@ -375,6 +382,11 @@ public function execute(InputInterface $input, OutputInterface $output) : int
$this->docker->applyState($instance_salt_name, "compile-skins");
$this->writer->endBlock();

// apply access state
$this->writer->beginBlock($output, "Apply access state");
$this->docker->applyState($instance_salt_name, "access");
$this->writer->endBlock();

// finalizing docker image
$this->writer->beginBlock($output, "Finalizing docker image");
$this->docker->commit($instance_name);
Expand All @@ -394,21 +406,50 @@ public function execute(InputInterface $input, OutputInterface $output) : int
}

// set folder permissions
$permission_dirs = [
"conf",
"volumes/data",
"volumes/db",
"volumes/ilias",
"volumes/etc/apache2",
"volumes/etc/php",
"volumes/index",
"volumes/logs"
];

$permission_files = [
"docker-compose.yml",
"README.md",
];

$this->writer->beginBlock($output, "Set folder permissions");
if ($options["global"]) {
$this->filesystem->chownRecursive(
$instance_path,
$user_name,
"doil"
);
$this->filesystem->chmod($instance_path, 0775, true);
$this->filesystem->chmodDirsOnly($instance_path, 02775);
foreach ($permission_dirs as $permission_dir) {
$this->filesystem->chownRecursive(
$instance_path . "/" . $permission_dir,
$user_name,
"doil"
);
$this->filesystem->chmodDirsOnly($instance_path . "/" . $permission_dir, 02775);
}
foreach ($permission_files as $permission_file) {
$this->filesystem->chmod($instance_path . "/" . $permission_file, 0775, true);
}
} else {
$this->filesystem->chownRecursive(
$instance_path,
$user_name,
$user_name
);
foreach ($permission_dirs as $permission_dir) {
$this->filesystem->chownRecursive(
$instance_path . "/" . $permission_dir,
$user_name,
$user_name
);
}
foreach ($permission_files as $permission_file) {
$this->filesystem->chownRecursive(
$instance_path . "/" . $permission_file,
$user_name,
$user_name
);
}
}
$this->writer->endBlock();

Expand Down
26 changes: 19 additions & 7 deletions app/src/Commands/Pack/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function configure() : void
$this
->addArgument("instance", InputArgument::REQUIRED, "Name of the instance to export")
->addOption("global", "g", InputOption::VALUE_NONE, "Determines if the instance is global")
->addOption("cron", "c", InputOption::VALUE_NONE, "Must be used for triggering export by cron")
;
}

Expand Down Expand Up @@ -86,13 +87,24 @@ public function execute(InputInterface $input, OutputInterface $output) : int

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

$this->docker->executeCommand(
$path,
$instance,
"bash",
"-c",
"mysqldump ilias > /var/ilias/data/ilias.sql"
);
if ($input->getOption("cron")) {
$this->docker->executeNoTTYCommand(
$path,
$instance,
"bash",
"-c",
"mysqldump ilias > /var/ilias/data/ilias.sql"
);
} else {
$this->docker->executeCommand(
$path,
$instance,
"bash",
"-c",
"mysqldump ilias > /var/ilias/data/ilias.sql"
);
}

$this->writer->endBlock();

$this->writer->beginBlock($output, "Exporting data");
Expand Down
2 changes: 1 addition & 1 deletion app/src/Commands/Pack/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function execute(InputInterface $input, OutputInterface $output) : int
$this->writer->beginBlock($output, "Importing database");
if (! $this->docker->isInstanceUp($path)) {
$this->docker->startContainerByDockerCompose($path);
sleep(15);
sleep(30);
}

if ($this->filesystem->exists($path . "/README.md")) {
Expand Down
19 changes: 19 additions & 0 deletions app/src/Lib/Docker/DockerShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,25 @@ public function executeCommand(string $path, string $name, ...$command) : void
$this->runTTY($cmd, $logger);
}

public function executeNoTTYCommand(string $path, string $name, ...$command) : void
{
$cmd = [
"docker-compose",
"-f",
$path . "/docker-compose.yml",
"exec",
"-T",
$name
];

$cmd = array_merge($cmd, $command);

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

$this->runNoTTYNoReturn($cmd, $logger);
}

public function executeBashCommandInsideContainer(string $name, ?string $working_dir, string ...$command) : void
{
$wd = "/";
Expand Down
13 changes: 13 additions & 0 deletions app/src/Lib/SymfonyShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,17 @@ protected function runTTY(array $commands, LoggerInterface $logger) : void
throw new ProcessFailedException($process);
}
}

protected function runNoTTYNoReturn(array $commands, LoggerInterface $logger) : void
{
$process = new Process($commands);
$process->setTimeout(36000);
$process->run();

// executes after the command finishes
if (! $process->isSuccessful() && ! $process->isTerminated()) {
$logger->error($process->getErrorOutput() ?: $process->getCommandLine());
throw new ProcessFailedException($process);
}
}
}
17 changes: 14 additions & 3 deletions setup/doil.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,23 @@ doil_get_conf() {
echo ${VALUE}
}

# If -T or --no-term is selected, the docker command runs without
# a terminal. Useful for starting doil by cron.
if [[ $1 == "-T" || $1 == "--no-term" ]]
then
TERMINAL="-i"
shift;
else
TERMINAL="-ti"
fi


MAP_USER=""
if [[ "$EUID" -ne 0 ]]
then
id -nG "$USER" | grep -qw "docker"
id -nG "$EUID" | grep -qw "docker"
DOCKER_GROUP=$?
id -nG "$USER" | grep -qw "doil"
id -nG "$EUID" | grep -qw "doil"
DOIL_GROUP=$?

if [ ${DOCKER_GROUP} = 1 ]
Expand Down Expand Up @@ -52,7 +63,7 @@ fi
DOCKER_GRP_ID=$(cat /etc/group | grep "^docker:" | cut -d : -f3)
DOIL_GRP_ID=$(cat /etc/group | grep "^doil:" | cut -d : -f3)

docker run --rm -ti \
docker run --rm "${TERMINAL}" \
-v /home:/home \
-v $(pwd):$(pwd) \
-v /var/run/docker.sock:/var/run/docker.sock \
Expand Down
6 changes: 1 addition & 5 deletions setup/stack/states/autoinstall/autoinstall/ilias-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@
"default_language" : "{{ language }}"
},
"http" : {
"path" : "{{ http_path }}",
"https_autodetection" : {
"header_name" : "X-Forwarded-Proto",
"header_value" : "https"
}
"path" : "{{ http_path }}"
},
"logging" : {
"enable" : true,
Expand Down
6 changes: 6 additions & 0 deletions setup/templates/minion/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ services:
hostname: %TPL_PROJECT_NAME%
domainname: %TPL_PROJECT_DOMAINNAME%
volumes:
- type: bind
source: ./volumes/mysql
target: /var/lib/mysql
- type: bind
source: ./volumes/etc/mysql
target: /etc/mysql
- type: bind
source: ./volumes/ilias
target: /var/www/html
Expand Down

0 comments on commit c3da421

Please sign in to comment.