-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·140 lines (132 loc) · 4.83 KB
/
run.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
#/bin/bash
set -e
servers=("catalog_server" "order_server" "frontend_server")
function finish {
echo "Cleanup"
for i in "${local_container_names[@]}"
do
docker stop $i && docker rm $i || echo "Failed to kill container $i. It is probably already killed or something went wrong."
done
# For remote cleanup
for i in ${!servers[@]}; do
ips=(${machines[i]//,/ })
for j in ${!ips[@]}; do
ip=${ips[j]}
if [[ "$ip" != *"host.docker.internal"* ]] && [[ "$ip" != *"localhost"* ]]
then
echo "Attempting to cleanup remote server $ip"
ssh -n ec2-user@"$ip" 'docker stop $(docker ps -aq) && docker rm $(docker ps -aq)' || echo "Failed to stop the server $i. It might have already been cleaned before or something went wrong."
fi
done
done
return
}
trap finish EXIT
trap finish INT
ports=("5000" "5001" "5002")
machines=()
local_container_names=()
container_names=()
if [[ "$1" == "" ]]
then
if [[ "$OSTYPE" == *"linux"* ]]
then
configFile="machines.txt.local.linux"
else
configFile="machines.txt.local"
fi
echo "No file passed as parameter. Using the default localhost file $configFile."
else
configFile=$1
fi
frontend_port=""
# Read N - Number of servers. Keep assigning nodes to them - Catalog, Order, and Frontend and run the app accordingly.
# Print the url of frontend server.
# Run the client and test cases.
echo "Setting up the servers on machines..."
while IFS= read -r line || [ -n "$line" ];
do
machines+=($line)
done < $configFile
for i in ${!servers[@]}; do
role=${servers[i]}
ips=(${machines[i]//,/ })
port_offset=${ports[$i]}
for j in ${!ips[@]}; do
port=$((port_offset+j*3))
ip=${ips[j]}
container_name=${role}_$j
if [[ "$ip" == *"host.docker.internal"* ]] || [[ "$ip" == *"localhost"* ]]
then
echo "Running $role on Localhost...."
docker load < docker/${role}.tar.gz
if [[ "$role" == "frontend_server" ]]
then
frontend_port=$port
if [[ "$OSTYPE" == "msys" ]]
then
docker run -v $(pwd -W)/$configFile:/app/config -d -p $port:$port --name $container_name $role --host=0.0.0.0 --port $port
else
if [[ "$OSTYPE" != *"linux"* ]]
then
docker run -v $(pwd)/$configFile:/app/config -d -p $port:$port --name $container_name $role --host=0.0.0.0 --port $port
else
docker run -v $(pwd)/$configFile:/app/config -d -p $port:$port --name $container_name --net=host $role --host=0.0.0.0 --port $port
fi
fi
else
if [[ "$OSTYPE" == "msys" ]]
then
docker run -v $(pwd -W)/$configFile:/app/config -d -p $port:$port --name $container_name $role $j
else
if [[ "$OSTYPE" != *"linux"* ]]
then
docker run -v $(pwd)/$configFile:/app/config -d -p $port:$port --name $container_name $role $j
else
docker run -v $(pwd)/$configFile:/app/config -d -p $port:$port --name $container_name --net=host $role $j
fi
fi
fi
sleep 3
status=0
docker ps | grep "$container_name" | grep -v grep >/dev/null 2>&1 || status=$?
local_container_names+=($container_name)
else
echo "Running role $role on remote machine $ip."
dir[$i]="temp_$i"
ssh -n ec2-user@"$ip" "rm -rf temp_$i && mkdir temp_$i && cd temp_$i && git clone https://github.com/CS677-Labs/Lab-3-Pygmy-The-book-store 1>/dev/null 2>&1 || echo \"Repo already present\""
scp $configFile ec2-user@"$ip":"temp_$i/Lab-3-Pygmy-The-book-store/config"
if [[ "$role" == "frontend_server" ]]
then
ssh -n ec2-user@$ip "cd temp_$i/Lab-3-Pygmy-The-book-store && docker load < docker/${role}.tar.gz && docker run -v \$(pwd)/config:/app/config -d -p $port:$port --name $container_name $role --host=0.0.0.0 --port $port"
else
ssh -n ec2-user@$ip "cd temp_$i/Lab-3-Pygmy-The-book-store && docker load < docker/${role}.tar.gz && docker run -v \$(pwd)/config:/app/config -d -p $port:$port --name $container_name $role $j"
fi
sleep 3
status=0
ssh -n ec2-user@"$ip" "docker ps | grep $container_name | grep -v grep >/dev/null 2>&1" || status=$?
container_names[i]=$container_name
fi
if [[ "$status" != 0 ]]
then
echo "Failed to start the server $role in machine with ip $ip. Exiting..." && exit 1
fi
done
done
if [[ "$ip" == *"host.docker.internal"* ]] || [[ "$ip" == *"localhost"* ]]
then
echo "Frontend server is running on localhost .... Pass this to the CLI to use it with this server."
else
echo "Frontend server is running on ${machines[2]}.... Pass this to the CLI to use it with this server."
fi
echo "Press 'q' to exit"
count=0
while : ; do
read k
if [[ "$k" == "q" ]]
then
echo "Quitting the program"
exit
fi
done
echo "---------------------------------------------------------------"