Skip to content
Commits on Source (47)
......@@ -57,7 +57,7 @@ script:
--web_group=www-data
--web_disable_url=http://localhost/disabled
--web_maintenance_url=http://localhost/maintenance
--restart_command="sudo apache2ctl graceful"
--restart_command="echo 'Restarting web server...'; sleep 3"
- provision services server_master add db -n
--service_type=mysql
......
#!/usr/bin/env php
<?php
require 'provision.php';
require 'provision-robo.php';
<?php
// We use PWD if available because getcwd() resolves symlinks, which
// could take us outside of the Drupal root, making it impossible to find.
$cwd = empty($_SERVER['PWD']) ? getcwd() : $_SERVER['PWD'];
// Set up autoloader
$loader = false;
if (file_exists($autoloadFile = __DIR__ . '/vendor/autoload.php')
|| file_exists($autoloadFile = __DIR__ . '/../vendor/autoload.php')
|| file_exists($autoloadFile = __DIR__ . '/../autoload.php')
|| file_exists($autoloadFile = __DIR__ . '/../../autoload.php')
) {
$loader = include_once($autoloadFile);
} else {
throw new \Exception("Could not locate autoload.php. cwd is $cwd; __DIR__ is " . __DIR__);
}
use Aegir\Provision\Console\ConsoleOutput;
use Aegir\Provision\Console\Config;
use Drupal\Console\Core\Style\DrupalStyle;
use Robo\Common\TimeKeeper;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Exception\CommandNotFoundException;
// Start Timer.
$timer = new TimeKeeper();
$timer->start();
try {
// Create input output objects.
$input = new ArgvInput($argv);
$output = new ConsoleOutput();
$io = new DrupalStyle($input, $output);
// Create a config object.
$config = new Config();
// Create the app.
$app = new \Aegir\Provision\Provision($config, $input, $output);
// Run the app.
$status_code = $app->run($input, $output);
}
catch (InvalidOptionException $e) {
$io->error("There was a problem with your console configuration: " . $e->getMessage(), 1);
$status_code = 1;
}
// Stop timer.
$timer->stop();
if ($output->isVerbose()) {
$output->writeln("<comment>" . $timer->formatDuration($timer->elapsed()) . "</comment> total time elapsed.");
}
exit($status_code);
<?php
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Input\ArrayInput;
use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\Console\Core\Utils\ArgvInputReader;
use Aegir\Provision\Application;
use Aegir\Provision\Console\ConsoleOutput;
set_time_limit(0);
ini_set('display_errors', 1);
......
......@@ -6,6 +6,7 @@
"type": "library",
"license": "GPL-2.0+",
"require": {
"consolidation/Robo": "^1.1",
"consolidation/annotated-command": "~2",
"drupal/console-core": "1.0.2",
"psr/log": "^1.0",
......
This diff is collapsed.
......@@ -7,8 +7,11 @@ use Aegir\Provision\Command\ServicesCommand;
use Aegir\Provision\Command\ShellCommand;
use Aegir\Provision\Command\StatusCommand;
use Aegir\Provision\Command\VerifyCommand;
use Aegir\Provision\Common\ProvisionAwareTrait;
use Aegir\Provision\Console\Config;
use Aegir\Provision\Console\ConsoleOutput;
use Drupal\Console\Core\Style\DrupalStyle;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Command\HelpCommand;
......@@ -19,6 +22,7 @@ use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Command\Command as BaseCommand;
//use Symfony\Component\DependencyInjection\ContainerInterface;
//use Drupal\Console\Annotations\DrupalCommandAnnotationReader;
......@@ -33,17 +37,6 @@ use Symfony\Component\Console\Application as BaseApplication;
*/
class Application extends BaseApplication
{
/**
* @var string
*/
const NAME = 'Aegir Provision';
/**
* @var string
*/
const VERSION = '4.x';
/**
* @var string
*/
......@@ -53,97 +46,42 @@ class Application extends BaseApplication
* @var string
*/
const DEFAULT_TIMEZONE = 'America/New_York';
use ProvisionAwareTrait;
use LoggerAwareTrait;
/**
* @var LoggerInterface
*/
public $logger;
/**
* @var DrupalStyle
*/
public $io;
/**
* @var \Symfony\Component\Console\Input\InputInterface
*/
public $input;
/**
* @var \Symfony\Component\Console\Output\OutputInterface
* @var ConsoleOutput
*/
public $output;
public $console;
/**
* Application constructor.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param \Aegir\Provision\Console\OutputInterface
*
* @throws \Exception
*/
public function __construct(InputInterface $input, OutputInterface $output)
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
{
// If no timezone is set, set Default.
if (empty(ini_get('date.timezone'))) {
date_default_timezone_set($this::DEFAULT_TIMEZONE);
}
// Load Configs
try {
$this->config = new Config();
}
catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
parent::__construct($this::NAME, $this::VERSION);
}
/**
* Prepare input and output arguments. Use this to extend the Application object so that $input and $output is fully populated.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*/
public function configureIO(InputInterface $input, OutputInterface $output) {
parent::configureIO($input, $output);
$this->io = new DrupalStyle($input, $output);
$this->input = $input;
$this->output = $output;
$this->logger = new ConsoleLogger($output,
[LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL]
);
}
/**
* @var Config
*/
private $config;
/**
* Getter for Configuration.
*
* @return Config
* Configuration object.
*/
public function getConfig()
{
return $this->config;
parent::__construct($name, $version);
}
/**
* Setter for Configuration.
* Make configureIO public so we can run it before ->run()
*
* @param Config $config
* Configuration object.
* @param InputInterface $input
* @param OutputInterface $output
*/
public function setConfig(Config $config)
public function configureIO(InputInterface $input, OutputInterface $output)
{
$this->config = $config;
parent::configureIO($input, $output);
}
/**
......@@ -161,7 +99,29 @@ class Application extends BaseApplication
return $commands;
}
/**
* Interrupts Command execution to add services like provision and logger.
*
* @param \Symfony\Component\Console\Command\Command $command
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return int
*/
protected function doRunCommand( BaseCommand $command, InputInterface $input, OutputInterface $output)
{
// Only setProvision if the command is using the trait.
if (method_exists($command, 'setProvision')) {
$command
->setProvision($this->getProvision())
->setLogger($this->logger)
;
}
$exitCode = parent::doRunCommand($command, $input, $output);
return $exitCode;
}
/**
* {@inheritdoc}
*
......@@ -181,145 +141,4 @@ class Application extends BaseApplication
return $inputDefinition;
}
/**
* Load all contexts into Context objects.
*
* @return array
*/
static function getAllContexts($name = '', $application = NULL) {
$contexts = [];
$config = new Config();
$context_files = [];
$finder = new \Symfony\Component\Finder\Finder();
$finder->files()->name('*' . $name . '.yml')->in($config->get('config_path') . '/provision');
foreach ($finder as $file) {
list($context_type, $context_name) = explode('.', $file->getFilename());
$context_files[$context_name] = [
'name' => $context_name,
'type' => $context_type,
'file' => $file,
];
}
foreach ($context_files as $context) {
$class = Context::getClassName($context['type']);
$contexts[$context['name']] = new $class($context['name'], $application);
}
if ($name && isset($contexts[$name])) {
return $contexts[$name];
}
elseif ($name && !isset($contexts[$name])) {
return NULL;
}
else {
return $contexts;
}
}
/**
* Load all server contexts.
*
* @param null $service
* @return mixed
* @throws \Exception
*/
static public function getAllServers($service = NULL) {
$servers = [];
$context_files = self::getAllContexts();
if (empty($context_files)) {
throw new \Exception('No server contexts found. Use `provision save` to create one.');
}
foreach ($context_files as $context) {
if ($context->type == 'server') {
$servers[$context->name] = $context;
}
}
return $servers;
}
/**
* Get a simple array of all contexts, for use in an options list.
* @return array
*/
public function getAllContextsOptions($type = NULL) {
$options = [];
foreach ($this->getAllContexts() as $name => $context) {
if ($type) {
if ($context->type == $type) {
$options[$name] = $context->name;
}
}
else {
$options[$name] = $context->type . ' ' . $context->name;
}
}
return $options;
}
/**
* Load the Aegir context with the specified name.
*
* @param $name
*
* @return \Aegir\Provision\Context
* @throws \Exception
*/
static public function getContext($name, Application $application = NULL) {
if (empty($name)) {
throw new \Exception('Context name must not be empty.');
}
if (empty(Application::getAllContexts($name, $application))) {
throw new \Exception('Context not found with name: ' . $name);
}
return Application::getAllContexts($name, $application);
}
/**
* Get a simple array of all servers, optionally specifying the the service_type to filter by ("http", "db" etc.)
* @param string $service_type
* @return array
*/
public function getServerOptions($service_type = '') {
$servers = [];
foreach ($this->getAllServers() as $server) {
if ($service_type && !empty($server->config['services'][$service_type])) {
$servers[$server->name] = $server->name . ': ' . $server->config['services'][$service_type]['type'];
}
elseif ($service_type == '') {
$servers[$server->name] = $server->name . ': ' . $server->config['services'][$service_type]['type'];
}
}
return $servers;
}
/**
* Check that a context type's service requirements are provided by at least 1 server.
*
* @param $type
* @return array
*/
static function checkServiceRequirements($type) {
$class_name = Context::getClassName($type);
// @var $context Context
$service_requirements = $class_name::serviceRequirements();
$services = [];
foreach ($service_requirements as $service) {
try {
if (empty(Application::getAllServers($service))) {
$services[$service] = 0;
}
else {
$services[$service] = 1;
}
} catch (\Exception $e) {
$services[$service] = 0;
}
}
return $services;
}
}
......@@ -2,7 +2,10 @@
namespace Aegir\Provision;
use Aegir\Provision\Common\ProvisionAwareTrait;
use Drupal\Console\Core\Style\DrupalStyle;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Command\Command as BaseCommand;
use Drupal\Console\Core\Command\Shared\CommandTrait;
......@@ -19,7 +22,8 @@ abstract class Command extends BaseCommand
{
use CommandTrait;
use ProvisionAwareTrait;
use LoggerAwareTrait;
/**
* @var \Symfony\Component\Console\Input\InputInterface
......@@ -50,7 +54,7 @@ abstract class Command extends BaseCommand
* @var string
*/
public $context_name;
/**
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
......@@ -70,7 +74,7 @@ abstract class Command extends BaseCommand
try {
// Load context from context_name argument.
$this->context_name = $this->input->getArgument('context_name');
$this->context = Application::getContext($this->context_name, $this->getApplication());
$this->context = $this->getProvision()->getContext($this->context_name);
}
catch (\Exception $e) {
......@@ -92,7 +96,7 @@ abstract class Command extends BaseCommand
$this->input->setArgument('context_name', $this->context_name);
try {
$this->context = Application::getContext($this->context_name, $this->getApplication());
$this->context = $this->getProvision()->getContext($this->context_name);
}
catch (\Exception $e) {
$this->context = NULL;
......@@ -104,11 +108,11 @@ abstract class Command extends BaseCommand
* Show a list of Contexts to the user for them to choose from.
*/
public function askForContext($question = 'Choose a context') {
if (empty($this->getApplication()->getAllContextsOptions())) {
if (empty($this->getProvision()->getAllContextsOptions())) {
throw new \Exception('No contexts available! use <comment>provision save</comment> to create one.');
}
$this->context_name = $this->io->choice($question, $this->getApplication()->getAllContextsOptions());
$this->context_name = $this->io->choice($question, $this->getProvision()->getAllContextsOptions());
}
/**
......
......@@ -8,6 +8,7 @@ use Aegir\Provision\Context;
use Aegir\Provision\Context\PlatformContext;
use Aegir\Provision\Context\ServerContext;
use Aegir\Provision\Context\SiteContext;
use Aegir\Provision\Provision;
use Aegir\Provision\Service;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\ArrayInput;
......@@ -152,7 +153,7 @@ class SaveCommand extends Command
// Check for context type service requirements.
$exit = FALSE;
$this->io->comment("Checking service requirements for context type {$context_type}...");
$reqs = Application::checkServiceRequirements($context_type);
$reqs = $this->getProvision()->checkServiceRequirements($context_type);
foreach ($reqs as $service => $available) {
if ($available) {
$this->io->successLite("Service $service: Available");
......@@ -169,9 +170,12 @@ class SaveCommand extends Command
}
$properties = $this->askForContextProperties();
$options = $this->askForContextProperties();
$options['name'] = $this->context_name;
$options['type'] = $this->context_type;
$class = Context::getClassName($this->input->getOption('context_type'));
$this->context = new $class($input->getArgument('context_name'), $this->getApplication(), $properties);
$this->context = new $class($input->getArgument('context_name'), $this->getProvision(), $options);
}
// Delete context config.
......@@ -227,7 +231,7 @@ class SaveCommand extends Command
*/
public function askForContext($question = 'Choose a context')
{
$options = $this->getApplication()->getAllContextsOptions();
$options = $this->getProvision()->getAllContextsOptions();
// If there are options, add "new" to the list.
if (count($options)) {
......@@ -305,7 +309,7 @@ class SaveCommand extends Command
// $context_name = $this->io->ask($all_services[$type]);
// }
// $context = Application::getContext($context_name);
// $context = Provision::getContext($context_name);
$this->io->info("Adding required service $type...");
......@@ -348,7 +352,7 @@ class SaveCommand extends Command
$contexts[$property] = $this->input->getOption($property);
try {
$context = Application::getContext($contexts[$property]);
$context = $this->getProvision()->getContext($contexts[$property]);
}
catch (\Exception $e) {
throw new \Exception("Context set by option --{$property} does not exist.");
......@@ -359,7 +363,7 @@ class SaveCommand extends Command
}
}
else {
$contexts[$property] = $this->io->choice("Select $property context", $this->getApplication()->getAllContextsOptions($context_type));
$contexts[$property] = $this->io->choice("Select $property context", $this->getProvision()->getAllContextsOptions($context_type));
}
}
return $contexts;
......
......@@ -219,16 +219,16 @@ class ServicesCommand extends Command
}
// All other context types are associating with servers that provide the service.
else {
if (empty($this->getApplication()->getServerOptions($service))) {
if (empty($this->getProvision()->getServerOptions($service))) {
throw new \Exception("No servers providing $service service were found. Create one with `provision save` or use `provision services` to add to an existing server.");
}
$server = $this->input->getArgument('server')?
$this->input->getArgument('server'):
$this->io->choice('Which server?', $this->getApplication()->getServerOptions($service));
$this->io->choice('Which server?', $this->getProvision()->getServerOptions($service));
// Then ask for all options.
$server_context = $this->getApplication()->getContext($server);
$server_context = $this->getProvision()->getContext($server);
$properties = $this->askForServiceProperties($service);
$this->io->info("Using $service service from server $server...");
......
......@@ -43,6 +43,7 @@ class StatusCommand extends Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getProvision();
if ($input->getArgument('context_name')) {
$rows = [['Configuration File', $this->context->config_path]];
......@@ -58,8 +59,9 @@ class StatusCommand extends Command
}
else {
$headers = ['Provision CLI Configuration'];
$rows = [['Configuration File', $this->getApplication()->getConfig()->getConfigPath()]];
$config = $this->getApplication()->getConfig()->all();
$rows = [];
$config = $this->getProvision()->getConfig()->toArray();
unset($config['options']);
foreach ($config as $key => $value) {
$rows[] = [$key, $value];
}
......@@ -67,14 +69,14 @@ class StatusCommand extends Command
// Lookup all contexts
$rows = [];
foreach ($this->getApplication()->getAllContexts() as $context) {
foreach ($this->getProvision()->getAllContexts() as $context) {
$rows[] = [$context->name, $context->type];
}
$headers = ['Contexts'];
$this->io->table($headers, $rows);
// Offer to output a context status.
$options = $this->getApplication()->getAllContextsOptions();
$options = $this->getProvision()->getAllContextsOptions();
$options['none'] = 'none';
$context = $this->io->choiceNoList('Get status for', $options, 'none');
......
......@@ -60,6 +60,11 @@ class VerifyCommand extends Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (empty($this->context)) {
throw new \Exception("You must specify a context to verify.");
}
$this->io->title(strtr("Verify %type: %name", [
'%name' => $this->context_name,
'%type' => $this->context->type,
......@@ -75,8 +80,7 @@ class VerifyCommand extends Command
}
*/
$message = $this->context->verify();
$message = $this->context->verifyCommand();
$this->io->comment($message);
}
}
<?php
namespace Aegir\Provision\Common;
use Aegir\Provision\Context;
trait ContextAwareTrait
{
/**
* @var Context
*/
protected $context = NULL;
/**
* @param Context $context
*
* @return $this
*/
public function setContext(Context $context = NULL)
{
$this->context = $context;
return $this;
}
/**
* @return Context
*/
public function getContext()
{
return $this->context;
}
}
<?php
namespace Aegir\Provision\Common;
use Aegir\Provision\Context;
use Symfony\Component\Process\Process;
use Twig\Node\Expression\Unary\NegUnary;
trait ProcessAwareTrait
{
/**
* @var Process
*/
protected $process = NULL;
// /**
// * @var string
// */
// protected $command;
/**
* @param Process $process
*
* @return $this
*/
protected function setProcess(Process $process = NULL)
{
$this->process = $process;
return $this;
}
/**
* @return Process
*/
public function getProcess()
{
if (is_null($this->process)) {
$this->process = new Process($this->command);
}
return $this->process;
}
/**
* @param $command
*
* @return $this
*/
public function setCommand($command) {
$this->command = $command;
return $this;
}
/**
* @return string
*/
public function getCommand() {
return $this->command;
}
public function execute() {
$this->process->run();
}
}
<?php
namespace Aegir\Provision\Common;
use Aegir\Provision\Provision;
trait ProvisionAwareTrait
{
/**
* @var Provision
*/
protected $provision = NULL;
/**
* @param Provision $provision
*
* @return $this
*/
public function setProvision(Provision $provision = NULL)
{
$this->provision = $provision;
return $this;
}
/**
* @return Provision
*/
public function getProvision()
{
if (is_null($this->provision)) {
return Provision::getProvision();
}
return $this->provision;
}
}
......@@ -2,7 +2,7 @@
namespace Aegir\Provision\ConfigDefinition;
use Aegir\Provision\Application;
use Aegir\Provision\Common\ProvisionAwareTrait;
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
......@@ -28,6 +28,8 @@ use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
*/
class ContextNodeDefinition extends ScalarNodeDefinition
{
use ProvisionAwareTrait;
protected function createNode()
{
/**
......@@ -50,12 +52,13 @@ class ContextNodeDefinition extends ScalarNodeDefinition
*/
public function validateContext($value)
{
$this->setProvision($this->getNode()->getAttribute('provision'));
// No need to do anything else.
// If there is no context named $value, getContext() throws an exception for us.
Application::getContext($value);
// If context_type is specified, Validate that the desired context is the right type.
if ($this->getNode()->getAttribute('context_type') && Application::getContext($value)->type != $this->getNode()->getAttribute('context_type')) {
if ($this->getNode()->getAttribute('context_type') && $this->getProvision()->getContext($value)->type != $this->getNode()->getAttribute('context_type')) {
throw new InvalidConfigurationException(strtr('The context specified for !name must be type !type.', [
'!name' => $this->name,
'!type' => $this->getNode()->getAttribute('context_type'),
......@@ -69,8 +72,8 @@ class ContextNodeDefinition extends ScalarNodeDefinition
$this->getNode()->getAttribute('service_requirement'):
$path[2]
;
Application::getContext($value)->getService($service);
$this->getProvision()->getContext($value)->getService($service);
}
return $value;
}
......
......@@ -2,65 +2,48 @@
namespace Aegir\Provision\Console;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Console\Exception\InvalidOptionException;
/**
* Class Config.
* Class Config
* @package Aegir\Provision\Console
*
* Many thanks to pantheon-systems/terminus. Inspired by DefaultConfig
*/
class Config implements ConfigurationInterface
class Config extends ProvisionConfig
{
/**
* Configuration values array.
*
* @var array
*/
protected $config = [];
/**
* Path to config YML file.
*
* @var string
*/
private $config_path = '';
/**
* Filename of config YML file.
*
* @var string
*/
private $config_filename = '.provision.yml';
const CONFIG_FILENAME = '.provision.yml';
/**
* {@inheritdoc}
* DefaultsConfig constructor.
*/
public function __construct()
{
$this->config_path = $this->getHomeDir().'/'.$this->config_filename;
try {
$processor = new Processor();
$configs = func_get_args();
if (file_exists($this->config_path)) {
$configs[] = Yaml::parse(file_get_contents($this->config_path));
}
$this->config = $processor->processConfiguration($this, $configs);
} catch (\Exception $e) {
throw new Exception(
'There is an error with your configuration: '.$e->getMessage()
);
}
parent::__construct();
$this->set('root', $this->getProvisionRoot());
$this->set('php', $this->getPhpBinary());
$this->set('php_version', PHP_VERSION);
$this->set('php_ini', get_cfg_var('cfg_file_path'));
$this->set('script', $this->getProvisionScript());
$this->set('os_version', php_uname('v'));
$this->set('user_home', $this->getHomeDir());
$this->set('aegir_root', $this->getHomeDir());
$this->set('script_user', $this->getScriptUser());
$this->set('config_path', $this->getHomeDir() . '/config');
$file = $this->get('user_home') . DIRECTORY_SEPARATOR . Config::CONFIG_FILENAME;
$this->set('console_config_file', $file);
$this->extend(new YamlConfig($this->get('user_home') . DIRECTORY_SEPARATOR . Config::CONFIG_FILENAME));
$this->extend(new DotEnvConfig(getcwd()));
$this->extend(new EnvConfig());
$this->validateConfig();
}
/**
* Check configuration values against the current system.
*
......@@ -69,204 +52,108 @@ class Config implements ConfigurationInterface
protected function validateConfig() {
// Check that aegir_root is writable.
// @TODO: Create some kind of Setup functionality.
if (!is_writable($this->config['aegir_root'])) {
throw new \Exception(
"There is an error with your configuration. The folder set to 'aegir_root' ({$this->config['aegir_root']}) is not writable. Fix this or change the aegir_root value in the file {$this->config_path}."
if (!is_writable($this->get('aegir_root'))) {
throw new InvalidOptionException(
"The folder set to 'aegir_root' ({$this->get('aegir_root')}) is not writable. Fix this or change the aegir_root value in the file {$this->get('console_config_file')}"
);
}
// Check that config_path exists and is writable.
if (!file_exists($this->config['config_path'])) {
throw new \Exception(
"There is an error with your configuration. The folder set to 'config_path' ({$this->config['config_path']}) does not exist. Create it or change the config_path value in the file {$this->config_path}."
// If config_path does not exist and we cannot create it...
if (!file_exists($this->get('config_path')) && !mkdir($this->get('config_path'))) {
throw new InvalidOptionException(
"The folder set to 'config_path' ({$this->get('config_path')}) does not exist, and cannot be created. Create it manually or change the 'config_path' value in the file {$this->get('console_config_file')}."
);
}
elseif (!is_writable($this->config['config_path'])) {
throw new \Exception(
"There is an error with your configuration. The folder set to 'config_path' ({$this->config['config_path']}) is not writable. Fix this or change the config_path value in the file {$this->config_path}."
elseif (!is_writable($this->get('config_path'))) {
throw new InvalidOptionException(
"The folder set to 'config_path' ({$this->get('config_path')}) is not writable. Fix this or change the config_path value in the file {$this->get('console_config_file')}."
);
}
elseif (!file_exists($this->config['config_path'] . '/provision')) {
mkdir($this->config['config_path'] . '/provision');
elseif (!file_exists($this->get('config_path') . '/provision')) {
mkdir($this->get('config_path') . '/provision');
}
// Ensure that script_user is the user.
$real_script_user = $this->getScriptUser();
if ($this->config['script_user'] != $real_script_user) {
throw new \Exception(
"There is an error with your configuration. The user set as 'script_user' ({$this->config['script_user']}) is not the currently running user ({$real_script_user}). Change to user {$this->config['script_user']} or change the script_user value in the file {$this->config_path}."
if ($this->get('script_user') != $real_script_user) {
throw new InvalidOptionException(
"The user set as 'script_user' ({$this->get('script_user')}) is not the currently running user ({$real_script_user}). Change to user {$this->config->get('script_user')} or change the script_user value in the file {{$this->get('console_config_file')}}."
);
}
}
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$tree_builder = new TreeBuilder();
$root_node = $tree_builder->root('aegir');
$root_node
->children()
->scalarNode('aegir_root')
->defaultValue(Config::getHomeDir())
->end()
->scalarNode('script_user')
->defaultValue(Config::getScriptUser())
->end()
->scalarNode('config_path')
->defaultValue(Config::getHomeDir() . '/config')
->end()
->end();;
return $tree_builder;
}
/**
* Get a config param value.
*
* @param string $key
* Key of the param to get.
*
* @return mixed|null
* Value of the config param, or NULL if not present.
*/
public function get($key, $name = null)
{
if ($name) {
return array_key_exists(
$name,
$this->config[$key]
) ? $this->config[$key][$name] : null;
} else {
return $this->has($key) ? $this->config[$key] : null;
}
}
/**
* Check if config param is present.
*
* @param string $key
* Key of the param to check.
*
* @return bool
* TRUE if key exists.
*/
public function has($key)
{
return array_key_exists($key, $this->config);
}
/**
* Set a config param value.
*
* @param string $key
* Key of the param to get.
* @param mixed $val
* Value of the param to set.
* Get the name of the source for this configuration object.
*
* @return bool
* @return string
*/
public function set($key, $val)
public function getSourceName()
{
return $this->config[$key] = $val;
return 'Default';
}
/**
* Get all config values.
* Returns location of PHP with which to run Terminus
*
* @return array
* All config galues.
* @return string
*/
public function all()
protected function getPhpBinary()
{
return $this->config;
return defined('PHP_BINARY') ? PHP_BINARY : 'php';
}
/**
* Add a config param value to a config array.
* Finds and returns the root directory of Provision
*
* @param string $key
* Key of the group to set to.
* @param string|array $names
* Name of the new object to set.
* @param mixed $val
* Value of the new object to set.
*
* @return bool
* @param string $current_dir Directory to start searching at
* @return string
* @throws \Exception
*/
public function add($key, $names, $val)
protected function getProvisionRoot($current_dir = null)
{
if (is_array($names)) {
$array_piece = &$this->config[$key];
foreach ($names as $name_key) {
$array_piece = &$array_piece[$name_key];
}
return $array_piece = $val;
} else {
return $this->config[$key][$names] = $val;
if (is_null($current_dir)) {
$current_dir = dirname(__DIR__);
}
}
/**
* Remove a config param from a config array.
*
* @param $key
* @param $name
*
* @return bool
*/
public function remove($key, $name)
{
if (isset($this->config[$key][$name])) {
unset($this->config[$key][$name]);
return true;
} else {
return false;
if (file_exists($current_dir . DIRECTORY_SEPARATOR . 'composer.json')) {
return $current_dir;
}
$dir = explode(DIRECTORY_SEPARATOR, $current_dir);
array_pop($dir);
if (empty($dir)) {
throw new \Exception('Could not locate root to set PROVISION_ROOT.');
}
$dir = implode(DIRECTORY_SEPARATOR, $dir);
$root_dir = $this->getProvisionRoot($dir);
return $root_dir;
}
/**
* Saves the config class to file.
* Finds and returns the name of the script running Terminus functions
*
* @return bool
* @return string
*/
public function save()
protected function getProvisionScript()
{
// Create config folder if it does not exist.
$fs = new Filesystem();
$dumper = new Dumper();
try {
$fs->dumpFile($this->config_path, $dumper->dump($this->config, 10));
return true;
} catch (IOExceptionInterface $e) {
return false;
}
$debug = debug_backtrace();
$script_location = array_pop($debug);
$script_name = str_replace(
$this->getProvisionRoot() . DIRECTORY_SEPARATOR,
'',
$script_location['file']
);
return $script_name;
}
/**
* Determine the user running provision.
*/
static function getScriptUser() {
$real_script_user = posix_getpwuid(posix_geteuid());
return $real_script_user['name'];
}
/**
* Returns the appropriate home directory.
*
* Adapted from Terminus Package Manager by Ed Reel
*
* @author Ed Reel <@uberhacker>
* @url https://github.com/uberhacker/tpm
*
* @return string
*/
static function getHomeDir()
protected function getHomeDir()
{
$home = getenv('HOME');
if (!$home) {
......@@ -278,14 +165,14 @@ class Config implements ConfigurationInterface
$home = getenv('HOMEPATH');
}
}
return $home;
}
/**
* Determine the user running provision.
*/
public function getConfigPath() {
return $this->config_path;
protected function getScriptUser() {
$real_script_user = posix_getpwuid(posix_geteuid());
return $real_script_user['name'];
}
}
<?php
namespace Aegir\Provision\Console;
use BennerInformatics\Spinner\ProcessSpinner;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Console\Output\ConsoleOutput as BaseConsoleOutput;
use Symfony\Component\Process\Process;
/**
* Class Config.
*/
class ConsoleOutput extends BaseConsoleOutput
{
private $firstRun = true;
protected $process;
protected $spinFrames = ['/', '-', '\\', '|'];
protected $spinInterval = 85000;
/**
* Overwrites a previous message to the output.
*
* @param string $message The message
*/
public function erase($lines = 1)
{
if (!$this->firstRun) {
// Move the cursor to the beginning of the line
$this->write("\x0D");
// Erase the line
$this->write("\x1B[2K");
// Erase previous lines
if ($lines > 0) {
$this->write(str_repeat("\x1B[1A\x1B[2K", $lines));
}
}
$this->firstRun = false;
}
/**
* Inspired by https://github.com/BennerInformatics/php-process-spinner/
* @param $cmd
* @param string $start_message
* @param string $end_message
*
* @return bool
*/
public function exec($cmd, $start_message = 'Running command...') {
$spinPos = 0;
$this->process = new Process($cmd);
$this->process->start();
while ($this->process->isRunning()) {
$this->write(" <comment>{$this->spinFrames[$spinPos]} </comment>{$start_message}\r");
$spinPos = ($spinPos + 1) % count($this->spinFrames);
usleep($this->spinInterval);
}
if ($this->process->isSuccessful()) {
// Move the cursor to the beginning of the line
$this->write("\x0D");
// Erase the line
$this->write("\x1B[2K");
return true;
}
else {
throw new Exception("Running command {$cmd} failed: " . $this->process->getErrorOutput());
}
}
}
<?php
namespace Aegir\Provision\Console;
use Aegir\Provision\Provision;
/**
* Class DotEnvConfig
* @package Pantheon\Terminus\Config
*/
class DotEnvConfig extends ProvisionConfig
{
/**
* @var string
*/
protected $file;
/**
* DotEnvConfig constructor.
*/
public function __construct($dir)
{
parent::__construct();
$file = $dir . '/.env';
$this->setSourceName($file);
// Load environment variables from __DIR__/.env
if (file_exists($file)) {
// Remove comments (which start with '#')
$lines = file($file);
$lines = array_filter($lines, function ($line) {
return strpos(trim($line), '#') !== 0;
});
$info = parse_ini_string(implode($lines, "\n"));
$this->fromArray($info);
}
}
}
<?php
namespace Aegir\Provision\Console;
/**
* Class EnvConfig
* @package Aegir\Provision\Console
*/
class EnvConfig extends ProvisionConfig
{
protected $source_name = 'Environment Variable';
/**
* EnvConfig constructor.
*/
public function __construct()
{
parent::__construct();
// Add all of the environment vars that match our constant.
foreach ([$_SERVER, $_ENV] as $super) {
foreach ($super as $key => $val) {
if ($this->keyIsConstant($key)) {
$this->set($key, $val);
}
}
}
}
}