generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathexec.sh
More file actions
executable file
·135 lines (124 loc) · 3.83 KB
/
exec.sh
File metadata and controls
executable file
·135 lines (124 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
######################################################################
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. #
# SPDX-License-Identifier: MIT-0 #
######################################################################
source .env
if [ "${QUIET}" == "true" ]; then
VERBOSE=false
fi
if [ "${DEBUG}" == "true" ]; then
set -x
fi
if [ ! "$TO" == "$PM_TO" ]; then
echo $PM_TO > wd/.to
source .env
fi
if [ "${VERBOSE}" == "true" ]; then
echo ""
echo "Executing command on orchestrator ${TO} ..."
fi
case "${TO}" in
"compose")
if [ "${COMPOSE_CONTEXT_TYPE}" == "moby" ]; then
SERVICE_NAME=$1
if [ "$SERVICE_NAME" == "" ]; then
SERVICE_NAME=platform
fi
CONTAINER_INDEX=$2
if [ "$CONTAINER_INDEX" == "" ]; then
CONTAINER_INDEX=1
fi
BASHCMD="${@:3}"
if [ "$BASHCMD" == "" ]; then
BASHCMD=/bin/bash
fi
CMD="docker exec -it ${COMPOSE_PROJECT_NAME}${DOCKER_COMPOSE_SEP}${SERVICE_NAME}${DOCKER_COMPOSE_SEP}${CONTAINER_INDEX} ${BASHCMD} "
elif [ "${COMPOSE_CONTEXT_TYPE}" == "ecs" ]; then
echo ""
echo "Exec into container running on ECS is not supported"
echo ""
else
echo ""
echo "Unrecognized COMPOSE_CONTEXT_TYPE ${COMPOSE_CONTEXT_TYPE}"
echo "Supported context types are moby | ecs"
fi
;;
"swarm")
SERVICE_INDEX=$1
if [ "$SERVICE_INDEX" == "" ]; then
SERVICE_INDEX=1
fi
TASK_ID=${SWARM_STACK_NAME}_${SWARM_SERVICE_NAME}.${SERVICE_INDEX}
CONTAINER_ID=$(docker ps | grep ${TASK_ID} | cut -d ' ' -f 1)
if [ "${CONTAINER_ID}" == "" ]; then
CONTAINER_HOST=$(docker service ps ${SWARM_STACK_NAME}_${SWARM_SERVICE_NAME} -f desired-state=running --format="{{.Node}} {{.Name }} {{.ID}}" | grep ${TASK_ID} | cut -d ' ' -f 1)
echo ""
if [ "${CONTAINER_HOST}" == "" ]; then
echo "Could not find running task ${TASK_ID}"
else
echo "Cannot connect to service task ${TASK_ID}"
echo "Only connections to tasks running on the local host are supported"
echo "You may connect to the node running this task ($CONTAINER_HOST) and use docker exec locally"
echo " or configure remote docker daemon access."
fi
echo ""
CMD=""
else
CMD="docker exec -it ${CONTAINER_ID} sh -c 'if [ -e /bin/bash ]; then /bin/bash; else sh; fi'"
fi
;;
"ecs")
CONTAINER_INDEX=1
if [ ! "$1" == "" ]; then
CONTAINER_INDEX=$1
fi
TASK_ID=$(./status.sh | grep RUNNING | head -n ${CONTAINER_INDEX} | tail -n 1 | cut -d '/' -f 2)
CMD="aws ecs execute-command --region ${REGION} --cluster ${ECS_CLUSTER} --command /bin/bash --task ${TASK_ID} --container ${CONTAINER} --interactive"
;;
"kubernetes")
POD_GROUP=$1
if [ "$1" == "" ]; then
POD_GROUP=platform
fi
CONTAINER_INDEX=$2
if [ "$2" == "" ]; then
CONTAINER_INDEX=1
fi
POD_CMD="${@:3}"
if [ "$POD_CMD" == "" ]; then
POD_CMD=bash
fi
CMD="unset DEBUG; ${KUBECTL} -n ${NAMESPACE} exec -it $( ${KUBECTL} -n ${NAMESPACE} get pod | grep Running | grep ${POD_GROUP} | head -n ${CONTAINER_INDEX} | tail -n 1 | cut -d ' ' -f 1 ) -- ${POD_CMD} "
;;
"lambda")
LAMBDA_PAYLOAD=""
if [ ! "$1" == "" ]; then
LAMBDA_PAYLOAD="--payload '$1'"
fi
CMD="aws lambda invoke --function-name ${LAMBDA_FUNCTION_NAME} ${LAMBDA_PAYLOAD} /tmp/${LAMBDA_FUNCTION_NAME}.out; cat /tmp/${LAMBDA_FUNCTION_NAME}.out"
;;
"batch")
echo ""
echo "exec operation is not supported for target orchestrator batch"
;;
*)
checkTO "${TO}"
if [ "$1" == "" ]; then
CMD="sh -c 'if [ -e /bin/bash ]; then /bin/bash; else sh; fi'"
else
CMD="$@"
fi
CMD="docker container exec -it ${CONTAINER} $CMD"
;;
esac
if [ "${VERBOSE}" == "true" ]; then
echo "${CMD}"
echo ""
fi
if [ "${DRY_RUN}" == "false" ]; then
eval "${CMD}"
fi
if [ "${DEBUG}" == "true" ]; then
set +x
fi