From c0ebe78089121c0ad23efb0af32c435bee543a3c Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 18 Apr 2021 15:09:51 +0200 Subject: Initial commit --- src/Command/RunCommand.php | 46 ++++++++++++++++++++++++++++++++++++++++ src/IAC.php | 25 ++++++++++++++++++++++ src/Module/AptModule.php | 48 ++++++++++++++++++++++++++++++++++++++++++ src/Module/ModuleInterface.php | 10 +++++++++ src/Module/State.php | 9 ++++++++ src/Task.php | 27 ++++++++++++++++++++++++ 6 files changed, 165 insertions(+) create mode 100644 src/Command/RunCommand.php create mode 100644 src/IAC.php create mode 100644 src/Module/AptModule.php create mode 100644 src/Module/ModuleInterface.php create mode 100644 src/Module/State.php create mode 100644 src/Task.php (limited to 'src') diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php new file mode 100644 index 0000000..048e94c --- /dev/null +++ b/src/Command/RunCommand.php @@ -0,0 +1,46 @@ +login($config['user'], PublicKeyLoader::load(file_get_contents($config['private_key_file'])))) { + throw new \Exception('Login failed'); + } + + $commands = []; + foreach ($config['tasks'] as $task) { + /**@var Task $task*/ + if (! $task->module->checkState()) { + $output->writeln('Adding commands from ' . get_class($task->module)); + array_push($commands, ...$task->module->getCommands()); + } + else { + $output->writeln('Skipping commands from ' . get_class($task->module)); + } + } + + $ssh->exec(implode(PHP_EOL, $commands)); + + return Command::SUCCESS; + } +} diff --git a/src/IAC.php b/src/IAC.php new file mode 100644 index 0000000..c4e476d --- /dev/null +++ b/src/IAC.php @@ -0,0 +1,25 @@ +add($defaultCommand); + $application->setDefaultCommand($defaultCommand->getName()); + + # ... other commands here + + $application->run(); + } +} diff --git a/src/Module/AptModule.php b/src/Module/AptModule.php new file mode 100644 index 0000000..ff4cb95 --- /dev/null +++ b/src/Module/AptModule.php @@ -0,0 +1,48 @@ +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 new file mode 100644 index 0000000..2963b5f --- /dev/null +++ b/src/Module/ModuleInterface.php @@ -0,0 +1,10 @@ +module = $module; + + return $this; + } + + public function setVars(array $vars): self + { + $this->vars = $vars; + + return $this; + } +} -- cgit v1.2.3