Skip to content

Commit 51d72eb

Browse files
committed
docker image support
we use python3.9 cuz the khl bot lib does not work in 3.10+ due to the `asyncio.Queue` change
1 parent baded43 commit 51d72eb

File tree

11 files changed

+150
-3
lines changed

11 files changed

+150
-3
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.pyc
2+
__pycache__

.github/workflows/image.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Docker Image
2+
3+
on:
4+
push:
5+
paths:
6+
- ".github/workflows/image.yml"
7+
- "chatbridge/**"
8+
- "lang/**"
9+
- "__main__.py"
10+
- "LICENSE"
11+
- "mcdreforged.plugin.json"
12+
- "**/requirements.txt"
13+
14+
jobs:
15+
image:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v3
19+
20+
- name: Set up QEMU
21+
uses: docker/setup-qemu-action@v2
22+
- name: Set up Docker Buildx
23+
uses: docker/setup-buildx-action@v2
24+
25+
- name: Login to Docker Hub
26+
uses: docker/login-action@v2
27+
with:
28+
username: fallenbreath
29+
password: ${{ secrets.DOCKERHUB_TOKEN }}
30+
31+
- name: Docker meta
32+
id: meta
33+
uses: docker/metadata-action@v4
34+
with:
35+
images: |
36+
fallenbreath/chatbridge
37+
tags: |
38+
type=ref,event=branch
39+
type=semver,pattern={{version}}
40+
41+
- name: Build and push
42+
uses: docker/build-push-action@v4
43+
with:
44+
platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64
45+
file: ./docker/Dockerfile
46+
context: .
47+
push: ${{ github.event_name != 'pull_request' }}
48+
tags: ${{ steps.meta.outputs.tags }}
49+
labels: ${{ steps.meta.outputs.labels }}

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ Enter `python ChatBridge.pyz` in command line to see possible helps
6969

7070
At launch, if the configure file is missing, chatbridge will automatically generate a default one and exit
7171

72+
## Docker Image
73+
74+
[![Docker](https://img.shields.io/docker/v/fallenbreath/chatbridge/latest)](https://hub.docker.com/r/fallenbreath/chatbridge)
75+
76+
Docker Hub image: [`fallenbreath/chatbridge`](https://hub.docker.com/r/fallenbreath/chatbridge)
77+
78+
Image name examples:
79+
80+
- `fallenbreath/chatbridge:latest`
81+
- `fallenbreath/chatbridge:v2.5.3`
82+
83+
Working directory: `/app`
84+
85+
Example usages: `docker run --rm fallenbreath/chatbridge:latest server`
86+
87+
See the [./docker](docker) directory in the repository for more details and docker compose example
88+
7289
## Requirement
7390

7491
Python 3.6+ required

chatbridge/impl/cli/cli_server.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import threading
23
import time
34
import traceback
@@ -87,7 +88,12 @@ def main():
8788
print('- Client #{}: name = {}, password = {}'.format(i + 1, client_info.name, client_info.password))
8889
server.add_client(client_info)
8990
server.start()
90-
server.console_loop()
91+
92+
if sys.stdin.isatty():
93+
server.console_loop()
94+
else:
95+
utils.wait_until_terminate()
96+
server.stop()
9197

9298

9399
if __name__ == '__main__':

chatbridge/impl/cqhttp/entry.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ def main():
184184
config = utils.load_config(ConfigFile, CqHttpConfig)
185185
chatClient = CqHttpChatBridgeClient.create(config)
186186
utils.start_guardian(chatClient)
187+
utils.register_exit_on_termination()
187188
print('Starting CQ Bot')
188189
cq_bot = CQBot(config)
189190
cq_bot.start()

chatbridge/impl/discord/entry.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ def main():
1313
stored.client = DiscordChatClient.create(stored.config)
1414
stored.bot = bot.create_bot()
1515
utils.start_guardian(stored.client)
16+
utils.register_exit_on_termination()
1617

1718
try:
1819
stored.bot.start_running()
1920
except (KeyboardInterrupt, SystemExit):
2021
stored.client.stop()
21-
except:
22+
except Exception:
2223
print(traceback.format_exc())
2324

2425
print('Bye~')

chatbridge/impl/kaiheila/entry.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ def main():
231231
config = utils.load_config(ConfigFile, KaiHeiLaConfig)
232232
chatClient = KhlChatBridgeClient.create(config)
233233
utils.start_guardian(chatClient)
234+
utils.register_exit_on_termination()
234235
print('Starting KHL Bot')
235236
khlBot = createKaiHeiLaBot()
236237
khlBot.startRunning()

chatbridge/impl/online/entry.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import collections
55
import functools
66
import re
7+
import sys
78
import traceback
89
from concurrent.futures.thread import ThreadPoolExecutor
910
from threading import Lock
@@ -135,7 +136,11 @@ def main():
135136
config = utils.load_config(ClientConfigFile, OnlineConfig)
136137
chatClient = OnlineChatClient.create(config)
137138
utils.start_guardian(chatClient)
138-
console_input_loop()
139+
if sys.stdin.isatty():
140+
console_input_loop()
141+
else:
142+
utils.wait_until_terminate()
143+
chatClient.stop()
139144

140145

141146
if __name__ == '__main__':

chatbridge/impl/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import json
22
import os
3+
import queue
4+
import signal
5+
import sys
36
import time
47
from threading import Thread
58
from typing import Type, TypeVar, Callable
@@ -40,3 +43,20 @@ def loop():
4043
thread = Thread(name='ChatBridge Guardian', target=loop, daemon=True)
4144
thread.start()
4245
return thread
46+
47+
48+
def wait_until_terminate():
49+
q = queue.Queue()
50+
signal.signal(signal.SIGINT, lambda s, _: q.put(s))
51+
signal.signal(signal.SIGTERM, lambda s, _: q.put(s))
52+
sig = q.get()
53+
print('Interrupted with {} ({})'.format(signal.Signals(sig).name, sig))
54+
55+
56+
def register_exit_on_termination():
57+
def callback(sig, _):
58+
print('Interrupted with {} ({}), exiting'.format(signal.Signals(sig).name, sig))
59+
sys.exit(0)
60+
61+
signal.signal(signal.SIGINT, callback)
62+
signal.signal(signal.SIGTERM, callback)

docker/Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
ARG PYTHON_VERSION=3.9
2+
3+
FROM python:${PYTHON_VERSION} as builder
4+
5+
RUN pip3 install mcdreforged
6+
7+
COPY chatbridge /build/chatbridge
8+
COPY lang /build/lang
9+
COPY __main__.py LICENSE mcdreforged.plugin.json requirements.txt /build/
10+
RUN cd /build \
11+
&& mcdreforged pack \
12+
&& find . -name "requirements.txt" -exec cat '{}' \; > requirements.all.txt
13+
14+
FROM python:${PYTHON_VERSION}-slim
15+
16+
COPY --from=builder /build/requirements.all.txt /app/
17+
RUN apt-get update \
18+
&& apt-get install -y --no-install-recommends gcc build-essential libffi-dev \
19+
&& rm -rf /var/lib/apt/lists/* \
20+
&& pip3 install -r /app/requirements.all.txt \
21+
&& pip3 cache purge \
22+
&& apt-get purge -y --auto-remove gcc build-essential libffi-dev
23+
24+
COPY --from=builder /build/ChatBridge.pyz /app/
25+
WORKDIR /app
26+
ENTRYPOINT ["python3", "ChatBridge.pyz"]

0 commit comments

Comments
 (0)