tripal.drush.inc

Contains function relating to drush-integration of this module.

File

tripal/tripal.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains function relating to drush-integration of this module.
  5. */
  6. /**
  7. * @defgroup tripal_drush Tripal Drush Integration
  8. * @{
  9. * Contains function relating to drush-integration of various tripal modules.
  10. * @}
  11. */
  12. /**
  13. * Describes each drush command implemented by the module
  14. *
  15. * @return
  16. * The first line of description when executing the help for a given command
  17. *
  18. * @ingroup tripal_drush
  19. */
  20. function tripal_drush_help($command) {
  21. switch ($command) {
  22. // TRIPAL JOBS
  23. case 'trp-run-jobs':
  24. return dt('Launches pending jobs waiting in the queue.');
  25. break;
  26. case 'trp-rerun-job':
  27. return dt('Rerun a job in the queue.');
  28. break;
  29. case 'trp-get-currjob':
  30. return dt('Returns details about the currently running tripal job including percent complete.');
  31. break;
  32. // Placeholders for unimplmeneted jobs
  33. case 'trp-show-job':
  34. break;
  35. case 'trp-revert-jobs':
  36. break;
  37. case 'trp-cancel-job':
  38. break;
  39. case 'trp-list-jobs':
  40. break;
  41. case 'trp-prepare-chado':
  42. break;
  43. case 'trp-set-permissions':
  44. break;
  45. }
  46. }
  47. /**
  48. * Registers a drush command and constructs the full help for that command.
  49. *
  50. * @return
  51. * And array of command descriptions
  52. *
  53. * @ingroup tripal_drush
  54. */
  55. function tripal_drush_command() {
  56. $items = array();
  57. $items['trp-get-currjob'] = array(
  58. 'description' => dt('Returns details about the currently running job including percent complete.'),
  59. 'arguments' => array(),
  60. 'examples' => array(
  61. 'Standard example' => 'drush trp-get-currjob',
  62. ),
  63. );
  64. $items['trp-run-jobs'] = array(
  65. 'description' => dt('Launches jobs waiting in the queue. Only one job can execute at a time unless the --parallel=1 option is provided.'),
  66. 'examples' => array(
  67. 'Single Job' => 'drush trp-run-jobs --username=administrator',
  68. 'Parallel Job' => 'drush trp-run-jobs --username=administrator --parallel=1',
  69. 'Max-jobs Job' => 'drush trp-run-jobs --username=administrator --parallel=1 --max_jobs=10',
  70. ),
  71. 'arguments' => array(),
  72. 'options' => array(
  73. 'user' => array(
  74. 'description' => dt('DEPRECATED. Conflicts with Drush 7.x --user argument. Please use the --username argument.'),
  75. ),
  76. 'username' => array(
  77. 'description' => dt('The Drupal user name for which the job should be run. The permissions for this user will be used.'),
  78. ),
  79. 'parallel' => dt('Normally jobs are executed one at a time. But if you are certain no conflicts will occur with other currently running jobs you may set this argument to a value of 1 to make the job run in parallel with other running jobs.'),
  80. 'job_id' => dt('Provide a job_id to run a specific job. Only jobs that have not been run already can be used'),
  81. 'max_jobs' => dt('Indicate the maximum number of concurrent jobs. Default is -1 (unlimited). Ignore if not running parallel jobs'),
  82. 'single' => dt('Execute only one queued job'),
  83. ),
  84. );
  85. $items['trp-rerun-job'] = array(
  86. 'description' => dt('Re-run a specific job from the queue.'),
  87. 'examples' => array(
  88. 'Single Job' => 'drush trp-rerun-job --username=administrator --job_id=2',
  89. 'Parallel Job' => 'drush trp-rerun-job --username=administrator --job_id=2 --parallel=1',
  90. 'Max-jobs Job' => 'drush trp-run-jobs --username=administrator --parallel=1 --max_jobs=10',
  91. ),
  92. 'arguments' => array(),
  93. 'options' => array(
  94. 'user' => array(
  95. 'description' => dt('DEPRECATED. Conflicts with Drush 7.x --user argument. Please use the --username argument.'),
  96. ),
  97. 'username' => array(
  98. 'description' => dt('The Drupal user name for which the job should be run. The permissions for this user will be used.'),
  99. ),
  100. 'job_id' => array(
  101. 'description' => dt('The job ID to run.'),
  102. 'required' => TRUE,
  103. ),
  104. 'parallel' => dt('Normally jobs are executed one at a time. But if you are certain no conflicts will occur with other currently running jobs you may set this argument to a value of 1 to make the job run in parallel with other running jobs.'),
  105. 'max_jobs' => dt('Indicate the maximum number of concurrent jobs. Default is -1 (unlimited). Ignore if not running parallel jobs'),
  106. 'single' => dt('Execute only one queued job'),
  107. ),
  108. );
  109. $items['trp-prepare-chado'] = array(
  110. 'description' => dt('Prepares a new Tripal installation with content types, requires the username of an administrator to run.'),
  111. 'arguments' => array(),
  112. 'examples' => array(
  113. 'Standard example' => 'drush trp-prepare-chado --user=administrator',
  114. ),
  115. );
  116. $items['trp-set-permissions'] = array(
  117. 'description' => dt('Gives view, edit, delete, create priveleges to administrators for all tripal content types.'),
  118. 'arguments' => array(),
  119. 'examples' => array(
  120. 'Standard example' => 'drush trp-set-permissions --user=administrator',
  121. ),
  122. );
  123. return $items;
  124. }
  125. /**
  126. * Set the user to run a drush job.
  127. *
  128. * @ingroup tripal_drush
  129. */
  130. function drush_tripal_set_user($username) {
  131. if ($username) {
  132. $sql = "SELECT uid FROM {users} WHERE name = :name";
  133. $results = db_query($sql, array(':name' => $username));
  134. $u = $results->fetchObject();
  135. if (!$u) {
  136. drush_print(date('Y-m-d H:i:s'));
  137. drush_print('ERROR: Please provide a valid username (--username argument) for running this job.');
  138. exit;
  139. }
  140. global $user;
  141. $user = user_load($u->uid);
  142. return $u->uid;
  143. }
  144. else {
  145. drush_print(date('Y-m-d H:i:s'));
  146. drush_print('ERROR: Please provide a username (--username argument) for running this job.');
  147. exit;
  148. }
  149. }
  150. /**
  151. * Executes jobs in the Tripal Jobs Queue.
  152. *
  153. * Executed when 'drush trp-run-job' is called.
  154. *
  155. * @ingroup tripal_drush
  156. */
  157. function drush_tripal_trp_run_jobs_install($username) {
  158. $parallel = drush_get_option('parallel');
  159. $job_id = drush_get_option('job_id');
  160. $max_jobs = drush_get_option('max_jobs', -1);
  161. $single = drush_get_option('single', 0);
  162. drush_tripal_set_user($username);
  163. drush_print("\n" . date('Y-m-d H:i:s'));
  164. if ($parallel) {
  165. drush_print("Tripal Job Launcher (in parallel)");
  166. if ($max_jobs !== -1) drush_print("Maximum number of jobs is " . $max_jobs);
  167. drush_print("Running as user '$username'");
  168. drush_print("-------------------");
  169. tripal_launch_job($parallel, $job_id, $max_jobs, $single);
  170. }
  171. else {
  172. drush_print("Tripal Job Launcher");
  173. drush_print("Running as user '$username'");
  174. drush_print("-------------------");
  175. tripal_launch_job(0, $job_id, $max_jobs, $single);
  176. }
  177. }
  178. /**
  179. * Executes jobs in the Tripal Jobs Queue.
  180. *
  181. * Executed when 'drush trp-run-job' is called.
  182. *
  183. * @ingroup tripal_drush
  184. */
  185. function drush_tripal_trp_run_jobs() {
  186. $parallel = drush_get_option('parallel');
  187. $job_id = drush_get_option('job_id');
  188. $max_jobs = drush_get_option('max_jobs', -1);
  189. $single = drush_get_option('single', 0);
  190. // Unfortunately later versions of Drush use the '--user' argument which
  191. // makes it incompatible with how Tripal was using it. For backwards
  192. // compatibility we will accept --user with a non numeric value only. The
  193. // numeric value should be for Drush. Tripal will instead use the
  194. // --username argument for the fture.
  195. $user = drush_get_option('user');
  196. $uname = drush_get_option('username');
  197. if ($user and is_numeric($user)) {
  198. }
  199. elseif ($user) {
  200. print "\nNOTE: Use of the --user argument is deprecated as it conflicts with the --user argument of Drush 7.x. Please now use --username instead.\n\n";
  201. $username = $user;
  202. }
  203. if ($uname) {
  204. $username = $uname;
  205. }
  206. drush_tripal_set_user($username);
  207. drush_print("\n" . date('Y-m-d H:i:s'));
  208. if ($parallel) {
  209. drush_print("Tripal Job Launcher (in parallel)");
  210. if ($max_jobs !== -1) drush_print("Maximum number of jobs is " . $max_jobs);
  211. drush_print("Running as user '$username'");
  212. drush_print("-------------------");
  213. tripal_launch_job($parallel, $job_id, $max_jobs, $single);
  214. }
  215. else {
  216. drush_print("Tripal Job Launcher");
  217. drush_print("Running as user '$username'");
  218. drush_print("-------------------");
  219. tripal_launch_job(0, $job_id, $max_jobs, $single);
  220. }
  221. }
  222. /**
  223. * Executes jobs in the Tripal Jobs Queue.
  224. *
  225. * Executed when 'drush trp-rerun-job' is called.
  226. *
  227. * @ingroup tripal_drush
  228. */
  229. function drush_tripal_trp_rerun_job() {
  230. // Unfortunately later versions of Drush use the '--user' argument which
  231. // makes it incompatible with how Tripal was using it. For backwards
  232. // compatabiliy we will accept --user with a non numeric value only. The
  233. // numeric value should be for Drush. Tripal will instead use the
  234. // --username argument for the fture.
  235. $user = drush_get_option('user');
  236. $uname = drush_get_option('username');
  237. print date('Y-m-d H:i:s') . ": USER: '$user', UNAME: '$uname'\n";
  238. if ($user and is_numeric($user)) {
  239. }
  240. elseif ($user) {
  241. print "\nNOTE: Use of the --user argument is deprecated as it conflicts with the --user argument of Drush 7.x. Please now use --username instead.\n\n";
  242. $username = $user;
  243. }
  244. if ($uname) {
  245. $username = $uname;
  246. }
  247. $parallel = drush_get_option('parallel');
  248. $job_id = drush_get_option('job_id');
  249. $max_jobs = drush_get_option('max_jobs', -1);
  250. $single = drush_get_option('single', 0);
  251. drush_tripal_set_user($username);
  252. $new_job_id = tripal_rerun_job($job_id, FALSE);
  253. drush_print("\n" . date('Y-m-d H:i:s'));
  254. if ($parallel) {
  255. drush_print("Tripal Job Launcher (in parallel)");
  256. drush_print("Running as user '$username'");
  257. drush_print("-------------------");
  258. tripal_launch_job($parallel, $new_job_id, $max_jobs, $single);
  259. }
  260. else {
  261. drush_print("Tripal Job Launcher");
  262. drush_print("Running as user '$username'");
  263. drush_print("-------------------");
  264. tripal_launch_job(0, $new_job_id, $max_jobs, $single);
  265. }
  266. }
  267. /**
  268. * Prints details about the current running job.
  269. *
  270. * Executed when 'drush trp-get-currjob' is called.
  271. *
  272. * @ingroup tripal_drush
  273. */
  274. function drush_tripal_trp_get_currjob() {
  275. $sql = "
  276. SELECT *
  277. FROM {tripal_jobs} TJ
  278. WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL
  279. ";
  280. $jobs = db_query($sql);
  281. foreach ($jobs as $job) {
  282. $job_pid = $job->pid;
  283. $output = "Name: " . $job->job_name . "\n" .
  284. "Submitted: " . date(DATE_RFC822, $job->submit_date) . "\n" .
  285. "Started: " . date(DATE_RFC822, $job->start_time) . "\n" .
  286. "Module: " . $job->modulename . "\n" .
  287. "Callback: " . $job->callback . "\n" .
  288. "Process ID: " . $job->pid . "\n" .
  289. "Progress: " . $job->progress . "%\n".
  290. "Current Date: " . date('Y-m-d H:i:s') . "\n";
  291. drush_print(date('Y-m-d H:i:s'));
  292. drush_print($output);
  293. }
  294. if (!$job_pid) {
  295. drush_print(date('Y-m-d H:i:s'));
  296. drush_print('There are currently no running jobs.');
  297. }
  298. //log to the command line with an OK status
  299. drush_log('Running tripal-current-job', 'ok');
  300. }
  301. /**
  302. * Prepares content types on the site after chado installation.
  303. *
  304. * Executed when 'drush trp-prepare-chado' is called.
  305. *
  306. * @ingroup tripal_drush
  307. */
  308. function drush_tripal_trp_prepare_chado() {
  309. $user = drush_get_option('user');
  310. $uname = drush_get_option('username');
  311. if ($user and is_numeric($user)) {
  312. }
  313. elseif ($user) {
  314. print "\nNOTE: Use of the --user argument is deprecated as it conflicts with the --user argument of Drush 7.x. Please now use --username instead.\n\n";
  315. $username = $user;
  316. }
  317. if ($uname) {
  318. $username = $uname;
  319. }
  320. drush_tripal_set_user($username);
  321. print_r("Now preparing the site by creating content types.\n");
  322. $prepare = drush_invoke_process('@self', 'php-eval', array("module_load_include('inc', 'tripal_chado', 'includes/setup/tripal_chado.setup'); tripal_chado_prepare_drush_submit();"), array());
  323. drush_invoke_process('@self', 'php-eval', array("module_load_include('inc', 'tripal', 'tripal.drush'); drush_tripal_trp_run_jobs_install(" . $username . ");"), array());
  324. if (!$prepare) {
  325. echo "An error occurred when attempting to install Chado. Please navigate to your new site and finish the installation process from the 'Install Tripal' section as described in the online help, found here http://tripal.info/tutorials/v3.x/installation/tripal \n";
  326. exit;
  327. }
  328. }
  329. /**
  330. * Sets permissions for the content types on the site.
  331. *
  332. * Executed when 'drush trp-set-permissions' is called.
  333. *
  334. * @ingroup tripal_drush
  335. */
  336. function drush_tripal_trp_set_permissions() {
  337. $user = drush_get_option('user');
  338. $uname = drush_get_option('username');
  339. if ($user and is_numeric($user)) {
  340. }
  341. elseif ($user) {
  342. print "\nNOTE: Use of the --user argument is deprecated as it conflicts with the --user argument of Drush 7.x. Please now use --username instead.\n\n";
  343. $username = $user;
  344. }
  345. if ($uname) {
  346. $username = $uname;
  347. }
  348. drush_tripal_set_user($username);
  349. $permissions = array();
  350. $bundles = tripal_get_content_types();
  351. foreach ($bundles as $bundles => $bundle) {
  352. array_push($permissions, ' view ' . $bundle->name, ' create ' . $bundle->name,
  353. ' edit ' . $bundle->name, ' delete ' . $bundle->name);
  354. }
  355. $string_permissions = implode(",", $permissions);
  356. $args4 = array('administrator', $string_permissions);
  357. $options4 = array();
  358. drush_invoke_process('@self', 'role-add-perm', $args4, $options4);
  359. drush_print(dt(""));
  360. drush_print(dt("Permissions is now complete."));
  361. }