Commit 5566d405 authored by anarcat's avatar anarcat

finish moving IP allocation to the frontend

this may not work, untested. the idea is that IP are now allocated when certificates are created in the frontend

this needs work, as it still relies on the old site/IP mapping to guess the next IPs

we also need to send that data to the backend now
parent 574cdd21
......@@ -90,3 +90,40 @@ function hosting_ip_delete($node) {
function hosting_ip_delete_revision($node) {
db_query("DELETE FROM {hosting_ip_addresses} WHERE vid=%d", $node->vid);
}
/**
* Pick an IP for a site on a given server.
*
* XXX: this needs to be re-written to rely on the hosting_ssl_cert table
*
* @return the first available IP on the server, or FALSE if no IP is available
*/
function hosting_ip_pick($node) {
// basically, we do a double-left-join on the ip_address table but then
// restrict the left table to the server we're interested in and the
// right tables to only sites (using a subselect, because I couldn't
// figure out how to do it with a join).
//
// took me about 40 minutes to figure out that bastard.
return db_result(db_query("SELECT server.ip_address FROM {hosting_ip_addresses} AS server
LEFT JOIN {hosting_ip_addresses} AS site ON site.ip_address = server.ip_address
and site.nid IN (SELECT nid FROM {node} WHERE type = 'site')
WHERE server.vid=%d AND site.nid IS NULL", $node->vid));
}
/**
* Allocate an IP for a given site on a given server.
*/
function hosting_ip_allocate($node) {
db_query("LOCK TABLES {hosting_ip_addresses}"); // make sure the IP is
// not allocated after
// the pick
$platform = node_load($node->platform);
$server = node_load($platform->web_server);
$ip = hosting_ip_pick($server);
if ($ip) {
db_query("INSERT INTO {hosting_ip_addresses} (vid, nid, ip_address) VALUES (%d, %d, '%s')", $node->vid, $node->nid, $ip);
}
db_query("UNLOCK TABLES {hosting_ip_addresses}");
return $ip;
}
\ No newline at end of file
......@@ -80,7 +80,6 @@ function hosting_site_post_hosting_install_task($task, $data) {
hosting_package_sync($packages);
hosting_package_instance_sync($task->ref->nid, $packages);
$task->ref->site_status = HOSTING_SITE_ENABLED;
$task->ref->ip_addresses = array_values($data['context']['site_ip_addresses']);
$task->ref->no_verify = TRUE;
$task->ref->verified = time();
if ($context['cron_key']) {
......@@ -195,7 +194,6 @@ function hosting_site_post_hosting_verify_task($task, $data) {
if ($task->ref->type == 'site') {
$task->ref->verified = time();
$task->ref->no_verify = TRUE;
$task->ref->ip_addresses = array_values($data['context']['site_ip_addresses']);
if ($data['context']['cron_key']) {
$task->ref->cron_key = $data['context']['cron_key'];
}
......
......@@ -405,6 +405,7 @@ function hosting_import_site($site_id, $data, $platform) {
$site->no_verify = TRUE;
$site->verified = time();
$site->client = $client->nid;
// XXX: check if the IP is allocated in the server??
$site->ip_addresses = array_values($data['site_ip_addresses']);
$site->cron_key = ($data['cron_key']) ? $data['cron_key'] : '';
......
......@@ -147,8 +147,6 @@ function hosting_site_insert(&$node) {
// Ensure that the last_cron value is set.
$node->last_cron = isset($node->last_cron) ? $node->last_cron : 0;
hosting_ip_save($node, FALSE);
db_query("INSERT INTO {hosting_site} (vid, nid, client, db_server, platform, profile, language, last_cron, cron_key, status, verified) VALUES (%d, %d, %d, %d, %d, %d, '%s', %d, '%s', %d, %d)",
$node->vid, $node->nid, $node->client, $node->db_server, $node->platform, $node->profile, $node->site_language, $node->last_cron, $node->cron_key, $node->site_status, $node->verified);
if ((empty($node->old_vid))) {
......
......@@ -193,10 +193,12 @@ function hosting_ssl_nodeapi_site_presave(&$node) {
if (($node->ssl_key == HOSTING_SSL_CUSTOM_KEY) && isset($node->ssl_key_new)) {
$node->ssl_key = hosting_ssl_filter_key($node->ssl_key_new);
}
// allocate the site's IP here
$node->ssl_key = hosting_ssl_save_key($node);
}
function hosting_ssl_nodeapi_site_insert($node) {
db_query("INSERT INTO {hosting_ssl_site} (vid, nid, ssl_enabled, ssl_key) VALUES (%d, %d, %d, %d)", $node->vid, $node->nid, $node->ssl_enabled, hosting_ssl_save_key($node->ssl_key, $node->ssl_enabled, $node->client));
db_query("INSERT INTO {hosting_ssl_site} (vid, nid, ssl_enabled, ssl_key) VALUES (%d, %d, %d, %d)", $node->vid, $node->nid, $node->ssl_enabled, $node->ssl_key);
}
function hosting_ssl_nodeapi_site_update($node) {
......@@ -206,7 +208,7 @@ function hosting_ssl_nodeapi_site_update($node) {
hosting_ssl_nodeapi_site_insert($node);
}
else {
db_query("UPDATE {hosting_ssl_site} SET ssl_enabled=%d, ssl_key=%d WHERE vid=%d", $node->ssl_enabled, hosting_ssl_save_key($node->ssl_key, $node->ssl_enabled, $node->client), $node->vid);
db_query("UPDATE {hosting_ssl_site} SET ssl_enabled=%d, ssl_key=%d WHERE vid=%d", $node->ssl_enabled, $node->ssl_key, $node->vid);
}
}
......@@ -228,34 +230,35 @@ function hosting_ssl_nodeapi_site_delete_revision($node) {
/**
* Store the SSL Cert key in the database.
*/
function hosting_ssl_save_key($key, $ssl_enabled = TRUE, $client = null) {
if (empty($ssl_enabled)) {
function hosting_ssl_save_key($node) {
if (empty($node->ssl_enabled)) {
return 0;
}
// we only save the new key if it's a string
if (!is_numeric($key)) {
$result = db_query("SELECT * FROM {hosting_ssl_cert} WHERE ssl_key = '%s'", $key);
if (!is_numeric($node->key)) {
$result = db_query("SELECT * FROM {hosting_ssl_cert} WHERE ssl_key = '%s'", $node->key);
if ($obj = db_fetch_object($result)) {
// update
if ($client != null) {
$obj->client = $client;
if ($node->client != null) {
$obj->client = $node->client;
}
drupal_write_record("hosting_ssl_cert", $obj, 'cid');
}
else {
// insert
$obj = new stdClass();
$obj->ssl_key = $key;
$obj->client = $client;
$obj->ssl_key = $node->key;
$obj->client = $node->client;
$obj->status = 0;
$obj->ip_address = hosting_ip_allocate($node);
drupal_write_record("hosting_ssl_cert", $obj);
}
return $obj->cid;
}
return $key;
return $node->key;
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment