diff options
author | Daniel Weipert <code@drogueronin.de> | 2021-04-18 15:09:51 +0200 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2021-04-18 15:09:51 +0200 |
commit | c0ebe78089121c0ad23efb0af32c435bee543a3c (patch) | |
tree | db5e9205bcd7181cfa41543ee8962e9d84cdb481 /src/Module |
Initial commit
Diffstat (limited to 'src/Module')
-rw-r--r-- | src/Module/AptModule.php | 48 | ||||
-rw-r--r-- | src/Module/ModuleInterface.php | 10 | ||||
-rw-r--r-- | src/Module/State.php | 9 |
3 files changed, 67 insertions, 0 deletions
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 @@ +<?php + +namespace PHPIAC\Module; + +use phpseclib3\Net\SSH2; + +class AptModule implements ModuleInterface +{ + public function __construct( + private string $package, + private string $state = State::PRESENT + ) {} + + public function checkState(): bool + { + global $ssh; + /**@var SSH2 $ssh*/ + $ssh->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 @@ +<?php + +namespace PHPIAC\Module; + +interface ModuleInterface +{ + public function checkState(): bool; + + public function getCommands(): array; +} diff --git a/src/Module/State.php b/src/Module/State.php new file mode 100644 index 0000000..c7f7af9 --- /dev/null +++ b/src/Module/State.php @@ -0,0 +1,9 @@ +<?php + +namespace PHPIAC\Module; + +class State +{ + public const PRESENT = 'present'; + public const ABSENT = 'absent'; +} |