In trying to prepare for running different drush's depending on what site we are on, I thought the time has come to fix the hard coded "provision-$TYPE" drush command.

For those that don't know, when you implement hook_hosting_tasks(), your task type's name must match a drush command "provision-$TYPE". It's automatic. No way to override it.

What I would like to do is allow hook_hosting_tasks() to define the "command" it should run, making simpler tasks possible.

For instance:


function hook_hosting_tasks() {
  $tasks['site']['sanitize'] = array(
    'title' => t('Sanitize Database'),
    'description' => t('Clean out your sites database by running drush sql-sanitize'),
    'dialog' => TRUE,
    'command' => 'sql-sanitize',
  );
  return $options;
}

Comments

Jon Pugh created an issue. See original summary.

Jon Pugh’s picture

diff --git a/task.hosting.inc b/task.hosting.inc
index c3a37b1..000a07c 100644
--- a/task.hosting.inc
+++ b/task.hosting.inc
@@ -156,7 +156,7 @@ function drush_hosting_task() {
   }
 
   // Run the actual command. Adding alias here to work around Drush API.
-  $output = provision_backend_invoke($alias, 'provision-' . $task->task_type, $task->args, $task->options, $mode);
+  $output = provision_backend_invoke($alias, $task->task_command, $task->args, $task->options, $mode);
   // Pass the drush output back to the HOOK_post_hosting_TASK_task() and
   // HOOK_hosting_TASK_task_rollback() hooks, as the $data argument.
   drush_set_context('HOSTING_DRUSH_OUTPUT', $output);
diff --git a/task/hosting_task.api.php b/task/hosting_task.api.php
index 4233d13..f2e31a4 100644
--- a/task/hosting_task.api.php
+++ b/task/hosting_task.api.php
@@ -20,6 +20,7 @@
  *   should be an array of tasks keyed by task type, the value should be an
  *   array that defines the task. Valid keys for defining tasks are:
  *   - 'title': (required) The human readable name of the task.
+ *   - 'command': (optional) The drush command to run. If not included, will assume "provision-TASKTYPE".
  *   - 'description': (optional) The human readable description of the task.
  *   - 'weight': (optional) The weight of the task when displayed in lists.
  *   - 'dialog' (optional) Set to TRUE to indicate that this task requires a
@@ -47,6 +48,7 @@ function hook_hosting_tasks() {
     'description' => t('Make a copy of a site.'),
     'weight' => 5,
     'dialog' => TRUE,
+    'command' => 'provision-clone',
   );
   return $options;
 }
diff --git a/task/hosting_task.module b/task/hosting_task.module
index 692a41d..204079c 100644
--- a/task/hosting_task.module
+++ b/task/hosting_task.module
@@ -866,6 +866,23 @@ function hosting_available_tasks($type = NULL, $reset = FALSE) {
 }
 
 /**
+ * Implements hook_hosting_tasks_alter()
+ *
+ * Set 'command' property for tasks that don't implement it.
+ */
+function hosting_task_hosting_tasks_alter(&$cache) {
+
+  // For any task type that doesn't use "command", add it as 'provision-$TYPE'.
+  foreach ($cache as &$tasks) {
+    foreach ($tasks as $type => &$task_type) {
+      if (!isset($task_type['command']) || empty($task_type['command'])) {
+        $task_type['command'] = 'provision-' . $type;
+      }
+    }
+  }
+}
+
+/**
  * Implements hook_action_info().
  */
 function hosting_task_action_info() {
@@ -1010,6 +1027,7 @@ function hosting_task_delete_related_tasks($nid) {
  * Implements hook_load().
  */
 function hosting_task_load($nodes) {
+  $types = module_invoke_all('hosting_tasks');
   foreach ($nodes as $nid => &$node) {
     $additions = db_query('SELECT task_type, executed, delta, rid, task_status FROM {hosting_task} WHERE vid = :vid', array(':vid' => $node->vid))->fetch();
     $result = db_query("SELECT name, value FROM {hosting_task_arguments} WHERE vid = :vid", array(':vid' => $node->vid));
@@ -1021,6 +1039,9 @@ function hosting_task_load($nodes) {
     foreach ($additions as $property => &$value) {
       $node->$property = $value;
     }
+
+    // Load task_command property from hook_hosting_tasks data.
+    $node->task_command = $types[$node->type][$node->task_type]['command'];
   }
 }
 
Jon Pugh’s picture

---

Jon Pugh’s picture

Project: Provision » Hosting

  • Jon Pugh committed ba45d82 on 2912492-task-command
    Issue #2912492: Lookup node type so we can load a task's command.
    
  • Jon Pugh committed c46ffd9 on 2912492-task-command
    Issue #2912492: Don't forget to alter.
    
colan’s picture

Maybe we can get the sanitizing done in #2888523: Add "Sanitize database" site task.

  • helmo committed 4f80c4c on 2912492-task-command
    Issue #2912492 by helmo: Api docs
    
helmo’s picture

Status: Needs review » Reviewed & tested by the community

This is a good preparation for provision 4.x .... And as it can be used today for e.g. #2888523: Add "Sanitize database" site task I suggest we merge it into 3.14.

  • Jon Pugh committed 18e9e61 on 7.x-3.x
    Issue #2912492: Add "command" property to hook_hosting_tasks() to allow...
  • helmo committed 4d0a006 on 7.x-3.x
    Issue #2912492: Merge remote-tracking branch 'origin/2912492-task-...
  • helmo committed 4f80c4c on 7.x-3.x
    Issue #2912492 by helmo: Api docs
    
  • Jon Pugh committed ba45d82 on 7.x-3.x
    Issue #2912492: Lookup node type so we can load a task's command.
    
  • Jon Pugh committed c46ffd9 on 7.x-3.x
    Issue #2912492: Don't forget to alter.
    
helmo’s picture

Status: Reviewed & tested by the community » Fixed

Merged

helmo’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.