From f7fc6fd54a5f750de8144b9b05d5ac173470c70a Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Tue, 20 Apr 2021 12:45:44 +0200 Subject: Adds Connection wrapper for SSH2 --- src/Command/RunCommand.php | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'src/Command') diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index 6d8d2c9..cc9c7fd 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -2,9 +2,8 @@ namespace PHPIAC\Command; +use PHPIAC\Connection; 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\Input\InputOption; @@ -14,36 +13,38 @@ class RunCommand extends Command { protected static $defaultName = 'run'; + /** + * @inheritDoc + */ protected function configure() { $this->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Path to config file', getcwd() . '/config.php'); } + /** + * @inheritDoc + */ protected function execute(InputInterface $input, OutputInterface $output): int { $config = include $input->getOption('config'); - 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'); - } + Connection::getInstance($config['host'], $config['user'], $config['private_key_file']); - $commands = []; foreach ($config['tasks'] as $task) { /**@var Task $task*/ + if (! $task->module->checkState()) { - $output->writeln($task->getName() . ': Adding commands from ' . get_class($task->module)); - array_push($commands, ...$task->module->getCommands()); + $output->writeln($task->getName()); + $output->writeln('Running'); + + $task->module->getCommands(); } else { - $output->writeln($task->getName() . ': Skipping commands from ' . get_class($task->module)); + $output->writeln($task->getName()); + $output->writeln('Skipping'); } } - // run commands in single exec call - $ssh->exec(implode(PHP_EOL, $commands)); - return Command::SUCCESS; } } -- cgit v1.2.3