summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Command/RunCommand.php6
-rw-r--r--src/Module/Module.php6
-rw-r--r--src/Module/Modules/AptModule.php54
-rw-r--r--src/Module/Modules/UserModule.php70
-rw-r--r--src/Task.php29
5 files changed, 38 insertions, 127 deletions
diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php
index 145b2d5..6d8d2c9 100644
--- a/src/Command/RunCommand.php
+++ b/src/Command/RunCommand.php
@@ -30,14 +30,14 @@ class RunCommand extends Command
}
$commands = [];
- foreach ($config['tasks'] as $taskName => $task) {
+ foreach ($config['tasks'] as $task) {
/**@var Task $task*/
if (! $task->module->checkState()) {
- $output->writeln($taskName . ': Adding commands from ' . get_class($task->module));
+ $output->writeln($task->getName() . ': Adding commands from ' . get_class($task->module));
array_push($commands, ...$task->module->getCommands());
}
else {
- $output->writeln($taskName . ': Skipping commands from ' . get_class($task->module));
+ $output->writeln($task->getName() . ': Skipping commands from ' . get_class($task->module));
}
}
diff --git a/src/Module/Module.php b/src/Module/Module.php
new file mode 100644
index 0000000..51672f9
--- /dev/null
+++ b/src/Module/Module.php
@@ -0,0 +1,6 @@
+<?php
+
+namespace PHPIAC\Module;
+
+abstract class Module implements ModuleInterface
+{}
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",
- ],
- };
- }
-}
diff --git a/src/Task.php b/src/Task.php
index a5698ea..8bb68e8 100644
--- a/src/Task.php
+++ b/src/Task.php
@@ -6,6 +6,7 @@ use PHPIAC\Module\ModuleInterface;
class Task
{
+ public string $name = '';
public ModuleInterface $module;
/**
@@ -14,6 +15,34 @@ class Task
public function __construct() {}
/**
+ * @param string $name
+ *
+ * @return $this
+ */
+ public function setName(string $name): self
+ {
+ $this->name = $name;
+
+ return $this;
+ }
+
+ /**
+ * @return string
+ */
+ public function getName(): string
+ {
+ if (! empty($this->name)) {
+ return $this->name;
+ }
+
+ $moduleReflection = new \ReflectionClass($this->module);
+ $firstProperty = $moduleReflection->getProperties()[0];
+ $firstProperty->setAccessible(true);
+
+ return $moduleReflection->getShortName() . ' - ' . $firstProperty->getValue($this->module);
+ }
+
+ /**
* @param ModuleInterface $module
*
* @return $this