findEc2InstancesByTags() public method

public findEc2InstancesByTags ( array $tags = [] ) : Collection
$tags array
return AwsInspector\Model\Collection
Example #1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $tags = $input->getOption('tag');
     $tags = $this->convertTags($tags);
     $mapping = ['InstanceId' => 'InstanceId', 'ImageId' => 'ImageId', 'State' => 'State.Name', 'SubnetId' => 'SubnetId', 'AZ' => 'Placement.AvailabilityZone', 'PublicIp' => 'PublicIpAddress', 'PrivateIp' => 'PrivateIpAddress', 'KeyName' => 'KeyName'];
     // dynamically add current tags
     foreach (array_keys($tags) as $tagName) {
         $mapping[$tagName] = 'Tags[?Key==`' . $tagName . '`].Value | [0]';
     }
     foreach ($input->getOption('column') as $tagName) {
         $mapping[$tagName] = 'Tags[?Key==`' . $tagName . '`].Value | [0]';
     }
     $repository = new Repository();
     $instanceCollection = $repository->findEc2InstancesByTags($tags);
     $rows = [];
     foreach ($instanceCollection as $instance) {
         /* @var $instance Instance */
         $rows[] = $instance->extractData($mapping);
     }
     if (count($rows)) {
         $table = new \Symfony\Component\Console\Helper\Table($output);
         $table->setHeaders(array_keys(end($rows)))->setRows($rows);
         $table->render();
     } else {
         $output->writeln('No matching instances found.');
     }
 }
 protected function interact(InputInterface $input, OutputInterface $output)
 {
     $instance = $input->getArgument('instance');
     if (empty($instance)) {
         // find instances based on tag(s)
         $tags = $input->getOption('tag');
         $tags = $this->convertTags($tags);
         $repository = new Repository();
         $instanceCollection = $repository->findEc2InstancesByTags($tags);
         $count = count($instanceCollection);
         if ($count == 0) {
             throw new \Exception('No instance found matching the given tags');
         } elseif ($count == 1) {
             $instanceObj = $instanceCollection->getFirst();
             /* @var $instanceObj Instance */
             $input->setArgument('instance', $instanceObj->getInstanceId());
         } else {
             $mapping = [];
             // dynamically add current tags
             foreach (array_keys($tags) as $tagName) {
                 $mapping[$tagName] = 'Tags[?Key==`' . $tagName . '`].Value | [0]';
             }
             foreach ($input->getOption('column') as $tagName) {
                 $mapping[$tagName] = 'Tags[?Key==`' . $tagName . '`].Value | [0]';
             }
             $labels = [];
             foreach ($instanceCollection as $instanceObj) {
                 /* @var $instanceObj Instance */
                 $instanceLabel = $instanceObj->getInstanceId();
                 $tmp = [];
                 foreach ($instanceObj->extractData($mapping) as $field => $value) {
                     if (!empty($value)) {
                         $tmp[] = "{$field}: {$value}";
                     }
                 }
                 if (count($tmp)) {
                     $labels[] = $instanceLabel . ' (' . implode('; ', $tmp) . ')';
                 } else {
                     $labels[] = $instanceLabel;
                 }
             }
             $helper = $this->getHelper('question');
             $question = new ChoiceQuestion('Please select an instance', $labels);
             $question->setErrorMessage('Instance %s is invalid.');
             $instance = $helper->ask($input, $output, $question);
             $output->writeln('Selected Instance: ' . $instance);
             list($instance) = explode(' ', $instance);
             $input->setArgument('instance', $instance);
         }
     }
     if (!$input->getOption('force')) {
         $helper = $this->getHelper('question');
         $question = new ConfirmationQuestion("Are you sure you want to terminate following instance? {$instance} [y/N] ", false);
         if (!$helper->ask($input, $output, $question)) {
             throw new \Exception('Operation aborted');
         }
         $input->setOption('force', true);
     }
 }
Example #3
0
 /**
  * Get jump host (bastion server)
  *
  * Overwrite this method in your inheriting class and return
  * a \AwsInspector\Model\Ec2\Instance representing your bastion server
  *
  * @return null|Instance
  */
 public function getJumpHost()
 {
     if ($config = $this->getInspectorConfiguration('jumptags')) {
         $ec2Repository = new Repository();
         return $ec2Repository->findEc2InstancesByTags($config)->getFirst();
     }
     return null;
 }
Example #4
0
 /**
  * Get jump host (bastion server)
  *
  * Overwrite this method in your inheriting class and return
  * a \AwsInspector\Model\Ec2\Instance representing your bastion server
  *
  * @return Instance|null
  * @throws \Exception
  */
 public function getJumpHost()
 {
     if ($config = $this->getInspectorConfiguration('jumptags')) {
         $ec2Repository = new Repository();
         $instances = $ec2Repository->findEc2InstancesByTags($config);
         if (count($instances) == 0) {
             throw new \Exception('Could not fund jump host for: ' . var_export($config, true));
         }
         return $instances->getFirst();
     }
     return null;
 }