diff --git a/src/Command/ServicesCommand.php b/src/Command/ServicesCommand.php index 89baac15f2cefdb92f214301ff29e4b5ecd2d5ef..2e343cdfb34de95cda0e475707fad24f6f934b7b 100644 --- a/src/Command/ServicesCommand.php +++ b/src/Command/ServicesCommand.php @@ -81,9 +81,9 @@ class ServicesCommand extends Command $input, $output ); - if (isset($this->context->type) && $this->context->type != 'server') { - throw new \Exception('Context must be a server.'); - } +// if (isset($this->context->type) && $this->context->type != 'server') { +// throw new \Exception('Context must be a server.'); +// } $this->sub_command = $input->getArgument('sub_command'); } @@ -120,19 +120,42 @@ class ServicesCommand extends Command $this->io->comment("Add Services"); $service = $this->io->choice('Which service?', $this->context->getServiceOptions()); - // Then ask which service type - $service_type = $this->io->choice('Which service type?', $this->context->getServiceTypeOptions($service)); - // Then ask for all options. - $properties = $this->askForServiceProperties($service); + // If server, ask which service type. + if ($this->context->type == 'server') { + $service_type = $this->io->choice('Which service type?', $this->context->getServiceTypeOptions($service)); - $this->io->info("Adding $service service $service_type..."); + // Then ask for all options. + $properties = $this->askForServiceProperties($service); - try { - $this->context->config['services'][$service] = [ + $this->io->info("Adding $service service $service_type..."); + + $services_key = 'services'; + $service_info = [ 'type' => $service_type, - 'properties' => $properties, ]; + } + // All other context types are associating with servers that provide the service. + else { + $server = $this->io->choice('Which server?', $this->getApplication()->getServerOptions($service)); + + // Then ask for all options. + $server_context = $this->getApplication()->getContext($server); + $properties = $this->askForServiceProperties($service); + + $this->io->info("Using $service service from server $server..."); + + $services_key = 'service_subscriptions'; + $service_info = [ + 'server' => $server, + ]; + } + + try { + $this->context->config[$services_key][$service] = $service_info; + if (!empty($properties)) { + $this->context->config[$services_key][$service]['properties'] = $properties; + } $this->context->save(); $this->io->success('Service saved to Context!'); } @@ -149,8 +172,9 @@ class ServicesCommand extends Command private function askForServiceProperties($service) { $class = $this->context->getAvailableServices($service); + $method = "{$this->context->type}_options"; - $options = $class::option_documentation(); + $options = $class::{$method}(); $properties = []; foreach ($options as $name => $description) { // If option does not exist, ask for it. diff --git a/src/Context.php b/src/Context.php index 2257312bb1b4f09cd72bf44c8f0852d8e599f523..ea9b954958a9f1713099952382277a8ec9d7b791 100644 --- a/src/Context.php +++ b/src/Context.php @@ -143,7 +143,7 @@ class Context * * @return array */ - public function getAvailableServices($service = NULL) { + public function getAvailableServices($service = '') { // Load all service classes $classes = []; @@ -246,7 +246,7 @@ class Context public function getConfigTreeBuilder() { $tree_builder = new TreeBuilder(); - $root_node = $tree_builder->root('server'); + $root_node = $tree_builder->root($this->type); $root_node ->children() ->scalarNode('name') @@ -255,15 +255,24 @@ class Context ->end(); // Load Services + if ($this->type == 'server') { + $services_key = 'services'; + $services_property = 'type'; + } + else { + $services_key = 'service_subscriptions'; + $services_property = 'server'; + } + $root_node ->children() - ->arrayNode('services') + ->arrayNode($services_key) ->prototype('array') ->children() - ->scalarNode('type') + ->scalarNode($services_property) ->isRequired(true) ->end() - ->append($this->addServiceProperties()) + ->append($this->addServiceProperties($services_key)) ->end() ->end(); @@ -287,18 +296,28 @@ class Context /** * Append Service class options_documentation to config tree. */ - public function addServiceProperties() + public function addServiceProperties($property_name = 'services') { $builder = new TreeBuilder(); $node = $builder->root('properties'); // Load config tree from Service type classes - if (!empty($this->getProperty('services')) && !empty($this->getProperty('services'))) { - foreach ($this->getProperty('services') as $service => $info) { + if (!empty($this->getProperty($property_name)) && !empty($this->getProperty($property_name))) { + foreach ($this->getProperty($property_name) as $service => $info) { + + // If type is empty, it's because it's in the ServerContext + if (empty($info['type'])) { + $server = $this->application->getContext($info['server']); + $service_type = ucfirst($server->getService($service)->type); + } + else { + $service_type = ucfirst($info['type']); + } $service = ucfirst($service); - $service_type = ucfirst($info['type']); $class = "\Aegir\Provision\Service\\{$service}\\{$service}{$service_type}Service"; - foreach ($class::option_documentation() as $name => $description) { + $method = "{$this->type}_options"; + + foreach ($class::{$method}() as $name => $description) { $node ->children() ->scalarNode($name)->end() diff --git a/src/Service.php b/src/Service.php index 46aba4d09c436f36962f4d2aa4dc3bfacc1873c0..4ca329690fcf20f4849ad880d0f7f1de190cb5ce 100644 --- a/src/Service.php +++ b/src/Service.php @@ -69,28 +69,31 @@ class Service { * Return a list of user configurable options that this service provides to Server Context objects. */ static function server_options() { - return [ - 'http_port' => 'The port which the web service is running on.', - 'web_group' => 'server with http: OS group for permissions; working default will be attempted', - ]; + return []; +// return [ +// 'http_port' => 'The port which the web service is running on.', +// 'web_group' => 'server with http: OS group for permissions; working default will be attempted', +// ]; } /** * Return a list of user configurable options that this service provides to Platform Context objects. */ static function platform_options() { - return [ - 'platform_extra_config' => 'Extra lines of configuration to add to this platform.', - ]; + return []; +// return [ +// 'platform_extra_config' => 'Extra lines of configuration to add to this platform.', +// ]; } /** * Return a list of user configurable options that this service provides to Site Context objects. */ static function site_options() { - return [ - 'site_mail' => 'The email address to use for the ServerAdmin configuration.', - ]; + return []; +// return [ +// 'site_mail' => 'The email address to use for the ServerAdmin configuration.', +// ]; } /** @@ -429,16 +432,6 @@ class Service { // return TRUE; // } - /** - * Return service-specific configuration options for help. - * - * @return - * array('option' => 'description') - */ - static function option_documentation() { - return array(); - } - /** * Save symlink for this server from /var/aegir/config/APPLICATION_NAME.conf -> /var/aegir/config/SERVER/APPLICATION_NAME.conf * diff --git a/src/Service/DbService.php b/src/Service/DbService.php index 517ac87f6939a57ac0b486726429901c3dd05400..7042a68581163d923bc5a14f31fa607dcc196bf2 100644 --- a/src/Service/DbService.php +++ b/src/Service/DbService.php @@ -24,7 +24,12 @@ class DbService extends Service const SERVICE_NAME = 'Database Server'; - static function option_documentation() + /** + * Implements Service::server_options() + * + * @return array + */ + static function server_options() { return [ 'master_db' => 'server with db: Master database connection info, {type}://{user}:{password}@{host}', @@ -32,6 +37,20 @@ class DbService extends Service ]; } + /** + * Implements Service::server_options() + * + * @return array + */ + static function site_options() + { + return [ + 'db_name' => 'The name of the database. Default: Automatically generated.', + 'db_user' => 'The username used to access the database. Default: Automatically generated.', + 'db_password' => 'The password used to access the database. Default: Automatically generated.', + ]; + } + /** * Register the db handler for sites, based on the db_server option. */ diff --git a/src/Service/HttpService.php b/src/Service/HttpService.php index 79c00cead451b6cfc07732889cf9bfb21d925a3c..0fbdc7a7e5a77fa4210818b1ff426e27c91505bf 100644 --- a/src/Service/HttpService.php +++ b/src/Service/HttpService.php @@ -24,16 +24,21 @@ class HttpService extends Service { protected $ssl_enabled = FALSE; - static function option_documentation() { - return array( + + /** + * Implements Service::server_options() + * + * @return array + */ + static function server_options() + { + return [ 'http_port' => 'The port which the web service is running on.', -// 'http_platformd_path' => 'The path to store platforms.', -// 'http_postd_path' => 'The path to store post configuration.', 'web_group' => 'server with http: OS group for permissions; working default will be attempted', 'web_disable_url' => 'server with http: URL disabled sites are redirected to; default {master_url}/hosting/disabled', 'web_maintenance_url' => 'server with http: URL maintenance sites are redirected to; default {master_url}/hosting/maintenance', 'restart_command' => 'The command to reload the web server configuration;' - ); + ]; } /**