From 3983548e7c0f107fa7b7cc3c4c36aa009590b481 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Mon, 19 Apr 2021 11:02:48 +0200 Subject: Moves Modules to own package Removes phar Renames bin 'n' stuff --- .gitignore | 1 - bin/compile | 10 -- bin/iac | 4 - bin/phpiac | 6 ++ composer.json | 21 +++- composer.lock | 211 +++++++++++++++++++++++++++++++------- index.php | 5 - src/Command/RunCommand.php | 6 +- src/Module/Module.php | 6 ++ src/Module/Modules/AptModule.php | 54 ---------- src/Module/Modules/UserModule.php | 70 ------------- src/Task.php | 29 ++++++ 12 files changed, 236 insertions(+), 187 deletions(-) delete mode 100644 bin/compile delete mode 100644 bin/iac create mode 100755 bin/phpiac delete mode 100644 index.php create mode 100644 src/Module/Module.php delete mode 100644 src/Module/Modules/AptModule.php delete mode 100644 src/Module/Modules/UserModule.php diff --git a/.gitignore b/.gitignore index 9c7e18a..57872d0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ /vendor/ -iac.phar diff --git a/bin/compile b/bin/compile deleted file mode 100644 index a36967e..0000000 --- a/bin/compile +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env php -setStub(Phar::createDefaultStub()); -$phar->buildFromDirectory(dirname(__DIR__), '/(src|vendor|index\.php)/'); diff --git a/bin/iac b/bin/iac deleted file mode 100644 index b645010..0000000 --- a/bin/iac +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env php -=7.2.5", + "symfony/deprecation-contracts": "^2.1", "symfony/polyfill-mbstring": "~1.0", "symfony/polyfill-php73": "^1.8", "symfony/polyfill-php80": "^1.15", @@ -374,6 +436,7 @@ "symfony/lock": "", "symfony/process": "" }, + "default-branch": true, "type": "library", "autoload": { "psr-4": { @@ -406,7 +469,75 @@ "terminal" ], "support": { - "source": "https://github.com/symfony/console/tree/v5.2.6" + "source": "https://github.com/symfony/console/tree/v5.3.0-BETA1" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2021-04-16T17:36:28+00:00" + }, + { + "name": "symfony/deprecation-contracts", + "version": "dev-main", + "source": { + "type": "git", + "url": "https://github.com/symfony/deprecation-contracts.git", + "reference": "5f38c8804a9e97d23e0c8d63341088cd8a22d627" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/5f38c8804a9e97d23e0c8d63341088cd8a22d627", + "reference": "5f38c8804a9e97d23e0c8d63341088cd8a22d627", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "default-branch": true, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "2.4-dev" + }, + "thanks": { + "name": "symfony/contracts", + "url": "https://github.com/symfony/contracts" + } + }, + "autoload": { + "files": [ + "function.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "A generic function and convention to trigger deprecation notices", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/deprecation-contracts/tree/v2.4.0" }, "funding": [ { @@ -422,11 +553,11 @@ "type": "tidelift" } ], - "time": "2021-03-28T09:42:18+00:00" + "time": "2021-03-23T23:28:01+00:00" }, { "name": "symfony/polyfill-ctype", - "version": "v1.22.1", + "version": "dev-main", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", @@ -444,6 +575,7 @@ "suggest": { "ext-ctype": "For best performance" }, + "default-branch": true, "type": "library", "extra": { "branch-alias": { @@ -505,7 +637,7 @@ }, { "name": "symfony/polyfill-intl-grapheme", - "version": "v1.22.1", + "version": "dev-main", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-intl-grapheme.git", @@ -523,6 +655,7 @@ "suggest": { "ext-intl": "For best performance" }, + "default-branch": true, "type": "library", "extra": { "branch-alias": { @@ -586,7 +719,7 @@ }, { "name": "symfony/polyfill-intl-normalizer", - "version": "v1.22.1", + "version": "dev-main", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-intl-normalizer.git", @@ -604,6 +737,7 @@ "suggest": { "ext-intl": "For best performance" }, + "default-branch": true, "type": "library", "extra": { "branch-alias": { @@ -670,7 +804,7 @@ }, { "name": "symfony/polyfill-mbstring", - "version": "v1.22.1", + "version": "dev-main", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", @@ -688,6 +822,7 @@ "suggest": { "ext-mbstring": "For best performance" }, + "default-branch": true, "type": "library", "extra": { "branch-alias": { @@ -750,7 +885,7 @@ }, { "name": "symfony/polyfill-php73", - "version": "v1.22.1", + "version": "dev-main", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php73.git", @@ -765,6 +900,7 @@ "require": { "php": ">=7.1" }, + "default-branch": true, "type": "library", "extra": { "branch-alias": { @@ -829,7 +965,7 @@ }, { "name": "symfony/polyfill-php80", - "version": "v1.22.1", + "version": "dev-main", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php80.git", @@ -844,6 +980,7 @@ "require": { "php": ">=7.1" }, + "default-branch": true, "type": "library", "extra": { "branch-alias": { @@ -912,29 +1049,30 @@ }, { "name": "symfony/service-contracts", - "version": "v2.2.0", + "version": "dev-main", "source": { "type": "git", "url": "https://github.com/symfony/service-contracts.git", - "reference": "d15da7ba4957ffb8f1747218be9e1a121fd298a1" + "reference": "f040a30e04b57fbcc9c6cbcf4dbaa96bd318b9bb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/service-contracts/zipball/d15da7ba4957ffb8f1747218be9e1a121fd298a1", - "reference": "d15da7ba4957ffb8f1747218be9e1a121fd298a1", + "url": "https://api.github.com/repos/symfony/service-contracts/zipball/f040a30e04b57fbcc9c6cbcf4dbaa96bd318b9bb", + "reference": "f040a30e04b57fbcc9c6cbcf4dbaa96bd318b9bb", "shasum": "" }, "require": { "php": ">=7.2.5", - "psr/container": "^1.0" + "psr/container": "^1.1" }, "suggest": { "symfony/service-implementation": "" }, + "default-branch": true, "type": "library", "extra": { "branch-alias": { - "dev-master": "2.2-dev" + "dev-main": "2.4-dev" }, "thanks": { "name": "symfony/contracts", @@ -971,7 +1109,7 @@ "standards" ], "support": { - "source": "https://github.com/symfony/service-contracts/tree/master" + "source": "https://github.com/symfony/service-contracts/tree/v2.4.0" }, "funding": [ { @@ -987,20 +1125,20 @@ "type": "tidelift" } ], - "time": "2020-09-07T11:33:47+00:00" + "time": "2021-04-01T10:43:52+00:00" }, { "name": "symfony/string", - "version": "v5.2.6", + "version": "5.x-dev", "source": { "type": "git", "url": "https://github.com/symfony/string.git", - "reference": "ad0bd91bce2054103f5eaa18ebeba8d3bc2a0572" + "reference": "01454c66c88a6bb4449dcdeb913e463e075f331b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/string/zipball/ad0bd91bce2054103f5eaa18ebeba8d3bc2a0572", - "reference": "ad0bd91bce2054103f5eaa18ebeba8d3bc2a0572", + "url": "https://api.github.com/repos/symfony/string/zipball/01454c66c88a6bb4449dcdeb913e463e075f331b", + "reference": "01454c66c88a6bb4449dcdeb913e463e075f331b", "shasum": "" }, "require": { @@ -1017,6 +1155,7 @@ "symfony/translation-contracts": "^1.1|^2", "symfony/var-exporter": "^4.4|^5.0" }, + "default-branch": true, "type": "library", "autoload": { "psr-4": { @@ -1054,7 +1193,7 @@ "utf8" ], "support": { - "source": "https://github.com/symfony/string/tree/v5.2.6" + "source": "https://github.com/symfony/string/tree/v5.3.0-BETA1" }, "funding": [ { @@ -1070,12 +1209,12 @@ "type": "tidelift" } ], - "time": "2021-03-17T17:12:15+00:00" + "time": "2021-03-17T17:12:23+00:00" } ], "packages-dev": [], "aliases": [], - "minimum-stability": "stable", + "minimum-stability": "dev", "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, diff --git a/index.php b/index.php deleted file mode 100644 index 92972cf..0000000 --- a/index.php +++ /dev/null @@ -1,5 +0,0 @@ - $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 @@ +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 @@ -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; /** @@ -13,6 +14,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 * -- cgit v1.2.3