-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathci-build.sh
executable file
·72 lines (61 loc) · 3.02 KB
/
ci-build.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
#!/bin/sh
# Tested on ubuntu 18.04
# Generating random number between $FPORT and $EPORT for port binding
FPORT=1025;
EPORT=9999;
RANDHTTP=$(( ( RANDOM % $FPORT ) + $EPORT ))
RANDSSL=$(( ( RANDOM % $FPORT ) + $EPORT ))
RANDDASH=$(( ( RANDOM % $FPORT ) + $EPORT ))
# Name of the Docker container provided in ARG $1
NAME=$1
check_port_availability () {
HTTP_PORT_CHECK=$1
SSL_PORT_CHECK=$2
DASH_PORT_CHECK=$3
if [ $HTTP_PORT_CHECK -eq $SSL_PORT_CHECK ] || [ $HTTP_PORT_CHECK -eq $DASH_PORT_CHECK ] || [ $SSL_PORT_CHECK -eq $DASH_PORT_CHECK ]; then
printf "\n\nRandom Port Collision...Randomizing HTTP Port!\n\n"
RANDHTTP=$(( ( RANDOM % $FPORT ) + $EPORT )) # Randomizing port
exit
fi
if [ $SSL_PORT_CHECK -eq $DASH_PORT_CHECK ]; then
printf "\n\nRandom Port Collision...Randomizing HTTP Port!\n\n"
RANDSSL=$(( ( RANDOM % $FPORT ) + $EPORT )) # Randomizing port
exit
fi
for USED_PORT in $( netstat -ltn | sed -rne '/^tcp/{/:\>/d;s/.*:([0-9]+)\>.*/\1/p}' | sort -n | uniq ); do
if [ $HTTP_PORT_CHECK -eq $USED_PORT ]; then
printf "\n\n$HTTP_PORT_CHECK conflicts with open port: $USED_PORT...Randomizing HTTP Port!\n\n"
RANDHTTP=$(( ( RANDOM % $FPORT ) + $EPORT ))
exit
elif [ $SSL_PORT_CHECK -eq $USED_PORT ]; then
printf "\n\n$SSL_PORT_CHECK conflicts with open port: $USED_PORT...Randomizing HTTPS Port!\n\n"
RANDSSL=$(( ( RANDOM % $FPORT ) + $EPORT )) # Randomizing port
exit
elif [ $DASH_PORT_CHECK -eq $USED_PORT ]; then
printf "\n\n$DASH_PORT_CHECK conflicts with open port: $USED_PORT...Randomizing Dashboard/API Port!\n\n"
RANDDASH=$(( ( RANDOM % $FPORT ) + $EPORT )) # Randomizing port
exit
fi
done
return
}
port_sanity=$(check_port_availability $RANDHTTP $RANDSSL $RANDDASH)
# Port check and randomize
# Loop until all ports are random
if [ -z "$port_sanity" ]; then
printf "\nWe will run the container with these randomly assigned ports:\nHTTP port $RANDHTTP\nHTTPS port $RANDSSL\nDashboard port $RANDDASH\n\n"
else
port_sanity=$(check_port_availability $RANDHTTP $RANDSSL $RANDDASH)
fi
# Run container
# Make sure this Container is not running
printf "Make sure a Container with the designated name is not running..."
OUTPUT="$(docker stop $NAME)"
if echo "$OUTPUT" | grep -c "No such container"; then
echo "A container with name, $NAME, was stopped. Good to proceed.."
else
echo "No container with name, $NAME, exists. Good to proceed.."
fi
printf "\nGoing to run:\ndocker run -d -p $RANDHTTP:80 -p $RANDSSL:443 -p $RANDDASH:8080 -v '$(pwd)/test/etc/nginx/conf.d:/etc/nginx/conf.d' --name $NAME $NAME\n\n"
docker run -d -p $RANDHTTP:80 -p $RANDSSL:443 -p $RANDDASH:8080 -v "$(pwd)/etc/nginx/conf.d:/etc/nginx/conf.d" --name $NAME $NAME
exit