You are here

hosting_site.drush.inc in Hosting 7.4

Drush hooks for the Hosting site module.

File

site/hosting_site.drush.inc
View source
<?php

/**
 * @file
 * Drush hooks for the Hosting site module.
 */

/**
 * Implements hook_hosting_TASK_OBJECT_context_options().
 */
function hosting_hosting_site_context_options(&$task) {
  $task->context_options['db_server'] = hosting_context_name($task->ref->db_server);
  $task->context_options['platform'] = hosting_context_name($task->ref->platform);
  $task->context_options['uri'] = hosting_site_get_domain($task->ref->title);
  $task->context_options['language'] = $task->ref->site_language;
  $profile = node_load($task->ref->profile);
  if (!empty($profile->short_name)) {
    $task->context_options['profile'] = $profile->short_name;
  }
  else {
    $task->context_options['profile'] = '';
  }

  // @TODO: Legacy refactor needed. Move to Hosting Client module.
  $client = node_load($task->ref->client);
  $user = user_load($task->uid);
  if ($task->task_type != 'import' && $task->task_type != 'delete' && $task->task_type != 'verify') {
    $task->options['client_email'] = $user->mail;
  }
  $task->context_options['client_name'] = $client->uname;

  // @TODO: Move to Hosting Drupal module... when it exists.
  // Set "install_method" property, only if there is one present on the site node.
  // "profile" is used by default, we don't have to set it here.
  if (isset($task->ref->install_method) && !empty($task->ref->install_method)) {
    $task->context_options['install_method'] = $task->ref->install_method;
  }

  // Set File directories if they exist
  $task->context_options['file_public_path'] = $task->ref->file_public_path;
  $task->context_options['file_private_path'] = $task->ref->file_private_path;
  $task->context_options['file_temporary_path'] = $task->ref->file_temporary_path;
}

/**
 * Implements hook_drush_context_import().
 */
function hosting_site_drush_context_import($context, &$node) {
  if ($context->type == 'site') {
    if (!isset($node->title)) {
      $node->title = hosting_site_get_domain($context->uri);

      // force lowercase for existing uri
    }
    $node->site_language = $context->language;
    $node->cron_key = $context->cron_key;
    $node->db_server = hosting_context_nid($context->db_server->name);
    $node->platform = hosting_context_nid($context->platform->name);

    // TODO: abstract this to remove duplication with import post hooks.
    $profile = hosting_package_instance_load(array(
      'i.rid' => $node->platform,
      'p.short_name' => $context->profile,
    ));
    if (!$profile) {

      // For D6 to D7 upgrades, try changing default to standard.
      if (!empty($context->profile) && $context->profile == 'default') {
        $profile = hosting_package_instance_load(array(
          'i.rid' => $node->platform,
          'p.short_name' => 'standard',
        ));
        if ($profile) {
          drush_log(dt("Exception: No record found in hosting_package_instance table for install profile '!profile' and platform with NID !platform. Using standard profile instead.", array(
            '!profile' => $context->profile,
            '!platform' => $node->platform,
          )), 'warning');
        }
      }
    }

    // Log an error if there is still no profile.
    if (!$profile) {
      drush_log(dt("Exception: No record found in hosting_package_instance table for install profile '!profile' and platform with NID !platform. Unable to save install profile metadata for this site.", array(
        '!profile' => $context->profile,
        '!platform' => $node->platform,
      )), 'warning');
    }
    $node->profile = $profile->package_id;
    $client = node_load(HOSTING_DEFAULT_CLIENT);
    if ($context->client_name) {
      $client = hosting_import_client($context->client_name);
    }
    $node->client = $client->nid;
  }
}

/**
 * Implements drush_HOOK_pre_COMMAND()
 *
 * Map values of site node into command line arguments
 */
function drush_hosting_site_pre_hosting_task($task) {
  $task =& drush_get_context('HOSTING_TASK');
  if ($task->task_type == 'restore') {
    $backup = hosting_site_get_backup($task->task_args['bid']);
    $task->args[1] = $backup['filename'];
  }
  if ($task->task_type == 'backup-delete') {
    foreach ($task->task_args as $bid => $filename) {
      if ($filename !== '0') {
        $backups[] = $filename;
      }
    }
    $task->args[1] = implode(',', $backups);
  }
  if ($task->task_type == 'deploy') {

    // Create an array so as to not disrupt task_args.
    $task_arguments = $task->task_args;

    // Drush command arguments.
    $task->args[] = $task_arguments['target_git_reference'];
    unset($task_arguments['target_git_reference']);

    // Drush command options.
    $task->options['reset'] = intval($task_arguments['git_reset']);
    unset($task_arguments['git_reset']);

    // Load the rest of the task args into drush options.
    foreach ($task_arguments as $arg => $value) {
      $task->options[$arg] = $value;
    }
  }
}

/**
 * Implements hook_post_hosting_TASK_TYPE_task()
 */
function hosting_site_post_hosting_install_task($task, $data) {
  if ($task->ref->type == 'site') {
    $context = $data['context'];
    $packages = $context['packages'];
    hosting_package_sync($packages);
    hosting_package_instance_sync($task->ref->nid, $task->ref->type, $packages);
    $task->ref->site_status = HOSTING_SITE_ENABLED;
    $task->ref->no_verify = TRUE;
    $task->ref->verified = REQUEST_TIME;
    $task->ref->db_name = $data['self']['db_name'];
    if ($context['cron_key']) {
      $task->ref->cron_key = $context['cron_key'];
    }
    node_save($task->ref);
    if ($context['login_link']) {
      drush_log($context['login_link']);

      // we need to store the timestamp too because expire only means it can be deleted
      // after an amount of time, not will be deleted.
      $cache = array(
        'expire' => strtotime("+24 hours"),
        'link' => $context['login_link'],
      );
      cache_set('hosting:site:' . $task->ref->nid . ':login_link', $cache, 'cache', $cache['expire']);
    }
  }
}

/**
 * Implements the hosting_failed_install hook
 */
function hosting_site_hosting_install_task_rollback($task, $data) {

  // @TODO : we need to check the returned list of errors, not the code.
  if (drush_cmp_error('PROVISION_DRUPAL_SITE_INSTALLED')) {

    // Site has already been installed. Try to import instead.
    drush_log(dt("This site appears to be installed already. Generating an import task."));
    hosting_add_task($task->rid, 'import');
  }
  else {
    $task->ref->no_verify = TRUE;
    $task->ref->site_status = HOSTING_SITE_DISABLED;
    node_save($task->ref);
  }
}

/**
 * Implements hook_hosting_post_disable().
 * I am not very fond of the use of bitwise operators with negatives.
 */
function hosting_site_post_hosting_disable_task($task, $data) {
  if ($task->ref->type != 'site') {
    return;
  }
  if ($data['context']['backup_file'] && $data->ref->type == 'site') {
    $platform = node_load($task->ref->platform);
    hosting_site_add_backup($task->ref->nid, $platform->web_server, $data['context']['backup_file'], t('Generated before being disabled'), $data['context']['backup_file_size']);
  }
  $task->ref->site_status = HOSTING_SITE_DISABLED;
  $task->ref->no_verify = TRUE;
  node_save($task->ref);

  // Record the backup created during disable.
  $task->task_args['description'] = t('Pre-disable backup');
  hosting_site_post_hosting_backup_task($task, $data);
}

/**
 * Implements hook_hosting_post_restore().
 * I am not very fond of the use of bitwise operators with negatives.
 */
function hosting_site_post_hosting_restore_task($task, $data) {
  if ($data['context']['backup_file'] && $task->ref->type == 'site') {
    $platform = node_load($task->ref->platform);
    hosting_site_add_backup($task->ref->nid, $platform->web_server, $data['context']['backup_file'], t('Generated before being restored to a previous version'), $data['context']['backup_file_size']);
  }
}

/**
 * Implements hook_hosting_post_enable().
 */
function hosting_site_post_hosting_enable_task($task, $data) {
  if ($task->ref->type != 'site') {
    return;
  }
  $task->ref->site_status = HOSTING_SITE_ENABLED;
  $task->ref->no_verify = TRUE;
  node_save($task->ref);
}

/**
 * Implements hook_post_hosting_TASK_TYPE_task().
 */
function hosting_site_post_hosting_delete_task($task, $data) {
  if ($task->ref->type == 'site') {
    $task->ref->site_status = HOSTING_SITE_DELETED;
    $task->ref->no_verify = TRUE;
    node_save($task->ref);

    // Record the backup created during delete.
    $task->task_args['description'] = t('Pre-delete backup');
    hosting_site_post_hosting_backup_task($task, $data);

    // Clean up package instances
    hosting_package_instance_sync($task->ref->nid, 'site');
  }
}

/**
 * Implements hook_hosting_post_backup().
 *
 * Adds the data file that was saved to the site backup history.
 * This is needed to be able to restore.
 */
function hosting_site_post_hosting_backup_task($task, $data) {
  if ($data['context']['backup_file'] && $task->ref->type == 'site') {
    $platform = node_load($task->ref->platform);
    $desc = $task->task_args['description'];
    $desc = $desc ? $desc : t('Generated on request');
    hosting_site_add_backup($task->ref->nid, $platform->web_server, $data['context']['backup_file'], $desc, $data['context']['backup_file_size']);
  }
}

/**
 * Implements hook_post_hosting_import_task().
 */
function hosting_site_post_hosting_import_task($task, $data) {
  if ($task->ref->type != 'site') {
    return;
  }
  $site_nid = hosting_drush_import($task->ref->hosting_name);

  // Update the site node with the database name.
  $node = node_load($site_nid);
  $node->db_name = $data['self']['db_name'];
  node_save($node);
  $packages = $data['context']['packages'];
  hosting_package_sync($packages);
  hosting_package_instance_sync($task->ref->nid, $task->ref->type, $packages);
}

/**
 * Implements hook_post_hosting_deploy_task().
 *
 * Ensure post-verify task hooks are run after a deploy.
 */
function hosting_site_post_hosting_deploy_task($task, $data) {
  if ($task->ref->type == 'site') {
    hosting_site_post_hosting_verify_task($task, $data);
  }
}

/**
 * Implements hook_post_hosting_verify_task().
 */
function hosting_site_post_hosting_verify_task($task, $data) {
  if ($task->ref->type == 'site') {

    // Run hook_post_hosting_verify_task() on the platform for this site.
    $platform_task = _drush_hosting_task_create_platform_task($task);
    $platform_data = $data;
    hosting_platform_post_hosting_verify_task($platform_task, $platform_data);
    $task->ref->verified = REQUEST_TIME;
    $task->ref->no_verify = TRUE;
    $task->ref->db_name = $data['self']['db_name'];
    if ($data['context']['cron_key']) {
      $task->ref->cron_key = $data['context']['cron_key'];
    }
    node_save($task->ref);

    // @TODO: The $data from the function param seems to always be empty, if we figure out how to populate it from drush_hosting_task() remove this.
    // @see hosting_platform_post_hosting_verify_task()
    //    $data = provision_backend_invoke($task->ref->hosting_name, "provision-packages");
    // Populate site package data
    $packages_drupal = drush_get_option('packages', array(), 'drupal');
    hosting_package_sync($packages_drupal);
    $packages_site = drush_get_option('packages', array(), 'site');
    hosting_package_sync($packages_site);
    hosting_package_instance_sync($task->ref->nid, $task->ref->type, $packages_drupal, $packages_site);
  }
}

/**
 * Implements the hosting_failed_install hook
 */
function hosting_site_hosting_verify_task_rollback($task, $data) {
  if ($task->ref->type == 'site') {
    $task->ref->no_verify = TRUE;
    $task->ref->verified = 0;
    node_save($task->ref);
  }
}

/**
 * Implements hook_hosting_import_task_rollback().
 */
function hosting_site_hosting_import_task_rollback($task, $data) {
  if ($task->ref->type == 'site') {
    $task->ref->no_verify = TRUE;
    $task->ref->verified = 0;
    $task->ref->site_status = HOSTING_SITE_DISABLED;
    node_save($task->ref);
  }
}

/**
 * Implements hook_post_hosting_login_reset_task().
 */
function hosting_site_post_hosting_login_reset_task($task, $data) {
  if ($task->ref->type == 'site') {
    $task->ref->no_verify = TRUE;
    node_save($task->ref);
    $context = $data['context'];
    if ($context['login_link']) {

      // we need to store the timestamp too because expire only means it can be deleted
      // after an amount of time, not will be deleted.
      $cache = array(
        'expire' => strtotime("+24 hours"),
        'link' => $context['login_link'],
      );
      cache_set('hosting:site:' . $task->ref->nid . ':login_link', $cache, 'cache', $cache['expire']);
    }
  }
}

/**
 * Implements the hosting_post_backup_delete hook
 */
function hosting_site_post_hosting_backup_delete_task($task, $data) {
  if ($task->ref->type == 'site') {
    foreach ($task->task_args as $bid => $filename) {
      if ($filename !== '0') {
        hosting_site_delete_backup($bid);
      }
    }
  }
}

Functions

Namesort descending Description
drush_hosting_site_pre_hosting_task Implements drush_HOOK_pre_COMMAND()
hosting_hosting_site_context_options Implements hook_hosting_TASK_OBJECT_context_options().
hosting_site_drush_context_import Implements hook_drush_context_import().
hosting_site_hosting_import_task_rollback Implements hook_hosting_import_task_rollback().
hosting_site_hosting_install_task_rollback Implements the hosting_failed_install hook
hosting_site_hosting_verify_task_rollback Implements the hosting_failed_install hook
hosting_site_post_hosting_backup_delete_task Implements the hosting_post_backup_delete hook
hosting_site_post_hosting_backup_task Implements hook_hosting_post_backup().
hosting_site_post_hosting_delete_task Implements hook_post_hosting_TASK_TYPE_task().
hosting_site_post_hosting_deploy_task Implements hook_post_hosting_deploy_task().
hosting_site_post_hosting_disable_task Implements hook_hosting_post_disable(). I am not very fond of the use of bitwise operators with negatives.
hosting_site_post_hosting_enable_task Implements hook_hosting_post_enable().
hosting_site_post_hosting_import_task Implements hook_post_hosting_import_task().
hosting_site_post_hosting_install_task Implements hook_post_hosting_TASK_TYPE_task()
hosting_site_post_hosting_login_reset_task Implements hook_post_hosting_login_reset_task().
hosting_site_post_hosting_restore_task Implements hook_hosting_post_restore(). I am not very fond of the use of bitwise operators with negatives.
hosting_site_post_hosting_verify_task Implements hook_post_hosting_verify_task().