Skip to content

Commit

Permalink
Merge pull request #284 from wutongshufqw/V6-docker
Browse files Browse the repository at this point in the history
V6 docker
  • Loading branch information
vivien8261 authored Sep 19, 2024
2 parents 2b2cb8d + 85ce0b3 commit e450110
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Dockerfile
docker.sh
install.sh
31 changes: 31 additions & 0 deletions .github/workflows/docker-build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: ci

on:
push:

jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Set up QEMU
uses: docker/setup-qemu-action@v3
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
-
name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: amiyabot/amiyabot:latest
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 使用python3.9作为基础镜像
FROM mcr.microsoft.com/playwright/python:v1.31.1

# 设置数据卷
VOLUME [ "/amiyabot" ]

# 设置工作目录
WORKDIR /app

# 守护端口
EXPOSE 8088

# 拷贝当前目录下的所有文件到工作目录
COPY requirements.txt /app
COPY entrypoint.sh /app
COPY . /temp
RUN tar -zcvf amiyabot.tar.gz --exclude=/temp/.git --exclude=/temp/.vscode --exclude=/temp/.idea --exclude=/temp/docker.sh \
--exclude=/temp/entrypoint.sh --exclude=/temp/install.sh --exclude=/temp/Dockerfile /temp/*

# 安装依赖
RUN pip install -r requirements.txt
RUN playwright install --with-deps chromium

# 启动命令
ENTRYPOINT [ "bash", "entrypoint.sh" ]
71 changes: 71 additions & 0 deletions entrypoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import os
from pathlib import Path

try:
import yaml
except ImportError:
import subprocess

subprocess.run(["pip", "install", "pyyaml"])
import yaml

config_path = Path('config')

def load_config(name: str) -> dict:
with open(config_path / f'{name}.yaml', 'r') as f:
return yaml.safe_load(f)

def save_config(name: str, config: dict) -> None:
with open(config_path / f'{name}.yaml', 'w') as f:
yaml.safe_dump(config, f, encoding='utf-8', allow_unicode=True)

def set_database():
config = load_config('database')
if 'ENABLE_MYSQL' in os.environ and os.environ['ENABLE_MYSQL']:
config['mode'] = 'mysql'
else:
return
if 'MYSQL_HOST' in os.environ and os.environ['MYSQL_HOST']:
config['config']['host'] = os.environ['MYSQL_HOST']
if 'MYSQL_PORT' in os.environ and os.environ['MYSQL_PORT']:
config['config']['port'] = os.environ['MYSQL_PORT']
if 'MYSQL_USER' in os.environ and os.environ['MYSQL_USER']:
config['config']['user'] = os.environ['MYSQL_USER']
if 'MYSQL_PASSWORD' in os.environ and os.environ['MYSQL_PASSWORD']:
config['config']['password'] = os.environ['MYSQL_PASSWORD']
save_config('database', config)


def set_prefix():
config = load_config('prefix')
if 'PREFIX' in os.environ and os.environ['PREFIX']:
if os.environ['PREFIX'].startswith('[') and os.environ['PREFIX'].endswith(']'):
prefixs = os.environ['PREFIX'][1:-1].split(',')
new = []
for prefix in prefixs:
# remove space
new.append(prefix.strip().replace('\'', '').replace('\"', '').replace('`', ''))
prefixs = new
else:
prefixs = [os.environ['PREFIX']]
config['prefix_keywords'] = prefixs
save_config('prefix', config)


def set_server():
config = load_config('server')
config['host'] = '0.0.0.0'
if 'AUTH' in os.environ and os.environ['AUTH']:
config['authKey'] = os.environ['AUTH']
save_config('server', config)


def main():
set_database()
set_prefix()
set_server()


if __name__ == '__main__':
main()

18 changes: 18 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

BOT_FOLDER=/amiyabot

if [ ! -f "$BOT_FOLDER/first_run" ]; then
# step 1: 解压bot本体
tar -zxvf amiyabot.tar.gz -C $BOT_FOLDER
# step 2: 进入bot目录
cd $BOT_FOLDER
# step 3: 初始化配置文件
python entrypoint.py
# step 4: 标记已初始化
touch first_run
fi

# step 5: 运行bot
cd $BOT_FOLDER
python amiya.py
177 changes: 177 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#!/bin/bash

# step 1: 准备docker环境
if [ -x "$(command -v docker)" ]; then
echo "Docker 已安装"
else
echo "Docker 未安装, 开始安装..."
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce
fi

# step 2: 收集环境变量
while true; do
echo -n "是否启用mysql? [y/n] (默认: 使用sqlite): "
read mysql_enable
if [ "$mysql_enable" ]; then
if [ "$mysql_enable" = "y" ] || [ "$mysql_enable" = "Y" ]; then
mysql_enable=true
echo -n "请输入mysql主机地址 (默认: 127.0.0.1): "
read mysql_host
echo -n "请输入mysql端口 (默认: 3306): "
read mysql_port
echo -n "请输入mysql用户名: "
read mysql_user
echo -n "请输入mysql密码: "
read mysql_password
break
elif [ "$mysql_enable" = "n" ] || [ "$mysql_enable" = "N" ]; then
mysql_enable=false
break
else
echo "请输入y或n"
continue
fi
else
echo "请输入y或n"
continue
fi
done

while true; do
echo -n "是否修改前缀? [y/n] (默认: 兔兔): "
read prefix_enable
if [ "$prefix_enable" ]; then
if [ "$prefix_enable" = "y" ] || [ "$prefix_enable" = "Y" ]; then
echo -n "请输入前缀, 用','分隔: "
read prefix
# 将前缀转换为python列表
prefix="[\"$(echo $prefix | sed 's/,/\",\"/g')\"]"
break
elif [ "$prefix_enable" = "n" ] || [ "$prefix_enable" = "N" ]; then
break
else
echo "请输入y或n"
continue
fi
else
echo "请输入y或n"
continue
fi
done

while true; do
echo -n "是否修改AuthKey? [y/n] (默认: 无): "
read auth_enable
if [ "$auth_enable" ]; then
if [ "$auth_enable" = "y" ] || [ "$auth_enable" = "Y" ]; then
echo -n "请输入AuthKey: "
read auth
break
elif [ "$auth_enable" = "n" ] || [ "$auth_enable" = "N" ]; then
break
else
echo "请输入y或n"
continue
fi
else
echo "请输入y或n"
continue
fi
done

while true; do
echo -n "是否修改端口? [y/n] (默认: 8088): "
read port_enable
if [ "$port_enable" ]; then
if [ "$port_enable" = "y" ] || [ "$port_enable" = "Y" ]; then
echo -n "请输入端口: "
read port
break
elif [ "$port_enable" = "n" ] || [ "$port_enable" = "N" ]; then
port=8088
break
else
echo "请输入y或n"
continue
fi
else
echo "请输入y或n"
continue
fi
done

# # step 3: 运行docker

while true; do
echo -n "是否将amiyabot挂载到本地? [y/n] (默认挂载到amiyabot存储卷): "
read mount_enable
if [ "$mount_enable" ]; then
if [ "$mount_enable" = "y" ] || [ "$mount_enable" = "Y" ]; then
echo -n "请输入挂载路径 (默认: $HOME/amiyabot): "
read mount_path
if [ ! "$mount_path" ]; then
mount_path="$HOME/amiyabot"
fi
break
elif [ "$mount_enable" = "n" ] || [ "$mount_enable" = "N" ]; then
break
else
echo "请输入y或n"
continue
fi
else
echo "请输入y或n"
continue
fi
done

echo -n "请输入容器名称 (默认: amiyabot): "
read container_name
if [ ! "$container_name" ]; then
container_name="amiyabot"
fi

command="sudo docker run -d --name $container_name"
if [ "$mysql_enable" = true ]; then
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"
fi
if [ "$prefix" ]; then
command="$command -e PREFIX=\"$prefix\""
fi
if [ "$auth" ]; then
command="$command -e AUTH=$auth"
fi
if [ "$port" ]; then
command="$command -p $port:8088"
fi
if [ "$mount_path" ]; then
command="$command -v $mount_path:/amiyabot"
else
command="$command -v amiyabot:/amiyabot"
fi
command="$command amiyabot/amiyabot:latest"

while true; do
echo "即将运行命令: $command, 是否继续? [y/n] (默认: n)"
read confirm
if [ "$confirm" ]; then
if [ "$confirm" = "y" ]; then
eval $command
break
elif [ "$confirm" = "n" ]; then
echo "已取消"
break
else
echo "请输入y或n"
continue
fi
else
echo "请输入y或n"
continue
fi
done

0 comments on commit e450110

Please sign in to comment.