diff options
author | Daniel Weipert <code@drogueronin.de> | 2021-04-19 11:02:48 +0200 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2021-04-19 11:02:48 +0200 |
commit | 3983548e7c0f107fa7b7cc3c4c36aa009590b481 (patch) | |
tree | 071513d1c25459cb81dbf56e5acc5942424a732e /src/Module/Modules | |
parent | 671e3f1540f3a683d548dafd64c068ad6a64ac2f (diff) |
Moves Modules to own package
Removes phar
Renames bin
'n' stuff
Diffstat (limited to 'src/Module/Modules')
-rw-r--r-- | src/Module/Modules/AptModule.php | 54 | ||||
-rw-r--r-- | src/Module/Modules/UserModule.php | 70 |
2 files changed, 0 insertions, 124 deletions
diff --git a/src/Module/Modules/AptModule.php b/src/Module/Modules/AptModule.php deleted file mode 100644 index ccf4892..0000000 --- a/src/Module/Modules/AptModule.php +++ /dev/null @@ -1,54 +0,0 @@ -<?php - -namespace PHPIAC\Module\Modules; - -use PHPIAC\Module\ModuleInterface; -use PHPIAC\Module\State; -use phpseclib3\Net\SSH2; - -class AptModule implements ModuleInterface -{ - /** - * AptModule constructor. - * - * @param string $package - * @param string $state - */ - public function __construct( - private string $package, - private string $state = State::PRESENT - ) {} - - /** - * @inheritDoc - */ - public function checkState(): bool - { - global $ssh; - /**@var SSH2 $ssh*/ - $ssh->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/Module/Modules/UserModule.php b/src/Module/Modules/UserModule.php deleted file mode 100644 index 1749b58..0000000 --- a/src/Module/Modules/UserModule.php +++ /dev/null @@ -1,70 +0,0 @@ -<?php - -namespace PHPIAC\Module\Modules; - -use PHPIAC\Module\ModuleInterface; -use PHPIAC\Module\State; -use phpseclib3\Net\SSH2; - -class UserModule implements ModuleInterface -{ - /** - * UserModule constructor. - * - * @param string $name - * @param array $options - * @param string $state - */ - public function __construct( - private string $name, - private array $options = [], - private string $state = State::PRESENT - ) { - $this->options = array_replace([ - 'append' => false, - 'create_home' => true, - 'groups' => [], - 'shell' => '/bin/bash', - ], $options); - } - - /** - * @inheritDoc - */ - public function checkState(): bool - { - global $ssh; - /**@var SSH2 $ssh*/ - $ssh->enablePTY(); - - $ssh->exec("cat /etc/passwd | grep $this->name:"); - $hasUser = $ssh->read(); - - $state = match ($this->state) { - State::PRESENT => str_starts_with($hasUser, "$this->name:"), - State::ABSENT => empty($hasUser), - }; - - $ssh->disablePTY(); - - return $state; - } - - /** - * @inheritDoc - */ - public function getCommands(): array - { - return match ($this->state) { - State::PRESENT => [ - "sudo adduser $this->name --quiet" . - " --shell " . $this->options['shell'] . - ($this->options['create_home'] ? '' : ' --no-create-home'), - "sudo usermod -" . ($this->options['append'] ? 'a' : '') . "G " . implode(',', $this->options['groups']) . " $this->name" - ], - State::ABSENT => [ - "sudo apt remove -y $this->package", - ], - }; - } -} |