Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Created June 11, 2020 04:29
Show Gist options
  • Save velotiotech/54fc03b343bf3dff88c3f97122eae2ac to your computer and use it in GitHub Desktop.
Save velotiotech/54fc03b343bf3dff88c3f97122eae2ac to your computer and use it in GitHub Desktop.
#!/bin/bash
echo "DCOS Task Exec 2.0"
if [ "$#" -eq 0 ]; then
echo "Need task name or id as input. Exiting."
exit 1
fi
taskName=$1
taskCmd=${2:-bash}
TMP_TASKLIST_JSON=/tmp/dcostasklist.json
dcos task --json > $TMP_TASKLIST_JSON
taskExist=`cat /tmp/dcostasklist.json | jq --arg tname $taskName '.[] | if(.name == $tname ) then .name else empty end' -r | wc -l`
if [[ $taskExist -eq 0 ]]; then
echo "No task with name $taskName exists."
echo "Do you mean ?"
dcos task | grep $taskName | awk '{print $1}'
exit 1
fi
taskType=`cat $TMP_TASKLIST_JSON | jq --arg tname $taskName '[.[] | select(.name == $tname)][0] | .container.type' -r`
TaskId=`cat $TMP_TASKLIST_JSON | jq --arg tname $taskName '[.[] | select(.name == $tname)][0] | .id' -r`
if [[ $taskExist -ne 1 ]]; then
echo -e "More than one instances. Please select task ID for executing command.\n"
#allTaskIds=$(dcos task $taskName | tee /dev/tty | grep -v "NAME" | awk '{print $5}' | paste -s -d",")
echo ""
read TaskId
fi
if [[ $taskType != "DOCKER" ]]; then
echo "Task [ $taskName ] is of type MESOS Container."
execCmd="dcos task exec --interactive --tty $TaskId $taskCmd"
echo "Running [$execCmd]"
$execCmd
else
echo "Task [ $taskName ] is of type DOCKER Container."
taskNodeIP=`dcos task $TaskId | awk 'FNR == 2 {print $2}'`
echo "Task [ $taskName ] with task Id [ $TaskId ] is running on node [ $taskNodeIP ]."
taskContID=`dcos node ssh --option LogLevel=quiet --option StrictHostKeyChecking=no --private-ip=$taskNodeIP --master-proxy "docker ps -q --filter "label=MESOS_TASK_ID=$TaskId"" 2> /dev/null`
taskContID=`echo $taskContID | tr -d '\r'`
echo "Task Docker Container ID : [ $taskContID ]"
echo "Running [ docker exec -it $taskContID $taskCmd ]"
dcos node ssh --option StrictHostKeyChecking=no --option LogLevel=quiet --private-ip=$taskNodeIP --master-proxy "docker exec -it $taskContID $taskCmd" 2>/dev/null
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment