hosting_site.drush.inc

Drush hooks for the Hosting site module.

Functions

Namesort descending Description
drush_hosting_site_pre_hosting_task Map values of site node into command line arguments
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_hosting_post_DELETE().
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 implementation of the hosting_post_install hook
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().

File

site/hosting_site.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Drush hooks for the Hosting site module.
  5. */
  6. /**
  7. * Implements hook_hosting_TASK_OBJECT_context_options().
  8. */
  9. function hosting_hosting_site_context_options(&$task) {
  10. $task->context_options['db_server'] = hosting_context_name($task->ref->db_server);
  11. $task->context_options['platform'] = hosting_context_name($task->ref->platform);
  12. $task->context_options['uri'] = strtolower(trim($task->ref->title));
  13. $task->context_options['language'] = $task->ref->site_language;
  14. $profile = node_load($task->ref->profile);
  15. $task->context_options['profile'] = $profile->short_name;
  16. $client = node_load($task->ref->client);
  17. $user = user_load($task->uid);
  18. if ($task->task_type != 'import' && $task->task_type != 'delete' && $task->task_type != 'verify') {
  19. $task->options['client_email'] = $user->mail;
  20. }
  21. $task->context_options['client_name'] = $client->uname;
  22. }
  23. /**
  24. * Implements hook_drush_context_import().
  25. */
  26. function hosting_site_drush_context_import($context, &$node) {
  27. if ($context->type == 'site') {
  28. if (!isset($node->title)) {
  29. $node->title = strtolower(trim($context->uri)); // force lowercase for existing uri
  30. }
  31. $node->site_language = $context->language;
  32. $node->cron_key = $context->cron_key;
  33. $node->db_server = hosting_context_nid($context->db_server->name);
  34. $node->platform = hosting_context_nid($context->platform->name);
  35. // TODO: abstract this to remove duplication with import post hooks.
  36. $profile = hosting_package_instance_load(array(
  37. 'i.rid' => $node->platform,
  38. 'p.short_name' => $context->profile,
  39. ));
  40. if (!$profile) {
  41. // For D6 to D7 upgrades, try changing default to standard.
  42. if (!empty($context->profile) && $context->profile == 'default') {
  43. $profile = hosting_package_instance_load(array(
  44. 'i.rid' => $node->platform,
  45. 'p.short_name' => 'standard',
  46. ));
  47. if ($profile) {
  48. drush_log(dt("Could not find the Profile !profile for Platfrom ID !platform, substituted standard", array('!profile' => $context->profile, '!platform' => $node->platform)), 'warning');
  49. }
  50. }
  51. }
  52. // Log an error if there is still no profile.
  53. if (!$profile) {
  54. drush_set_error("HOSTING_SITE_IMPORT_ERROR", dt("Could not find the Profile !profile for Platfrom ID !platform", array('!profile' => $context->profile, '!platform' => $node->platform)));
  55. }
  56. $node->profile = $profile->package_id;
  57. $client = node_load(HOSTING_DEFAULT_CLIENT);
  58. if ($context->client_name) {
  59. $client = hosting_import_client($context->client_name);
  60. }
  61. $node->client = $client->nid;
  62. }
  63. }
  64. /**
  65. * Map values of site node into command line arguments
  66. */
  67. function drush_hosting_site_pre_hosting_task($task) {
  68. $task = &drush_get_context('HOSTING_TASK');
  69. if ($task->task_type == 'restore') {
  70. $backup = hosting_site_get_backup($task->task_args['bid']);
  71. $task->args[1] = $backup['filename'];
  72. }
  73. if ($task->task_type == 'backup-delete') {
  74. foreach ($task->task_args as $bid => $filename) {
  75. if ($filename !== '0') {
  76. $backups[] = $filename;
  77. }
  78. }
  79. $task->args[1] = implode(',', $backups);
  80. }
  81. }
  82. /**
  83. * implementation of the hosting_post_install hook
  84. */
  85. function hosting_site_post_hosting_install_task($task, $data) {
  86. if ($task->ref->type == 'site') {
  87. $context = $data['context'];
  88. $packages = $context['packages'];
  89. hosting_package_sync($packages);
  90. hosting_package_instance_sync($task->ref->nid, $task->ref->type, $packages);
  91. $task->ref->site_status = HOSTING_SITE_ENABLED;
  92. $task->ref->no_verify = TRUE;
  93. $task->ref->verified = REQUEST_TIME;
  94. $task->ref->db_name = $data['self']['db_name'];
  95. if ($context['cron_key']) {
  96. $task->ref->cron_key = $context['cron_key'];
  97. }
  98. node_save($task->ref);
  99. if ($context['login_link']) {
  100. drush_log($context['login_link']);
  101. // we need to store the timestamp too because expire only means it can be deleted
  102. // after an amount of time, not will be deleted.
  103. $cache = array(
  104. 'expire' => strtotime("+24 hours"),
  105. 'link' => $context['login_link'],
  106. );
  107. cache_set('hosting:site:' . $task->ref->nid . ':login_link', $cache, 'cache', $cache['expire'] );
  108. }
  109. }
  110. }
  111. /**
  112. * Implements the hosting_failed_install hook
  113. */
  114. function hosting_site_hosting_install_task_rollback($task, $data) {
  115. // @TODO : we need to check the returned list of errors, not the code.
  116. if (drush_cmp_error('PROVISION_DRUPAL_SITE_INSTALLED')) {
  117. // Site has already been installed. Try to import instead.
  118. drush_log(dt("This site appears to be installed already. Generating an import task."));
  119. hosting_add_task($task->rid, 'import');
  120. }
  121. else {
  122. $task->ref->no_verify = TRUE;
  123. $task->ref->site_status = HOSTING_SITE_DISABLED;
  124. node_save($task->ref);
  125. }
  126. }
  127. /**
  128. * Implements hook_hosting_post_disable().
  129. * I am not very fond of the use of bitwise operators with negatives.
  130. */
  131. function hosting_site_post_hosting_disable_task($task, $data) {
  132. if ($data['context']['backup_file'] && $data->ref->type == 'site') {
  133. $platform = node_load($task->ref->platform);
  134. hosting_site_add_backup($task->ref->nid, $platform->web_server, $data['context']['backup_file'], t('Generated before being disabled'), $data['context']['backup_file_size']);
  135. }
  136. $task->ref->site_status = HOSTING_SITE_DISABLED;
  137. $task->ref->no_verify = TRUE;
  138. node_save($task->ref);
  139. // Record the backup created during disable.
  140. $task->task_args['description'] = t('Pre-disable backup');
  141. hosting_site_post_hosting_backup_task($task, $data);
  142. }
  143. /**
  144. * Implements hook_hosting_post_restore().
  145. * I am not very fond of the use of bitwise operators with negatives.
  146. */
  147. function hosting_site_post_hosting_restore_task($task, $data) {
  148. if ($data['context']['backup_file'] && $task->ref->type == 'site') {
  149. $platform = node_load($task->ref->platform);
  150. 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']);
  151. }
  152. }
  153. /**
  154. * Implements hook_hosting_post_enable().
  155. */
  156. function hosting_site_post_hosting_enable_task($task, $data) {
  157. $task->ref->site_status = HOSTING_SITE_ENABLED;
  158. $task->ref->no_verify = TRUE;
  159. node_save($task->ref);
  160. }
  161. /**
  162. * Implements hook_hosting_post_DELETE().
  163. */
  164. function hosting_site_post_hosting_delete_task($task, $data) {
  165. if ($task->ref->type == 'site') {
  166. $task->ref->site_status = HOSTING_SITE_DELETED;
  167. $task->ref->no_verify = TRUE;
  168. hosting_context_delete($task->ref->nid);
  169. node_save($task->ref);
  170. // Record the backup created during delete.
  171. $task->task_args['description'] = t('Pre-delete backup');
  172. hosting_site_post_hosting_backup_task($task, $data);
  173. // Clean up package instances
  174. hosting_package_instance_sync($task->ref->nid, 'site');
  175. }
  176. }
  177. /**
  178. * Implements hook_hosting_post_backup().
  179. *
  180. * Adds the data file that was saved to the site backup history.
  181. * This is needed to be able to restore.
  182. */
  183. function hosting_site_post_hosting_backup_task($task, $data) {
  184. if ($data['context']['backup_file'] && $task->ref->type == 'site') {
  185. $platform = node_load($task->ref->platform);
  186. $desc = $task->task_args['description'];
  187. $desc = ($desc) ? $desc : t('Generated on request');
  188. hosting_site_add_backup($task->ref->nid, $platform->web_server, $data['context']['backup_file'], $desc, $data['context']['backup_file_size']);
  189. }
  190. }
  191. /**
  192. * Implements hook_post_hosting_import_task().
  193. */
  194. function hosting_site_post_hosting_import_task($task, $data) {
  195. $site_nid = hosting_drush_import($task->ref->hosting_name);
  196. // Update the site node with the database name.
  197. $node = node_load($site_nid);
  198. $node->db_name = $data['self']['db_name'];
  199. node_save($node);
  200. $packages = $data['context']['packages'];
  201. hosting_package_sync($packages);
  202. hosting_package_instance_sync($task->ref->nid, $task->ref->type, $packages);
  203. }
  204. /**
  205. * Implements hook_post_hosting_verify_task().
  206. */
  207. function hosting_site_post_hosting_verify_task($task, $data) {
  208. if ($task->ref->type == 'site') {
  209. $task->ref->verified = REQUEST_TIME;
  210. $task->ref->no_verify = TRUE;
  211. $task->ref->db_name = $data['self']['db_name'];
  212. if ($data['context']['cron_key']) {
  213. $task->ref->cron_key = $data['context']['cron_key'];
  214. }
  215. node_save($task->ref);
  216. $context = $data['context'];
  217. $packages = $context['packages'];
  218. hosting_package_sync($packages);
  219. hosting_package_instance_sync($task->ref->nid, $task->ref->type, $packages);
  220. }
  221. }
  222. /**
  223. * Implements the hosting_failed_install hook
  224. */
  225. function hosting_site_hosting_verify_task_rollback($task, $data) {
  226. if ($task->ref->type == 'site') {
  227. $task->ref->no_verify = TRUE;
  228. $task->ref->verified = 0;
  229. node_save($task->ref);
  230. }
  231. }
  232. /**
  233. * Implements hook_hosting_import_task_rollback().
  234. */
  235. function hosting_site_hosting_import_task_rollback($task, $data) {
  236. if ($task->ref->type == 'site') {
  237. $task->ref->no_verify = TRUE;
  238. $task->ref->verified = 0;
  239. $task->ref->site_status = HOSTING_SITE_DISABLED;
  240. node_save($task->ref);
  241. }
  242. }
  243. /**
  244. * Implements hook_post_hosting_login_reset_task().
  245. */
  246. function hosting_site_post_hosting_login_reset_task($task, $data) {
  247. if ($task->ref->type == 'site') {
  248. $task->ref->no_verify = TRUE;
  249. node_save($task->ref);
  250. $context = $data['context'];
  251. if ($context['login_link']) {
  252. // we need to store the timestamp too because expire only means it can be deleted
  253. // after an amount of time, not will be deleted.
  254. $cache = array(
  255. 'expire' => strtotime("+24 hours"),
  256. 'link' => $context['login_link'],
  257. );
  258. cache_set('hosting:site:' . $task->ref->nid . ':login_link', $cache, 'cache', $cache['expire'] );
  259. }
  260. }
  261. }
  262. /**
  263. * Implements the hosting_post_backup_delete hook
  264. */
  265. function hosting_site_post_hosting_backup_delete_task($task, $data) {
  266. if ($task->ref->type == 'site') {
  267. foreach ($task->task_args as $bid => $filename) {
  268. if ($filename !== '0') {
  269. hosting_site_delete_backup($bid);
  270. }
  271. }
  272. }
  273. }