public function getBlueprintLabels($filter = null) { $labels = []; foreach ($this->config->getBlueprintNames() as $blueprintName) { try { $effectiveStackName = $this->getBlueprint($blueprintName)->getStackName(); } catch (\StackFormation\Exception\InvalidStackNameException $e) { $effectiveStackName = '<fg=red>[Invalid stack name "' . $e->getStackName() . '"]</>'; } catch (\StackFormation\Exception\ValueResolverException $e) { $previousException = $e->getPrevious(); if ($previousException instanceof MissingEnvVarException) { $effectiveStackName = '<fg=red>[Missing env var "' . $previousException->getEnvVar() . '"]</>'; } else { throw $e; } } $label = $blueprintName; if (!is_null($filter) && !Finder::matchWildcard($filter, $label)) { continue; } if ($effectiveStackName != $blueprintName) { $label .= " <fg=yellow>(Effective: {$effectiveStackName})</>"; } $labels[] = $label; } return $labels; }
/** * @param $resourceStatusReason * @return array * @throws \Exception */ public function getDetailedLogFromResourceStatusReason($resourceStatusReason) { $logMessages = []; if (preg_match('/See the details in CloudWatch Log Stream: (.*)/', $resourceStatusReason, $matches)) { $logStream = $matches[1]; $logGroupName = Finder::findCloudWatchLogGroupByStream($logStream); if (empty($logGroupName)) { throw new \Exception('Could not find logGroupName for logStream: ' . $logStream); } $params = ['limit' => 20, 'logGroupName' => $logGroupName, 'logStreamName' => $logStream]; $cloudWatchLogClient = \AwsInspector\SdkFactory::getClient('CloudWatchLogs'); /* @var $cloudWatchLogClient \Aws\CloudWatchLogs\CloudWatchLogsClient */ $res = $cloudWatchLogClient->getLogEvents($params); $logMessages = array_merge(["==> Showing last 20 messages from {$logGroupName} -> {$logStream}"], $res->search('events[].message')); } elseif (preg_match('/WaitCondition received failed message:.*for uniqueId: (i-[0-9a-f]+)/', $resourceStatusReason, $matches)) { $instanceId = $matches[1]; $ec2Repo = new \AwsInspector\Model\Ec2\Repository(); $instance = $ec2Repo->findEc2InstanceBy('instance-id', $instanceId); if ($instance) { try { $res = $instance->exec('tail -50 /var/log/cloud-init-output.log'); $logMessages = array_merge(["==> Showing last 50 lines in /var/log/cloud-init-output.log"], $res['output']); } catch (FileNotFoundException $e) { $logMessages = ["Could not log in to instance '{$instanceId}' because the pem file could not be found"]; } } else { $logMessages = ["Could not find instance '{$instanceId}'"]; } } return $logMessages; }
protected function getResolvedStacks(InputInterface $input) { $stacks = Finder::find((array) $input->getArgument('stack'), $this->getStacks()); $except = $input->getOption('except'); if (!empty($except)) { if (($key = array_search($except, $stacks)) !== false) { unset($stacks[$key]); } } return $stacks; }
/** * Resolve wildcard * * @param $stackName * @return mixed * @throws \Exception */ public function resolveWildcard($stackName) { if (strpos($stackName, '*') === false) { return $stackName; } $helper = new \StackFormation\Helper(); $stacks = Finder::find($stackName, array_keys($this->getStacksFromApi())); if (count($stacks) == 0) { throw new \Exception("No matching stack found for '{$stackName}'"); } elseif (count($stacks) > 1) { throw new \Exception("Found more than one matching stack for '{$stackName}'."); } return end($stacks); }
/** * @param $logGroupName * @param string $logStreamNameFilter * @return \AwsInspector\Model\Collection */ public function findLogStreams($logGroupName, $logStreamNameFilter = null) { if (empty($logGroupName)) { throw new \InvalidArgumentException('LogGroupName cannot be empty'); } $result = $this->cloudWatchLogsClient->describeLogStreams(['logGroupName' => $logGroupName, 'orderBy' => 'LastEventTime', 'descending' => true]); $rows = $result->search('logStreams[]'); $collection = new \AwsInspector\Model\Collection(); foreach ($rows as $row) { if (!$logStreamNameFilter || Finder::matchWildcard($logStreamNameFilter, $row['logStreamName'])) { $collection->attach(new LogStream($row)); } } return $collection; }