-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
229 lines (216 loc) · 6.05 KB
/
install.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
#!/bin/bash
install_dir=$(pwd)/snapemu
db_password=''
web_domain=''
jwt_key=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 16)
RED='\e[31m'
RESET='\e[0m'
GREEN='\e[32m'
error_log() {
local input="$1"
echo -e "${RED}${input}${RESET}"
exit 1
}
info_log() {
local input="$1"
echo -e "${GREEN}${input}${RESET}"
}
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
else
echo "Only Ubuntu and Debian are supported"
exit 1
fi
install_docker_ubuntu_debian() {
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
}
check_docker_installed() {
if command -v docker &> /dev/null; then
local version=$(docker --version)
info_log "docker is installed:${version}"
else
ask_to_install_docker
check_docker_installed
fi
}
check_docker_running() {
if sudo systemctl is-active --quiet docker; then
info_log "Docker is running"
else
echo "Docker is not running, trying to start..."
sudo systemctl start docker
if sudo systemctl is-active --quiet docker; then
info_log "Docker has started successfully"
else
error_log "Docker startup failed, please check the problem"
exit 1
fi
fi
}
ask_to_install_docker() {
local answer
# 提示用户输入
echo -n -e "Do you want to install docker? (Y/n)"
read answer
answer="${answer:-Y}"
case "$answer" in
yes|y|Y)
;;
*)
error_log "exit setup"
exit 1
;;
esac
case "$OS" in
ubuntu|debian)
install_docker_ubuntu_debian
;;
*)
error_log "The operating system does not support it"
exit 1
;;
esac
}
check_install_dir() {
if [ -d "$install_dir" ]; then
error_log "The software has been installed in $install_dir"
else
info_log "The software will be installed in ${install_dir}"
mkdir -p $install_dir
fi
}
generate_docker_compose() {
cat <<EOF > ${install_dir}/compose.yaml
services:
traefik:
image: "traefik:v3.2"
restart: always
command:
- "--api.insecure=true"
- "--providers.docker"
- "--entrypoints.web.address=:80"
- "--entrypoints.web.forwardedheaders.insecure"
- "--entrypoints.websecure.address=:443"
- "--entrypoints.websecure.http3"
- "--certificatesresolvers.myresolver.acme.httpchallenge.entrypoint=web"
- "--certificatesresolvers.myresolver.acme.email=example@example.com"
- "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
ports:
- "80:80"
- "443:443"
volumes:
- "letsencrypt:/letsencrypt"
- "/var/run/docker.sock:/var/run/docker.sock:ro"
db:
image: 'postgres:16.3-bookworm'
restart: always
environment:
POSTGRES_PASSWORD: ${db_password}
POSTGRES_DB: snapemu
volumes:
- "db-data:/var/lib/postgresql/data"
redis:
image: 'redis:7.2.4-bookworm'
restart: always
api:
image: 'registry.cn-hongkong.aliyuncs.com/snap_emu/api:alpine-dev'
pull_policy: always
volumes:
- ./config:/etc/snapemu:ro
- "assets:/app/assets"
restart: always
command: ["./snap_api", "run"]
labels:
- "traefik.enable=true"
- "traefik.http.routers.snapemu.rule=Host(\`${web_domain}\`) && PathPrefix(\`/api\`)"
- "traefik.http.routers.snapemu.service=snapemu"
- "traefik.http.routers.snapemu.tls=true"
- "traefik.http.routers.snapemu.tls.certresolver=myresolver"
- "traefik.http.routers.snapemuhttp.rule=Host(\`${web_domain}\`) && PathPrefix(\`/api\`)"
- "traefik.http.routers.snapemuhttp.service=snapemu"
- "traefik.http.services.snapemu.loadbalancer.server.port=8000"
manager:
image: 'registry.cn-hongkong.aliyuncs.com/snap_emu/manager:alpine-dev'
pull_policy: always
ports:
- 1700:1700/udp
volumes:
- ./config:/etc/snapemu:ro
restart: always
command: ["./devices_manager", "run"]
web:
image: 'registry.cn-hongkong.aliyuncs.com/snap_emu/web:alpine-dev'
pull_policy: always
labels:
- "traefik.enable=true"
- "traefik.http.routers.web.rule=Host(\`${web_domain}\`)"
- "traefik.http.routers.web.service=web"
- "traefik.http.routers.web.tls=true"
- "traefik.http.routers.web.tls.certresolver=myresolver"
- "traefik.http.routers.web.rule=Host(\`${web_domain}\`)"
- "traefik.http.routers.web.service=web"
- "traefik.http.services.web.loadbalancer.server.port=80"
volumes:
letsencrypt:
db-data:
assets:
EOF
}
generate_config() {
local config_dir=${install_dir}/config
if [ -d "$config_dir" ]; then
error_log "The software has been installed in ${config_dir}"
else
mkdir -p $config_dir
fi
local api_config=${install_dir}/config/config.yaml
read -p "Enter the website domain name: " web_domain
read -p "Please enter the database password: " db_password
cat <<EOF > ${api_config}
log: INFO
device:
topic:
data: LoRa-Push-Data
event: LoRa-Event
down: LoRa-Down-Data
lorawan:
host: "0.0.0.0"
port: 1700
api:
model:
path: /etc/snapemu/model.yaml
port: 8000
host: "0.0.0.0"
web: "https://platform.snapemu.com"
redis:
host: redis
db: 0
db:
host: db
password: ${db_password}
username: postgres
db: snapemu
jwt_key: ${jwt_key}
EOF
}
start_run() {
cd $install_dir
sudo docker compose up -d
}
check_docker_installed
check_docker_running
check_install_dir
generate_config
generate_docker_compose
start_run