Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save teknikqa/5c9b5367d32fb6ad66cc20303c46f384 to your computer and use it in GitHub Desktop.

Select an option

Save teknikqa/5c9b5367d32fb6ad66cc20303c46f384 to your computer and use it in GitHub Desktop.

Revisions

  1. @heathdutton heathdutton created this gist Apr 26, 2015.
    75 changes: 75 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    #!/bin/bash
    # Runs an acquia task, and waits for the task to complete before continuing.
    # This is a helper script, to be used in others as needed.

    if [[ $1 = "" ]] || [[ $2 = "" ]]
    then
    echo "Runs an acquia drush command, waiting for the results before continuing."
    echo "Can be used as a replacement for drush."
    echo
    echo " Usage: $0 <site-alias> <ac-drush-command>"
    echo " Example: $0 @site.prod ac-database-instance-backup site"
    exit 1
    fi

    RED='\033[0;31m'
    GREEN='\033[0;32m'
    NC='\033[0m'

    ac_command="$2"
    drush_command="drush "$@""

    if [[ $ac_command = "ac-code-deploy" ]] ||
    [[ $ac_command = "ac-code-path-deploy" ]] ||
    [[ $ac_command = "ac-database-add" ]] ||
    [[ $ac_command = "ac-database-copy" ]] ||
    [[ $ac_command = "ac-database-delete" ]] ||
    [[ $ac_command = "ac-database-instance-backup" ]] ||
    [[ $ac_command = "ac-database-instance-backup-delete" ]] ||
    [[ $ac_command = "ac-database-instance-backup-restore" ]] ||
    [[ $ac_command = "ac-domain-add" ]] ||
    [[ $ac_command = "ac-domain-delete" ]] ||
    [[ $ac_command = "ac-domain-move" ]] ||
    [[ $ac_command = "ac-domain-purge" ]] ||
    [[ $ac_command = "ac-environment-install" ]] ||
    [[ $ac_command = "ac-files-copy" ]]
    then
    echo "${0//.sh/}:"
    echo " Command: $drush_command"

    json="$($drush_command --format=json)"
    id="$(echo $json | sed "s/},/\n/g" | sed 's/.*"id":"\([^"]*\)".*/\1/')"

    echo " Task: $id"

    oldlogs=""
    while [[ $state != "done" && $state != "error" ]]
    do
    # Checking consumes resources, so wait for 3 seconds between checks.
    sleep 3
    json="$(drush $1 ac-task-info $id --format=json)"
    state="$(echo $json | sed "s/},/\n/g" | sed 's/.*"state":"\([^"]*\)".*/\1/')"
    newlogs="$(echo $json | sed "s/},/\n/g" | sed 's/.*"logs":"\([^"]*\)".*/\1/')"
    if [[ $newlogs != $oldlogs ]]
    then
    logdiff=${newlogs//"$oldlogs"/}
    logdiff=${logdiff//"\n"/}
    if [[ $logdiff != "" ]]
    then
    echo " $logdiff"
    fi
    fi
    oldlogs="$newlogs"
    done

    if [[ $state = "error" ]]
    then
    echo -e " State: ${RED}$state${NC}"
    exit 1
    else
    echo -e " State: ${GREEN}$state${NC}"
    fi
    else
    # Fall back to standard drush command if this is not a known asyncronous command
    drush "$@"
    fi