diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..8fce603 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +data/ diff --git a/.gitignore b/.gitignore index 02c3087..8025234 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,7 @@ export/ *.pyc #backup -backup/ \ No newline at end of file +backup/ + +# Docker development - data for mongodb +data/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0744b51 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3.7 + +WORKDIR /usr/src/ + +RUN pip install --upgrade pip + +COPY docker-requirements.txt ./requirements.txt +RUN pip install --no-cache-dir -r requirements.txt + +VOLUME [ "/usr/src/app" ] +WORKDIR /usr/src/app/ + +#ENTRYPOINT [ "python", "/usr/src/app/pollmaster.py" ] +ENTRYPOINT [ "python", "/usr/src/app/launcher.py" ] + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..1cccd34 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +version: "3.5" +services: + bot: + build: . + links: + - "mongodb:db" + volumes: + - type: bind + source: . + target: /usr/src/app + mongodb: + image: mongo:5.0.5 + environment: + - MONGO_INITDB_ROOT_USERNAME=pmroot + - MONGO_INITDB_ROOT_PASSWORD=changeme + volumes: + - type: bind + source: ./data + target: /data/db + - type: bind + source: ./mongo-init.js + target: /docker-entrypoint-initdb.d/mongo-init.js + read_only: true + ports: + - "27107:27107" + diff --git a/docker-requirements.txt b/docker-requirements.txt new file mode 100644 index 0000000..3f9b744 --- /dev/null +++ b/docker-requirements.txt @@ -0,0 +1,38 @@ +# This file adjusts some of the dependencies to a +# version that seems to function in Docker. If +# the main requirements.txt file is updated, those +# changes could also be made in here. If this file +# becomes an exact match to requirements.txt, update +# Dockerfile to reference requirements.txt, then +# delete this file. +aiohttp +async-timeout==3.0.1 +asyncpg==0.21.0 +attrs==20.2.0 +certifi==2020.6.20 +chardet==3.0.4 +cycler==0.10.0 +dateparser==0.7.4 +dblpy==0.3.4 +discord.py==1.5.1 +idna==2.10 +idna-ssl==1.1.0 +kiwisolver==1.3.0 +matplotlib==3.2.1 +motor==2.3.0 +multidict +numpy==1.19.2 +pymongo==3.11.0 +pyparsing==2.4.7 +python-dateutil==2.8.1 +pytz==2020.1 +ratelimiter==1.2.0.post0 +regex==2020.10.23 +requests==2.24.0 +six==1.15.0 +typing-extensions==3.7.4.3 +tzlocal==2.1 +Unidecode==1.1.1 +urllib3==1.25.11 +websockets==8.1 +yarl==1.4.2 diff --git a/docker.md b/docker.md new file mode 100644 index 0000000..54709cb --- /dev/null +++ b/docker.md @@ -0,0 +1,79 @@ +# Setting up Pollmaster for Docker Development + +## Purpose + +These instructions are intended to help configure a development environment. They have not been designed or written with considerations for security, and are therefore not appropriate to run in a production environment. + +## Requirements + +These steps have been tested on Ubuntu 20.04 + +[Docker Engine: Community](https://docs.docker.com/install/linux/docker-ce/ubuntu/) + +## Installation + +Execute the following commands from a terminal window: +```sh +git clone https://github.com/matnad/pollmaster.git +cd pollmaster +mkdir data +``` +## Setup app and bot in Discord + +- Setup an app and a bot using [Creating a Bot Account](https://discordpy.readthedocs.io/en/latest/discord.html#creating-a-bot-account) + +## Running the application + +- Create a secrets.py in essentials folder in the project. You can use the following template + +```python +class Secrets: + def __init__(self): + self.dbl_token = '' # DBL token (only needed for public bot) + self.mongo_db = 'mongodb://username:password@db:27017/pollmaster' # Replace with credentials in mongo-init.js + self.bot_token = '' # official discord bot token + self.mode = 'development' # or production + +SECRETS = Secrets() +``` + +- Update essentials/settings.py ; set the owner\_id to the developer's User ID in Discord +- Run the following commands from a terminal window, within the pollmaster working directory: +```sh +docker-compose build +docker-compose up -d +``` +- When making changes to the Python code, run these commands to restart the bot: +```sh +docker stop pollmaster_bot_1 +docker start pollmaster_bot_1 +``` + +## Invite the bot in Discord + +- Generate url to invite the bot using [Inviting Your Bot](https://discordpy.readthedocs.io/en/latest/discord.html#inviting-your-bot) +- Specify permissions by using the following bit format of the bot permissions appended to the bot invitation url and paste the url in browser and follow the instructions as given in the above url + +> &permissions=1073867840 + +- Now you will see the bot in your Discord channel +- Try commands like pm!help and pm!new + +## Log files + +- You can view the log file pollmaster.log in the pollmaster directory +- These commands will also output anything from the two docker containers: +```sh +docker logs pollmaster_bot_1 +docker logs pollmaster_mongodb_1 +``` + +## Cleanup + +When finished development work, run this command from the pollmaster working copy to remove the docker containers: +```sh +docker-compose down +``` + +If you would like to clear the MongoDB database files, remove the data/ directory as well. + diff --git a/mongo-init.js b/mongo-init.js new file mode 100644 index 0000000..621f0e9 --- /dev/null +++ b/mongo-init.js @@ -0,0 +1,15 @@ +db = db.getSiblingDB('pollmaster'); + +db.createUser( + { + user: "pm", + pwd: "pwdhere", + roles: [ + { + role: "dbOwner", + db: "pollmaster" + } + ] + } +); + diff --git a/setup.md b/setup.md index 50c8217..31919b2 100644 --- a/setup.md +++ b/setup.md @@ -37,6 +37,7 @@ class Secrets: SECRETS = Secrets() ``` +- Update essentials/settings.py ; set the owner\_id to the developer's User ID in Discord - Run the application using python pollmaster.py - You can see the following : ``` @@ -55,4 +56,4 @@ Servers verified. Bot running. ## Log files -- You can view the log file pollmaster.log in the pollmaster directory \ No newline at end of file +- You can view the log file pollmaster.log in the pollmaster directory