-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathezws.sh
executable file
·258 lines (220 loc) · 9.69 KB
/
ezws.sh
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
set -e
VERSION=0.0.1
command=$1
friendlyName=$2
sshfsRemoteDirectory=$3
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# SO: https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
RED='\033[0;31m'
GRAY='\033[90m'
CYAN='\033[96m'
NC='\033[0m' # No Color
echo "$command"
case $command in
startup|connect|sftp|sshfs|bind|stop)
# this file should have instance_id and pathToPrivateKey
# lets check if the entered name is correct
instanceFilenames=$(ls $current_dir/.*_instance)
correct_name=false
for filename in $instanceFilenames; do
friendlyNameFound=$(echo $filename | sed 's/\.//' | sed "s~$current_dir/~~" | sed "s/_instance//")
if [[ "$friendlyName" == "$friendlyNameFound" ]]
then correct_name=true;
fi
done
if [ "$correct_name" != true ]
then
echo -e "${RED}friendly name of${NC} ${GREEN}"$friendlyName"${NC} ${RED} not found (did you spell it right?)"
echo -e "\t use 'ezws.sh list' to see a list of available friendly names${NC}"
exit
fi
instance_id=$(cat "$current_dir/.$friendlyName"_instance)
pathToPrivateKey=$(cat "$current_dir/.$friendlyName"_key)
profile=$(cat "$current_dir/.$friendlyName"_profile)
;;
add|list|reindex) # do nothing
;;
*) # print help
echo "ezws.sh version $VERSION"
echo -e "${RED}invalid command option${NC}"
echo "listing available commands:"
echo " add: add instance and key to registered machines"
echo " list: list out all instances friendly names and ids"
echo " connect: ssh to an already running instance"
echo " startup: start a aws instance and ssh to it"
echo " stop: stop a running instance"
echo " sftp: sftp to an already running instance"
echo " sshfs: use sshfs to mount the root directory (default) or a specified directory (command line argument) of an already running instance to the cwd"
echo " bind: bind a port to an already running instance"
echo " reindex: add all the added machines to tab completion"
echo "exiting"
exit
;;
esac
robust_get_ip() {
# Try public IP, and if none, the private IP (used if on a VPN)
publicIpAddress="$(describe --instance-ids $instance_id --query 'Reservations[*].Instances[*].PublicIpAddress' --output text --profile $profile)"
if [$publicIpAddress == '']
then
privateIpAddress="$(describe --instance-ids $instance_id --query 'Reservations[*].Instances[*].PrivateIpAddress' --output text --profile $profile)"
echo $privateIpAddress
else
echo $publicIpAddress
fi
}
describe() {
# echo is how we are returning a string value
# first case, catches if the time-clock is out of sync
# second case is the typical behavior
output=$(aws ec2 describe-instances "$@")
if [[ $output == *"An error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials"* ]]; then
# see this link for more info: https://webcache.googleusercontent.com/search?q=cache:K4H2wZL_tfIJ:https://forums.aws.amazon.com/thread.jspa%3FmessageID%3D722197
echo -e "${RED}[error]${NC} your clock is out of sync please run the following command, then try again">&2
echo -e 'sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"'>&2
return 1
fi
echo $output
}
case $command in
startup)
aws ec2 start-instances --instance-ids $instance_id --profile $profile
#wait until the node is up and running
node_stopped=true
while $node_stopped; do
sleep 0.5s
state="$(describe --instance-ids $instance_id --query 'Reservations[*].Instances[*].State.Name' --output text --profile $profile)"
if [ "$state" = "running" ]
then node_stopped=false; sleep 2s;
fi
done
;;
connect)
state="$(describe --instance-ids $instance_id --query 'Reservations[*].Instances[*].State.Name' --output text --profile $profile)"
if [ "$state" != "running" ]
then echo "machine not running"; exit;
fi
#grab the ip address from aws
IpAddress="$(robust_get_ip)"
#add node to known hosts (skip fingerprint question)
ssh-keyscan -H $IpAddress >> ~/.ssh/known_hosts
# add key to ssh-agent to enable key forwarding
case "$(uname -s)" in
Linux*) ssh-add $pathToPrivateKey;;
Darwin*) ssh-add -K $pathToPrivateKey;;
*) echo -e "${RED}[warning]${NC} unrecognized OS. SSH key not automatically added to agent. Use `ssh-add` to add your private key to ssh agent for ssh key forwarding"
esac
#connect to node
echo $IpAddress
ssh -A -i $pathToPrivateKey ubuntu@$IpAddress
;;
bind)
state="$(describe --instance-ids $instance_id --query 'Reservations[*].Instances[*].State.Name' --output text --profile $profile)"
if [ "$state" != "running" ]
then echo "machine not running"; exit;
fi
#connect to node
localPort=$3
remotePort=$4
#grab the ip address from aws
IpAddress="$(robust_get_ip)"
case $localPort in
[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9]) #just check if it is a 4- or 5-digit number
# echo "valid local port"
;;
*)
echo "invalid local port"
echo "exiting"
exit
;;
esac
case $remotePort in
[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9]) #just check if it is a 4- or 5-digit number
# echo "valid remote port"
;;
*)
echo "invalid remote port"
echo "exiting"
exit
;;
esac
echo "binding port $localPort to $IpAddress:$remotePort"
ssh -N -f -L localhost:$localPort:localhost:$remotePort ubuntu@$IpAddress -i $pathToPrivateKey
;;
sftp)
state="$(describe --instance-ids $instance_id --query 'Reservations[*].Instances[*].State.Name' --output text --profile $profile)"
if [ "$state" != "running" ]
then echo "machine not running"; exit;
fi
#grab the ip address from aws
IpAddress="$(robust_get_ip)"
#connect to node
echo $IpAddress
sftp -i $pathToPrivateKey ubuntu@$IpAddress
;;
sshfs)
state="$(describe --instance-ids $instance_id --query 'Reservations[*].Instances[*].State.Name' --output text --profile $profile)"
if [ "$state" != "running" ]
then echo "machine not running"; exit;
fi
#grab the ip address from aws
IpAddress="$(robust_get_ip)"
#connect to node
echo $IpAddress
sshfs ubuntu@$IpAddress:/$sshfsRemoteDirectory . -o IdentityFile=$pathToPrivateKey
;;
stop)
read -r -p "Are you sure no one else is using this machine? [y/n] " response
case "$response" in
[yY][eE][sS]|[yY])
aws ec2 stop-instances --instance-ids $instance_id --profile $profile
;;
*)
echo "stopping operation cancelled - $instance_id is still running!"
exit
;;
esac
;;
add)
# look for private key and get the oldest one
oldestPrivateKey=$(ls -t ~/.ssh/*.pem | tail -1)
read -r -p "enter a friendly name for this instance: " friendlyName
read -r -p "enter the instance id: " instance_id
read -r -p "enter the path to the private ssh key: [$oldestPrivateKey]" pathToPrivateKey
read -r -p "enter profile: [default] " profile
profile=${profile:-default}
pathToPrivateKey=${pathToPrivateKey:-$oldestPrivateKey}
# save info to files
echo "$instance_id" > "$current_dir/.$friendlyName"_instance
echo "$pathToPrivateKey" > "$current_dir/.$friendlyName"_key
echo "$profile" > "$current_dir/.$friendlyName"_profile
echo ".$friendlyName*" >> $current_dir/.gitignore
# add friendly name to tab completer
sed -i'' -e "/local hostname/ s/\"$/ $friendlyName\"/" $current_dir/bash_completion.d/ezws.sh
echo "done please resource your ~/.bashrc (Linux) or ~/.bash_profile (Mac OS) to use the updated tab completion"
;;
list)
instanceFilenames=$(ls $current_dir/.*_instance)
for filename in $instanceFilenames; do
friendlyName=$(echo $filename | sed 's/\.//' | sed "s~$current_dir/~~" | sed "s/_instance//")
profile=$(cat "$current_dir/.$friendlyName"_profile)
state="$(describe --instance-ids $(cat $filename) --query 'Reservations[*].Instances[*].State.Name' --output text --profile $profile)"
if [ "$state" != "running" ]; then
echo -e "$(echo $friendlyName: $(cat $filename)) ${GRAY} (not running) ${NC}"
else
echo -e "$(echo $friendlyName: $(cat $filename)) ${CYAN} (running) ${NC}"
fi
done
;;
reindex)
instanceFilenames=$(ls $current_dir/.*_instance)
for filename in $instanceFilenames; do
friendlyName=$(echo $filename | sed 's/\.//' | sed "s~$current_dir/~~" | sed "s/_instance//")
# add friendly name to tab completer
sed -i'' -e "/local hostname/ s/\"$/ $friendlyName\"/" $current_dir/bash_completion.d/ezws.sh
done
echo "done please resource your ~/.bashrc (Linux) or ~/.bash_profile (Mac OS) to use the updated tab completion"
;;
*)
;;
esac