-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_ec2_functions.sh
executable file
·69 lines (63 loc) · 1.88 KB
/
aws_ec2_functions.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
#!/bin/bash -e
# You need to install the AWS Command Line Interface from http://aws.amazon.com/cli/
if [[ ! $PEM ]];
then
echo "Please make sure that a key is set by entering 'export PEM=' followed by a path to key you have chosen."
exit 0;
fi
aws ec2 wait instance-running --instance-ids $INSTANCEID
echo "ENDPOINT for $INSTANCEID is: $ENDPOINT"
read -r -d '' instructions <<-EOF
Type 'connect' to access the server, 'terminate_instance' to shut it down after exiting
Type 'upload [FILE]' to send a file to the server, 'download [FILE]' to retrieve a file from the server.
EOF
echo "$instructions"
export instructions=$instructions
function instructions() {
echo "$instructions"
}
export oldPS1=$PS1
function upload() {
filepath=$1
scp -C -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -i $PEM $filepath ubuntu@${ENDPOINT}:/home/ubuntu/;
}
function download() {
remotefilepath=$1
scp -C -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -i $PEM ubuntu@${ENDPOINT}:/home/ubuntu/$remotefilepath ./;
}
function connect() {
ssh -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -i $PEM ubuntu@${ENDPOINT}
}
function wait_terminate_instance() {
aws ec2 wait instance-terminated --instance-ids $INSTANCEID;
if [[ $mypid ]]
then
if [[ $(ps -q ${mypid} -o comm=) ]]
then
jobs
fg %1 2>/dev/null
fi
fi
echo $INSTANCEID terminated
if [[ $terminatepid ]]
then
if [[ $(ps -q ${terminatepid} -o comm=) ]]
then
jobs
fg %1 2>/dev/null
fi
fi
exit
}
function terminate_instance() {
aws ec2 terminate-instances --instance-ids $INSTANCEID;
echo "terminating $INSTANCEID ...";
sleep 1;
wait_terminate_instance & terminatepid=$!
}
export -f instructions
export -f wait_terminate_instance
export -f terminate_instance
export -f upload
export -f download
export -f connect