diff options
author | Daniel Weipert <code@drogueronin.de> | 2021-04-18 15:09:51 +0200 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2021-04-18 15:09:51 +0200 |
commit | c0ebe78089121c0ad23efb0af32c435bee543a3c (patch) | |
tree | db5e9205bcd7181cfa41543ee8962e9d84cdb481 /src/Command |
Initial commit
Diffstat (limited to 'src/Command')
-rw-r--r-- | src/Command/RunCommand.php | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php new file mode 100644 index 0000000..048e94c --- /dev/null +++ b/src/Command/RunCommand.php @@ -0,0 +1,46 @@ +<?php + +namespace PHPIAC\Command; + +use PHPIAC\Task; +use phpseclib3\Crypt\PublicKeyLoader; +use phpseclib3\Net\SSH2; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class RunCommand extends Command +{ + protected static $defaultName = 'run'; + + protected function configure() + {} + + protected function execute(InputInterface $input, OutputInterface $output) + { + $cwd = getcwd(); + $config = include $cwd . '/config.php'; + + global $ssh; + $ssh = new SSH2($config['host']); + if (! $ssh->login($config['user'], PublicKeyLoader::load(file_get_contents($config['private_key_file'])))) { + throw new \Exception('Login failed'); + } + + $commands = []; + foreach ($config['tasks'] as $task) { + /**@var Task $task*/ + if (! $task->module->checkState()) { + $output->writeln('Adding commands from ' . get_class($task->module)); + array_push($commands, ...$task->module->getCommands()); + } + else { + $output->writeln('Skipping commands from ' . get_class($task->module)); + } + } + + $ssh->exec(implode(PHP_EOL, $commands)); + + return Command::SUCCESS; + } +} |