You are here

hosting_server.install in Hosting 6.2

Same filename and directory in other branches
  1. 7.4 server/hosting_server.install
  2. 7.3 server/hosting_server.install

File

server/hosting_server.install
View source
<?php

/**
 * Implementation of hook_schema().
 */
function hosting_server_schema() {
  $schema['hosting_server'] = array(
    'fields' => array(
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'vid',
    ),
  );
  $schema['hosting_ip_addresses'] = array(
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'ip_address' => array(
        'type' => 'text',
        'size' => 'small',
        'not null' => TRUE,
      ),
      'id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'indexes' => array(
      'nid' => array(
        'nid',
      ),
    ),
  );
  $schema['hosting_service'] = array(
    'fields' => array(
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'service' => array(
        'type' => 'text',
        'size' => 'small',
        'not null' => TRUE,
      ),
      'type' => array(
        'type' => 'text',
        'size' => 'small',
        'not null' => TRUE,
      ),
      'restart_cmd' => array(
        'type' => 'text',
        'size' => 'big',
        'not null' => FALSE,
      ),
      'port' => array(
        'type' => 'int',
        'default' => 0,
        'unsigned' => TRUE,
        'not null' => FALSE,
      ),
      'available' => array(
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 1,
      ),
    ),
    'indexes' => array(
      'vid' => array(
        'vid',
      ),
    ),
  );
  return $schema;
}
function hosting_server_install() {

  // Create tables.
  drupal_install_schema('hosting_server');
}
function hosting_server_uninstall() {
  drupal_uninstall_schema('hosting_server');
}
function hosting_server_update_6000() {
  $return = array();
  db_add_index($return, 'hosting_service', 'vid', array(
    'vid',
  ));
  return $return;
}

/**
 * Drop the drush_path and backup_path columns which are no longer needed.
 */
function hosting_server_update_6001() {
  $ret = array();
  db_drop_field($ret, "hosting_server", "drush_path");
  db_drop_field($ret, "hosting_server", "backup_path");
  return $ret;
}

/**
 * Drop the script_user and config_path columns which are no longer needed.
 * Also remove hosting_service entries which are no longer relevant.
 */
function hosting_server_update_6002() {
  $ret = array();
  db_query("DELETE FROM {hosting_service} WHERE service='server'");
  db_drop_field($ret, "hosting_server", "script_user");
  db_drop_field($ret, "hosting_server", "config_path");
  return $ret;
}

/**
 * Bring the port and restart command from hosting_web_server into hosting_service.
 */
function hosting_server_update_6003() {
  db_add_field($ret, 'hosting_service', 'port', array(
    'type' => 'int',
    'default' => 0,
    'unsigned' => TRUE,
    'not null' => FALSE,
  ));
  db_add_field($ret, 'hosting_service', 'restart_cmd', array(
    'type' => 'text',
    'size' => 'big',
    'not null' => FALSE,
  ));
  return $ret;
}

/**
 * Add the IP address table.
 */
function hosting_server_update_6004() {
  $return = array();

  // create the table so we can rename it.
  // retarded, i know.
  $schema = array(
    'fields' => array(
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'ip_address' => array(
        'type' => 'text',
        'size' => 'small',
        'not null' => TRUE,
      ),
    ),
    'indexes' => array(
      'vid' => array(
        'vid',
      ),
    ),
  );
  db_create_table($return, 'hosting_server_ip', $schema);
  return $return;
}

/**
 * Rename the IP address table so that sites can also use it.
 */
function hosting_server_update_6005() {
  $ret = array();
  db_rename_table($ret, 'hosting_server_ip', 'hosting_ip_addresses');
  return $ret;
}

/**
 * Add primary key for IPs, drop versionning
 */
function hosting_server_update_6200() {
  $ret = array();
  db_drop_index($ret, 'hosting_ip_addresses', 'vid');
  db_drop_field($ret, "hosting_ip_addresses", "vid");
  db_add_field($ret, "hosting_ip_addresses", "id", array(
    'type' => 'serial',
    'unsigned' => TRUE,
    'not null' => TRUE,
  ), array(
    'primary key' => array(
      'id',
    ),
  ));
  $r = db_query("SELECT nid, ip_address FROM {hosting_ip_addresses}");
  for ($i = 0; $row = db_fetch_object($r); $i++) {
    $ret[] = update_sql("UPDATE {hosting_ip_addresses} SET id={$i} WHERE nid={$row->nid} AND ip_address='{$row->ip_address}'");
  }
  db_add_index($ret, 'hosting_ip_addresses', 'nid', array(
    'nid',
  ));
  return $ret;
}

/**
 * Create SSL cert to IP mapping table.
 *
 * We create the SSL cert to IP mapping table here instead of in the ssl
 * module as we need to cleanup the ip_address table after and we can't sync
 * across modules. Since this module is a dependency of hosting_site_ssl, this
 * is not a problem.
 *
 * If SSL is not installed, this is a noop.
 */
function hosting_server_update_6201() {
  if (!db_table_exists('hosting_ssl_site')) {
    return array(
      array(
        'success' => TRUE,
      ),
    );
  }
  $ret = array();
  $schema = array(
    'fields' => array(
      // cert id
      'cid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      // reference to the hosting_ip_addresses table
      'ip_address' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'cid' => array(
        'cid',
      ),
      'ip_address' => array(
        'ip_address',
      ),
    ),
  );
  db_create_table($ret, 'hosting_ssl_cert_ips', $schema);
  return $ret;
}

/**
 * Migrate site-specific IPs to server-specific IPs.
 *
 * We inject into the new certificate to IP mapping table the correct
 * associations, by looking at the server IPs matching the IPs allocated to
 * sites.
 *
 * If SSL is not installed, this is a noop.
 */
function hosting_server_update_6202() {
  if (!db_table_exists('hosting_ssl_site')) {
    return array(
      array(
        'success' => TRUE,
      ),
    );
  }
  $ret = array();

  /* this is rather complicated:
   *
   * we need to first find the SSL certificate of the site, then look at the
   * IP of the sites with SSL certs, match that IP with the IP of a server (we
   * assume it's the same server!).
   *
   * Verification query:
   *
   * SELECT cert.cid,cert.ssl_key,server_ip.ip_address,server_ip.nid,server_ip.id FROM hosting_ssl_site ssl_site
   * INNER JOIN hosting_ssl_cert cert ON cert.cid = ssl_site.ssl_key
   * INNER JOIN hosting_ip_addresses site_ip ON site_ip.nid = ssl_site.nid
   * INNER JOIN hosting_site site ON site.nid = ssl_site.nid
   * INNER JOIN hosting_ip_addresses server_ip ON server_ip.ip_address = site_ip.ip_address
   * INNER JOIN hosting_platform p ON site.platform = p.nid AND p.web_server = server_ip.nid
   * WHERE ssl_enabled > 0 AND site.status > -2 GROUP BY cid ;
   *
   * This should show a meaningful list of SSL certificates -> IPs mappings.
   *
   */
  $ret[] = update_sql("INSERT INTO {hosting_ssl_cert_ips} (cid, ip_address)\n  SELECT cert.cid,server_ip.id FROM {hosting_ssl_site} ssl_site\n    INNER JOIN {hosting_ssl_cert} cert ON cert.cid = ssl_site.ssl_key\n    INNER JOIN {hosting_ip_addresses} site_ip ON site_ip.nid = ssl_site.nid\n    INNER JOIN {hosting_site} site ON site.nid = ssl_site.nid\n    INNER JOIN {hosting_ip_addresses} server_ip ON server_ip.ip_address = site_ip.ip_address\n    INNER JOIN {hosting_platform} p ON site.platform = p.nid AND p.web_server = server_ip.nid\n    WHERE ssl_enabled > %d AND site.status > %d GROUP BY cid", HOSTING_SSL_DISABLED, HOSTING_SITE_DELETED);
  return $ret;
}

/**
 * Cleanup site-specific IP mappings.
 *
 * Those are now associated only to servers again, and different modules
 * should manage their own mappings.
 *
 * @todo enable this in 3.x? or maybe after the RC cycle of 2.x? we keep the
 * data right now because the above request is quite error-prone and we are
 * not sure we cover all cases.
 */

/* function hosting_server_update_6203() { */

/*   $ret = array(); */

/*   $ret[] = update_sql("DELETE {hosting_ip_addresses} FROM {hosting_ip_addresses} JOIN {node} ON hosting_ip_addresses.nid = node.nid WHERE node.type <> 'server';"); */

/*   return $ret; */

/* } */

Functions

Namesort descending Description
hosting_server_install
hosting_server_schema Implementation of hook_schema().
hosting_server_uninstall
hosting_server_update_6000
hosting_server_update_6001 Drop the drush_path and backup_path columns which are no longer needed.
hosting_server_update_6002 Drop the script_user and config_path columns which are no longer needed. Also remove hosting_service entries which are no longer relevant.
hosting_server_update_6003 Bring the port and restart command from hosting_web_server into hosting_service.
hosting_server_update_6004 Add the IP address table.
hosting_server_update_6005 Rename the IP address table so that sites can also use it.
hosting_server_update_6200 Add primary key for IPs, drop versionning
hosting_server_update_6201 Create SSL cert to IP mapping table.
hosting_server_update_6202 Migrate site-specific IPs to server-specific IPs.