View source
<?php
module_load_include('inc', 'deploy', 'deploy.manager');
module_load_include('inc', 'deploy', 'deploy.core');
module_load_include('inc', 'deploy', 'deploy.deploy');
define('DEPLOY_STATUS_FAILED', 0);
define('DEPLOY_STATUS_STARTED', 1);
define('DEPLOY_STATUS_PROCESSING', 2);
define('DEPLOY_STATUS_DEPLOYED', 3);
define('DEPLOY_STATUS_PUBLISHED', 4);
function deploy_permission() {
return array(
'administer deployments' => array(
'title' => t('Administer deployments'),
'description' => t('Perform administrative tasks around deployments, like plans and endpoints.'),
),
);
}
function deploy_ctools_plugin_type() {
return array(
'authenticators' => array(
'cache' => TRUE,
'use hooks' => TRUE,
'classes' => array(
'handler',
),
),
'services' => array(
'cache' => TRUE,
'use hooks' => TRUE,
'classes' => array(
'handler',
),
),
'aggregators' => array(
'cache' => TRUE,
'use hooks' => TRUE,
'classes' => array(
'handler',
),
),
'processors' => array(
'cache' => TRUE,
'use hooks' => TRUE,
'classes' => array(
'handler',
),
),
);
}
function deploy_get_aggregator_plugins() {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'aggregators');
}
function deploy_get_aggregator_plugin($name) {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'aggregators', $name);
}
function deploy_get_processor_plugins() {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'processors');
}
function deploy_get_processor_plugin($name) {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'processors', $name);
}
function deploy_get_authenticator_plugins() {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'authenticators');
}
function deploy_get_authenticator_plugin($name) {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'authenticators', $name);
}
function deploy_get_service_plugins() {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'services');
}
function deploy_get_service_plugin($name) {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'services', $name);
}
function deploy_plan_create($schema, $item) {
$plan = new DeployPlan();
foreach ($schema['fields'] as $field => $info) {
if (!empty($info['serialize'])) {
$plan->{$field} = unserialize($item->{$field});
}
else {
$plan->{$field} = $item->{$field};
}
}
return $plan;
}
function deploy_plan_load($name) {
$plans = deploy_plan_load_all();
if (isset($plans[$name])) {
drupal_alter("deploy_plan_load", $plans[$name]);
return $plans[$name];
}
return FALSE;
}
function deploy_plan_load_all($args = array()) {
$type = !empty($args) ? 'conditions' : 'all';
ctools_include('export');
$plans = ctools_export_load_object('deploy_plans', $type, $args);
if (isset($plans)) {
foreach ($plans as &$plan) {
$plan
->load();
}
return $plans;
}
}
function deploy_plan_load_all_enabled($args = array()) {
$type = !empty($args) ? 'conditions' : 'all';
ctools_include('export');
$enabled_plans = array();
$all_plans = ctools_export_load_object('deploy_plans', $type, $args);
if (isset($all_plans)) {
foreach ($all_plans as $plan) {
if (!empty($plan->disabled)) {
continue;
}
$plan
->load();
$enabled_plans[$plan->name] = $plan;
}
return $enabled_plans;
}
}
function deploy_plan_get_options($args = array()) {
$plans = deploy_plan_load_all($args);
$options = array();
foreach ($plans as $plan_name => $info) {
$options[$plan_name] = $info->title;
}
return $options;
}
function deploy_plan_get_status($name) {
$query = db_select('deploy_deployments', 'dd');
$query
->join('deploy_log', 'dl', 'dd.lid = dl.lid');
$status = $query
->fields('dl', array(
'status',
))
->condition('dd.plan_name', $name)
->orderBy('dl.timestamp', 'DESC')
->range(0, 1)
->execute()
->fetchField();
return $status;
}
function deploy_endpoint_create($schema, $item) {
$endpoint = new DeployEndpoint();
foreach ($schema['fields'] as $field => $info) {
if (!empty($info['serialize'])) {
$endpoint->{$field} = unserialize($item->{$field});
}
else {
$endpoint->{$field} = $item->{$field};
}
}
return $endpoint;
}
function deploy_endpoint_load($name) {
$endpoints = deploy_endpoint_load_all();
if (isset($endpoints[$name])) {
return $endpoints[$name];
}
return FALSE;
}
function deploy_endpoint_load_all() {
ctools_include('export');
$endpoints = ctools_export_load_object('deploy_endpoints', 'all');
if (isset($endpoints)) {
return $endpoints;
}
}
function deploy_plan_save($data) {
$plan = new DeployPlan();
foreach ($data as $key => $value) {
$plan->{$key} = $value;
}
$returned = ctools_export_crud_save('deploy_plans', $plan);
if (SAVED_NEW !== $returned && SAVED_UPDATED !== $returned) {
throw new DeployPlanException('Failed to create plan.');
}
return $plan;
}
function deploy_entity_dependency_iterator() {
$plugins = array();
$plugins['deploy_iterator'] = array(
'title' => t('Deploy Iterator'),
'description' => t('Default iterator for Deploy to return UUID entities and invoke Deploy-specific hooks.'),
'handler' => 'DeployIterator',
'file' => 'includes/DeployIterator.inc',
);
return $plugins;
}
function deploy_iterator($entities, $plan = NULL) {
if (is_null($plan) || empty($plan->dependency_plugin)) {
$iterator = new DeployIterator($entities);
return new EntityDependencyIteratorIterator($iterator);
}
elseif (!empty($plan->dependency_plugin)) {
$class_name = ctools_plugin_load_class('entity_dependency', 'iterator', $plan->dependency_plugin, 'handler');
$iterator = new $class_name($entities);
return new EntityDependencyIteratorIterator($iterator);
}
}
function deploy_get_operation_info($event_name = NULL) {
$cache =& drupal_static(__FUNCTION__);
if (empty($cache)) {
$operations = module_invoke_all('deploy_operation_info');
foreach (array(
'preprocess',
'postprocess',
) as $event) {
if (!isset($operations[$event])) {
$operations[$event] = array();
}
}
$cache = $operations;
}
if (!empty($event_name)) {
return $cache[$event_name];
}
return $cache;
}
function deploy_cron_queue_info() {
return array(
'deploy_deploy' => array(
'worker callback' => 'deploy_queue_worker_deploy',
'time' => 60,
),
'deploy_publish' => array(
'worker callback' => 'deploy_queue_worker_publish',
'time' => 60,
),
);
}
function deploy_queue_worker_deploy($entity, &$context = NULL) {
$endpoint = deploy_endpoint_load($entity->__metadata['endpoint_name']);
$plan = deploy_plan_load($entity->__metadata['plan_name']);
if ($plan && $endpoint) {
$entities = array(
array(
'type' => $entity->__metadata['type'],
'id' => $entity->__metadata['id'],
),
);
$iterator = deploy_iterator($entities, $plan);
$endpoint
->deploy($entity->__metadata['deployment_key'], $iterator, $entity->__metadata['lock_name']);
}
}
function deploy_queue_worker_publish($entity, &$context = NULL) {
$endpoint = deploy_endpoint_load($entity->__metadata['endpoint_name']);
$plan = deploy_plan_load($entity->__metadata['plan_name']);
if ($plan && $endpoint) {
$entities = array(
array(
'type' => $entity->__metadata['type'],
'id' => $entity->__metadata['id'],
),
);
$iterator = deploy_iterator($entities, $plan);
$endpoint
->publish($entity->__metadata['deployment_key'], $iterator, $entity->__metadata['lock_name']);
$context['results'][$entity->__metadata['endpoint_name']] = $entity->__metadata['plan_name'];
}
}
function deploy_batch_finished_operation($success, $results, $operations) {
if ($success) {
foreach ($results as $endpoint_name => $plan_name) {
$endpoint = deploy_endpoint_load($endpoint_name);
$plan = deploy_plan_load($plan_name);
drupal_set_message(t('Plan %plan has been deployed and published to %endpoint.', array(
'%plan' => $plan->title,
'%endpoint' => $endpoint->title,
)));
}
}
}
function deploy_status_info($status = NULL, $key = NULL) {
$info = array(
DEPLOY_STATUS_FAILED => array(
'title' => t('Failed'),
'keyed message' => 'Deployment %key failed.',
'watchdog' => WATCHDOG_ERROR,
'class' => 'error',
),
DEPLOY_STATUS_STARTED => array(
'title' => t('Started'),
'keyed message' => 'Deployment of %key started.',
'watchdog' => WATCHDOG_INFO,
'class' => 'warning',
),
DEPLOY_STATUS_PROCESSING => array(
'title' => t('Processing'),
'keyed message' => 'Deployment %key is processing.',
'watchdog' => WATCHDOG_INFO,
'class' => 'warning',
),
DEPLOY_STATUS_DEPLOYED => array(
'title' => t('Deployed'),
'watchdog' => WATCHDOG_INFO,
'keyed message' => 'Deployment %key was deployed.',
'class' => 'warning',
),
DEPLOY_STATUS_PUBLISHED => array(
'title' => t('Published'),
'keyed message' => 'Deployment %key was published.',
'watchdog' => WATCHDOG_INFO,
'class' => 'status',
),
);
if ($status === NULL && $key === NULL) {
return $info;
}
elseif ($status !== NULL && $status !== FALSE && $key === NULL) {
if (isset($info[$status])) {
return $info[$status];
}
}
elseif ($status !== NULL && $status !== FALSE && $key !== NULL && $key !== FALSE) {
if (isset($info[$status][$key])) {
return $info[$status][$key];
}
}
return FALSE;
}
function deploy_log($key, $status, $message = '', $variables = array(), $timestamp = NULL) {
$info = deploy_status_info($status);
if ($status == DEPLOY_STATUS_FAILED) {
watchdog_exception('deploy', $message);
}
else {
watchdog('deploy', $info['keyed message'], array(
'%key' => $key,
), $info['watchdog']);
}
if ($timestamp === NULL) {
$timestamp = time();
}
if ($key && !uuid_is_valid($key)) {
$plan_name = $key;
$key = uuid_generate();
db_insert('deploy_deployments')
->fields(array(
'plan_name' => $plan_name,
'uuid' => $key,
))
->execute();
}
if (uuid_is_valid($key)) {
$lid = db_insert('deploy_log')
->fields(array(
'uuid' => $key,
'status' => $status,
'message' => $message,
'timestamp' => $timestamp,
))
->execute();
db_update('deploy_deployments')
->fields(array(
'lid' => $lid,
))
->condition('uuid', $key)
->execute();
return $key;
}
return FALSE;
}