From 20d9f63918d3b2bd75853182063f43eb36d7cd8a Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 28 Apr 2021 13:58:55 +0200 Subject: Adjusts modules --- src/AptModule.php | 4 +--- src/CopyModule.php | 14 +++++++++----- src/FileModule.php | 4 +--- src/GitModule.php | 18 ++++-------------- src/Support/HandlesFiles.php | 27 +++++++++++++++++++++++++++ src/TemplateModule.php | 16 +++++++++++----- src/UfwModule.php | 28 ++++++++++++++++++++++++---- src/UserModule.php | 4 +--- 8 files changed, 78 insertions(+), 37 deletions(-) create mode 100644 src/Support/HandlesFiles.php diff --git a/src/AptModule.php b/src/AptModule.php index c133521..8820a13 100644 --- a/src/AptModule.php +++ b/src/AptModule.php @@ -36,7 +36,7 @@ class AptModule extends Module /** * @inheritDoc */ - public function getCommands(): array + public function execute(): void { if ($this->state === State::PRESENT) { Connection::exec("sudo apt install -y $this->package"); @@ -44,7 +44,5 @@ class AptModule extends Module else if ($this->state === State::ABSENT) { Connection::exec("sudo apt remove -y $this->package"); } - - return []; } } diff --git a/src/CopyModule.php b/src/CopyModule.php index 069f594..1f0b5c3 100644 --- a/src/CopyModule.php +++ b/src/CopyModule.php @@ -4,15 +4,17 @@ namespace PHPIAC\Modules; use PHPIAC\Connection; use PHPIAC\Module\Module; +use PHPIAC\Modules\Support\HandlesFiles; use PHPIAC\Modules\Support\HasPermissions; class CopyModule extends Module { - use HasPermissions; + use HasPermissions, HandlesFiles; protected string $src; protected string $dest; + protected bool $force = false; protected bool $remoteSrc = false; /** @@ -28,13 +30,17 @@ class CopyModule extends Module */ public function checkState(): bool { - return false; + if ($this->force) { + return false; + } + + return $this->fileExists($this->dest); } /** * @inheritDoc */ - public function getCommands(): array + public function execute(): void { if ($this->remoteSrc) { Connection::exec("sudo cp -r $this->src $this->dest"); @@ -44,7 +50,5 @@ class CopyModule extends Module } Connection::exec($this->getPermissions($this->dest)); - - return []; } } diff --git a/src/FileModule.php b/src/FileModule.php index 95ba771..31efa82 100644 --- a/src/FileModule.php +++ b/src/FileModule.php @@ -26,7 +26,7 @@ class FileModule extends Module /** * @inheritDoc */ - public function getCommands(): array + public function execute(): void { if ($this->state === State::PRESENT) { Connection::exec( @@ -37,7 +37,5 @@ class FileModule extends Module else { Connection::exec("sudo rm -rf $this->path"); } - - return []; } } diff --git a/src/GitModule.php b/src/GitModule.php index 3927992..39112a2 100644 --- a/src/GitModule.php +++ b/src/GitModule.php @@ -4,11 +4,12 @@ namespace PHPIAC\Modules; use PHPIAC\Connection; use PHPIAC\Module\Module; +use PHPIAC\Modules\Support\HandlesFiles; use PHPIAC\Modules\Support\HasPermissions; class GitModule extends Module { - use HasPermissions; + use HasPermissions, HandlesFiles; protected string $repo; protected string $dest; @@ -18,28 +19,17 @@ class GitModule extends Module */ public function checkState(): bool { - Connection::enablePty(); - - Connection::exec("ls $this->dest"); - $ls = Connection::read(); - - $state = ! str_contains($ls, 'No such file or directory'); - - Connection::disablePty(); - - return $state; + return $this->fileExists($this->dest); } /** * @inheritDoc */ - public function getCommands(): array + public function execute(): void { Connection::exec( "sudo git clone $this->repo $this->dest" . PHP_EOL . $this->getPermissions($this->dest) ); - - return []; } } diff --git a/src/Support/HandlesFiles.php b/src/Support/HandlesFiles.php new file mode 100644 index 0000000..8db6975 --- /dev/null +++ b/src/Support/HandlesFiles.php @@ -0,0 +1,27 @@ +force) { + return false; + // TODO: ContentsEqual?(); + } + + return $this->fileExists($this->dest); } /** * @inheritDoc */ - public function getCommands(): array + public function execute(): void { $loader = new FilesystemLoader(dirname($this->src)); $twig = new Environment($loader); @@ -36,7 +44,5 @@ class TemplateModule extends Module Connection::put($this->dest, $rendered); Connection::exec($this->getPermissions($this->dest)); - - return []; } } diff --git a/src/UfwModule.php b/src/UfwModule.php index 1ec4452..02be515 100644 --- a/src/UfwModule.php +++ b/src/UfwModule.php @@ -18,13 +18,35 @@ class UfwModule extends Module */ public function checkState(): bool { - return false; + $state = false; + + Connection::enablePty(); + + Connection::exec('sudo ufw status'); + $status = Connection::read(); + + if ($this->state === State::ENABLED) { + $rule = strtoupper($this->rule); + Connection::exec("sudo ufw status | grep '$this->name\|$rule'"); + $statusGrep = Connection::read(); + + $state = + str_contains($status, 'Status: active') && + str_contains($statusGrep, $this->name) && str_contains($statusGrep, $rule); + } + else if ($this->state === State::DISABLED) { + $state = str_contains($status, 'Status: inactive'); + } + + Connection::disablePty(); + + return $state; } /** * @inheritDoc */ - public function getCommands(): array + public function execute(): void { Connection::exec(implode(PHP_EOL, [ "sudo ufw $this->rule $this->name", @@ -33,7 +55,5 @@ class UfwModule extends Module State::DISABLED => "sudo ufw disable", }, ])); - - return []; } } diff --git a/src/UserModule.php b/src/UserModule.php index c230976..7347fed 100644 --- a/src/UserModule.php +++ b/src/UserModule.php @@ -40,7 +40,7 @@ class UserModule extends Module /** * @inheritDoc */ - public function getCommands(): array + public function execute(): void { if ($this->state === State::PRESENT) { Connection::exec(implode(PHP_EOL, [ @@ -53,7 +53,5 @@ class UserModule extends Module else if ($this->state === State::ABSENT) { Connection::exec("sudo userdel $this->username"); } - - return []; } } -- cgit v1.2.3