summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2021-04-28 13:58:55 +0200
committerDaniel Weipert <code@drogueronin.de>2021-04-28 13:58:55 +0200
commit20d9f63918d3b2bd75853182063f43eb36d7cd8a (patch)
treeb6f48898297f761dcec8532b524844cc968ec565 /src
parent11dc8c730dda2c5bd38cb386f96331c5ce3cac9c (diff)
Adjusts modulesv1.0.0
Diffstat (limited to 'src')
-rw-r--r--src/AptModule.php4
-rw-r--r--src/CopyModule.php14
-rw-r--r--src/FileModule.php4
-rw-r--r--src/GitModule.php18
-rw-r--r--src/Support/HandlesFiles.php27
-rw-r--r--src/TemplateModule.php16
-rw-r--r--src/UfwModule.php28
-rw-r--r--src/UserModule.php4
8 files changed, 78 insertions, 37 deletions
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 @@
+<?php
+
+namespace PHPIAC\Modules\Support;
+
+use PHPIAC\Connection;
+
+trait HandlesFiles
+{
+ /**
+ * @param string $path
+ *
+ * @return bool
+ */
+ public function fileExists(string $path): bool
+ {
+ Connection::enablePty();
+
+ Connection::exec("ls $path");
+ $ls = Connection::read();
+
+ $state = ! str_contains($ls, 'No such file or directory');
+
+ Connection::disablePty();
+
+ return $state;
+ }
+}
diff --git a/src/TemplateModule.php b/src/TemplateModule.php
index 515d891..9bbb6f9 100644
--- a/src/TemplateModule.php
+++ b/src/TemplateModule.php
@@ -4,30 +4,38 @@ namespace PHPIAC\Modules;
use PHPIAC\Connection;
use PHPIAC\Module\Module;
+use PHPIAC\Modules\Support\HandlesFiles;
use PHPIAC\Modules\Support\HasPermissions;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
class TemplateModule extends Module
{
- use HasPermissions;
+ use HasPermissions, HandlesFiles;
protected string $src;
protected string $dest;
protected array $vars;
+ protected bool $force = false;
+
/**
* @inheritDoc
*/
public function checkState(): bool
{
- return false;
+ if ($this->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 [];
}
}