-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathec2
More file actions
executable file
·176 lines (157 loc) · 6.09 KB
/
Copy pathec2
File metadata and controls
executable file
·176 lines (157 loc) · 6.09 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
BASE_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $BASE_PATH/bootstrap.sh
SSH_REMOTE_PORT=443
SSH=$(which ssh)
SCP=$(which scp)
RSYNC=$(which rsync)
function get_instance_details() {
QUERY_PARAM="Reservations[].Instances[].[PublicDnsName,Tags[?Key=='Name'].Value[],InstanceType,PublicIpAddress,InstanceId,State]"
FILTER_PARAM="Name=instance-state-name,Values=running,stopped"
INSTANCES_DETAILS=$($ENV_PATH/bin/aws ec2 describe-instances --query $QUERY_PARAM --filters $FILTER_PARAM)
echo $INSTANCES_DETAILS | $ENV_PATH/bin/python -c 'import sys, json; j=json.load(sys.stdin); l="\n".join([sys.argv[1].format(i+1,x[0],x[1][0],x[2],x[3],x[4],x[5]["Name"]) for i,x in enumerate(j)]); print l' "{} {} {} {} {} {} {}"
}
function connect_instance() {
if [ -z "$INSTANCES_PLAIN_DETAILS" ]; then
echo "No instances. Exiting."
exit 0
fi
# ./awscli ec2 authorize-security-group-ingress --group-id "sg-3e355f46" --protocol tcp --port 22 --cidr 67.185.162.138/2
LINE=1
if [ 0$1 -gt 0 ]; then
LINE=$1
fi
EC2_DNS_NAME="$(echo "$INSTANCES_PLAIN_DETAILS" | head -n $LINE | tail -n 1 | cut -d ' ' -f 2)"
echo "Connecting to [$EC2_DNS_NAME]"
$SSH -o StrictHostKeyChecking=no $EC2_DNS_NAME -p $SSH_REMOTE_PORT
return $?
}
function list_instances() {
if [ "$1" == "-s" ]; then
while read line; do
echo $line | cut -d ' ' -f 2- | column -s' ' -t
INSTANCE_DNS="$(echo $line | cut -d ' ' -f 2)"
UPTIME_BOX=$($SSH -n -o StrictHostKeyChecking=no $INSTANCE_DNS -p 443 -q 'uptime' 2>/dev/null)
FREEMEM_BOX=$($SSH -n -o StrictHostKeyChecking=no $INSTANCE_DNS -p 443 -q "free -t -m | grep Mem | awk '{print int((\$3 - \$7) / \$2 * 100)}'" 2>/dev/null)
echo " $UPTIME_BOX | Mem: $FREEMEM_BOX%"
done <<< "$INSTANCES_PLAIN_DETAILS"
else
echo "$INSTANCES_PLAIN_DETAILS" | column -s' ' -t
fi
}
function sync_folder_instance() {
if [ -z "$INSTANCES_PLAIN_DETAILS" ]; then
echo "No instances. Exiting."
exit 0
fi
# ./awscli ec2 authorize-security-group-ingress --group-id "sg-3e355f46" --protocol tcp --port 22 --cidr 67.185.162.138/2
LINE=1
if [ 0$1 -gt 0 ]; then
LINE=$1
shift
fi
EC2_DNS_NAME="$(echo "$INSTANCES_PLAIN_DETAILS" | head -n $LINE | tail -n 1 | cut -d ' ' -f 2)"
REMOTE_PATH=$($SSH -o StrictHostKeyChecking=no $EC2_DNS_NAME -p $SSH_REMOTE_PORT pwd)
$RSYNC -rutve "ssh -o StrictHostKeyChecking=no -p $SSH_REMOTE_PORT" --exclude ".*/" $BASE_PATH $EC2_DNS_NAME:$REMOTE_PATH --delete
if [ "$1" == "both" ];then
$RSYNC -rutve "ssh -o StrictHostKeyChecking=no -p $SSH_REMOTE_PORT" --exclude ".*/" $EC2_DNS_NAME:$REMOTE_PATH/$(basename $BASE_PATH) $BASE_PATH/..
fi
}
function pull_instance() {
LINE=1
if [[ $1 =~ ^-?[0-9]+$ ]]; then
LINE=$1
shift
if [ -r "$1" ]; then
FILE="$1"
shift
fi
elif [ "$1" ]; then
FILE="$1"
shift
fi
EC2_DNS_NAME="$(echo "$INSTANCES_PLAIN_DETAILS" | head -n $LINE | tail -n 1 | cut -d ' ' -f 2)"
echo "Copying [$FILE] from [$EC2_DNS_NAME]"
$SCP -o StrictHostKeyChecking=no -P $SSH_REMOTE_PORT "$EC2_DNS_NAME:$FILE" /tmp $@
}
function copy_instance() {
LINE=1
if [[ $1 =~ ^-?[0-9]+$ ]]; then
LINE=$1
shift
if [ -r "$1" ]; then
FILE="$1"
shift
fi
elif [ -r "$1" ]; then
FILE="$1"
shift
fi
EC2_DNS_NAME="$(echo "$INSTANCES_PLAIN_DETAILS" | head -n $LINE | tail -n 1 | cut -d ' ' -f 2)"
echo "Copying [$FILE] to [$EC2_DNS_NAME]"
$SCP -o StrictHostKeyChecking=no -P $SSH_REMOTE_PORT "$FILE" $EC2_DNS_NAME:/tmp $@
}
function tunnel_instance() {
if [ -z "$INSTANCES_PLAIN_DETAILS" ]; then
echo "No instances. Exiting."
exit 0
fi
LINE=1
if [ 0$1 -gt 0 ]; then
LINE=$1
shift
fi
EC2_DNS_NAME="$(echo "$INSTANCES_PLAIN_DETAILS" | head -n $LINE | tail -n 1 | cut -d ' ' -f 2)"
REMOTE_PORT="80"
if [ -n "$1" -a "${1:-0}" -gt 0 ]; then
REMOTE_PORT="$1"
shift
fi
LOCAL_PORT="8090"
if [ -n "$1" -a "${1:-0}" -gt 0 ]; then
LOCAL_PORT="$1"
shift
fi
LSOF="$(which lsof)"
if $LSOF -Pi :$LOCAL_PORT -sTCP:LISTEN -t >/dev/null ; then
echo "Port $LOCAL_PORT is in use"
exit 1
fi
echo "Trying to open 127.0.0.1:$LOCAL_PORT [http://127.0.0.1:$LOCAL_PORT] connected to $EC2_DNS_NAME:$REMOTE_PORT"
echo "Use Crtl-C to end"
$SSH -N -L $LOCAL_PORT:127.0.0.1:$REMOTE_PORT -p $SSH_REMOTE_PORT -o StrictHostKeyChecking=no $EC2_DNS_NAME
return $?
}
function show_limits() {
$ENV_PATH/bin/aws gamelift describe-ec2-instance-limits --query 'EC2InstanceLimits[].{EC2InstanceType:EC2InstanceType,CurrentInstances:CurrentInstances,InstanceLimit:InstanceLimit}' --output text | sort -k3 -n
}
function stop_instance() {
LINE=1
if [ 0$1 -gt 0 ]; then
LINE=$1
shift
fi
EC2_INSTANCE_ID="$(echo "$INSTANCES_PLAIN_DETAILS" | head -n $LINE | tail -n 1 | cut -d ' ' -f 6)"
$ENV_PATH/bin/aws ec2 stop-instances --instance-ids "$EC2_INSTANCE_ID"
}
function start_instance() {
LINE=1
if [ 0$1 -gt 0 ]; then
LINE=$1
shift
fi
EC2_INSTANCE_ID="$(echo "$INSTANCES_PLAIN_DETAILS" | head -n $LINE | tail -n 1 | cut -d ' ' -f 6)"
$ENV_PATH/bin/aws ec2 start-instances --instance-ids "$EC2_INSTANCE_ID"
}
load_configuration
INSTANCES_PLAIN_DETAILS="$(get_instance_details)"
[[ "$1" == "connect" ]] && shift && connect_instance $@ && exit $?
[[ "$1" == "list" ]] && shift && list_instances $@ && exit $?
[[ "$1" == "sync" ]] && shift && sync_folder_instance $@ && exit $?
[[ "$1" == "copy" ]] && shift && copy_instance $@ && exit $?
[[ "$1" == "pull" ]] && shift && pull_instance $@ && exit $?
[[ "$1" == "tunnel" ]] && shift && tunnel_instance $@ && exit $?
[[ "$1" == "limits" ]] && shift && show_limits $@ && exit $?
[[ "$1" == "stop" ]] && shift && stop_instance $@ && exit $?
[[ "$1" == "start" ]] && shift && start_instance $@ && exit $?
list_instances