hosting_platform.module in Hosting 6.2
Same filename and directory in other branches
Platform node type definition.
File
platform/hosting_platform.moduleView source
<?php
/**
* @file
* Platform node type definition.
*/
/**
* This platform has been deleted.
*/
define('HOSTING_PLATFORM_DELETED', -2);
/**
* This platform has been locked, new sites will not be allowed to use it.
*/
define('HOSTING_PLATFORM_LOCKED', -1);
/**
* This platform has been queued for creation.
*/
define('HOSTING_PLATFORM_QUEUED', 0);
/**
* This platform is created and enabled, sites may be deployed on it.
*/
define('HOSTING_PLATFORM_ENABLED', 1);
/**
* Implementation of hook_menu().
*/
function hosting_platform_menu() {
$items = array();
$items['hosting/platforms'] = array(
'title' => 'Platforms',
'description' => 'List of platforms',
'page callback' => 'hosting_platform_listing',
'access arguments' => array(
'view platform',
),
'type' => MENU_NORMAL_ITEM,
);
$items['hosting/platforms/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['hosting/platforms/add'] = array(
'title' => 'Add platform',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_goto',
'page arguments' => array(
'node/add/platform',
),
'access callback' => 'node_access',
'access arguments' => array(
'create',
'platform',
),
);
return $items;
}
/**
* Implementation of hook_node_info().
*/
function hosting_platform_node_info() {
#configuration
$types["platform"] = array(
"type" => 'platform',
"name" => 'Platform',
"module" => 'hosting_platform',
"has_title" => TRUE,
"title_label" => t('Name'),
"description" => hosting_node_help("platform"),
"has_body" => 0,
"body_label" => '',
"min_word_count" => 0,
);
return $types;
}
/**
* Implementation of hook_hosting_tasks().
*/
function hosting_platform_hosting_tasks() {
$tasks = array();
$tasks['platform']['verify'] = array(
'title' => t('Verify'),
'description' => t('Verify that the platform is correctly installed and working.'),
'weight' => 10,
'provision_save' => TRUE,
);
$tasks['platform']['delete'] = array(
'title' => t('Delete'),
'description' => t('Deleting this platform will completely remove it from the hosting system.
This process can not be undone. It can not be performed if you have sites currently
running on this platform.
Are you really sure you want to delete this platform?'),
'weight' => 10,
'dialog' => TRUE,
);
$tasks['platform']['lock'] = array(
'title' => t('Lock'),
'description' => t('Locking this platform will not delete or disable it or its sites, but will
prevent any new sites from being created on it. This may be useful when you have sites that
cannot be migrated onto a newer platform, but you wish to prevent other users or clients
from continuing to provision on this platform. The platform can be unlocked later.
Are you really sure you want to lock this platform?'),
'weight' => 10,
);
$tasks['platform']['unlock'] = array(
'title' => t('Unlock'),
'description' => t('Unlocking this platform will allow sites to be provisioned on it.
Are you really sure you want to unlock this platform?'),
'weight' => 10,
);
return $tasks;
}
/**
* Implementation of hook_perm().
*/
function hosting_platform_perm() {
return array(
'administer platforms',
'create platform',
'view platform',
'edit platform',
'delete platform',
'view locked platforms',
'create sites on locked platforms',
);
}
/**
* Implementation of hook_access().
*/
function hosting_platform_access($op, $node, $account) {
if ($op != 'create' && hosting_feature('client')) {
// we rely on hosting_client_node_grants() instead of global configuration
return NULL;
}
switch ($op) {
case 'create':
return user_access('create platform', $account);
break;
case 'update':
return user_access('edit platform', $account);
break;
case 'view':
return user_access('view platform', $account);
break;
}
}
/**
* Implements hook_node_operations().
*/
function hosting_platform_node_operations() {
$operations = array();
$operations['hosting-platform-verify'] = array(
'label' => t('Platform: Verify'),
'callback' => 'hosting_platform_op_verify',
);
$operations['hosting-platform-lock'] = array(
'label' => t('Platform: Lock'),
'callback' => 'hosting_platform_op_lock',
);
$operations['hosting-platform-unlock'] = array(
'label' => t('Platform: Unlock'),
'callback' => 'hosting_platform_op_unlock',
);
$operations['hosting-platform-delete'] = array(
'label' => t('Platform: Delete'),
'callback' => 'hosting_platform_op_delete',
);
return $operations;
}
/**
* Callback for platform verify operation.
*/
function hosting_platform_op_verify($nodes) {
foreach ($nodes as $nid) {
hosting_add_task($nid, 'verify');
}
}
/**
* Callback for platform lock operation.
*/
function hosting_platform_op_lock($nodes) {
foreach ($nodes as $nid) {
hosting_add_task($nid, 'lock');
}
}
/**
* Callback for platform unlock operation.
*/
function hosting_platform_op_unlock($nodes) {
foreach ($nodes as $nid) {
hosting_add_task($nid, 'unlock');
}
}
/**
* Callback for platform delete operation.
*/
function hosting_platform_op_delete($nodes) {
foreach ($nodes as $nid) {
hosting_add_task($nid, 'delete');
}
}
/**
* Small helper function to get platforms that haven't been deleted.
*/
function _hosting_get_platforms() {
$return = array();
$result = db_query("SELECT n.nid, n.title FROM {node} n LEFT JOIN {hosting_platform} h ON n.nid = h.nid \n WHERE n.type='platform' AND n.status=1 AND h.status <> %d ORDER BY n.title", HOSTING_PLATFORM_DELETED);
while ($server = db_fetch_object($result)) {
$return[$server->nid] = $server->title;
}
return $return;
}
/**
* Small helper function to get platforms that haven't been deleted or locked.
*/
function _hosting_get_enabled_platforms() {
$return = array();
$result = db_query("SELECT n.nid, n.title FROM {node} n LEFT JOIN {hosting_platform} h ON n.nid = h.nid \n WHERE n.type='platform' AND n.status=1 AND h.status <> %d ORDER BY n.title", HOSTING_PLATFORM_LOCKED);
while ($server = db_fetch_object($result)) {
$return[$server->nid] = $server->title;
}
return $return;
}
/**
* Small helper function to get platforms that have been locked.
*/
function _hosting_get_locked_platforms() {
$return = array();
$result = db_query("SELECT n.nid, n.title FROM {node} n LEFT JOIN {hosting_platform} h ON n.nid = h.nid \n WHERE n.type='platform' AND n.status=1 AND h.status = %d ORDER BY n.title", HOSTING_PLATFORM_LOCKED);
while ($server = db_fetch_object($result)) {
$return[$server->nid] = $server->title;
}
return $return;
}
/**
* Small helper function to check if a platform is locked.
*/
function _hosting_platform_is_locked($node) {
if (in_array($node, array_keys(_hosting_get_locked_platforms()))) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Implementation of hook_form().
*/
function hosting_platform_form(&$node) {
$type = node_get_types('type', $node);
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#required' => TRUE,
'#description' => t('Choose a unique descriptive name for your platform. You very likely want this to be something like "Drupal 6.20".'),
'#size' => 40,
'#default_value' => $node->title,
'#maxlength' => 255,
);
// allow edition if the node is in creation, or if wasn't verified correctly
// *and* we're not using a makefile. The reason while we don't allow editing
// the path if the makefile was specified is that there's the possibility
// that the platform path was actually created when the node was saved the
// first time and we have cruft lying around to cleanup.
if (!$node->nid || !$node->verified && !$node->makefile) {
$form['publish_path'] = array(
'#type' => 'textfield',
'#title' => t('Publish path'),
'#required' => TRUE,
'#description' => t('The absolute path on the filesystem where the sites will be hosted. This needs to be created manually and initialized before your platform works properly. It also needs to be a unique path not already in use by a platform on any server.<br />For example, run the following shell commands:<pre>%commands</pre>Your publish path is the absolute path to the directory that gets created.<br />Alternatively, you can specify a makefile below, and the platform will be created automatically if the path specified here does not exist.<br />You are still required to enter the absolute path above, as it will be treated as the target directory by the makefile.', array(
'%commands' => "cd /var/aegir/platforms\ndrush dl drupal\n",
)),
'#size' => 40,
'#default_value' => $node->publish_path,
'#maxlength' => 255,
);
}
else {
// display it
$form['info']['publish_path'] = array(
'#type' => 'item',
'#title' => t('Publish path'),
'#value' => $node->publish_path,
);
// send it on form submission
$form['publish_path'] = array(
'#type' => 'hidden',
'#value' => $node->publish_path,
);
}
$form['makefile'] = array(
'#type' => 'textfield',
'#title' => t('Makefile'),
'#description' => t('The absolute path on the filesystem or public URL of a makefile that will be used to create the platform in the directory specified above. If the directory already exists, this file will be ignored.'),
'#size' => 40,
'#default_value' => $node->makefile,
'#maxlength' => 255,
);
$form['make_working_copy'] = array(
'#type' => 'radios',
'#title' => t('Drush make option'),
'#default_value' => isset($form['#node']->make_working_copy) ? $form['#node']->make_working_copy : FALSE,
'#options' => array(
FALSE => t('Normal - Discards SCM files'),
TRUE => t('Working copy - preserves SCM files'),
),
);
$form['#after_build'][] = 'hosting_platform_form_platform_after_build';
$servers = hosting_get_servers('http');
if (sizeof($servers) > 1) {
$form['web_server'] = array(
'#type' => 'radios',
'#title' => t('Web server'),
'#description' => t('The web server the sites will be hosted on.'),
'#options' => $servers,
'#default_value' => $node->web_server ? $node->web_server : HOSTING_DEFAULT_WEB_SERVER,
);
}
else {
reset($servers);
$form['web_server'] = array(
'#type' => 'hidden',
'#value' => key($servers),
);
}
foreach (array(
'verified',
'platform_status',
) as $extra_attribute) {
$form["{$extra_attribute}"] = array(
'#type' => 'value',
'#value' => $node->{$extra_attribute},
);
}
return $form;
}
function hosting_platform_form_platform_after_build($form) {
drupal_add_js(drupal_get_path('module', 'hosting_platform') . '/hosting_platform.js');
if (isset($form['makefile']['#attributes']['class'])) {
$form['makefile']['#attributes']['class'] .= ' hosting-platform-working-copy-source';
}
else {
$form['makefile']['#attributes']['class'] = ' hosting-platform-working-copy-source';
}
if (isset($form['make_working_copy']['#attributes']['class'])) {
$form['make_working_copy']['#attributes']['class'] .= ' hosting-platform-working-copy-target';
}
else {
$form['make_working_copy']['#attributes']['class'] = ' hosting-platform-working-copy-target';
}
return $form;
}
/**
* Hide the delete button on platform nodes.
*/
function hosting_platform_form_alter(&$form, &$form_state, $form_id) {
// Remove delete button from platform edit form, unless the platform's already been deleted via the Delete task
if ($form_id == 'platform_node_form') {
$node = $form['#node'];
if ($node->platform_status !== '-2') {
$form['buttons']['delete']['#type'] = 'hidden';
}
}
// Override the 'Bulk operations' title.
if ($form_id == 'views_bulk_operations_form_hosting_platform_list_page_platforms') {
$form['select']['#title'] = 'Platform operations';
}
}
/**
* Implementation of hook_insert().
*/
function hosting_platform_insert($node) {
if (!isset($node->no_verify)) {
hosting_add_task($node->nid, 'verify');
}
db_query("INSERT INTO {hosting_platform} (vid, nid, publish_path, makefile, verified, web_server, status, make_working_copy) VALUES (%d, %d, '%s', '%s', %d, %d, %d, %d)", $node->vid, $node->nid, $node->publish_path, $node->makefile, $node->verified, $node->web_server, $node->platform_status, $node->make_working_copy);
if (!$node->old_vid) {
hosting_context_register($node->nid, 'platform_' . preg_replace("/[!\\W]/", "", $node->title));
}
}
/**
* Implementation of hook_update().
*
* As an existing node is being updated in the database, we need to do our own
* database updates.
*/
function hosting_platform_update($node) {
// if this is a new node or we're adding a new revision,
if (!empty($node->revision)) {
hosting_platform_insert($node);
}
else {
if ($node->platform_status == HOSTING_PLATFORM_DELETED) {
$node->no_verify = TRUE;
}
db_query("UPDATE {hosting_platform} SET publish_path = '%s', makefile = '%s', web_server = %d, verified = %d, status= %d, make_working_copy = %d WHERE nid=%d", $node->publish_path, $node->makefile, $node->web_server, $node->verified, $node->platform_status, $node->make_working_copy, $node->nid);
}
if (!$node->no_verify) {
hosting_add_task($node->nid, 'verify');
}
}
/**
* Implementation of hook_nodeapi_TYPE_OP().
*/
function hosting_nodeapi_platform_delete_revision(&$node) {
db_query('DELETE FROM {hosting_platform} WHERE vid = %d', $node->vid);
}
/**
* Implementation of hook_delete().
*/
function hosting_platform_delete($node) {
db_query('DELETE FROM {hosting_platform} WHERE nid = %d', $node->nid);
db_query('DELETE FROM {hosting_package_instance} WHERE rid=%d', $node->nid);
hosting_context_delete($node->nid);
$result = db_query("SELECT distinct nid FROM {hosting_task} WHERE rid=%d", $node->nid);
while ($nid = db_fetch_object($result)) {
node_delete($nid->nid);
}
$result = db_query("SELECT distinct nid FROM {hosting_site} WHERE platform=%d", $node->nid);
while ($nid = db_fetch_object($result)) {
node_delete($nid->nid);
}
}
/**
* Implementation of hook_validate().
*/
function hosting_platform_validate($node, &$form) {
// Make sure the platform name is unique, to avoid context clashes.
if ($node->op != t('Delete') && ($result = db_fetch_object(db_query("SELECT n.title AS name FROM {hosting_platform} AS h INNER JOIN {node} AS n ON n.nid = h.nid WHERE n.title = '%s' AND n.nid <> %d AND h.status >= %d", $node->title, $node->nid, HOSTING_PLATFORM_QUEUED)))) {
form_set_error('title', t('Platform name %name is already defined. Platform names must be unique across all servers.', array(
'%name' => $result->name,
)));
}
// Make sure the path is unique. Remote servers can't have the same path to a platform that is in use by another server.
if ($node->op != t('Delete') && ($result = db_fetch_object(db_query("SELECT n.title AS name FROM {hosting_platform} AS h INNER JOIN {node} AS n ON n.nid = h.nid WHERE publish_path = '%s' AND n.nid <> %d AND h.status >= %d", hosting_path_normalize($node->publish_path), $node->nid, HOSTING_PLATFORM_QUEUED)))) {
form_set_error('publish_path', t('Path is already in use by platform %name. Platform paths must be unique across all servers.', array(
'%name' => $result->name,
)));
}
if (is_null($node->web_server)) {
form_set_error('web_server', t('Platform needs to be associated with a webserver. Make sure you have a verified webserver on this Aegir install!'));
}
}
/**
* Implementation of hook_load().
*
* @param node
* Node object
*/
function hosting_platform_load($node) {
$additions = db_fetch_object(db_query('SELECT publish_path, makefile, verified, web_server, status AS platform_status, make_working_copy FROM {hosting_platform} WHERE vid = %d', $node->vid));
// Avoid PHP 5.4 warning when platform doesn't exist yet.
// See: https://drupal.org/node/1940378
$additions = $additions ? $additions : new stdClass();
$iid = db_result(db_query("SELECT iid FROM {hosting_package_instance} i left join {hosting_package} p on p.nid=i.package_id WHERE p.package_type='platform' AND i.rid=%d", $node->nid));
$additions->release = hosting_package_instance_load($iid);
$additions->profiles = hosting_get_profiles($node->nid, 'short_name');
return $additions;
}
/**
* Implementation of hook_load().
*
* @param arg
* Node's numeric nid
*/
function hosting_platform_node_load($arg) {
if (!is_numeric($arg)) {
return FALSE;
}
if ($node = node_load($arg)) {
if ($node->type == 'platform') {
return $node;
}
}
return FALSE;
}
/**
* Implementation of hook_view().
*/
function hosting_platform_view($node, $teaser = FALSE, $page = FALSE) {
modalframe_parent_js();
$node->content['info'] = array(
'#prefix' => '<div id="hosting-platform-info" class="hosting-info-list">',
'#suffix' => '</div>',
);
$node->content['info']['verified'] = array(
'#type' => 'item',
'#title' => t('Verified'),
'#value' => hosting_format_interval($node->verified),
'#weight' => -10,
);
$node->content['info']['publish_path'] = array(
'#type' => 'item',
'#title' => t('Publish path'),
'#value' => filter_xss($node->publish_path),
'#weight' => -8,
);
$node->content['info']['web_server'] = array(
'#type' => 'item',
'#title' => t('Web server'),
'#value' => _hosting_node_link($node->web_server),
'#weight' => -7,
);
$node->content['info']['status'] = array(
'#type' => 'item',
'#title' => t('Status'),
'#value' => _hosting_platform_status($node->platform_status),
);
if ($node->makefile) {
$node->content['info']['makefile'] = array(
'#type' => 'item',
'#title' => t('Drush makefile'),
'#value' => preg_match('/^http|^ftp/', $node->makefile) ? l('makefile', $node->makefile) : filter_xss($node->makefile),
);
}
if ($node->release) {
$release = sprintf("%s %s", $node->release->title, $node->release->version);
$node->content['info']['release'] = array(
'#type' => 'item',
'#title' => t('Release'),
'#value' => _hosting_node_link($node->release->nid, $release),
'#weight' => -6,
);
}
// Task list
if ($page && $node->nid) {
$node->content['tasks_view'] = array(
'#type' => 'item',
'#value' => hosting_task_table($node),
'#prefix' => '<div id="hosting-task-list">',
'#suffix' => '</div>',
'#weight' => 10,
);
$settings['hostingTaskRefresh'] = array(
'nid' => $node->nid,
'changed' => $node->changed,
);
drupal_add_js($settings, 'setting');
drupal_add_js(drupal_get_path('module', 'hosting_task') . '/hosting_task.js');
}
return $node;
}
/**
* Implements hook_hosting_site_site_list_filters().
*/
function hosting_platform_hosting_site_site_list_filters() {
return array(
'platform',
);
}
function _hosting_platform_status_codes() {
$codes = array(
HOSTING_PLATFORM_QUEUED => array(
'label' => 'Queued',
'class' => 'hosting-queue',
),
HOSTING_PLATFORM_ENABLED => array(
'label' => 'Enabled',
'class' => 'hosting-success',
),
HOSTING_PLATFORM_DELETED => array(
'label' => 'Deleted',
'class' => 'hosting-error',
),
HOSTING_PLATFORM_LOCKED => array(
'label' => 'Locked',
'class' => 'hosting-disable',
),
);
return $codes;
}
function _hosting_platform_status($status) {
static $labels;
$labels = _hosting_platform_status_codes();
return is_object($status) ? $labels[$status->platform_status]['label'] : $labels[$status]['label'];
}
/**
* Implementation of hook_hosting_summary().
*/
function hosting_platform_hosting_summary() {
$summary = array();
if (user_access('view locked platforms')) {
$platforms = _hosting_get_platforms();
$summary['platforms'] = theme('item_list', array_map('_hosting_node_link', array_keys($platforms)), t('Platforms'));
}
else {
if (user_access('view platform')) {
$platforms = _hosting_get_enabled_platforms();
$summary['platforms'] = theme('item_list', array_map('_hosting_node_link', array_keys($platforms)), t('Platforms'));
}
}
return $summary;
}
/**
* Implementation of hook_views_api().
*/
function hosting_platform_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'hosting_platform'),
);
}
/**
* Define the classes that correspond to the platform status.
*/
function _hosting_platform_list_class($status) {
static $labels;
$labels = _hosting_platform_status_codes();
return is_object($status) ? $labels[$status->platform_status]['class'] : $labels[$status]['class'];
}
/**
* Simple list for top level navigation.
*/
function hosting_platform_listing() {
$platforms = _hosting_get_platforms();
$header = array(
t('Platform'),
t('Release'),
t('Server'),
t('Verified'),
t('Sites'),
);
$rows[] = array();
$total = 0;
foreach ($platforms as $nid => $title) {
$row = array();
$node = node_load($nid);
$row[] = array(
'class' => 'hosting-status',
'data' => _hosting_node_link($nid),
);
$release = sprintf("%s %s", $node->release->title, $node->release->version);
$row[] = _hosting_node_link($node->release->nid, $release);
$row[] = _hosting_node_link($node->web_server);
$row[] = hosting_format_interval($node->verified);
$total += $count = hosting_site_count($nid, array(
HOSTING_SITE_ENABLED,
HOSTING_SITE_DISABLED,
HOSTING_SITE_QUEUED,
));
$row[] = $count;
$rows[] = array(
'data' => $row,
'class' => _hosting_platform_list_class($node->platform_status),
);
}
$row = array();
$row[] = array(
'data' => t('Total sites hosted'),
'colspan' => 4,
);
$row[] = $total;
$rows[] = $row;
return theme('table', $header, $rows, array(
'class' => 'hosting-table',
));
}
Functions
Constants
Name | Description |
---|---|
HOSTING_PLATFORM_DELETED | This platform has been deleted. |
HOSTING_PLATFORM_ENABLED | This platform is created and enabled, sites may be deployed on it. |
HOSTING_PLATFORM_LOCKED | This platform has been locked, new sites will not be allowed to use it. |
HOSTING_PLATFORM_QUEUED | This platform has been queued for creation. |