-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
master states:442 distribute update token, and enable doil to trigger
instance updates via url
- Loading branch information
Showing
20 changed files
with
399 additions
and
7 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
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,155 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* Copyright (c) 2022 - Daniel Weise <[email protected]> - Extended GPL, see LICENSE */ | ||
|
||
namespace CaT\Doil\Commands\Instances; | ||
|
||
use CaT\Doil\Lib\Posix\Posix; | ||
use CaT\Doil\Lib\Docker\Docker; | ||
use CaT\Doil\Lib\ConsoleOutput\Writer; | ||
use CaT\Doil\Lib\FileSystem\Filesystem; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\ConfirmationQuestion; | ||
use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
|
||
class SetUpdateTokenCommand extends Command | ||
{ | ||
protected static $defaultName = "instances:set-update-token"; | ||
protected static $defaultDescription = | ||
"<fg=red>!NEEDS SUDO PRIVILEGES!</> This command sets a update token for all instances" | ||
; | ||
|
||
protected Docker $docker; | ||
protected Posix $posix; | ||
protected Filesystem $filesystem; | ||
protected Writer $writer; | ||
|
||
public function __construct(Docker $docker, Posix $posix, Filesystem $filesystem, Writer $writer) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->docker = $docker; | ||
$this->posix = $posix; | ||
$this->filesystem = $filesystem; | ||
$this->writer = $writer; | ||
} | ||
|
||
public function configure() : void | ||
{ | ||
$this | ||
->setAliases(["sut"]) | ||
->addOption("token", "t", InputOption::VALUE_REQUIRED, "Update token as string") | ||
->addOption("global", "g", InputOption::VALUE_NONE, "Determines if an instance is global or not") | ||
; | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output) : int | ||
{ | ||
if (! $this->posix->isSudo()) { | ||
$this->writer->error( | ||
$output, | ||
"Please execute this script as sudo user!" | ||
); | ||
return Command::FAILURE; | ||
} | ||
|
||
$token = $input->getOption("token"); | ||
|
||
$home_dir = $this->posix->getHomeDirectory($this->posix->getUserId()); | ||
|
||
$path = "/usr/local/share/doil/instances"; | ||
$suffix = "global"; | ||
if (! $input->getOption("global")) { | ||
$path = "$home_dir/.doil/instances"; | ||
$suffix = "local"; | ||
} | ||
|
||
$instances = $this->filesystem->getFilesInPath($path); | ||
if (count($instances) == 0) { | ||
$this->writer->error( | ||
$output, | ||
"No instances found!", | ||
"Use <fg=gray>doil instances:ls --help</> for more information." | ||
); | ||
return Command::FAILURE; | ||
} | ||
|
||
$question = new ConfirmationQuestion( | ||
"This will also update 'update_token' in your doil config. Want to continue? [yN]: ", | ||
false | ||
); | ||
|
||
$helper = $this->getHelper("question"); | ||
if (!$helper->ask($input, $output, $question)) { | ||
$output->writeln("Abort by user!"); | ||
return Command::FAILURE; | ||
} | ||
|
||
$this->filesystem->replaceLineInFile("/etc/doil/doil.conf", "/update_token=.*/", "update_token=" . $token); | ||
|
||
foreach ($instances as $i) { | ||
$started = $this->startInstance($output, $path, $i); | ||
sleep(3); | ||
$this->applyUpdateToken($output, $i . "." . $suffix, $token); | ||
$this->docker->commit($i . "_" . $suffix); | ||
if ($started) { | ||
$this->stopInstance($output, $path, $i); | ||
} | ||
} | ||
return Command::SUCCESS; | ||
} | ||
|
||
protected function startInstance(OutputInterface $output, string $path, string $instance) : bool | ||
{ | ||
if (! $this->hasDockerComposeFile($path . "/" . $instance, $output)) { | ||
throw new InvalidArgumentException("Can't find a suitable docker-compose.yml file in $path/$instance"); | ||
} | ||
|
||
if (! $this->docker->isInstanceUp($path . "/" . $instance)) { | ||
$this->writer->beginBlock($output, "Start instance $instance"); | ||
$this->docker->startContainerByDockerCompose($path . "/" . $instance); | ||
$this->writer->endBlock(); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected function stopInstance(OutputInterface $output, string $path, string $instance) : string | ||
{ | ||
if ($this->docker->isInstanceUp($path . "/" . $instance)) { | ||
$this->writer->beginBlock($output, "Stop instance $instance"); | ||
$this->docker->stopContainerByDockerCompose($path . "/" . $instance); | ||
$this->writer->endBlock(); | ||
} | ||
|
||
return $instance; | ||
} | ||
|
||
protected function hasDockerComposeFile(string $path, OutputInterface $output) : bool | ||
{ | ||
if ($this->filesystem->exists($path . "/docker-compose.yml")) { | ||
return true; | ||
} | ||
|
||
$output->writeln("<fg=red>Error:</>"); | ||
$output->writeln("\tCan't find a suitable docker-compose file in this directory '$path'."); | ||
$output->writeln("\tIs this the right directory?"); | ||
$output->writeln("\tSupported filenames: docker-compose.yml"); | ||
|
||
return false; | ||
} | ||
|
||
protected function applyUpdateToken(OutputInterface $output, string $salt_key, string $token) | ||
{ | ||
$this->writer->beginBlock($output, "Apply update token to $salt_key"); | ||
$this->docker->setGrain($salt_key, "update_token", $token); | ||
$this->docker->refreshGrains($salt_key); | ||
$this->docker->applyState($salt_key, "set-update-token"); | ||
$this->writer->endBlock(); | ||
} | ||
} |
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
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 @@ | ||
description = Inject an update hook file into docroot |
8 changes: 8 additions & 0 deletions
8
setup/stack/states/ilias-update-hook/ilias-update-hook/init.sls
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,8 @@ | ||
{% set ilias_version = salt['grains.get']('ilias_version', '8.0') %} | ||
|
||
/var/www/html/.update_hook.php: | ||
file.managed: | ||
- source: salt://ilias-update-hook/update_hook.php.j2 | ||
- template: jinja | ||
- context: | ||
ilias_version: {{ ilias_version }} |
Oops, something went wrong.