summaryrefslogtreecommitdiff
path: root/src/AptModule.php
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2021-04-19 11:00:50 +0200
committerDaniel Weipert <code@drogueronin.de>2021-04-19 11:00:50 +0200
commitd4d5ae3bb6566311b2d42cf888e463b62f6cf0dc (patch)
tree6e60223291492e8e1f52739de9b8bd7ec36fb91b /src/AptModule.php
Initial commit
Diffstat (limited to 'src/AptModule.php')
-rw-r--r--src/AptModule.php54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/AptModule.php b/src/AptModule.php
new file mode 100644
index 0000000..9b882e8
--- /dev/null
+++ b/src/AptModule.php
@@ -0,0 +1,54 @@
+<?php
+
+namespace PHPIAC\Modules;
+
+use PHPIAC\Module\Module;
+use PHPIAC\Module\State;
+use phpseclib3\Net\SSH2;
+
+class AptModule extends Module
+{
+ /**
+ * 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"],
+ };
+ }
+}