-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-api-docker.sh
executable file
·136 lines (116 loc) · 6.81 KB
/
start-api-docker.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
#!/bin/bash
# get host name
os_name=$(uname -s)
white="\033[0;37m"
green="\033[1;32m"
end_style="\033[0m"
# test if sudo command exists
if command -v sudo &> /dev/null
then
SUDO="sudo"
else
SUDO=""
fi
# 检查 Docker 是否正在运行
if ! docker info > /dev/null 2>&1; then
echo -e "\033[1;31mDocker is not running. Please start Docker and try again.\033[0m"
exit 1
fi
# get local ip address according to the operating system
if [ "$os_name" = "Linux" ]; then
ip_address=$(hostname -I | awk '{print $1}')
elif [ "$os_name" = "Darwin" ]; then
ip_address=$(ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | head -n 1)
else
echo "Unsupported OS"
os_name=Windows
ip_address=localhost
fi
# get public ip address
public_ip_address=$(curl -s https://api.ipify.org)
echo -e "\033[2J\033[0;0H"
echo -e "${white}---------------------------------------------------------------------------------------${end_style}"
echo -e "${white}| UPC System Start Script |${end_style}"
echo -e "${white}|-------------------------------------------------------------------------------------|${end_style}"
echo -e "${white}| ██╗ ██╗██████╗ ██████╗ |${end_style}"
echo -e "${white}| ██║ ██║██╔══██╗██╔════╝ |${end_style}"
echo -e "${white}| ██║ ██║██████╔╝██║ |${end_style}"
echo -e "${white}| ██║ ██║██╔═══╝ ██║ |${end_style}"
echo -e "${white}| ╚██████╔╝██║ ╚██████╗ |${end_style}"
echo -e "${white}| ╚═════╝ ╚═╝ ╚═════╝ |${end_style}"
echo -e "${white}|-------------------------------------------------------------------------------------|${end_style}"
echo -e "${white}| Backend |${end_style}"
echo -e "${white}---------------------------------------------------------------------------------------${end_style}"
echo ""
echo -e "\033[37mYour Host type: \033[1;33m$os_name${end_style}"
echo -e "\033[37mYour Local IP Address: \033[1;33m$ip_address${end_style}"
echo -e "\033[37mYour Public IP Address: \033[1;33m$public_ip_address${end_style}"
echo ""
default_api_url="http://$ip_address:4000"
default_central_server_url="http://$ip_address:8000"
# Ask user to input API host URL
echo -e "\033[1;37m1. Please enter your API host URL${end_style} ${white}(press Enter for default:${end_style} \033[32mhttp://$ip_address:4000${end_style}${white}):${end_style}"
if ! read -e -p '(default): ' -i "$default_api_url" URL 2>/dev/null;
then
# if failed, use 'read' command without '-i' option
read -e -p "(default: $default_api_url): " URL
else
# if succeed, use 'read' command with '-i' option
read -e -p $'\033[0;33m(default)\033[0m: ' -i "$default_api_url" URL
fi
API_URL=${URL:-http://$ip_address:4000}
API_PORT=$(echo $API_URL | cut -d':' -f3)
API_PORT=${API_PORT:-4000}
echo -e "\033[97mAPI Host URL: ${green}\033[4m$API_URL${end_style}"
echo ""
# Ask user to input central register server URL
echo -e "\033[1;37m2. Please enter your central register server URL${end_style} ${white}(press Enter for default:${end_style} \033[32mhttp://$ip_address:8000${end_style}${white}):${end_style} "
if ! read -e -p '(default): ' -i "$default_central_server_url" URL 2>/dev/null;
then
# if failed, use 'read' command without '-i' option
read -e -p "(default: $default_central_server_url): " CENTRAL_SERVER
else
# if succeed, use 'read' command with '-i' option
read -e -p $'\033[0;33m(default)\033[0m: ' -i "$default_central_server_url" CENTRAL_SERVER
fi
CENTRAL_SERVER=${CENTRAL_SERVER:-http://$ip_address:8000}
echo -e "\033[97mCentral Register Server URL: ${green}\033[4m$CENTRAL_SERVER${end_style}"
echo ""
sleep 1
echo -e "\033[2J\033[0;0H"
echo -e "${green}---------------------------------------------------------------------------------------${end_style}"
echo -e "${green}| UPC System Start Script |${end_style}"
echo -e "${green}|-------------------------------------------------------------------------------------|${end_style}"
echo -e "${green}| ██╗ ██╗██████╗ ██████╗ |${end_style}"
echo -e "${green}| ██║ ██║██╔══██╗██╔════╝ |${end_style}"
echo -e "${green}| ██║ ██║██████╔╝██║ |${end_style}"
echo -e "${green}| ██║ ██║██╔═══╝ ██║ |${end_style}"
echo -e "${green}| ╚██████╔╝██║ ╚██████╗ |${end_style}"
echo -e "${green}| ╚═════╝ ╚═╝ ╚═════╝ |${end_style}"
echo -e "${green}|-------------------------------------------------------------------------------------|${end_style}"
echo -e "${green}| Backend |${end_style}"
echo -e "${green}---------------------------------------------------------------------------------------${end_style}"
# check if there are dangling images
dangling_images=$($SUDO docker images -f "dangling=true" -q)
if [ -n "$dangling_images" ]; then
# if there are dangling images, remove them
$SUDO docker rmi $dangling_images
fi
# replace the ip address in files, and start the docker container
if [ "${os_name}" = "Windows" ]; then
$SUDO docker pull afterlifexx/upc-api:latest && \
$SUDO docker run -e API_URL=$API_URL \
-e CENTRAL_SERVER=$CENTRAL_SERVER \
-e API_PORT=$API_PORT \
-v "//var/run/docker.sock:/var/run/docker.sock" \
-p $API_PORT:$API_PORT -it --rm \
afterlifexx/upc-api:latest
else
$SUDO docker pull afterlifexx/upc-api:latest && \
$SUDO docker run -e API_URL=$API_URL \
-e CENTRAL_SERVER=$CENTRAL_SERVER \
-e API_PORT=$API_PORT \
-v /var/run/docker.sock:/var/run/docker.sock \
-p $API_PORT:$API_PORT -it --rm \
afterlifexx/upc-api:latest
fi