Skip to content

Commit e450110

Browse files
authored
Merge pull request #284 from wutongshufqw/V6-docker
V6 docker
2 parents 2b2cb8d + 85ce0b3 commit e450110

File tree

6 files changed

+325
-0
lines changed

6 files changed

+325
-0
lines changed

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Dockerfile
2+
docker.sh
3+
install.sh
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: ci
2+
3+
on:
4+
push:
5+
6+
jobs:
7+
docker:
8+
runs-on: ubuntu-latest
9+
steps:
10+
-
11+
name: Checkout
12+
uses: actions/checkout@v4
13+
-
14+
name: Set up QEMU
15+
uses: docker/setup-qemu-action@v3
16+
-
17+
name: Set up Docker Buildx
18+
uses: docker/setup-buildx-action@v3
19+
-
20+
name: Login to Docker Hub
21+
uses: docker/login-action@v3
22+
with:
23+
username: ${{ secrets.DOCKERHUB_USERNAME }}
24+
password: ${{ secrets.DOCKERHUB_TOKEN }}
25+
-
26+
name: Build and push
27+
uses: docker/build-push-action@v6
28+
with:
29+
context: .
30+
push: true
31+
tags: amiyabot/amiyabot:latest

Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 使用python3.9作为基础镜像
2+
FROM mcr.microsoft.com/playwright/python:v1.31.1
3+
4+
# 设置数据卷
5+
VOLUME [ "/amiyabot" ]
6+
7+
# 设置工作目录
8+
WORKDIR /app
9+
10+
# 守护端口
11+
EXPOSE 8088
12+
13+
# 拷贝当前目录下的所有文件到工作目录
14+
COPY requirements.txt /app
15+
COPY entrypoint.sh /app
16+
COPY . /temp
17+
RUN tar -zcvf amiyabot.tar.gz --exclude=/temp/.git --exclude=/temp/.vscode --exclude=/temp/.idea --exclude=/temp/docker.sh \
18+
--exclude=/temp/entrypoint.sh --exclude=/temp/install.sh --exclude=/temp/Dockerfile /temp/*
19+
20+
# 安装依赖
21+
RUN pip install -r requirements.txt
22+
RUN playwright install --with-deps chromium
23+
24+
# 启动命令
25+
ENTRYPOINT [ "bash", "entrypoint.sh" ]

entrypoint.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import os
2+
from pathlib import Path
3+
4+
try:
5+
import yaml
6+
except ImportError:
7+
import subprocess
8+
9+
subprocess.run(["pip", "install", "pyyaml"])
10+
import yaml
11+
12+
config_path = Path('config')
13+
14+
def load_config(name: str) -> dict:
15+
with open(config_path / f'{name}.yaml', 'r') as f:
16+
return yaml.safe_load(f)
17+
18+
def save_config(name: str, config: dict) -> None:
19+
with open(config_path / f'{name}.yaml', 'w') as f:
20+
yaml.safe_dump(config, f, encoding='utf-8', allow_unicode=True)
21+
22+
def set_database():
23+
config = load_config('database')
24+
if 'ENABLE_MYSQL' in os.environ and os.environ['ENABLE_MYSQL']:
25+
config['mode'] = 'mysql'
26+
else:
27+
return
28+
if 'MYSQL_HOST' in os.environ and os.environ['MYSQL_HOST']:
29+
config['config']['host'] = os.environ['MYSQL_HOST']
30+
if 'MYSQL_PORT' in os.environ and os.environ['MYSQL_PORT']:
31+
config['config']['port'] = os.environ['MYSQL_PORT']
32+
if 'MYSQL_USER' in os.environ and os.environ['MYSQL_USER']:
33+
config['config']['user'] = os.environ['MYSQL_USER']
34+
if 'MYSQL_PASSWORD' in os.environ and os.environ['MYSQL_PASSWORD']:
35+
config['config']['password'] = os.environ['MYSQL_PASSWORD']
36+
save_config('database', config)
37+
38+
39+
def set_prefix():
40+
config = load_config('prefix')
41+
if 'PREFIX' in os.environ and os.environ['PREFIX']:
42+
if os.environ['PREFIX'].startswith('[') and os.environ['PREFIX'].endswith(']'):
43+
prefixs = os.environ['PREFIX'][1:-1].split(',')
44+
new = []
45+
for prefix in prefixs:
46+
# remove space
47+
new.append(prefix.strip().replace('\'', '').replace('\"', '').replace('`', ''))
48+
prefixs = new
49+
else:
50+
prefixs = [os.environ['PREFIX']]
51+
config['prefix_keywords'] = prefixs
52+
save_config('prefix', config)
53+
54+
55+
def set_server():
56+
config = load_config('server')
57+
config['host'] = '0.0.0.0'
58+
if 'AUTH' in os.environ and os.environ['AUTH']:
59+
config['authKey'] = os.environ['AUTH']
60+
save_config('server', config)
61+
62+
63+
def main():
64+
set_database()
65+
set_prefix()
66+
set_server()
67+
68+
69+
if __name__ == '__main__':
70+
main()
71+

entrypoint.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
BOT_FOLDER=/amiyabot
4+
5+
if [ ! -f "$BOT_FOLDER/first_run" ]; then
6+
# step 1: 解压bot本体
7+
tar -zxvf amiyabot.tar.gz -C $BOT_FOLDER
8+
# step 2: 进入bot目录
9+
cd $BOT_FOLDER
10+
# step 3: 初始化配置文件
11+
python entrypoint.py
12+
# step 4: 标记已初始化
13+
touch first_run
14+
fi
15+
16+
# step 5: 运行bot
17+
cd $BOT_FOLDER
18+
python amiya.py

install.sh

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#!/bin/bash
2+
3+
# step 1: 准备docker环境
4+
if [ -x "$(command -v docker)" ]; then
5+
echo "Docker 已安装"
6+
else
7+
echo "Docker 未安装, 开始安装..."
8+
sudo apt-get update
9+
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
10+
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
11+
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
12+
sudo apt-get update
13+
sudo apt-get install -y docker-ce
14+
fi
15+
16+
# step 2: 收集环境变量
17+
while true; do
18+
echo -n "是否启用mysql? [y/n] (默认: 使用sqlite): "
19+
read mysql_enable
20+
if [ "$mysql_enable" ]; then
21+
if [ "$mysql_enable" = "y" ] || [ "$mysql_enable" = "Y" ]; then
22+
mysql_enable=true
23+
echo -n "请输入mysql主机地址 (默认: 127.0.0.1): "
24+
read mysql_host
25+
echo -n "请输入mysql端口 (默认: 3306): "
26+
read mysql_port
27+
echo -n "请输入mysql用户名: "
28+
read mysql_user
29+
echo -n "请输入mysql密码: "
30+
read mysql_password
31+
break
32+
elif [ "$mysql_enable" = "n" ] || [ "$mysql_enable" = "N" ]; then
33+
mysql_enable=false
34+
break
35+
else
36+
echo "请输入y或n"
37+
continue
38+
fi
39+
else
40+
echo "请输入y或n"
41+
continue
42+
fi
43+
done
44+
45+
while true; do
46+
echo -n "是否修改前缀? [y/n] (默认: 兔兔): "
47+
read prefix_enable
48+
if [ "$prefix_enable" ]; then
49+
if [ "$prefix_enable" = "y" ] || [ "$prefix_enable" = "Y" ]; then
50+
echo -n "请输入前缀, 用','分隔: "
51+
read prefix
52+
# 将前缀转换为python列表
53+
prefix="[\"$(echo $prefix | sed 's/,/\",\"/g')\"]"
54+
break
55+
elif [ "$prefix_enable" = "n" ] || [ "$prefix_enable" = "N" ]; then
56+
break
57+
else
58+
echo "请输入y或n"
59+
continue
60+
fi
61+
else
62+
echo "请输入y或n"
63+
continue
64+
fi
65+
done
66+
67+
while true; do
68+
echo -n "是否修改AuthKey? [y/n] (默认: 无): "
69+
read auth_enable
70+
if [ "$auth_enable" ]; then
71+
if [ "$auth_enable" = "y" ] || [ "$auth_enable" = "Y" ]; then
72+
echo -n "请输入AuthKey: "
73+
read auth
74+
break
75+
elif [ "$auth_enable" = "n" ] || [ "$auth_enable" = "N" ]; then
76+
break
77+
else
78+
echo "请输入y或n"
79+
continue
80+
fi
81+
else
82+
echo "请输入y或n"
83+
continue
84+
fi
85+
done
86+
87+
while true; do
88+
echo -n "是否修改端口? [y/n] (默认: 8088): "
89+
read port_enable
90+
if [ "$port_enable" ]; then
91+
if [ "$port_enable" = "y" ] || [ "$port_enable" = "Y" ]; then
92+
echo -n "请输入端口: "
93+
read port
94+
break
95+
elif [ "$port_enable" = "n" ] || [ "$port_enable" = "N" ]; then
96+
port=8088
97+
break
98+
else
99+
echo "请输入y或n"
100+
continue
101+
fi
102+
else
103+
echo "请输入y或n"
104+
continue
105+
fi
106+
done
107+
108+
# # step 3: 运行docker
109+
110+
while true; do
111+
echo -n "是否将amiyabot挂载到本地? [y/n] (默认挂载到amiyabot存储卷): "
112+
read mount_enable
113+
if [ "$mount_enable" ]; then
114+
if [ "$mount_enable" = "y" ] || [ "$mount_enable" = "Y" ]; then
115+
echo -n "请输入挂载路径 (默认: $HOME/amiyabot): "
116+
read mount_path
117+
if [ ! "$mount_path" ]; then
118+
mount_path="$HOME/amiyabot"
119+
fi
120+
break
121+
elif [ "$mount_enable" = "n" ] || [ "$mount_enable" = "N" ]; then
122+
break
123+
else
124+
echo "请输入y或n"
125+
continue
126+
fi
127+
else
128+
echo "请输入y或n"
129+
continue
130+
fi
131+
done
132+
133+
echo -n "请输入容器名称 (默认: amiyabot): "
134+
read container_name
135+
if [ ! "$container_name" ]; then
136+
container_name="amiyabot"
137+
fi
138+
139+
command="sudo docker run -d --name $container_name"
140+
if [ "$mysql_enable" = true ]; then
141+
command="$command -e ENABLE_MYSQL=true -e MYSQL_HOST=$mysql_host -e MYSQL_PORT=$mysql_port -e MYSQL_USER=$mysql_user -e MYSQL_PASSWORD=$mysql_password"
142+
fi
143+
if [ "$prefix" ]; then
144+
command="$command -e PREFIX=\"$prefix\""
145+
fi
146+
if [ "$auth" ]; then
147+
command="$command -e AUTH=$auth"
148+
fi
149+
if [ "$port" ]; then
150+
command="$command -p $port:8088"
151+
fi
152+
if [ "$mount_path" ]; then
153+
command="$command -v $mount_path:/amiyabot"
154+
else
155+
command="$command -v amiyabot:/amiyabot"
156+
fi
157+
command="$command amiyabot/amiyabot:latest"
158+
159+
while true; do
160+
echo "即将运行命令: $command, 是否继续? [y/n] (默认: n)"
161+
read confirm
162+
if [ "$confirm" ]; then
163+
if [ "$confirm" = "y" ]; then
164+
eval $command
165+
break
166+
elif [ "$confirm" = "n" ]; then
167+
echo "已取消"
168+
break
169+
else
170+
echo "请输入y或n"
171+
continue
172+
fi
173+
else
174+
echo "请输入y或n"
175+
continue
176+
fi
177+
done

0 commit comments

Comments
 (0)