From 82e4d1ca348748e8cdde86f65e62221968cabed5 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 18 Apr 2021 21:46:12 +0200 Subject: Slight refactoring --- src/Command/RunCommand.php | 17 +++++++------ src/Module/AptModule.php | 48 ----------------------------------- src/Module/ModuleInterface.php | 6 +++++ src/Module/Modules/AptModule.php | 54 ++++++++++++++++++++++++++++++++++++++++ src/Task.php | 22 ++++++++-------- 5 files changed, 81 insertions(+), 66 deletions(-) delete mode 100644 src/Module/AptModule.php create mode 100644 src/Module/Modules/AptModule.php (limited to 'src') diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index 048e94c..145b2d5 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -7,6 +7,7 @@ use phpseclib3\Crypt\PublicKeyLoader; use phpseclib3\Net\SSH2; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class RunCommand extends Command @@ -14,12 +15,13 @@ class RunCommand extends Command protected static $defaultName = 'run'; protected function configure() - {} + { + $this->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Path to config file', getcwd() . '/config.php'); + } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { - $cwd = getcwd(); - $config = include $cwd . '/config.php'; + $config = include $input->getOption('config'); global $ssh; $ssh = new SSH2($config['host']); @@ -28,17 +30,18 @@ class RunCommand extends Command } $commands = []; - foreach ($config['tasks'] as $task) { + foreach ($config['tasks'] as $taskName => $task) { /**@var Task $task*/ if (! $task->module->checkState()) { - $output->writeln('Adding commands from ' . get_class($task->module)); + $output->writeln($taskName . ': Adding commands from ' . get_class($task->module)); array_push($commands, ...$task->module->getCommands()); } else { - $output->writeln('Skipping commands from ' . get_class($task->module)); + $output->writeln($taskName . ': Skipping commands from ' . get_class($task->module)); } } + // run commands in single exec call $ssh->exec(implode(PHP_EOL, $commands)); return Command::SUCCESS; diff --git a/src/Module/AptModule.php b/src/Module/AptModule.php deleted file mode 100644 index ff4cb95..0000000 --- a/src/Module/AptModule.php +++ /dev/null @@ -1,48 +0,0 @@ -enablePTY(); - $ssh->exec("which $this->package"); - - if ($this->state === State::PRESENT) { - $state = ! empty($ssh->read()); - } - else if ($this->state === State::ABSENT) { - $state = empty($ssh->read()); - } - - $ssh->disablePTY(); - - return $state; - } - - public function getCommands(): array - { - if ($this->state === State::PRESENT) { - return [ - "sudo apt install -y $this->package", - ]; - } - else if ($this->state === State::ABSENT) { - return [ - "sudo apt remove -y $this->package", - ]; - } - - return []; - } -} diff --git a/src/Module/ModuleInterface.php b/src/Module/ModuleInterface.php index 2963b5f..649a095 100644 --- a/src/Module/ModuleInterface.php +++ b/src/Module/ModuleInterface.php @@ -4,7 +4,13 @@ namespace PHPIAC\Module; interface ModuleInterface { + /** + * @return bool + */ public function checkState(): bool; + /** + * @return array + */ public function getCommands(): array; } diff --git a/src/Module/Modules/AptModule.php b/src/Module/Modules/AptModule.php new file mode 100644 index 0000000..ccf4892 --- /dev/null +++ b/src/Module/Modules/AptModule.php @@ -0,0 +1,54 @@ +enablePTY(); + + $ssh->exec("dpkg -l $this->package | grep 'ii'"); + $dpkg = $ssh->read(); + + $state = match ($this->state) { + State::PRESENT => str_starts_with($dpkg, 'ii'), + State::ABSENT => str_contains($dpkg, 'no packages found'), + }; + + $ssh->disablePTY(); + + return $state; + } + + /** + * @inheritDoc + */ + public function getCommands(): array + { + return match ($this->state) { + State::PRESENT => ["sudo apt install -y $this->package"], + State::ABSENT => ["sudo apt remove -y $this->package"], + }; + } +} diff --git a/src/Task.php b/src/Task.php index 598f55c..a5698ea 100644 --- a/src/Task.php +++ b/src/Task.php @@ -6,22 +6,22 @@ use PHPIAC\Module\ModuleInterface; class Task { - public function __construct( - public ModuleInterface $module, - public array $vars, - ) {} + public ModuleInterface $module; + /** + * Task constructor. + */ + public function __construct() {} + + /** + * @param ModuleInterface $module + * + * @return $this + */ public function setModule(ModuleInterface $module): self { $this->module = $module; return $this; } - - public function setVars(array $vars): self - { - $this->vars = $vars; - - return $this; - } } -- cgit v1.2.3