Last active
June 27, 2018 06:20
-
-
Save pkazi/aae04b92c10cfebce98e64d5b25bd3f0 to your computer and use it in GitHub Desktop.
Execute commands in any dcos task. dcos task exec cli is only for mesos containers, this script can be used to ssh into mesos and docker containers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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