Skip to content
Commits on Source (9)
......@@ -73,6 +73,17 @@ class Application extends BaseApplication
parent::__construct($name, $version);
}
/**
* Make configureIO public so we can run it before ->run()
*
* @param InputInterface $input
* @param OutputInterface $output
*/
public function configureIO(InputInterface $input, OutputInterface $output)
{
parent::configureIO($input, $output);
}
/**
* Initializes all the default commands.
*/
......
......@@ -153,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 = Provision::checkServiceRequirements($context_type);
$reqs = $this->getProvision()->checkServiceRequirements($context_type);
foreach ($reqs as $service => $available) {
if ($available) {
$this->io->successLite("Service $service: Available");
......
......@@ -80,7 +80,7 @@ class VerifyCommand extends Command
}
*/
$message = $this->context->verify();
$message = $this->context->verifyCommand();
}
}
<?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();
}
}
......@@ -476,32 +476,33 @@ class Context implements BuilderAwareInterface
*
* If this context is a Service Subscriber, the provider service will be verified first.
*/
public function verify() {
public function verifyCommand() {
$return_codes = [];
// Run verify method on all services.
foreach ($this->getServices() as $type => $service) {
$friendlyName = $service->getFriendlyName();
if ($this->isProvider()) {
$this->getProvision()->io()->section("Verify service: {$friendlyName}");
foreach ($service->verify() as $type => $verify_success) {
$return_codes[] = $verify_success? 0: 1;
}
}
else {
$this->getProvision()->io()->section("Verify service: {$friendlyName} on {$service->provider->name}");
// First verify the service provider.
foreach ($service->verifyProvider() as $verify_part => $verify_success) {
$return_codes[] = $verify_success? 0: 1;
}
// Then run "verify" on the subscriptions.
foreach ($this->getSubscription($type)->verify() as $type => $verify_success) {
$return_codes[] = $verify_success? 0: 1;
}
}
$return_codes[] = $service->verify() ? 0 : 1;
}
//
// if ($this->isProvider()) {
// $this->getProvision()->io()->section("Verify service: {$friendlyName}");
// foreach ($service->verify() as $type => $verify_success) {
// $return_codes[] = $verify_success? 0: 1;
// }
// }
// else {
// $this->getProvision()->io()->section("Verify service: {$friendlyName} on {$service->provider->name}");
//
// // First verify the service provider.
// foreach ($service->verifyProvider() as $verify_part => $verify_success) {
// $return_codes[] = $verify_success? 0: 1;
// }
//
// // Then run "verify" on the subscriptions.
// foreach ($this->getSubscription($type)->verify() as $type => $verify_success) {
// $return_codes[] = $verify_success? 0: 1;
// }
// }
// }
// If any service verify failed, exit with a non-zero code.
if (count(array_filter($return_codes))) {
......
......@@ -89,16 +89,18 @@ class Provision implements ConfigAwareInterface, ContainerAwareInterface, Logger
$this
->setConfig($config)
->setInput($input)
->setOutput($output)
;
// Create Application.
$application = new Application(self::APPLICATION_NAME, self::VERSION);
$application
->setProvision($this)
->setLogger($logger);
->setLogger($logger)
;
$application->configureIO($input, $output);
$this->setInput($input);
$this->setOutput($output);
// Create and configure container.
$container = Robo::createDefaultContainer($input, $output, $application, $config);
$this->setContainer($container);
......@@ -158,7 +160,7 @@ class Provision implements ConfigAwareInterface, ContainerAwareInterface, Logger
public function run(InputInterface $input, OutputInterface $output) {
$status_code = $this->runner->run($input, $output);
return $status_code;
}
......@@ -238,7 +240,7 @@ class Provision implements ConfigAwareInterface, ContainerAwareInterface, Logger
/**
* Return all available contexts.
*
* @return array
* @return array|Context
*/
public function getAllContexts($name = '') {
if ($name && isset($this->contexts[$name])) {
......@@ -259,9 +261,9 @@ class Provision implements ConfigAwareInterface, ContainerAwareInterface, Logger
* @return mixed
* @throws \Exception
*/
static public function getAllServers($service = NULL) {
protected function getAllServers($service = NULL) {
$servers = [];
$context_files = self::getAllContexts();
$context_files = $this->getAllContexts();
if (empty($context_files)) {
throw new \Exception('No server contexts found. Use `provision save` to create one.');
}
......@@ -356,7 +358,7 @@ class Provision implements ConfigAwareInterface, ContainerAwareInterface, Logger
* @param $type
* @return array
*/
static function checkServiceRequirements($type) {
public function checkServiceRequirements($type) {
$class_name = Context::getClassName($type);
// @var $context Context
......@@ -365,7 +367,7 @@ class Provision implements ConfigAwareInterface, ContainerAwareInterface, Logger
$services = [];
foreach ($service_requirements as $service) {
try {
if (empty(Provision::getAllServers($service))) {
if (empty($this->getAllServers($service))) {
$services[$service] = 0;
}
else {
......
......@@ -8,17 +8,20 @@
namespace Aegir\Provision;
use Aegir\Provision\Common\ContextAwareTrait;
use Aegir\Provision\Common\ProvisionAwareTrait;
use Psr\Log\LoggerAwareTrait;
use Robo\Common\BuilderAwareTrait;
use Robo\Common\OutputAdapter;
use Robo\Contract\BuilderAwareInterface;
class Service implements BuilderAwareInterface
{
use BuilderAwareTrait;
use ProvisionAwareTrait;
use ContextAwareTrait;
use LoggerAwareTrait;
public $type;
public $properties;
......@@ -46,7 +49,9 @@ class Service implements BuilderAwareInterface
function __construct($service_config, Context $provider_context)
{
$this->provider = $provider_context;
$this->setContext($provider_context);
$this->setProvision($provider_context->getProvision());
$this->setLogger($provider_context->getProvision()->getLogger());
$this->type = $service_config['type'];
$this->properties = $service_config['properties'];
......@@ -77,30 +82,54 @@ class Service implements BuilderAwareInterface
return "\Aegir\Provision\Service\\{$service}Service";
}
}
/**
* React to the `provision verify` command.
*/
function verify()
{
return [
'configuration' => $this->writeConfigurations(),
'service' => $this->restartService(),
];
}
/**
* React to `provision verify` command when run on a subscriber, to verify the service's provider.
*
* This is used to allow skipping of the service restart.
* React to the verify command. Passes off to the method verifySite, verifyServer, verifyPlatform.
* @return mixed
*/
function verifyProvider()
{
return [
'configuration' => $this->writeConfigurations(),
];
public function verify() {
$method = 'verify' . ucfirst($this->getContext()->type);
$this->getProvision()->getLogger()->info("Running {method}", [
'method' => $method
]);
return $this::$method();
}
//
// /**
// * React to the `provision verify` command.
// */
// function verifySite()
// {
// return [
// 'configuration' => $this->writeConfigurations(),
// 'service' => $this->restartService(),
// ];
// }
//
// /**
// * React to the `provision verify` command.
// */
// function verifyPlatform()
// {
// return [
// 'configuration' => $this->writeConfigurations(),
// 'service' => $this->restartService(),
// ];
// }
//
// /**
// * React to `provision verify` command when run on a subscriber, to verify the service's provider.
// *
// * This is used to allow skipping of the service restart.
// */
// function verifyServer()
// {
// return [
// 'configuration' => $this->writeConfigurations(),
// ];
// }
/**
* Run the services "restart_command".
* @return bool
......
......@@ -12,6 +12,7 @@ namespace Aegir\Provision\Service;
use Aegir\Provision\Context\SiteContext;
use Aegir\Provision\Service;
use Aegir\Provision\ServiceInterface;
use Aegir\Provision\ServiceSubscription;
use Symfony\Component\Console\Output\OutputInterface;
......@@ -20,7 +21,7 @@ use Symfony\Component\Console\Output\OutputInterface;
*
* @package Aegir\Provision\Service
*/
class DbService extends Service
class DbService extends Service implements ServiceInterface
{
const SERVICE = 'db';
......@@ -89,7 +90,7 @@ class DbService extends Service
/**
* React to the `provision verify` command on Server contexts
*/
function verify() {
function verifyServer() {
$this->creds = array_map('urldecode', parse_url($this->properties['master_db']));
if (!isset($this->creds['port'])) {
......@@ -135,11 +136,11 @@ class DbService extends Service
/**
* React to the `provision verify` command on subscriber contexts (sites and platforms)
*/
function verifySubscription(ServiceSubscription $serviceSubscription) {
$this->subscription = $serviceSubscription;
function verifySite() {
$this->subscription = $this->getContext()->getSubscription($this->type);
// Check for database
$this->create_site_database($serviceSubscription->context);
$this->create_site_database($this->getContext());
$this->creds_root = array_map('urldecode', parse_url($this->properties['master_db']));
......@@ -169,6 +170,10 @@ class DbService extends Service
}
}
public function verifyPlatform() {
}
/**
* React to `provision verify` command when run on a subscriber, to verify the service's provider.
*
......
<?php
/**
* @file
* The Provision HttpApacheService class.
*
* @see \Provision_Service_http_apache
*/
namespace Aegir\Provision\Service\Http;
use Aegir\Provision\Robo\ProvisionCollectionBuilder;
use Aegir\Provision\Service\Http\Apache\Configuration\PlatformConfiguration;
use Aegir\Provision\Service\Http\Apache\Configuration\ServerConfiguration;
use Aegir\Provision\Service\Http\Apache\Configuration\SiteConfiguration;
use Aegir\Provision\Service\HttpService;
use Aegir\Provision\ServiceSubscription;
use Robo\Result;
use Aegir\Provision\Service\Http\Php\PhpServer;
use Symfony\Component\Process\Process;
/**
* Class HttpPhpService
*
* @package Aegir\Provision\Service\Http
*/
class HttpPhpService extends HttpService
{
const SERVICE_TYPE = 'php';
const SERVICE_TYPE_NAME = 'PHP Server';
public function verifyServer()
{
$host = $this->getContext()->getProperty('remote_host');
$port = $this->getProperty('http_port');
$this->getProvision()->getLogger()->info('Running server at {host}:{port}', [
'host' => $host,
'port' => $port,
]);
//
$this->getContext()->getBuilder()->build(PhpServer::class, ['port' => $port]);
// $server = new PhpServer($port);
/** @var PhpServer $server */
$server = $this->getContext()->getBuilder()->task(PhpServer::class, ['port' => $port]);
$server->host($host);
$server->dir(__DIR__ . '/Php/servertest');
$server->background();
$server->run();
//
$pid = $server->getProcess()->getPid();
$this->getProvision()->getLogger()->info('Server started at {host}:{port} running as process {pid}', [
'host' => $host,
'port' => $port,
'pid' => $pid,
]);
}
public function verifySite()
{
}
public function verifySubscription(ServiceSubscription $subscription)
{
print_r($subscription->context->getProperty('root'));
$this->application->getBuilder()->taskServer($this->getProperty('http_port'))
->dir('.');;
}
}
<?php
namespace Aegir\Provision\Service\Http\Php;
use Robo\Common\ExecCommand;
use Robo\Common\ExecTrait;
class PhpServer extends \Robo\Task\Development\PhpServer {
/**
* @var int
*/
protected $port;
/**
* @var string
*/
protected $host = '127.0.0.1';
/**
* @var string
*/
protected $command = 'php -S %s:%d ';
/**
* @param int $port
*/
public function __construct($port)
{
$this->port = $port;
if (strtolower(PHP_OS) === 'linux') {
$this->command = 'exec php -S %s:%d ';
}
}
/**
* @param string $host
*
* @return $this
*/
public function host($host)
{
$this->host = $host;
return $this;
}
/**
* @param string $path
*
* @return $this
*/
public function dir($path)
{
$this->command .= "-t $path";
return $this;
}
/**
* {@inheritdoc}
*/
public function getCommand()
{
return sprintf($this->command, $this->host, $this->port);
}
public function run() {
$this->executeCommand($this->getCommand());
}
public function getProcess() {
return $this->process;
}
public function task() {
}
}
\ No newline at end of file
<h1><?php print "It Works!"; ?></h1>
<p>
<?php print "This page is being generated by PHP web server." ?>
</p>
......@@ -11,6 +11,7 @@ namespace Aegir\Provision\Service;
//require_once DRUSH_BASE_PATH . '/commands/core/rsync.core.inc';
use Aegir\Provision\Service;
use Aegir\Provision\ServiceInterface;
use Aegir\Provision\ServiceSubscription;
use Consolidation\AnnotatedCommand\CommandFileDiscovery;
......@@ -19,7 +20,7 @@ use Consolidation\AnnotatedCommand\CommandFileDiscovery;
*
* @package Aegir\Provision\Service
*/
class HttpService extends Service {
class HttpService extends Service implements ServiceInterface {
const SERVICE = 'http';
const SERVICE_NAME = 'Web Server';
......@@ -57,7 +58,7 @@ class HttpService extends Service {
*
* This is used to allow skipping of the service restart.
*/
function verifyProvider()
function verifyServer()
{
return [
'configuration' => $this->writeConfigurations(),
......@@ -67,14 +68,18 @@ class HttpService extends Service {
/**
* React to the `provision verify` command on Server contexts
*/
function verifySubscription(ServiceSubscription $serviceSubscription) {
$this->subscription = $serviceSubscription;
function verifySite() {
$this->subscription = $this->getContext()->getSubscription();
return [
'configuration' => $this->writeConfigurations($serviceSubscription),
'configuration' => $this->writeConfigurations($this->subscription),
'service' => $this->restartService(),
];
}
//
function verifyPlatform() {
}
//
// /**
// * Support the ability to cloak the database credentials using environment variables.
// */
......
<?php
namespace Aegir\Provision;
interface ServiceInterface
{
/**
* Triggered on `provision verify` command on Site contexts.
*/
public function verifySite();
/**
* Triggered on `provision verify` command on Platform contexts.
*/
public function verifyPlatform();
/**
* Triggered on `provision verify` command on Site contexts.
*/
public function verifyServer();
}
\ No newline at end of file
......@@ -9,6 +9,7 @@
namespace Aegir\Provision;
use Aegir\Provision\Common\ContextAwareTrait;
use Aegir\Provision\Common\ProvisionAwareTrait;
class ServiceSubscription {
......@@ -20,13 +21,14 @@ class ServiceSubscription {
public $properties = [];
use ProvisionAwareTrait;
use ContextAwareTrait;
function __construct(
Context $context,
$server,
$service_name
) {
$this->context = $context;
$this->setContext($context);
$this->setProvision($context->getProvision());
$this->server = $this->getProvision()->getContext($server);
......@@ -36,7 +38,6 @@ class ServiceSubscription {
if (isset($context->config['service_subscriptions'][$service_name]['properties'])) {
$this->properties = $context->config['service_subscriptions'][$service_name]['properties'];
}
}
public function verify() {
......