provision_tests.drush.inc

Some tests for hostmaster and provison.

These could live in Hostmaster or Provision, and there are advantages and disadvantages to both. But I decided that I'd just get on with it and pop them into Provision.

Functions

Namesort descending Description
drush_provision_tests_dl_module_to_site Helper function to download a module to a site.
drush_provision_tests_en_module_on_site Helper function to enable a module on a site.
drush_provision_tests_install_platform Helper function to install a platform.
drush_provision_tests_install_site Helper function to install a site.
drush_provision_tests_migrate_site Migrates a site from one platform to another.
drush_provision_tests_new_run Drush command to run the provision tests.
drush_provision_tests_provision_demo_content Drush command to setup some demo platforms and sites.
drush_provision_tests_remove_platform Helper function to remove a platform.
drush_provision_tests_remove_site Helper function to delete a site.
drush_provision_tests_run Drush command to run the provision tests.
drush_provision_tests_run_remaining_tasks Run all remaining hosting tasks.
provision_tests_drush_command Implementation of hook_drush_command().

Constants

File

provision-tests/provision_tests.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Some tests for hostmaster and provison.
  5. *
  6. * These could live in Hostmaster or Provision, and there are advantages and
  7. * disadvantages to both. But I decided that I'd just get on with it and pop
  8. * them into Provision.
  9. */
  10. define('PROVISION_TESTS_BUILDS_REPO', dirname(__FILE__) . '/makes');
  11. define('PROVISION_TESTS_DEFAULT_LOCK_WAIT', 60);
  12. define('PROVISION_TESTS_LOCK_TIMEOUT', 3600.0);
  13. /**
  14. * Implementation of hook_drush_command().
  15. */
  16. function provision_tests_drush_command() {
  17. $items['provision-tests-run'] = array(
  18. 'description' => dt('Runs provision tests'),
  19. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  20. // Although we're a provision command, we require hostmaster to be around to
  21. // run the tests correctly
  22. 'drupal dependencies' => array(
  23. 'hosting',
  24. ),
  25. 'options' => array(
  26. 'lock-wait' => dt('Time to wait to acquire a lock on the task queue. Defaults to :wait seconds.', array(':wait' => PROVISION_TESTS_DEFAULT_LOCK_WAIT)),
  27. ),
  28. );
  29. $items['provision-tests-new-run'] = array(
  30. 'description' => dt('Runs NEW provision tests'),
  31. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  32. // Although we're a provision command, we require hostmaster to be around to
  33. // run the tests correctly
  34. 'drupal dependencies' => array(
  35. 'hosting',
  36. ),
  37. 'options' => array(
  38. 'lock-wait' => dt('Time to wait to acquire a lock on the task queue. Defaults to :wait seconds.', array(':wait' => PROVISION_TESTS_DEFAULT_LOCK_WAIT)),
  39. ),
  40. );
  41. $items['provision-demo-content'] = array(
  42. 'description' => dt('Setup some demo content on a fresh Aegir install'),
  43. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  44. // Although we're a provision command, we require hostmaster to be around to
  45. // run the tests correctly
  46. 'drupal dependencies' => array(
  47. 'hosting',
  48. ),
  49. 'options' => array(
  50. 'lock-wait' => dt('Time to wait to acquire a lock on the task queue. Defaults to :wait seconds.', array(':wait' => PROVISION_TESTS_DEFAULT_LOCK_WAIT)),
  51. ),
  52. );
  53. return $items;
  54. }
  55. /**
  56. * Drush command to run the provision tests.
  57. */
  58. function drush_provision_tests_run() {
  59. if (!drush_confirm(dt('This command should only be run on a clean Aegir install, and data may be lost! Do you want to continue?'))) {
  60. return drush_user_abort();
  61. }
  62. // Disable the tasks queue, we run them manually instead.
  63. $queue_status_initial = variable_get('hosting_queue_tasks_enabled', '0');
  64. variable_set('hosting_queue_tasks_enabled', '0');
  65. $lock_wait = drush_get_option('lock-wait', PROVISION_TESTS_DEFAULT_LOCK_WAIT);
  66. if (!lock_wait('hosting_queue_tasks_running', $lock_wait) || drush_get_option('force', FALSE)) {
  67. if (lock_acquire('hosting_queue_tasks_running', PROVISION_TESTS_LOCK_TIMEOUT)) {
  68. drush_log('Acquired lock on task queue.');
  69. }
  70. elseif (drush_get_option('force', FALSE)) {
  71. drush_log('Bypassing lock on task queue.', 'warning');
  72. }
  73. else {
  74. drush_die(dt('Cannot acquire lock on task queue.'));
  75. }
  76. }
  77. else {
  78. drush_die(dt("Cannot acquire lock on task queue after waiting :wait seconds. A longer wait time can be set with the --lock-wait option.", array(':wait' => $lock_wait)));
  79. }
  80. drush_provision_tests_install_platform('drupal6');
  81. drush_provision_tests_install_platform('drupal7');
  82. // Install some sites.
  83. drush_provision_tests_install_site('drupal6', 'drupal6-default', 'default');
  84. drush_provision_tests_install_site('drupal7', 'drupal7-standard', 'standard');
  85. drush_provision_tests_install_site('drupal7', 'drupal7-minimal', 'minimal');
  86. // Remove the sites.
  87. drush_provision_tests_remove_site('drupal6-default');
  88. drush_provision_tests_remove_site('drupal7-standard');
  89. drush_provision_tests_remove_site('drupal7-minimal');
  90. // Create some sites and migrate them.
  91. drush_provision_tests_install_platform('drupal6', 'drupal6_other');
  92. drush_provision_tests_install_site('drupal6', 'drupal6-migrate-drupal6-other', 'default');
  93. drush_provision_tests_dl_module_to_site('token', 'drupal6-migrate-drupal6-other');
  94. drush_provision_tests_migrate_site('drupal6-migrate-drupal6-other', 'drupal6_other');
  95. drush_provision_tests_en_module_on_site('token', 'drupal6-migrate-drupal6-other');
  96. drush_provision_tests_remove_site('drupal6-migrate-drupal6-other');
  97. drush_provision_tests_remove_platform('drupal6_other');
  98. drush_provision_tests_install_platform('drupal7', 'drupal7_other');
  99. drush_provision_tests_install_site('drupal7', 'drupal7-migrate-drupal7-other', 'standard');
  100. drush_provision_tests_dl_module_to_site('token', 'drupal7-migrate-drupal7-other');
  101. drush_provision_tests_migrate_site('drupal7-migrate-drupal7-other', 'drupal7_other');
  102. drush_provision_tests_en_module_on_site('token', 'drupal7-migrate-drupal7-other');
  103. drush_provision_tests_remove_site('drupal7-migrate-drupal7-other');
  104. drush_provision_tests_remove_platform('drupal7_other');
  105. // Create some sites, and upgrade them
  106. drush_provision_tests_install_site('drupal6', 'drupal6-upgrade-drupal7', 'default');
  107. drush_provision_tests_migrate_site('drupal6-upgrade-drupal7', 'drupal7');
  108. drush_provision_tests_remove_site('drupal6-upgrade-drupal7');
  109. // Clean up a little.
  110. drush_provision_tests_remove_platform('drupal6');
  111. drush_provision_tests_remove_platform('drupal7');
  112. // Restore the tasks queue status:
  113. variable_set('hosting_queue_tasks_enabled', $queue_status_initial);
  114. drush_log(dt('Releasing lock on task queue.'));
  115. lock_release('hosting_queue_tasks_running');
  116. if (drush_get_error() != DRUSH_SUCCESS) {
  117. drush_set_error(drush_get_error(), 'Running tests failed');
  118. exit(1);
  119. }
  120. drush_log(dt('Tests completed successfully'), 'success');
  121. }
  122. /**
  123. * Drush command to run the provision tests.
  124. */
  125. function drush_provision_tests_new_run() {
  126. if (!drush_confirm(dt('This command should only be run on a clean Aegir install, and data may be lost! Do you want to continue?'))) {
  127. return drush_user_abort();
  128. }
  129. // Disable the tasks queue, we run them manually instead.
  130. $queue_status_initial = variable_get('hosting_queue_tasks_enabled', '0');
  131. variable_set('hosting_queue_tasks_enabled', '0');
  132. drush_log('Acquiring tasks lock.');
  133. $lock_wait = drush_get_option('lock-wait', PROVISION_TESTS_DEFAULT_LOCK_WAIT);
  134. if (!lock_wait('hosting_queue_tasks_running', $lock_wait) || drush_get_option('force', FALSE)) {
  135. if (lock_acquire('hosting_queue_tasks_running', PROVISION_TESTS_LOCK_TIMEOUT)) {
  136. drush_log('Acquired lock on task queue.');
  137. }
  138. elseif (drush_get_option('force', FALSE)) {
  139. drush_log('Bypassing lock on task queue.', 'warning');
  140. }
  141. else {
  142. drush_die(dt('Cannot acquire lock on task queue.'));
  143. }
  144. }
  145. else {
  146. drush_die(dt("Cannot acquire lock on task queue after waiting :wait seconds. A longer wait time can be set with the --lock-wait option.", array(':wait' => $lock_wait)));
  147. }
  148. // 'Stable' D8 e.g. latest (beta) release.
  149. drush_provision_tests_install_platform('drupal8');
  150. drush_provision_tests_install_site('drupal8', 'drupal8-minimal', 'standard');
  151. // Dev D8 latest git commit.
  152. drush_provision_tests_install_platform('drupal8dev');
  153. drush_provision_tests_install_site('drupal8dev', 'drupal8-dev-minimal', 'standard');
  154. // Migrate a D8 site from the current release to the latest commit.
  155. drush_provision_tests_install_site('drupal8', 'drupal8-upgrade-d8', 'standard');
  156. drush_provision_tests_migrate_site('drupal8-upgrade-d8', 'drupal8dev');
  157. // Cleanup.
  158. drush_provision_tests_remove_site('drupal8-upgrade-d8');
  159. drush_provision_tests_remove_site('drupal8-minimal');
  160. drush_provision_tests_remove_site('drupal8-dev-minimal');
  161. drush_provision_tests_remove_platform('drupal8');
  162. drush_provision_tests_remove_platform('drupal8dev');
  163. // Restore the tasks queue status:
  164. variable_set('hosting_queue_tasks_enabled', $queue_status_initial);
  165. drush_log('Releasing tasks lock.');
  166. lock_release('hosting_queue_tasks_running');
  167. if (drush_get_error() != DRUSH_SUCCESS) {
  168. drush_set_error(drush_get_error(), 'Running tests failed');
  169. exit(1);
  170. }
  171. drush_log(dt('Tests completed successfully'), 'success');
  172. }
  173. /**
  174. * Drush command to setup some demo platforms and sites.
  175. *
  176. * To use these demo sites let names like drupal7-standard.aegir.example.com
  177. * resolve to the IP of this Aegir server. E.g. in your hosts file.
  178. */
  179. function drush_provision_tests_provision_demo_content() {
  180. if (!drush_confirm(dt('This command should only be run on a clean Aegir install, and data may be lost! Do you want to continue?'))) {
  181. return drush_user_abort();
  182. }
  183. // Disable the tasks queue, we run them manually instead.
  184. $queue_status_initial = variable_get('hosting_queue_tasks_enabled', '0');
  185. variable_set('hosting_queue_tasks_enabled', '0');
  186. drush_log('Acquiring tasks lock.');
  187. $lock_wait = drush_get_option('lock-wait', PROVISION_TESTS_DEFAULT_LOCK_WAIT);
  188. if (!lock_wait('hosting_queue_tasks_running', $lock_wait) || drush_get_option('force', FALSE)) {
  189. if (lock_acquire('hosting_queue)tasks_running', PROVISION_TESTS_LOCK_TIMEOUT)) {
  190. drush_log('Acquired lock on task queue.');
  191. }
  192. elseif (drush_get_option('force', FALSE)) {
  193. drush_log('Bypassing lock on task queue.', 'warning');
  194. }
  195. else {
  196. drush_die(dt('Cannot acquire lock on task queue.'));
  197. }
  198. }
  199. else {
  200. drush_die(dt("Cannot acquire lock on task queue after waiting :wait seconds. A longer wait time can be set with the --lock-wait option.", array(':wait' => $lock_wait)));
  201. }
  202. // Start with D8 as it needs the most work/testing.
  203. drush_provision_tests_install_platform('drupal8');
  204. drush_provision_tests_install_site('drupal8', 'drupal8-minimal', 'minimal');
  205. // Prepare some platforms.
  206. drush_provision_tests_install_platform('drupal6');
  207. drush_provision_tests_install_platform('drupal7');
  208. drush_provision_tests_install_platform('openatrium2');
  209. // Install some sites.
  210. drush_provision_tests_install_site('drupal6', 'drupal6-default', 'default');
  211. drush_provision_tests_install_site('drupal7', 'drupal7-standard', 'standard');
  212. drush_provision_tests_install_site('drupal7', 'drupal7-minimal', 'minimal');
  213. drush_provision_tests_install_site('openatrium2', 'openatrium2-openatrium', 'openatrium');
  214. // Restore the tasks queue status:
  215. variable_set('hosting_queue_tasks_enabled', $queue_status_initial);
  216. drush_log('Releasing tasks lock.');
  217. lock_release('hosting_queue_tasks_running');
  218. if (drush_get_error() != DRUSH_SUCCESS) {
  219. drush_set_error(drush_get_error(), 'Setting up demo content failed');
  220. exit(1);
  221. }
  222. drush_log(dt('Demo content setup successfully'), 'success');
  223. }
  224. /**
  225. * Helper function to install a platform.
  226. */
  227. function drush_provision_tests_install_platform($platform_name, $platform_alias = NULL) {
  228. if (is_null($platform_alias)) {
  229. $platform_alias = $platform_name;
  230. }
  231. drush_log(dt('Building platform: @platform and adding to hostmaster.', array('@platform' => $platform_alias)), 'ok');
  232. $args = array(
  233. PROVISION_TESTS_BUILDS_REPO . "/$platform_name.make",
  234. "/var/aegir/platforms/$platform_alias"
  235. );
  236. $opts = array();
  237. if (version_compare(drush_core_version(), '7', '>=')) {
  238. $opts[] = '--shallow-clone';
  239. }
  240. drush_invoke_process('@none', 'make', $args, $opts);
  241. $args = array(
  242. "@platform_$platform_alias",
  243. );
  244. $options = array(
  245. 'root' => "/var/aegir/platforms/$platform_alias",
  246. 'context_type' => 'platform',
  247. );
  248. drush_invoke_process('@none', 'provision-save', $args, $options);
  249. provision_backend_invoke('@hostmaster', 'hosting-import', array("@platform_$platform_alias",));
  250. drush_provision_tests_run_remaining_tasks();
  251. }
  252. /**
  253. * Helper function to remove a platform.
  254. */
  255. function drush_provision_tests_remove_platform($platform_name) {
  256. drush_log(dt('Removing platform: @platform.', array('@platform' => $platform_name)), 'ok');
  257. provision_backend_invoke('@hostmaster', 'hosting-task', array("@platform_$platform_name", 'delete'), array('force' => TRUE));
  258. drush_provision_tests_run_remaining_tasks();
  259. }
  260. /**
  261. * Helper function to install a site.
  262. */
  263. function drush_provision_tests_install_site($platform_name, $site, $profile_name) {
  264. drush_log(dt('Installing: @site on platform: @platform with profile: @profile.', array('@site' => "$site.aegir.example.com", '@platform' => $platform_name, '@profile' => $profile_name)), 'ok');
  265. $args = array(
  266. "@$site.aegir.example.com",
  267. );
  268. $options = array(
  269. 'uri' => "$site.aegir.example.com",
  270. 'context_type' => 'site',
  271. 'platform' => "@platform_$platform_name",
  272. 'profile' => $profile_name,
  273. 'db_server' => '@server_localhost',
  274. 'root' => "/var/aegir/platforms/$platform_name",
  275. 'client_email' => 'this.email@is.invalid',
  276. );
  277. drush_invoke_process('@none', 'provision-save', $args, $options);
  278. $args = array();
  279. $options = array(
  280. 'client_email' => 'this.email@is.invalid',
  281. );
  282. provision_backend_invoke("@$site.aegir.example.com", 'provision-install', $args, $options);
  283. provision_backend_invoke('@hostmaster', 'hosting-task', array("@platform_$platform_name", 'verify'), array('force' => TRUE));
  284. drush_provision_tests_run_remaining_tasks();
  285. }
  286. /**
  287. * Helper function to download a module to a site.
  288. */
  289. function drush_provision_tests_dl_module_to_site($module, $site) {
  290. drush_log(dt('Downloading module: @module to site: @site.', array('@site' => "$site.aegir.example.com", '@module' => $module)), 'ok');
  291. $args = array(
  292. $module,
  293. );
  294. $options = array(
  295. // Normally 'dl' in a site context should know to put it in the site-
  296. // specific modules directory, but not through provision_backend_invoke()?
  297. 'destination' => "sites/$site.aegir.example.com/modules",
  298. );
  299. provision_backend_invoke("@$site.aegir.example.com", 'dl', $args, $options);
  300. drush_provision_tests_run_remaining_tasks();
  301. }
  302. /**
  303. * Helper function to enable a module on a site.
  304. */
  305. function drush_provision_tests_en_module_on_site($module, $site) {
  306. drush_log(dt('Enabling module: @module on site: @site.', array('@site' => "$site.aegir.example.com", '@module' => $module)), 'ok');
  307. $args = array(
  308. $module,
  309. );
  310. provision_backend_invoke("@$site.aegir.example.com", 'en', $args);
  311. // Failing 'en' only results in a warning, so we need to set our own error.
  312. $log = array_slice(drush_get_log(), 10);
  313. foreach ($log as $serial => $entry) {
  314. if ($entry['type'] == 'warning' && $entry['message'] == "$module was not found and will not be enabled.") {
  315. drush_set_error('DRUSH_FRAMEWORK_ERROR', "Error enabling module: $module on site: $site.");
  316. }
  317. }
  318. drush_provision_tests_run_remaining_tasks();
  319. }
  320. /**
  321. * Helper function to delete a site.
  322. */
  323. function drush_provision_tests_remove_site($site) {
  324. drush_log(dt('Removing: @site.', array('@site' => "$site.aegir.example.com")), 'ok');
  325. provision_backend_invoke('@hostmaster', 'hosting-task', array("@$site.aegir.example.com", 'delete'), array('force' => TRUE));
  326. drush_provision_tests_run_remaining_tasks();
  327. }
  328. /**
  329. * Migrates a site from one platform to another.
  330. *
  331. * @param $site
  332. * The site to migrate.
  333. * @param $target
  334. * The target platform to migrate to.
  335. */
  336. function drush_provision_tests_migrate_site($site, $target) {
  337. drush_log(dt('Migrating: @site to platform: @platform.', array('@site' => "$site.aegir.example.com", '@platform' => $target)), 'ok');
  338. // Do the migrate.
  339. provision_backend_invoke("@$site.aegir.example.com", 'provision-migrate', array("@platform_$target",));
  340. // Import the site into the frontend.
  341. provision_backend_invoke('@hostmaster', 'hosting-import', array("@$site.aegir.example.com",));
  342. // Verify the $target platform.
  343. provision_backend_invoke('@hostmaster', 'hosting-task', array("@platform_$target", 'verify'), array('force' => TRUE));
  344. // Import and verify the site.
  345. provision_backend_invoke('@hostmaster', 'hosting-import', array("@$site.aegir.example.com",));
  346. provision_backend_invoke('@hostmaster', 'hosting-task', array("@$site.aegir.example.com", 'verify'), array('force' => TRUE));
  347. drush_provision_tests_run_remaining_tasks();
  348. }
  349. /**
  350. * Run all remaining hosting tasks.
  351. */
  352. function drush_provision_tests_run_remaining_tasks() {
  353. $tasks = array();
  354. $result = db_query("SELECT t.nid FROM {hosting_task} t INNER JOIN {node} n ON t.vid = n.vid WHERE t.task_status = :status ORDER BY n.changed, n.nid ASC", array(':status' => 0));
  355. while ($node = $result->fetch()) {
  356. $tasks[$node->nid] = node_load($node->nid);
  357. }
  358. foreach ($tasks as $task) {
  359. provision_backend_invoke('@hostmaster', "hosting-task", array($task->nid), array('force' => TRUE));
  360. }
  361. }