protected function execute(InputInterface $input, OutputInterface $output) { $configHelper = $this->getHelper('configuration'); /* @var $configHelper GlobalConfigurationHelper */ try { $configHelper->getConfiguration()->getRepositoryConfiguration($input->getArgument('repository')); throw new RepositoryExistsException($input->getArgument('repository')); } catch (NoSuchRepositoryException $ex) { // no op } if (!Util::isSshRepositoryUrl($input->getArgument('repository'))) { throw new NoSshRepositoryException($input->getArgument('repository')); } $repositoryConfig = new RepositoryConfiguration(); $repositoryConfig->setSshAlias($input->getOption('alias') ?: sha1($input->getArgument('repository') . time())); $sshKeyFile = new \SplFileInfo($input->getArgument('key')); if (!$sshKeyFile->isFile()) { throw new NotAFileException($sshKeyFile); } $repositoryConfig->setIdentityFile($sshKeyFile->getRealPath()); $sshConfigFile = new \SplFileInfo($configHelper->getConfiguration()->getSshDirectory() . '/config'); if (!file_exists($sshConfigFile->getPathname())) { FsUtil::touch($sshConfigFile->getPathname()); } if (!$sshConfigFile->isFile()) { throw new NotAFileException($sshConfigFile); } if (!$sshConfigFile->isReadable()) { throw new UnreadableFileException($sshConfigFile); } if (!$sshConfigFile->isWritable()) { throw new UnwritableFileException($sshConfigFile); } $sshConfigLines = file($sshConfigFile); try { if (Util::addSshAliasLines($sshConfigLines, $input->getArgument('repository'), $repositoryConfig)) { FsUtil::file_put_contents($sshConfigFile, implode(PHP_EOL, $sshConfigLines)); $output->writeln(sprintf('Added section <info>%s</info> to <info>%s</info>', 'Host ' . $repositoryConfig->getSshAlias(), $sshConfigFile->getPathname()), OutputInterface::VERBOSITY_VERBOSE); } else { $output->writeln(sprintf('Added section <info>%s</info> already exists in <info>%s</info>', 'Host ' . $repositoryConfig->getSshAlias(), $sshConfigFile->getPathname()), OutputInterface::VERBOSITY_VERBOSE); } } catch (SshAliasExistsException $ex) { throw new SshAliasExistsException($ex->getAliasName(), $ex->getLineNumber(), $sshConfigFile); } $configHelper->getConfiguration()->setRepositoryConfiguration($input->getArgument('repository'), $repositoryConfig); $configHelper->getConfiguration()->write(); $output->writeln(sprintf('Registered private key <info>%s</info> for repository <info>%s</info>', $repositoryConfig->getIdentityFile(), $input->getArgument('repository'))); return 0; }
public static function addSshAliasLines(array &$sshConfigLines, $repository, RepositoryConfiguration $repositoryConfig) { $repoParts = Util::parseRepositoryUrl($repository); $newConfigLines = ['Host ' . $repositoryConfig->getSshAlias(), 'HostName ' . $repoParts['host'], 'User ' . $repoParts['user'], 'IdentityFile ' . $repositoryConfig->getIdentityFile()]; $foundAliasLines = self::findSshAliasLines($sshConfigLines, $repositoryConfig->getSshAlias()); if (!$foundAliasLines) { $sshConfigLines = array_merge($sshConfigLines, $newConfigLines); return true; } else { $presentAliasLines = array_map(function ($i) use($sshConfigLines) { return $sshConfigLines[$i]; }, $foundAliasLines); foreach ($newConfigLines as $newConfigLine) { if (!in_array($newConfigLine, $presentAliasLines)) { throw new SshAliasExistsException($repositoryConfig->getSshAlias(), $foundAliasLines[0]); } } return false; } }
public function setRepositoryConfiguration($repositoryName, RepositoryConfiguration $conf) { $this->setConfigOption(['repositories', $repositoryName], $conf->getConfig()); }
protected function execute(InputInterface $input, OutputInterface $output) { $configHelper = $this->getHelper('configuration'); /* @var $configHelper GlobalConfigurationHelper */ $processHelper = $this->getHelper('process'); /* @var $processHelper ProcessHelper */ $questionHelper = $this->getHelper('question'); /* @var $questionHelper QuestionHelper */ if (!$input->getArgument('application')) { $input->setArgument('application', basename($input->getArgument('repository'), '.git')); } try { $repositoryConfiguration = $configHelper->getConfiguration()->getRepositoryConfiguration($input->getArgument('repository')); } catch (NoSuchRepositoryException $ex) { $repositoryConfiguration = null; } $repositoryParts = Util::parseRepositoryUrl($input->getArgument('repository')); if (!Util::isSshRepositoryUrl($repositoryParts)) { $input->setOption('no-deploy-key', true); } $application = Application::create($configHelper->getConfiguration(), $input->getArgument('application')); if (!is_dir($application->getPath())) { FsUtil::mkdir($application->getPath(), true); $output->writeln(sprintf('Created directory <info>%s</info>', $application->getPath()), OutputInterface::VERBOSITY_VERY_VERBOSE); } NotEmptyException::assert($application->getPath()); if (!$repositoryConfiguration && !$input->getOption('no-deploy-key')) { $output->writeln('You do not have a deploy key configured for this repository.', OutputInterface::VERBOSITY_VERBOSE); $repositoryConfiguration = new RepositoryConfiguration(); $repositoryConfiguration->setSshAlias(sha1($input->getArgument('repository')) . '-' . basename($input->getArgument('application'))); /* * Generate a new deploy key, link it to the repository and print it. */ $keyFile = $configHelper->getConfiguration()->getSshDirectory() . '/id_rsa-' . $repositoryConfiguration->getSshAlias(); try { $this->getApplication()->find('repository:generate-key')->run(new ArrayInput(['key' => $keyFile, '--comment' => 'clic-deploy-key-' . $repositoryConfiguration->getSshAlias() . '@' . gethostname(), '--target-repository' => $input->getArgument('repository'), '--print-public-key' => true]), $output); $repositoryConfiguration = $configHelper->getConfiguration()->getRepositoryConfiguration($input->getArgument('repository')); } catch (FileExistsException $ex) { $repositoryConfiguration->setIdentityFile($ex->getFilename()); $output->writeln(sprintf('Key <info>%s</info> already exists. Not generating a new one.', $ex->getFilename())); } /* * Ask to add it as a deploy key to the repo */ $output->writeln('<comment>Please set the public key printed above as a deploy key for the repository</comment>'); while (!$questionHelper->ask($input, $output, new ConfirmationQuestion('Is the deploy key uploaded?'))) { } } if ($repositoryConfiguration && !$input->getOption('no-deploy-key')) { /* * If there is a configuration now, save it */ $configHelper->getConfiguration()->setRepositoryConfiguration($input->getArgument('repository'), $repositoryConfiguration); $configHelper->getConfiguration()->write(); } else { $repositoryConfiguration = null; } /* * Run a git clone for the application */ $gitClone = ProcessBuilder::create(['git', 'clone', Util::replaceRepositoryUrl($repositoryParts, $repositoryConfiguration), $application->getPath()])->setTimeout(null)->getProcess(); $processHelper->mustRun($output, $gitClone, null, null, OutputInterface::VERBOSITY_NORMAL, OutputInterface::VERBOSITY_NORMAL); $configHelper->getConfiguration()->removeApplication($application->getName()); // Clean up application again, so application:add further down does not complain. $this->getApplication()->find('application:add')->run(new ArrayInput(['application' => $input->getArgument('application'), '--remote' => $input->getArgument('repository')]), $output); if ($input->getOption('override')) { $input1 = new ArrayInput(['application' => $input->getArgument('application'), 'config-file' => $input->getOption('override')]); if ($input->getOption('override-type')) { $input1->setOption('type', $input->getOption('override-type')); } $this->getApplication()->find('application:override')->run($input1, $output); } if (!$input->getOption('no-scripts')) { /* * Run post-clone script */ return $this->getApplication()->find('application:execute')->run(new ArrayInput(['script' => 'post-clone', 'application' => $input->getArgument('application')]), $output); } return 0; }