diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..8712ca5 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,60 @@ +name: Docker + +on: [push, pull_request] + +env: + IMAGE_NAME: crobot + +jobs: + push: + runs-on: ubuntu-latest + if: github.event_name == 'push' + + steps: + - uses: actions/checkout@v2 + + - name: Docker Login + env: + DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} + DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} + run: | + echo "${DOCKER_PASSWORD}" | docker login -u ${DOCKER_USERNAME} --password-stdin + + - name: Build image + run: | + IMAGE_ID=${{ github.repository }} + # Change all uppercase to lowercase + IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') + + docker pull $IMAGE_ID:build || true + docker pull $IMAGE_ID:latest || true + docker build --target build \ + --cache-from=$IMAGE_ID:build \ + --tag $IMAGE_NAME:build . + docker build --target runtime \ + --cache-from=$IMAGE_ID:build \ + --cache-from=$IMAGE_ID:latest \ + --tag $IMAGE_NAME . + + - name: Push image + run: | + IMAGE_ID=${{ github.repository }} + # Change all uppercase to lowercase + IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') + + # Strip git ref prefix from version + VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') + + # Strip "v" prefix from tag name + [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') + + # Use Docker `latest` tag convention + [ "$VERSION" == "master" ] && VERSION=latest + + echo IMAGE_ID=$IMAGE_ID + echo VERSION=$VERSION + + docker tag $IMAGE_NAME $IMAGE_ID:$VERSION + docker tag $IMAGE_NAME:build $IMAGE_ID:build + docker push $IMAGE_ID:$VERSION + docker push $IMAGE_ID:build diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..30eacd3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,33 @@ +FROM python:alpine AS build +LABEL git="https://github.com/Aeriqu/CroBot" +ARG KYTEA_VERSION=0.4.7 + +WORKDIR /app + +RUN apk add --no-cache build-base libffi-dev && \ +wget http://www.phontron.com/kytea/download/kytea-${KYTEA_VERSION}.tar.gz && \ +tar xf kytea-${KYTEA_VERSION}.tar.gz && \ +cd kytea-${KYTEA_VERSION} && ./configure && make && \ +mkdir /app/kytea && make install prefix=/app/kytea + +RUN pip install virtualenv && python -m virtualenv venv +COPY /requirements.txt requirements.txt +RUN ./venv/bin/pip install \ +--global-option=build_ext \ +--global-option="-L/app/kytea/lib/" \ +--global-option="-I/app/kytea/include/" \ +-r requirements.txt + + +FROM python:alpine AS runtime +RUN apk add --no-cache libstdc++ +WORKDIR /app +COPY --from=build /app/venv venv +# we copy kytea/{bin,include,lib,share} +COPY --from=build /app/kytea/ /usr/local/ +ENV PATH="/app/venv/bin:$PATH" +RUN ln -s /usr/local/share/kytea/model.bin + +COPY . . +RUN ./db_init.py +CMD ./run.py diff --git a/README.md b/README.md index 48f65e0..6f75587 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,15 @@ To properly use kytea, a model file is required. You can obtain one from the [ky source](http://www.phontron.com/kytea/#download). -### Set up +## Set up + +### Docker setup + +1. Install [Docker](https://docs.docker.com/get-docker/) +2. Set up a settings.ini file using [the one in the repo](https://github.com/Aeriqu/CroBot/blob/master/settings.ini) as a template. +3. Run ``docker run -d -v /path/to/anywhere/sdvxCharts.db:/app/sdvxCharts.db -v /path/to/your/settings.ini:/app/settings.ini aeriqu/crobot`` + +### Traditional setup #### General setup @@ -34,8 +42,9 @@ source](http://www.phontron.com/kytea/#download). sdvxin: 1. Obtain a kytea model, name it ``model.bin``, and place it in the same directory as ``run.py`` -2. Run ``db_init.py`` to initialize the database and download the metadata from sdvx.in -3. Edit the configuration file for an api key for azure's cognitive translation features +2. Run ``db_init.py`` to initialize the database +3. Execute ``!sdvxin update`` in a channel with the bot to download the metadata from sdvx.in (this will take a while) +4. Edit the configuration file for an api key for azure's cognitive translation features ## License diff --git a/db_init.py b/db_init.py old mode 100644 new mode 100755 index 77ca66a..e426130 --- a/db_init.py +++ b/db_init.py @@ -1,11 +1,9 @@ #!/usr/bin/env python3 import asyncio -import CroBot.features.sdvxin.sdvx import CroBot.features.sdvxin.database async def upd(): await CroBot.features.sdvxin.database.recreate_db() - await CroBot.features.sdvxin.sdvx.update() def main(): loop = asyncio.new_event_loop() diff --git a/run.py b/run.py old mode 100644 new mode 100755 index f191fe8..e7a8ba2 --- a/run.py +++ b/run.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 import configparser import discord from discord.utils import get