From 824af34cbfe296382794f10b9eb7e2c39ced39c0 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 2 Nov 2019 16:56:03 -0400 Subject: [PATCH 1/6] move keys.py and options.py to the data directory --- .gitignore | 1 + Makefile | 45 +++++++++++++++++++++------------------------ main.py | 4 ++-- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index d96e447..d91594c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ cty.json cty.zip botenv/ +data/ ######################################################### diff --git a/Makefile b/Makefile index 82306f8..ea665f7 100644 --- a/Makefile +++ b/Makefile @@ -1,31 +1,24 @@ -# A quick installation script for discord bots. -# +# A quick installation script for painless discord bots. +# v1.1.0 # Copyright (c) 2019 0x5c # Released under the terms of the MIT license. -# -# https://github.com/0x5c/discord.py-quickinstall +# Part of: +# https://github.com/0x5c/quick-bot-no-pain -#### Setup #### .DEFAULT_GOAL := help -## Variables ## +### Variables ### # Those are the defaults; they can be over-ridden if specified # at en environment level or as 'make' arguments. BOTENV ?= botenv -PY3DOT ?= 7 -PIP_OUTPUT ?= q +PYTHON_BIN ?= python3.7 +PIP_OUTPUT ?= -q -# Define some rules as phony -.PHONY: help install clean onlyenv - - - -#### Targets #### - -## Support targets ## +### Support targets ### +.PHONY: help help: @echo "" @echo "\033[97m>>>>>> Default dummy target <<<<<<" @@ -35,16 +28,17 @@ help: @echo "\033[0m" -## Actual install/setup targets ## +### Actual install/setup targets ### # Main install target +.PHONY: install install: $(BOTENV)/req_done options.py keys.py # Virual environment setup $(BOTENV)/success: ifneq ("$(wildcard ./$(BOTENV).)",) @echo "\033[94m--> Creating the virtual environment...\033[0m" - @python3.$(PY3DOT) -m venv $(BOTENV) + @$(PYTHON_BIN) -m venv $(BOTENV) @touch $(BOTENV)/success endif @@ -52,17 +46,19 @@ endif $(BOTENV)/req_done: requirements.txt $(BOTENV)/success @echo "\033[34;1m--> Installing the dependencies...\033[0m" @. $(BOTENV)/bin/activate; \ - pip install -${PIP_OUTPUT} -U pip setuptools wheel; \ - pip install -${PIP_OUTPUT} -U -r requirements.txt + pip install ${PIP_OUTPUT} -U pip setuptools wheel; \ + pip install ${PIP_OUTPUT} -U -r requirements.txt @touch $(BOTENV)/req_done + @mkdir ./data # Copying templates options.py keys.py: @echo "\033[34;1m--> Copying template files...\033[0m" - @cp -nv ./templates/template_$@ ./$@ - @touch ./$@ + @cp -nv ./templates/template_$@ ./data/$@ + @touch ./data/$@ # Deletes the python cache and the virtual environment +.PHONY: clean clean: @echo "\033[34;1m--> Removing python cache files...\033[0m" rm -rf __pycache__ @@ -70,8 +66,9 @@ clean: rm -rf $(BOTENV) -## Dev targets ## +### Dev targets ### -## Weird dev targets ## +### Special targets ### +.PHONY: onlyenv onlyenv: $(BOTENV)/success diff --git a/main.py b/main.py index 911307e..636839c 100644 --- a/main.py +++ b/main.py @@ -15,8 +15,8 @@ from discord.ext import commands, tasks import info -import options as opt -import keys +from data import options as opt +from data import keys as keys # --- Settings --- From 34cf4101312e6cda98f20d8d7cfb81f8af561a1e Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 2 Nov 2019 16:56:53 -0400 Subject: [PATCH 2/6] add docker deployment files --- .dockerignore | 1 + Dockerfile | 37 ++++++++++++++++++++++++++++++++++ docker-compose.yml | 7 +++++++ docker-run.sh | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 docker-run.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..8fce603 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +data/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..20b5a97 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,37 @@ +FROM python:3-alpine + +COPY . /app +WORKDIR /app + +VOLUME /app/data + +RUN \ + echo "**** install build packages ****" && \ + apk add --no-cache --virtual=build-dependencies \ + g++ \ + git \ + gcc \ + libxml2-dev \ + libxslt-dev \ + openssl-dev \ + python3-dev && \ + echo "**** install runtime packages ****" && \ + apk add --no-cache \ + openssl \ + py3-lxml \ + py3-pip \ + python3 && \ + echo "**** install pip packages ****" && \ + pip3 install -U pip setuptools wheel && \ + pip3 install -r requirements.txt && \ + echo "**** clean up ****" && \ + apk del --purge \ + build-dependencies && \ + rm -rf \ + /root/.cache \ + /tmp/* \ + /var/cache/apk/* && \ + echo "**** prepare scripts ****" && \ + chmod +x docker-run.sh + +CMD ["sh", "docker-run.sh", "--pass-errors"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c2541fd --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,7 @@ +version: '3' +services: + bot: + image: "classabbyamp/discord-qrm-bot:latest" + container_name: "qrmbot" + volumes: + - "./data:/app/data:rw" diff --git a/docker-run.sh b/docker-run.sh new file mode 100644 index 0000000..dcddcf2 --- /dev/null +++ b/docker-run.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +# A wrapper script for painless discord bots. +# v1.0.0 +# Copyright (c) 2019 0x5c +# Released under the terms of the MIT license. +# Part of: +# https://github.com/0x5c/quick-bot-no-pain + + +# Argument handling # ? TODO: Argument passing ? +if [[ $1 == '--pass-errors' ]]; then + _PASS_ERRORS=1 +fi + + +# A function called when the bot exits to decide what to do +code_handling() { + case $err in + 0) + echo "$_message: exiting" + exit 0 # The bot whishes to stay alone. + ;; + 42) + echo "$_message: restarting" + return # The bot whishes to be restarted (returns to the loop). + ;; + *) + if [[ $_PASS_ERRORS -eq 0 ]]; then # The bot crashed and: + echo "$_message: restarting" + return # ...we should return to the loop to restart it. + else + echo "$_message: exiting (--pass-errors)" + exit $err # ...we should just exit and pass the code to our parent (probably a daemon/service manager). + fi + ;; + esac +} + + +echo "$0: Starting bot..." + +# The loop +while true; do + python3 main.py + err=$? + _message="$0: The bot exited with [$err]" + code_handling +done From 4743a2524fd137435dedc028b3d8386a6d55f5e3 Mon Sep 17 00:00:00 2001 From: Abigail Gold Date: Sun, 3 Nov 2019 10:43:43 -0500 Subject: [PATCH 3/6] i'm a dummy --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 636839c..56b89ab 100644 --- a/main.py +++ b/main.py @@ -16,7 +16,7 @@ from discord.ext import commands, tasks import info from data import options as opt -from data import keys as keys +from data import keys # --- Settings --- From a65d8bd580ed8f0f716c675eec9b86bc7e189f47 Mon Sep 17 00:00:00 2001 From: Abigail Gold <5366828+classabbyamp@users.noreply.github.com> Date: Mon, 4 Nov 2019 09:51:07 -0500 Subject: [PATCH 4/6] Update Makefile Co-Authored-By: 0x5c <0x5c.dev@gmail.com> --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ea665f7..22b5698 100644 --- a/Makefile +++ b/Makefile @@ -52,7 +52,7 @@ $(BOTENV)/req_done: requirements.txt $(BOTENV)/success @mkdir ./data # Copying templates -options.py keys.py: +options.py keys.py: | data @echo "\033[34;1m--> Copying template files...\033[0m" @cp -nv ./templates/template_$@ ./data/$@ @touch ./data/$@ From 7bf5071bb972791fbb8df2200fab068b93503a54 Mon Sep 17 00:00:00 2001 From: Abigail Gold <5366828+classabbyamp@users.noreply.github.com> Date: Mon, 4 Nov 2019 09:51:17 -0500 Subject: [PATCH 5/6] Update Makefile Co-Authored-By: 0x5c <0x5c.dev@gmail.com> --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 22b5698..200bc29 100644 --- a/Makefile +++ b/Makefile @@ -49,7 +49,9 @@ $(BOTENV)/req_done: requirements.txt $(BOTENV)/success pip install ${PIP_OUTPUT} -U pip setuptools wheel; \ pip install ${PIP_OUTPUT} -U -r requirements.txt @touch $(BOTENV)/req_done - @mkdir ./data + +data: + mkdir data # Copying templates options.py keys.py: | data From 0418ae16a66546ca7b804504fb54197806b8766b Mon Sep 17 00:00:00 2001 From: Abigail Gold Date: Mon, 4 Nov 2019 12:17:59 -0500 Subject: [PATCH 6/6] updates based on PR review, and new code in quickbot --- Dockerfile | 12 +++++------- README-DOCKER.md | 11 +++++++++++ README.md | 20 +++++++++++++++++++ docker-compose.yml | 2 +- docker-run.sh | 49 ---------------------------------------------- run.sh | 41 ++++++++++++++++++++++++++++++-------- 6 files changed, 70 insertions(+), 65 deletions(-) create mode 100644 README-DOCKER.md delete mode 100644 docker-run.sh diff --git a/Dockerfile b/Dockerfile index 20b5a97..fc7dd48 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ FROM python:3-alpine COPY . /app WORKDIR /app -VOLUME /app/data +ENV PYTHON_BIN python3 RUN \ echo "**** install build packages ****" && \ @@ -13,11 +13,11 @@ RUN \ gcc \ libxml2-dev \ libxslt-dev \ - openssl-dev \ + libressl-dev \ python3-dev && \ echo "**** install runtime packages ****" && \ apk add --no-cache \ - openssl \ + libressl \ py3-lxml \ py3-pip \ python3 && \ @@ -30,8 +30,6 @@ RUN \ rm -rf \ /root/.cache \ /tmp/* \ - /var/cache/apk/* && \ - echo "**** prepare scripts ****" && \ - chmod +x docker-run.sh + /var/cache/apk/* -CMD ["sh", "docker-run.sh", "--pass-errors"] +CMD ["/bin/sh", "run.sh", "--pass-errors", "--no-botenv"] diff --git a/README-DOCKER.md b/README-DOCKER.md new file mode 100644 index 0000000..45b19d7 --- /dev/null +++ b/README-DOCKER.md @@ -0,0 +1,11 @@ +A sample `docker-compose.yml` file: + +```yaml +version: '3' +services: + bot: + image: "classabbyamp/discord-qrm-bot:latest" + container_name: "qrmbot" + volumes: + - "./data:/app/data:rw" +``` diff --git a/README.md b/README.md index fb42e69..b3bfa1d 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,26 @@ A discord bot with ham radio functionalities. An independent rewrite of qrmbot-discord. +## Running + +### Docker + +See [README-DOCKER.md](./README-DOCKER.md) + +### Without Docker + +Prep the environment. For more information on extra options, see the [quick-bot-no-pain Makefile documentation](https://github.com/0x5c/quick-bot-no-pain/blob/master/docs/makefile.md). + +``` +$ make install +``` + +Run. For more information on options, see the [quick-bot-no-pain run.sh documentation](https://github.com/0x5c/quick-bot-no-pain/blob/master/docs/run.sh.md). + +``` +$ run.sh +``` + ## Copyright Copyright (C) 2019 Abigail Gold, 0x5c diff --git a/docker-compose.yml b/docker-compose.yml index c2541fd..183d669 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ version: '3' services: bot: - image: "classabbyamp/discord-qrm-bot:latest" + build: . container_name: "qrmbot" volumes: - "./data:/app/data:rw" diff --git a/docker-run.sh b/docker-run.sh deleted file mode 100644 index dcddcf2..0000000 --- a/docker-run.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/bash - -# A wrapper script for painless discord bots. -# v1.0.0 -# Copyright (c) 2019 0x5c -# Released under the terms of the MIT license. -# Part of: -# https://github.com/0x5c/quick-bot-no-pain - - -# Argument handling # ? TODO: Argument passing ? -if [[ $1 == '--pass-errors' ]]; then - _PASS_ERRORS=1 -fi - - -# A function called when the bot exits to decide what to do -code_handling() { - case $err in - 0) - echo "$_message: exiting" - exit 0 # The bot whishes to stay alone. - ;; - 42) - echo "$_message: restarting" - return # The bot whishes to be restarted (returns to the loop). - ;; - *) - if [[ $_PASS_ERRORS -eq 0 ]]; then # The bot crashed and: - echo "$_message: restarting" - return # ...we should return to the loop to restart it. - else - echo "$_message: exiting (--pass-errors)" - exit $err # ...we should just exit and pass the code to our parent (probably a daemon/service manager). - fi - ;; - esac -} - - -echo "$0: Starting bot..." - -# The loop -while true; do - python3 main.py - err=$? - _message="$0: The bot exited with [$err]" - code_handling -done diff --git a/run.sh b/run.sh index a48c23e..cf1d67b 100644 --- a/run.sh +++ b/run.sh @@ -1,7 +1,7 @@ -#!/bin/bash +#!/bin/sh # A wrapper script for painless discord bots. -# v1.0.0 +# v1.2.0 # Copyright (c) 2019 0x5c # Released under the terms of the MIT license. # Part of: @@ -9,13 +9,34 @@ # If $BOTENV is not defined, default to 'botenv' -if [[ -z ${BOTENV+x} ]]; then +if [ -z "$BOTENV" ]; then BOTENV='botenv' fi -# Argument handling # ? TODO: Argument passing ? -if [[ $1 == '--pass-errors' ]]; then - _PASS_ERRORS=1 + +# Argument handling +_PASS_ERRORS=0 +_NO_BOTENV=0 +while [ ! -z "$1" ]; do + case $1 in + --pass-errors) + _PASS_ERRORS=1 + ;; + --no-botenv) + _NO_BOTENV=1 + ;; + --) + shift + break + ;; + esac + shift +done + + +# If $PYTHON_BIN is not defined, default to 'python3.7' +if [ $_NO_BOTENV -eq 1 -a -z "$PYTHON_BIN" ]; then + PYTHON_BIN='python3.7' fi @@ -31,7 +52,7 @@ code_handling() { return # The bot whishes to be restarted (returns to the loop). ;; *) - if [[ $_PASS_ERRORS -eq 0 ]]; then # The bot crashed and: + if [ $_PASS_ERRORS -eq 0 ]; then # The bot crashed and: echo "$_message: restarting" return # ...we should return to the loop to restart it. else @@ -47,7 +68,11 @@ echo "$0: Starting bot..." # The loop while true; do - ./$BOTENV/bin/python3 main.py + if [ $_NO_BOTENV -eq 1 ]; then + "$PYTHON_BIN" main.py $@ + else + ./$BOTENV/bin/python3 main.py $@ + fi err=$? _message="$0: The bot exited with [$err]" code_handling