tripal.drush.inc 14 KB

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