hosting.ip.inc

General IP address CRUD routines.

Functions

Namesort descending Description
hosting_ip_allocate Allocate an IP for a given site on a given server.
hosting_server_ip_allocate Allocate a single IP for a single server.
_hosting_ip_delete IP Utility function for hook_delete.
_hosting_ip_load IP Utility function for hook_load.
_hosting_ip_save IP Utility function for hook_update/hook_insert.
_hosting_ip_validate Validate that IP list is valid. IP utility function for hook_validate.
_hosting_ip_view Display the ip address on the node. IP utility function for hook_view.

File

server/hosting.ip.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * General IP address CRUD routines.
  5. */
  6. /**
  7. * Display the ip address on the node. IP utility function for hook_view.
  8. */
  9. function _hosting_ip_view(&$node) {
  10. $ip_list = isset($node->ip_addresses) ? $node->ip_addresses : array();
  11. if (sizeof($ip_list)) {
  12. $node->content['info']['ip_addresses'] = array(
  13. '#type' => 'item',
  14. '#title' => t('IP addresses'),
  15. '#markup' => implode('<br />', $ip_list),
  16. );
  17. }
  18. }
  19. /**
  20. * IP Utility function for hook_update/hook_insert.
  21. */
  22. function _hosting_ip_save($node, $update = FALSE) {
  23. $ips = isset($node->ip_addresses) ? (array) $node->ip_addresses : array();
  24. foreach ($ips as $id => $ip) {
  25. $ip = trim($ip);
  26. if (empty($ip)) {
  27. // empty fields are considered removed
  28. db_delete('hosting_ip_addresses')
  29. ->condition('id', $id)
  30. ->execute();
  31. }
  32. else {
  33. $num_updated = db_update('hosting_ip_addresses')
  34. ->fields(array(
  35. 'ip_address' => $ip,
  36. ))
  37. ->condition('id', $id)
  38. ->execute();
  39. }
  40. }
  41. $ips = isset($node->new_ip_addresses) ? (array) $node->new_ip_addresses : array();
  42. foreach ($ips as $id => $ip) {
  43. if (!empty($ip)) {
  44. // new entries are prefixed with the string 'new', insert those
  45. db_insert('hosting_ip_addresses')
  46. ->fields(array(
  47. 'nid' => $node->nid,
  48. 'ip_address' => $ip,
  49. ))
  50. ->execute();
  51. }
  52. }
  53. }
  54. /**
  55. * Validate that IP list is valid. IP utility function for hook_validate.
  56. */
  57. function _hosting_ip_validate($node) {
  58. $ips = isset($node->ip_addresses) ? $node->ip_addresses : array();
  59. foreach ($ips as $id => $ip) {
  60. $ip = trim($ip);
  61. if (empty($ip)) {
  62. // deletion, look if the IP is in use
  63. $cid = db_query('SELECT ssl_key FROM {hosting_ssl_cert_ips} i INNER JOIN {hosting_ssl_cert} c ON c.cid = i.cid WHERE ip_address = :id', array(':id' => $id))->fetchField();
  64. if ($cid) {
  65. form_set_error("ip_addresses][$id", t('Cannot remove IP associated with certificate %cert.', array('%cert' => $cid)));
  66. }
  67. } elseif (!_hosting_valid_ip($ip)) {
  68. form_set_error("ip_addresses][$id", t('Invalid IP address: %ip.', array(
  69. '%ip' => $ip )));
  70. }
  71. }
  72. $ips = isset($node->new_ip_addresses) ? (array) $node->new_ip_addresses : array();
  73. foreach ($ips as $id => $ip) {
  74. if (!empty($ip) && !_hosting_valid_ip($ip)) {
  75. form_set_error("ip_addresses][$id", t('Invalid IP address: %ip.', array(
  76. '%ip' => $ip )));
  77. }
  78. }
  79. }
  80. /**
  81. * IP Utility function for hook_load.
  82. */
  83. function _hosting_ip_load($node) {
  84. $ip_list = array();
  85. $result = db_query("SELECT id,ip_address FROM {hosting_ip_addresses} WHERE nid = :nid ORDER BY id", array(':nid' => $node->nid));
  86. while ($obj = $result->fetch()) {
  87. $ip_list[$obj->id] = $obj->ip_address;
  88. }
  89. return $ip_list;
  90. }
  91. /**
  92. * IP Utility function for hook_delete.
  93. */
  94. function _hosting_ip_delete($node) {
  95. db_delete('hosting_ip_addresses')
  96. ->condition('nid', $node->nid)
  97. ->execute();
  98. }
  99. /**
  100. * Allocate an IP for a given site on a given server.
  101. */
  102. function hosting_ip_allocate($cert, $site) {
  103. // make sure the IP is not allocated while we pick ours
  104. db_query("LOCK TABLES {hosting_ssl_cert_ips} WRITE, {hosting_ip_addresses} WRITE");
  105. $platform = node_load($site->platform);
  106. $server = node_load($platform->web_server);
  107. // IP allocation for master server
  108. $ips = array();
  109. // IP allocation for nodes in the cluster
  110. // XXX: this should be in classes, see below
  111. switch ($server->services['http']->type) {
  112. case 'pack':
  113. foreach ($server->services['http']->master_servers as $s) {
  114. $ips[] = hosting_server_ip_allocate($s);
  115. }
  116. foreach ($server->services['http']->slave_servers as $s) {
  117. $ips[] = hosting_server_ip_allocate($s);
  118. }
  119. break;
  120. case 'cluster':
  121. foreach ($server->services['http']->web_servers as $s) {
  122. $ips[] = hosting_server_ip_allocate($s);
  123. }
  124. break;
  125. default:
  126. $ips = array(hosting_server_ip_allocate($server->nid));
  127. }
  128. foreach ($ips as $ip) {
  129. if (!$ip) {
  130. // IP allocation failed, unlock tables and return false
  131. db_query("UNLOCK TABLES");
  132. return $ip;
  133. }
  134. }
  135. // IP allocation succeeded, actually insert all required entries
  136. foreach ($ips as $ip) {
  137. if ($ip) {
  138. $id = db_insert('hosting_ssl_cert_ips')
  139. ->fields(array(
  140. 'cid' => $cert->cid,
  141. 'ip_address' => $ip,
  142. ))
  143. ->execute();
  144. }
  145. }
  146. db_query("UNLOCK TABLES");
  147. return $ips;
  148. }
  149. /**
  150. * Allocate a single IP for a single server.
  151. *
  152. * XXX: this should be in the 'server' class (which doesn't exist yet)
  153. * or at least in the services class
  154. *
  155. * @param int $server
  156. * The server node ID.
  157. *
  158. * @return int
  159. * Id of the next available IP.
  160. */
  161. function hosting_server_ip_allocate($server) {
  162. // guess the next available IP
  163. return db_query("SELECT hosting_ip_addresses.id
  164. FROM {hosting_ip_addresses}
  165. LEFT JOIN {hosting_ssl_cert_ips}
  166. ON (hosting_ip_addresses.id = hosting_ssl_cert_ips.ip_address)
  167. WHERE hosting_ssl_cert_ips.ip_address IS NULL
  168. AND nid = :nid LIMIT 1;",
  169. array(':nid' => $server)
  170. )->fetchField();
  171. }