hosting_server.install 7.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
// $Id$


/**
 * 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,
      ),
23 24


25 26 27 28
    ),
    'primary key' => array('vid'),
  );

29
  $schema['hosting_ip_addresses'] = array(
30
   'fields' => array(
31
      'id' => array(
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
        '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,
      ),
    ),
49
    'primary key' => array('id'),
50 51
    'indexes' => array(
      'vid' => array('vid'),
52
      'ip_address' => array('ip_address'),
53 54 55 56
    ),

  );

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  $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,
      ),
81 82 83 84 85 86 87 88 89 90 91
      'restart_cmd' => array(
        'type' => 'text',
        'size' => 'big',
        'not null' => FALSE,
      ),
      'port' => array(
        'type' => 'int',
        'default' => 0,
        'unsigned' => TRUE,
        'not null' => FALSE,
      ),
92 93 94 95 96
      'available' => array(
        'type' => 'int',
        'size' => 'small', 
        'not null' => TRUE,
        'default' => 1,
97 98 99 100
      ),
    ),
    'indexes' => array(
      'vid' => array('vid'),
101 102 103
    ),
  );

104 105 106 107 108 109 110 111
  return $schema;
}

function hosting_server_install() {
  // Create tables.
  drupal_install_schema('hosting_server');
}

112 113 114 115 116 117 118 119 120 121 122
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;
}
123 124 125 126 127 128 129 130 131 132 133 134 135


/**
 * 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;
}

136 137 138 139 140 141 142 143 144 145 146 147 148

/**
 * 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;
}

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

/**
 * 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;
}
169 170 171 172 173 174 175

/**
 * Add the IP address table.
 */
function hosting_server_update_6004() {

  $return = array();
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  // 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);
202 203 204

  return $return;
}
205 206 207 208 209 210 211 212 213

/**
 * 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;
}
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

/**
 * Add primary key for IPs, drop versionning
 */
function hosting_server_update_6200() {
  $ret = array();
  db_add_field($ret, "hosting_ip_addresses", "id", array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  db_drop_field($ret, "hosting_ip_addresses", "vid");
  $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_primary_key($ret, 'hosting_ip_addresses', array('id'));
  return $ret;
}
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

/**
 * 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.
 */
function hosting_server_update_6201() {
  $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.
 *
anarcat's avatar
anarcat committed
268 269 270
 * 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.
271 272 273 274
 */
function hosting_server_update_6202() {
  $ret = array();

anarcat's avatar
anarcat committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
  /* 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!).
   */
  $ret[] = update_sql("INSERT INTO {hosting_ssl_cert_ips} (cid, ip_address)
  SELECT cid, server_ip.nid 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 {node} servers ON server_ip.nid = servers.nid
    WHERE ssl_enabled > %d AND site.status > %d AND servers.type = 'server'
    GROUP BY cid ORDER BY cert.cid, site.nid",
290 291 292 293 294 295 296 297 298 299 300 301 302 303
                HOSTING_SSL_DISABLED, HOSTING_SITE_DELETED);
  return $ret;
}

/**
 * Cleanup site-specific IP mappings.
 *
 * Those are now associated to certificates.
 */
function hosting_server_update_6202() {
  $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;
}