Commit 2e391279 authored by Adrian Rossouw's avatar Adrian Rossouw

Introduce the example service type with the basic implementation for...

Introduce the example service type with the basic implementation for provision. This matches the front end example and is fully working.
parent f7f88620
This is an example template for the example service in provision.
The value set in the UI for this service was : <?php print $this->example_field; ?>
<?php
// $Id$
/**
* @file
* A 'basic' implementation of the 'example' service type.
*/
/**
* A class containing the 'basic' implementation of the 'example' service.
*
* This class is conditionally loaded when the "--example_service_type=basic"
* option is passed to provision-save commands run on servers.
*
* The above flag is generated by the hosting counterpart of this class, which
* provides the front end to configure all these fields.
*
* The responsibilities of this class include responding and saving any
* values that are passed to it, and also to override the portions of
* the public API for this service that are necessary.
*/
class provisionService_example_basic extends provisionService_example {
/**
* Some common options handled upstream by the base service classes.
*/
/**
* this service needs to have a port specified for it.
*/
public $has_port = TRUE;
/**
* the default value for the port input.
*/
function default_port() {
return 12345;
}
/**
* this service needs to be restarted with a shell command.
*/
public $has_restart_cmd = TRUE;
/**
* The default value for the restart command input.
*/
function default_restart_cmd() {
return "/usr/bin/true";
}
/**
* Initialize this class, including option handling.
*/
function init() {
// REMEMBER TO CALL THE PARENT!
parent::init();
/**
* Setting and storing a value.
*
* You will most commonly use :
* $this->server->setProperty('example_field', 'default');
*
* This helper will check for an existing saved value, overridden
* by a command line option falling back to the default.
*
* This is the format used by everything you want configurable from
* the front end or command line.
*
* These values will be saved in ~/.drush/server_name.drush.alias.inc.
*/
$this->server->setProperty('example_field', 'default');
/**
* Non configurable values.
*
* If you want to generate values for use in your code or templates,
* but don't want them to be overriden you would use the following format.
*
* $this->server->example_config_path = $this->server->config_path . '/example.d'
*
* This will mean the value will change if you change the config path, but
* you dont need to pass the right input to your command to get there,
* and it's impossible to change the values.
*/
$this->server->example_config_path = $this->server->config_path . '/example.d';
}
/**
* Implementation of service verify.
*/
/**
* Public API implementation.
*
* We will just implement part of the API to show what is needed.
*/
/**
* Generate a server level config file.
*/
function create_server_config() {
// Create the configuration file directory.
provision_file()->create_dir($this->server->example_config_path, dt("Example configuration"), 0700);
// Sync the directory to the remote server if needed.
$this->sync($this->server->example_config_path);
// Generate the configuration file.
$config = new provisionConfig_example($this->context, array('extra' => 'fields'));
$config->write();
// Sync the config to a remote server if necessary.
$this->sync($config->filename());
}
/**
* Delete the server level config file.
*/
function delete_server_config() {
$config = new provisionConfig_example($this->context, array('extra' => 'fields'));
$config->unlink();
// Sync the config to a remote server if necessary.
$this->sync($config->filename());
}
}
/**
* A basic configuration file class.
*
* Just a file containing the value passed to us.
*/
class provisionConfig_example extends provisionConfig {
/**
* Template file to load. In the same directory as this class definition.
*/
public $template = 'basic.tpl.php';
/**
* Where the file generated will end up.
*
* It is extremely important that this path is only made up of information
* relative to this class, and not use drush_get_option or the d() accessor.
*/
function filename() {
return $this->example_config_path . '/example.conf';
}
}
<?php
// $Id$
/**
* @file
* An example of the provision service api.
*
* Declares a new service type and a basic implementation of it.
* It matches the same service definition in the hosting front end.
*/
include_once(dirname(__FILE__) . '/../provision.service.inc');
/**
* Expose the service type this extension defines to provision.
*
* Implementations are then conditionally loaded when the option
* "--$service_service_type=$type" is passed to the provision-save
* command of a server.
*
* Implementations are automatically loaded from :
* provision/$service/$type/$type_service.inc.
*
* @return
* An array with the service type the key, and the default implementation the value.
*/
function example_provision_services() {
return array('example' => NULL);
}
/**
* The service type base class.
*
* All implementations of the service type will inherit this class.
* This class should define the 'public API' to be used by the rest
* of the system, which should not expose implementation details.
*/
class provisionService_example extends provisionService {
public $service = 'http';
/**
* Initialize the service along with the server object.
*/
function init() {
// REMEMBER TO CALL THE PARENT!
parent::init();
/**
* We do not need to use this in our example.
*
* You would extend this if you needed to save values
* for all possible implementation of this service type.
*/
}
/**
* Called on provision-verify.
*
* We change what we will do based on what the
* type of object the command is being run against.
*/
function verify() {
switch (d()->type) {
case 'server':
$this->create_server_config();
break;
case 'platform':
$this->create_platform_config();
break;
case 'site':
$this->create_site_config();
break;
}
$this->parse_configs();
}
/**
* PUBLIC API!
*
* These are just a basic example, the thing to notice here is that these
* methods are just stubs for later implementations to extend.
*
* If a specific implementation doesn't need to parse the configuration files
* that are generated for instance, it can just not implement the stub.
*/
/**
* Commonly something like running the restart_cmd or sending SIGHUP to a process.
*/
function parse_configs() {
return TRUE;
}
/**
* Generate a site specific configuration file
*/
function create_site_config() {
return TRUE;
}
/**
* Remove an existing site configuration file.
*/
function delete_site_config() {
return TRUE;
}
/**
* Add a new platform specific configuration file.
*/
function create_platform_config() {
return TRUE;
}
/**
* Remove an existing platform configuration file.
*/
function delete_platform_config() {
return TRUE;
}
/**
* Create a new server specific configuration file.
*/
function create_server_config() {
return TRUE;
}
/**
* Remove an existing server specific configuration file
*/
function delete_server_config() {
return TRUE;
}
}
<?php
// $Id$
/**
* @file
* Drush invoke API hooks for the 'provision-install' command.
*
* This file contains an example of how the developer is intended to
* interact with the services defined by the hosting-service API.
*/
/**
* Implementation of drush_hook_provision_install.
*
* Create the site specific configuration for this service.
*
* In our basic implementation we do not have an implementation
* of this API method, because it wasn't necessary.
*
* If the service implementationd doesn't define this method,
* or no implementation has been selected, nothing will happen.
*/
function drush_example_provision_install() {
/**
* Using the d() accessor.
*
* Every object that aegir manages (namely servers, platforms and sites),
* has an associated 'named context' that we manage for it.
*
* You can run the provision commands on different objects, simply
* by specifying the context name before the drush command, such as:
*
* drush @server_master provision-verify
*
* When you have called a command in this way, you can use the d()
* function without any arguments to retrieve the object representing
* the current context.
*/
if (d()->type == 'site') {
// only run this code on site objects.
/**
* Calling service methods.
*
* All the provision context objects can register which
* servers handle specific services for them.
*
* To call the correct methods, you just need to use the
* the 'service' method with the service type you want
* to call as it's only argument.
*/
d()->service('example')->create_site_config();
}
}
/**
* Implementation of drush_hook_provision_install_rollback.
*
* All drush invoke hooks allow you to recover if drush_set_error()
* was set in the previously called hooks.
*
* By defining a _rollback variant, you can cleanly handle cases where
* issues occured, in this case deleting the config file created in the
* previous step.
*/
function drush_example_provision_install_rollback() {
d()->service('example')->delete_site_config();
}
/**
* Implementation of drush_hook_post_provision_install.
*
* We can now perform such operations as restarting the server,
* by calling the public api of the service we have created.
*/
function drush_example_post_provision_install() {
d()->service('example')->parse_configs();
}
......@@ -87,7 +87,6 @@ class provisionService_http_apache extends provisionService_http {
$config = new provisionConfig_apache_server($this->context, $this->config_data());
$config->write();
dlm($config->filename());
// Create a symlink to the site specific apache config in the top level config directory of each server.
// This way you can still include the same file across all servers.
......
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