Commit ca23dd4e authored by Jon Pugh's avatar Jon Pugh

Implement ConfigurationInterface for ServerContext, and process it in the...

Implement ConfigurationInterface for ServerContext, and process it in the Aegir\Provision\Context construct class.  The ConfigurationInterface provides a handy way to define and validate config values, perfect for defining our Contexts.
parent 4934bb93
......@@ -29,9 +29,9 @@ abstract class Command extends BaseCommand
protected $output;
/**
* @var Config
* @var \Aegir\Provision\Console\Config
*/
private $config;
protected $config;
/**
* @param InputInterface $input An InputInterface instance
......
......@@ -81,11 +81,13 @@ class SaveCommand extends Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$server = new ServerContext($input->getArgument('context_name'), $input->getOptions(), $this->getApplication()->getConfig()->all());
$output->writeln(
"Saving context: ".$input->getArgument('context_name')
"Saving context: ".print_r($server,1)
);
$command = 'drush provision-save '.$input->getArgument('context_name');
$this->process($command);
// $command = 'drush provision-save '.$input->getArgument('context_name');
// $this->process($command);
}
}
......@@ -22,7 +22,7 @@ class Config implements ConfigurationInterface
*
* @var array
*/
private $config = [];
protected $config = [];
/**
* Path to config YML file.
......@@ -249,7 +249,7 @@ class Config implements ConfigurationInterface
/**
* Determine the user running provision.
*/
protected function getScriptUser() {
static function getScriptUser() {
$real_script_user = posix_getpwuid(posix_geteuid());
return $real_script_user['name'];
}
......@@ -264,7 +264,7 @@ class Config implements ConfigurationInterface
*
* @return string
*/
protected function getHomeDir()
static function getHomeDir()
{
$home = getenv('HOME');
if (!$home) {
......
......@@ -6,6 +6,9 @@
namespace Aegir\Provision;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Yaml\Yaml;
/**
* Base context class.
*/
......@@ -35,8 +38,32 @@ class Context
/**
* Constructor for the context.
*/
function __construct($name)
function __construct($name, $options, $console_config)
{
$this->name = $name;
$this->console_config = $console_config;
$this->config_path = $console_config['config_path'] . '/provision/' . $this->type . '.' . $this->name . '.yml';
$configs = [];
try {
$processor = new Processor();
if (file_exists($this->config_path)) {
$configs[] = Yaml::parse(file_get_contents($this->config_path));
}
else {
// Load command line options into properties
foreach ($this->option_documentation() as $option => $description) {
$this->properties[$option] = $options[$option];
}
}
$this->config = $processor->processConfiguration($this, $configs);
} catch (\Exception $e) {
throw new \Exception(
"There is an error with the configuration for $this->type $this->name: " . $e->getMessage()
);
}
}
}
......@@ -2,7 +2,10 @@
namespace Aegir\Provision\Context;
use Aegir\Provision\Console\Config;
use Aegir\Provision\Context;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* Class ServerContext
......@@ -11,9 +14,14 @@ use Aegir\Provision\Context;
*
* @see \Provision_Context_server
*/
class ServerContext extends Context
class ServerContext extends Context implements ConfigurationInterface
{
/**
* @var string
* 'server', 'platform', or 'site'.
*/
public $type = 'server';
static function option_documentation()
{
$options = [
......@@ -25,4 +33,34 @@ class ServerContext extends Context
return $options;
}
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$tree_builder = new TreeBuilder();
$root_node = $tree_builder->root('server');
$root_node
->children()
->scalarNode('name')
->defaultValue($this->name)
->end()
->scalarNode('remote_host')
->defaultValue($this->properties['remote_host'])
->end()
->scalarNode('script_user')
->defaultValue($this->properties['script_user'])
->end()
->scalarNode('aegir_root')
->defaultValue($this->properties['aegir_root'])
->end()
->scalarNode('master_url')
->defaultValue($this->properties['master_url'])
->end()
->end();
return $tree_builder;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment