|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of Ymir command-line tool. |
| 7 | + * |
| 8 | + * (c) Carl Alexander <support@ymirapp.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Ymir\Cli\Command\Network; |
| 15 | + |
| 16 | +use Symfony\Component\Console\Input\InputArgument; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Filesystem\Filesystem; |
| 19 | +use Ymir\Cli\ApiClient; |
| 20 | +use Ymir\Cli\CliConfiguration; |
| 21 | +use Ymir\Cli\Command\AbstractCommand; |
| 22 | +use Ymir\Cli\Console\ConsoleOutput; |
| 23 | +use Ymir\Cli\ProjectConfiguration; |
| 24 | + |
| 25 | +class AddBastionHostCommand extends AbstractCommand |
| 26 | +{ |
| 27 | + /** |
| 28 | + * The name of the command. |
| 29 | + * |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + public const NAME = 'network:bastion:add'; |
| 33 | + |
| 34 | + /** |
| 35 | + * The file system. |
| 36 | + * |
| 37 | + * @var Filesystem |
| 38 | + */ |
| 39 | + private $filesystem; |
| 40 | + |
| 41 | + /** |
| 42 | + * The path to the user's home directory. |
| 43 | + * |
| 44 | + * @var string |
| 45 | + */ |
| 46 | + private $homeDirectory; |
| 47 | + |
| 48 | + /** |
| 49 | + * Constructor. |
| 50 | + */ |
| 51 | + public function __construct(ApiClient $apiClient, CliConfiguration $cliConfiguration, Filesystem $filesystem, string $homeDirectory, ProjectConfiguration $projectConfiguration) |
| 52 | + { |
| 53 | + parent::__construct($apiClient, $cliConfiguration, $projectConfiguration); |
| 54 | + |
| 55 | + $this->filesystem = $filesystem; |
| 56 | + $this->homeDirectory = rtrim($homeDirectory, '/'); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * {@inheritdoc} |
| 61 | + */ |
| 62 | + protected function configure() |
| 63 | + { |
| 64 | + $this |
| 65 | + ->setName(self::NAME) |
| 66 | + ->setDescription('Add a bastion host to the network') |
| 67 | + ->addArgument('network', InputArgument::OPTIONAL, 'The ID or name of the network to add a bastion host to'); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * {@inheritdoc} |
| 72 | + */ |
| 73 | + protected function perform(InputInterface $input, ConsoleOutput $output) |
| 74 | + { |
| 75 | + $network = $this->apiClient->getNetwork($this->determineNetwork('Which network would like to add a bastion host to', $input, $output)); |
| 76 | + |
| 77 | + $bastionHost = $this->apiClient->addBastionHost((int) $network->get('id')); |
| 78 | + |
| 79 | + $output->infoWithDelayWarning('Bastion host added'); |
| 80 | + $output->newLine(); |
| 81 | + $output->warn('SSH private key:'); |
| 82 | + $output->newLine(); |
| 83 | + $output->writeln($bastionHost['private_key']); |
| 84 | + |
| 85 | + if (!is_dir($this->homeDirectory.'/.ssh') || !$output->confirm('Would you like to create the SSH private key in your ~/.ssh directory?')) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + $privateKeyFilename = $this->homeDirectory.'/.ssh/ymir-'.$network->get('name'); |
| 90 | + |
| 91 | + if ($this->filesystem->exists($privateKeyFilename) && !$output->confirm('A SSH key already exists for this network. Do you want to overwrite it?', false)) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + $this->filesystem->dumpFile($privateKeyFilename, $bastionHost->get('private_key')); |
| 96 | + $this->filesystem->chmod($privateKeyFilename, 0600); |
| 97 | + |
| 98 | + $output->infoWithValue('SSH private key created', $privateKeyFilename); |
| 99 | + |
| 100 | + $sshConfigFilename = $this->homeDirectory.'/.ssh/config'; |
| 101 | + |
| 102 | + if (!$this->filesystem->exists($sshConfigFilename) || !$output->confirm('Would you like to configure SSH to connect to the bastion host?')) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + $output->infoWithWarning('Waiting for bastion host to get assigned a public domain name', 'takes a few minutes'); |
| 107 | + |
| 108 | + $bastionHost = $this->wait(function () use ($bastionHost) { |
| 109 | + $bastionHost = $this->apiClient->getBastionHost((int) $bastionHost->get('id')); |
| 110 | + |
| 111 | + return 'available' === $bastionHost->get('status') ? $bastionHost : []; |
| 112 | + }, 20, 15); |
| 113 | + |
| 114 | + $this->filesystem->appendToFile($sshConfigFilename, sprintf("\nHost %s\n User ec2-user\n IdentitiesOnly yes\n IdentityFile %s\n", $bastionHost->get('endpoint'), $privateKeyFilename)); |
| 115 | + |
| 116 | + $output->newLine(); |
| 117 | + $output->infoWithValue('SSH configured. Login using', sprintf('ssh %s', $bastionHost->get('endpoint'))); |
| 118 | + } |
| 119 | +} |
0 commit comments