Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrizio Branca committed Jun 30, 2016
2 parents c44bb63 + 373d1e4 commit d3a6fba
Show file tree
Hide file tree
Showing 47 changed files with 1,249 additions and 650 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ blueprints:
- Environment variable lookup with default value fallback: `{env:<var>:<defaultValue>}` -> value of environment variable 'var' falling back to 'defaultValue' if env var is not set
- Stack/global variable lookup: `{var:<var>}` -> value variable 'var'
- Current timestamp: `{tstamp}` -> e.g. '1453151115'
- Clean: `{clean:2.1.7}` -> '217' (removes all characters that aren't allowed in stack names
- Switch profile: `[profile:<profileName>:...]` will switch to a different profile and evaluate the second parameter there. This is useful in cross account setups.

Output and resource lookup allow you to "connect" stacks to each other by wiring the output or resources created in
one stack to the input parameters needed in another stack that sits on top of the first one without manually
Expand Down Expand Up @@ -152,6 +154,14 @@ blueprints:
[...]
```

Switch Profile Example (in this example an AMI is baked in a different account and shared with this account)
```
blueprints:
- stackname: mystack
parameters:
BaseAmi: '[profile:myDevAccountProfile:{output:bakestack:BaseAmi}]'
```

### Conditional parameter values

You might end up deploying the same stacks to multiple environments or accounts. Instead of duplicating the blueprints (or using YAML reference) you'll probably
Expand Down
5 changes: 3 additions & 2 deletions src/AwsInspector/Command/Profile/EnableCommand.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AwsInspector\Command\Profile;

use StackFormation\Profile\Manager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -28,7 +29,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
$profile = $input->getArgument('profile');
if (empty($profile)) {

$profileManager = new \AwsInspector\ProfileManager();
$profileManager = new Manager();

$helper = $this->getHelper('question');
$question = new ChoiceQuestion(
Expand All @@ -47,7 +48,7 @@ protected function interact(InputInterface $input, OutputInterface $output)

protected function execute(InputInterface $input, OutputInterface $output)
{
$profileManager = new \AwsInspector\ProfileManager();
$profileManager = new Manager();
$file = $profileManager->writeProfileToDotEnv($input->getArgument('profile'));
$output->writeln('File written: ' . $file);
}
Expand Down
3 changes: 2 additions & 1 deletion src/AwsInspector/Command/Profile/ListCommand.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AwsInspector\Command\Profile;

use StackFormation\Profile\Manager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -18,7 +19,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$profileManager = new \AwsInspector\ProfileManager();
$profileManager = new Manager();

$rows=[];
foreach($profileManager->listAllProfiles() as $profileName) {
Expand Down
39 changes: 10 additions & 29 deletions src/AwsInspector/SdkFactory.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,23 @@

namespace AwsInspector;

/**
* @deprecated
*/
class SdkFactory {

protected static $sdks=[];

/**
* @return \Aws\Sdk
*/
public static function getSdk($profile='default')
{
if (!isset(self::$sdks[$profile])) {
$params = [
'version' => 'latest',
'region' => getenv('AWS_DEFAULT_REGION'),
'retries' => 20
];
if ($profile != 'default') {
$profileManager = new ProfileManager();
$profileConfig = $profileManager->getProfileConfig($profile);
$params['region'] = $profileConfig['region'];
$params['credentials'] = [
'key' => $profileConfig['access_key'],
'secret' => $profileConfig['secret_key']
];
}
self::$sdks[$profile] = new \Aws\Sdk($params);
}
return self::$sdks[$profile];
}

/**
* @param string $client
* @return \Aws\AwsClientInterface
* @throws \Exception
* @deprecated
*/
public static function getClient($client, $profile='default', array $args=[]) {
return self::getSdk($profile)->createClient($client, $args);
public static function getClient($client, $profile=null, array $args=[]) {
static $profileManager;
if (empty($profileManager)) {
$profileManager = new \StackFormation\Profile\Manager();
}
return $profileManager->getClient($client, $profile, $args);
}

}
81 changes: 21 additions & 60 deletions src/StackFormation/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,15 @@ class Blueprint {
*/
protected $name;
protected $blueprintConfig;
protected $placeholderResolver;
protected $valueResolver;

public function __construct(
$name,
array $blueprintConfig,
PlaceholderResolver $placeholderResolver,
ConditionalValueResolver $valueResolver
)
public function __construct($name, array $blueprintConfig, ValueResolver $valueResolver)
{
if (!is_string($name)) {
throw new \InvalidArgumentException('Name must be a string');
}
$this->name = $name;
$this->blueprintConfig = $blueprintConfig;
$this->placeholderResolver = $placeholderResolver;
$this->valueResolver = $valueResolver;
}

Expand All @@ -39,7 +32,7 @@ public function getTags($resolvePlaceholders=true)
if (isset($this->blueprintConfig['tags'])) {
foreach ($this->blueprintConfig['tags'] as $key => $value) {
if ($resolvePlaceholders) {
$value = $this->placeholderResolver->resolvePlaceholders($value, $this, 'tag', $key);
$value = $this->valueResolver->resolvePlaceholders($value, $this, 'tag', $key);
}
$tags[] = ['Key' => $key, 'Value' => $value];
}
Expand All @@ -49,40 +42,25 @@ public function getTags($resolvePlaceholders=true)

public function getStackName()
{
return $this->placeholderResolver->resolvePlaceholders($this->name, $this, 'stackname');
$stackName = $this->valueResolver->resolvePlaceholders($this->name, $this, 'stackname');
Helper::validateStackname($stackName);
return $stackName;
}

public function getProfile()
public function getProfile($resolvePlaceholders=true)
{
if (isset($this->blueprintConfig['profile'])) {
$value = $this->blueprintConfig['profile'];
if (is_array($value)) {
$value = $this->valueResolver->resolveConditionalValue($value, $this);
if ($resolvePlaceholders) {
$value = $this->valueResolver->resolvePlaceholders($this->blueprintConfig['profile'], $this, 'profile');
}
$value = $this->placeholderResolver->resolvePlaceholders($value, $this, 'profile');
return $value;
}
return false;
}

public function enforceProfile()
{
// TODO: loading profiles shouldn't be done within a blueprint!
if ($profile = $this->getProfile()) {
if ($profile == 'USE_IAM_INSTANCE_PROFILE') {
echo "Using IAM instance profile\n";
} else {
$profileManager = new \AwsInspector\ProfileManager();
$profileManager->loadProfile($profile);
echo "Loading Profile: $profile\n";
}
}
return null;
}

public function getPreprocessedTemplate()
{
$this->enforceProfile();

if (empty($this->blueprintConfig['template']) || !is_array($this->blueprintConfig['template'])) {
throw new \Exception('No template(s) found');
}
Expand All @@ -108,8 +86,6 @@ public function getParameters($resolvePlaceholders=true)
{
$parameters = [];

$this->enforceProfile();

if (!isset($this->blueprintConfig['parameters'])) {
return [];
}
Expand All @@ -120,18 +96,15 @@ public function getParameters($resolvePlaceholders=true)
throw new \Exception("Invalid parameter key '$parameterKey'.");
}

if (is_array($parameterValue)) {
$parameterValue = $this->valueResolver->resolveConditionalValue($parameterValue, $this);
}
if (is_null($parameterValue)) {
throw new \Exception("Parameter $parameterKey is null.");
}
if (!is_scalar($parameterValue)) {
throw new \Exception('Invalid type for value');
}

if ($resolvePlaceholders) {
$parameterValue = $this->placeholderResolver->resolvePlaceholders($parameterValue, $this, 'parameter', $parameterKey);
$parameterValue = $this->valueResolver->resolvePlaceholders($parameterValue, $this, 'parameter', $parameterKey);
}
if (!is_scalar($parameterValue)) {
throw new \Exception('Invalid type for value');
}

$tmp = [
Expand Down Expand Up @@ -164,15 +137,20 @@ public function getParameters($resolvePlaceholders=true)

return $parameters;
}


/**
* @return array
* @throws \Exception
*/
public function getBeforeScripts()
{
$scripts = [];
if (isset($this->blueprintConfig['before']) && is_array($this->blueprintConfig['before']) && count($this->blueprintConfig['before']) > 0) {
$scripts = $this->blueprintConfig['before'];
}
foreach ($scripts as &$script) {
$script = $this->placeholderResolver->resolvePlaceholders($script, $this, 'script');
$script = $this->valueResolver->resolvePlaceholders($script, $this, 'script');
$script = str_replace('###CWD###', CWD, $script);
}
return $scripts;
}
Expand Down Expand Up @@ -218,29 +196,12 @@ public function getVars()
return isset($this->blueprintConfig['vars']) ? $this->blueprintConfig['vars'] : [];
}

public function executeBeforeScripts()
{
$scripts = $this->getBeforeScripts();
if (count($scripts) == 0) {
return;
}

$cwd = getcwd();
chdir($this->getBasePath());

passthru(implode("\n", $scripts), $returnVar);
if ($returnVar !== 0) {
throw new \Exception('Error executing commands');
}
chdir($cwd);
}

public function getBlueprintReference()
{
// this is how we reference a stack back to its blueprint
$blueprintReference = array_merge(
['Name' => $this->name],
$this->placeholderResolver->getDependencyTracker()->getUsedEnvironmentVariables()
$this->valueResolver->getDependencyTracker()->getUsedEnvironmentVariables()
);

$encodedValues = http_build_query($blueprintReference);
Expand Down
Loading

0 comments on commit d3a6fba

Please sign in to comment.