diff options
| author | Daniel Weipert <code@drogueronin.de> | 2021-04-20 12:44:20 +0200 | 
|---|---|---|
| committer | Daniel Weipert <code@drogueronin.de> | 2021-04-20 12:44:20 +0200 | 
| commit | 11dc8c730dda2c5bd38cb386f96331c5ce3cac9c (patch) | |
| tree | b2c03112114af00e33ce8e1276194906064fcf93 /src | |
| parent | d4d5ae3bb6566311b2d42cf888e463b62f6cf0dc (diff) | |
Adds a bunch of new Modules
Diffstat (limited to 'src')
| -rw-r--r-- | src/AptModule.php | 38 | ||||
| -rw-r--r-- | src/CopyModule.php | 50 | ||||
| -rw-r--r-- | src/FileModule.php | 43 | ||||
| -rw-r--r-- | src/GitModule.php | 45 | ||||
| -rw-r--r-- | src/Support/HasPermissions.php | 15 | ||||
| -rw-r--r-- | src/Support/Permissions.php | 28 | ||||
| -rw-r--r-- | src/TemplateModule.php | 42 | ||||
| -rw-r--r-- | src/UfwModule.php | 39 | ||||
| -rw-r--r-- | src/UserModule.php | 61 | 
9 files changed, 304 insertions, 57 deletions
diff --git a/src/AptModule.php b/src/AptModule.php index 9b882e8..c133521 100644 --- a/src/AptModule.php +++ b/src/AptModule.php @@ -2,41 +2,33 @@  namespace PHPIAC\Modules; +use PHPIAC\Connection;  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 -    ) {} +    protected string $package; + +    protected string $state = State::PRESENT; +    protected bool $updateCache = false;      /**       * @inheritDoc       */      public function checkState(): bool      { -        global $ssh; -        /**@var SSH2 $ssh*/ -        $ssh->enablePTY(); +        Connection::enablePty(); -        $ssh->exec("dpkg -l $this->package | grep 'ii'"); -        $dpkg = $ssh->read(); +        Connection::exec("dpkg -l $this->package | grep 'ii'"); +        $dpkg = Connection::read();          $state = match ($this->state) {              State::PRESENT => str_starts_with($dpkg, 'ii'),              State::ABSENT => str_contains($dpkg, 'no packages found'),          }; -        $ssh->disablePTY(); +        Connection::disablePty();          return $state;      } @@ -46,9 +38,13 @@ class AptModule extends Module       */      public function getCommands(): array      { -        return match ($this->state) { -            State::PRESENT => ["sudo apt install -y $this->package"], -            State::ABSENT => ["sudo apt remove -y $this->package"], -        }; +        if ($this->state === State::PRESENT) { +            Connection::exec("sudo apt install -y $this->package"); +        } +        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 new file mode 100644 index 0000000..069f594 --- /dev/null +++ b/src/CopyModule.php @@ -0,0 +1,50 @@ +<?php + +namespace PHPIAC\Modules; + +use PHPIAC\Connection; +use PHPIAC\Module\Module; +use PHPIAC\Modules\Support\HasPermissions; + +class CopyModule extends Module +{ +    use HasPermissions; + +    protected string $src; +    protected string $dest; + +    protected bool $remoteSrc = false; + +    /** +     * @inheritDoc +     */ +    public function __construct(array $config) +    { +        parent::__construct($config); +    } + +    /** +     * @inheritDoc +     */ +    public function checkState(): bool +    { +        return false; +    } + +    /** +     * @inheritDoc +     */ +    public function getCommands(): array +    { +        if ($this->remoteSrc) { +            Connection::exec("sudo cp -r $this->src $this->dest"); +        } +        else { +            Connection::put($this->dest, $this->src); +        } + +        Connection::exec($this->getPermissions($this->dest)); + +        return []; +    } +} diff --git a/src/FileModule.php b/src/FileModule.php new file mode 100644 index 0000000..95ba771 --- /dev/null +++ b/src/FileModule.php @@ -0,0 +1,43 @@ +<?php + +namespace PHPIAC\Modules; + +use PHPIAC\Connection; +use PHPIAC\Module\Module; +use PHPIAC\Module\State; +use PHPIAC\Modules\Support\HasPermissions; + +class FileModule extends Module +{ +    use HasPermissions; + +    protected string $path; + +    protected string $state = State::PRESENT; + +    /** +     * @inheritDoc +     */ +    public function checkState(): bool +    { +        return false; +    } + +    /** +     * @inheritDoc +     */ +    public function getCommands(): array +    { +        if ($this->state === State::PRESENT) { +            Connection::exec( +                "sudo touch $this->path" . PHP_EOL . +                $this->getPermissions($this->path) +            ); +        } +        else { +            Connection::exec("sudo rm -rf $this->path"); +        } + +        return []; +    } +} diff --git a/src/GitModule.php b/src/GitModule.php new file mode 100644 index 0000000..3927992 --- /dev/null +++ b/src/GitModule.php @@ -0,0 +1,45 @@ +<?php + +namespace PHPIAC\Modules; + +use PHPIAC\Connection; +use PHPIAC\Module\Module; +use PHPIAC\Modules\Support\HasPermissions; + +class GitModule extends Module +{ +    use HasPermissions; + +    protected string $repo; +    protected string $dest; + +    /** +     * @inheritDoc +     */ +    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; +    } + +    /** +     * @inheritDoc +     */ +    public function getCommands(): array +    { +        Connection::exec( +            "sudo git clone $this->repo $this->dest" . PHP_EOL . +            $this->getPermissions($this->dest) +        ); + +        return []; +    } +} diff --git a/src/Support/HasPermissions.php b/src/Support/HasPermissions.php new file mode 100644 index 0000000..c42d71d --- /dev/null +++ b/src/Support/HasPermissions.php @@ -0,0 +1,15 @@ +<?php + +namespace PHPIAC\Modules\Support; + +trait HasPermissions +{ +    protected string $owner = ''; +    protected string $group = ''; +    protected int $mode = 0; + +    public function getPermissions($path): string +    { +        return new Permissions($path, $this->owner, $this->group, $this->mode); +    } +} diff --git a/src/Support/Permissions.php b/src/Support/Permissions.php new file mode 100644 index 0000000..daabf5a --- /dev/null +++ b/src/Support/Permissions.php @@ -0,0 +1,28 @@ +<?php + +namespace PHPIAC\Modules\Support; + +class Permissions +{ +    public function __construct( +        protected string $path, +        protected string $owner = '', +        protected string $group = '', +        protected int $mode = 0, +    ) {} + +    public function __toString(): string +    { +        $permissions = []; + +        if (! empty($this->owner) || ! empty($this->group)) { +            $permissions[] = "sudo chown -R $this->owner:$this->group $this->path"; +        } + +        if (! empty($this->mode)) { +            $permissions[] = "sudo chmod -R $this->mode $this->path"; +        } + +        return implode(PHP_EOL, $permissions); +    } +} diff --git a/src/TemplateModule.php b/src/TemplateModule.php new file mode 100644 index 0000000..515d891 --- /dev/null +++ b/src/TemplateModule.php @@ -0,0 +1,42 @@ +<?php + +namespace PHPIAC\Modules; + +use PHPIAC\Connection; +use PHPIAC\Module\Module; +use PHPIAC\Modules\Support\HasPermissions; +use Twig\Environment; +use Twig\Loader\FilesystemLoader; + +class TemplateModule extends Module +{ +    use HasPermissions; + +    protected string $src; +    protected string $dest; +    protected array $vars; + +    /** +     * @inheritDoc +     */ +    public function checkState(): bool +    { +        return false; +    } + +    /** +     * @inheritDoc +     */ +    public function getCommands(): array +    { +        $loader = new FilesystemLoader(dirname($this->src)); +        $twig = new Environment($loader); +        $rendered = $twig->render(basename($this->src), $this->vars); + +        Connection::put($this->dest, $rendered); + +        Connection::exec($this->getPermissions($this->dest)); + +        return []; +    } +} diff --git a/src/UfwModule.php b/src/UfwModule.php new file mode 100644 index 0000000..1ec4452 --- /dev/null +++ b/src/UfwModule.php @@ -0,0 +1,39 @@ +<?php + +namespace PHPIAC\Modules; + +use PHPIAC\Connection; +use PHPIAC\Module\Module; +use PHPIAC\Module\State; + +class UfwModule extends Module +{ +    protected string $rule; +    protected string $name; + +    protected string $state = State::ENABLED; + +    /** +     * @inheritDoc +     */ +    public function checkState(): bool +    { +        return false; +    } + +    /** +     * @inheritDoc +     */ +    public function getCommands(): array +    { +        Connection::exec(implode(PHP_EOL, [ +            "sudo ufw $this->rule $this->name", +            match ($this->state) { +                State::ENABLED => "sudo ufw --force enable", +                State::DISABLED => "sudo ufw disable", +            }, +        ])); + +        return []; +    } +} diff --git a/src/UserModule.php b/src/UserModule.php index fcab5a8..c230976 100644 --- a/src/UserModule.php +++ b/src/UserModule.php @@ -2,50 +2,37 @@  namespace PHPIAC\Modules; +use PHPIAC\Connection;  use PHPIAC\Module\Module;  use PHPIAC\Module\State; -use phpseclib3\Net\SSH2;  class UserModule extends Module  { -    /** -     * UserModule constructor. -     * -     * @param string $username -     * @param array $options -     * @param string $state -     */ -    public function __construct( -        private string $username, -        private array $options = [], -        private string $state = State::PRESENT -    ) { -        $this->options = array_replace([ -            'append' => false, -            'create_home' => true, -            'groups' => [], -            'shell' => '/bin/bash', -        ], $options); -    } +    protected string $username; +    protected string $password; + +    protected bool $append = false; +    protected bool $createHome = true; +    protected array $groups = []; +    protected string $shell = '/bin/bash'; +    protected string $state = State::PRESENT;      /**       * @inheritDoc       */      public function checkState(): bool      { -        global $ssh; -        /**@var SSH2 $ssh*/ -        $ssh->enablePTY(); +        Connection::enablePty(); -        $ssh->exec("cat /etc/passwd | grep $this->username:"); -        $hasUser = $ssh->read(); +        Connection::exec("cat /etc/passwd | grep $this->username:"); +        $hasUser = Connection::read();          $state = match ($this->state) {              State::PRESENT => str_starts_with($hasUser, "$this->username:"),              State::ABSENT => empty($hasUser),          }; -        $ssh->disablePTY(); +        Connection::disablePty();          return $state;      } @@ -55,16 +42,18 @@ class UserModule extends Module       */      public function getCommands(): array      { -        return match ($this->state) { -            State::PRESENT => [ +        if ($this->state === State::PRESENT) { +            Connection::exec(implode(PHP_EOL, [                  "sudo adduser $this->username --quiet" . -                    " --shell " . $this->options['shell'] . -                    ($this->options['create_home'] ? '' : ' --no-create-home'), -                "sudo usermod -" . ($this->options['append'] ? 'a' : '') . "G " . implode(',', $this->options['groups']) . " $this->username" -            ], -            State::ABSENT => [ -                "sudo userdel $this->username", -            ], -        }; +                " --shell " . $this->shell . +                ($this->createHome ? '' : ' --no-create-home'), +                "sudo usermod -" . ($this->append ? 'a' : '') . "G " . implode(',', $this->groups) . " $this->username" +            ])); +        } +        else if ($this->state === State::ABSENT) { +            Connection::exec("sudo userdel $this->username"); +        } + +        return [];      }  }  | 
