aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Joseph <[email protected]>2018-06-13 21:12:52 +0100
committerGravatar Joseph <[email protected]>2018-06-13 21:12:52 +0100
commitcfb02eaeaea37da231d490f701e3aa017fc6494b (patch)
tree491ab31f565eb687ec07ad7bb4b3e4f702a06b54
parentRevert "Cast to list in events.py so there is no 'filter object does not have... (diff)
parentMerge branch 'ci-container' into 'master' (diff)
Merge branch 'master' of gitlab.com:python-discord/projects/bot
-rw-r--r--.dockerignore1
-rw-r--r--.gitignore4
-rw-r--r--.gitlab-ci.yml29
-rw-r--r--.travis.yml30
-rw-r--r--CONTRIBUTING.md (renamed from .github/CONTRIBUTING.md)12
-rw-r--r--Pipfile3
-rw-r--r--bot/__init__.py54
-rw-r--r--bot/__main__.py14
-rw-r--r--bot/cogs/bot.py4
-rw-r--r--bot/cogs/cogs.py8
-rw-r--r--bot/cogs/events.py9
-rw-r--r--bot/cogs/logging.py2
-rw-r--r--bot/cogs/snakes.py4
-rw-r--r--bot/constants.py9
-rw-r--r--bot/utils/service_discovery.py22
-rw-r--r--config-default.yml7
-rw-r--r--docker/ci.Dockerfile20
-rw-r--r--scripts/deploy.sh2
18 files changed, 155 insertions, 79 deletions
diff --git a/.dockerignore b/.dockerignore
index ae09cac97..e332b562d 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -5,6 +5,7 @@ __pycache__
.vagrant
.pytest_cache
.github
+.gitlab
.cache
Vagrantfile
.coverage
diff --git a/.gitignore b/.gitignore
index 3202baa92..4321d9324 100644
--- a/.gitignore
+++ b/.gitignore
@@ -106,8 +106,8 @@ ENV/
# Vagrant
.vagrant
-# JSON logfile
-log.json
+# Logfiles
+log.*
# Custom user configuration
config.yml
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 000000000..3d13ef5f1
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,29 @@
+image: pythondiscord/bot-ci:latest
+
+stages:
+ - test
+ - build
+
+test:
+ tags:
+ - docker
+
+ stage: test
+ cache:
+ paths:
+ - ".venv"
+
+ script:
+ - pipenv sync --dev
+ - pipenv run lint
+
+build:
+ tags:
+ - docker
+
+ services:
+ - docker:dind
+
+ stage: build
+ script:
+ - sh scripts/deploy.sh
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index 4d70f870e..000000000
--- a/.travis.yml
+++ /dev/null
@@ -1,30 +0,0 @@
-language: python
-python:
- - "3.6"
-
-branches:
- only:
- - "master"
-
-sudo: required
-
-services:
- - docker
-
-env:
- global:
- - PIPENV_VENV_IN_PROJECT=1
- - PIPENV_IGNORE_VIRTUALENVS=1
-
-install:
- - pip install pipenv
- - pipenv sync --dev --three
-script:
- - pipenv run python -m flake8
-after_success:
- - bash scripts/deploy.sh
-
-cache: pip
-
-notifications:
- email: false
diff --git a/.github/CONTRIBUTING.md b/CONTRIBUTING.md
index 8b803acad..f8deaee04 100644
--- a/.github/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -7,18 +7,17 @@ contributions may be rejected on the basis of a contributor failing to follow th
## Rules
1. **No force-pushes** or modifying the Git history in any way.
-1. If you have direct access to the repository, **create a branch for your changes** and create a pull request for that branch.
+1. If you have direct access to the repository, **create a branch for your changes** and create a merge request for that branch.
If not, fork it and work on a separate branch there.
* Some repositories require this and will reject any direct pushes to `master`. Make this a habit!
-1. If someone is working on a pull request, **do not open your own pull request for the same task**. Instead, leave some comments
- on the existing pull request. Communication is key, and there's no point in two separate implementations of the same thing.
+1. If someone is working on a merge request, **do not open your own merge request for the same task**. Instead, leave some comments
+ on the existing merge request. Communication is key, and there's no point in two separate implementations of the same thing.
* One option is to fork the other contributor's repository, and submit your changes to their branch with your
- own pull request. If you do this, we suggest following these guidelines when interacting with their repository
+ own merge request. If you do this, we suggest following these guidelines when interacting with their repository
as well.
1. **Adhere to the prevailing code style**, which we enforce using [flake8](http://flake8.pycqa.org/en/latest/index.html).
* Additionally, run `flake8` against your code before you push it. Your commit will be rejected by the build server
- if it fails to lint. For an automatic way to do this, check out
- [our article on Git hooks](https://github.com/discord-python/site/wiki/Git-Hooks).
+ if it fails to lint.
1. **Don't fight the framework**. Every framework has its flaws, but the frameworks we've picked out have been carefully
chosen for their particular merits. If you can avoid it, please resist reimplementing swathes of framework logic - the
work has already been done for you!
@@ -40,3 +39,4 @@ requests or changes by contributors, where you believe you have something valuab
## Footnotes
This document was inspired by the [Glowstone contribution guidelines](https://github.com/GlowstoneMC/Glowstone/blob/dev/docs/CONTRIBUTING.md).
+
diff --git a/Pipfile b/Pipfile
index e6d1366c6..9b0e530ec 100644
--- a/Pipfile
+++ b/Pipfile
@@ -39,7 +39,10 @@ python_version = "3.6"
[scripts]
start = "python -m bot"
+lint = "python -m flake8"
build = "docker build -t pythondiscord/bot:latest -f docker/Dockerfile ."
push = "docker push pythondiscord/bot:latest"
buildbase = "docker build -t pythondiscord/bot-base:latest -f docker/Dockerfile.base ."
pushbase = "docker push pythondiscord/bot-base:latest"
+buildci = "docker build -t pythondiscord/bot-ci:latest -f docker/ci.Dockerfile ."
+pushci = "docker push pythondiscord/bot-ci:latest"
diff --git a/bot/__init__.py b/bot/__init__.py
index a8272981c..9529902ab 100644
--- a/bot/__init__.py
+++ b/bot/__init__.py
@@ -1,9 +1,9 @@
import ast
import logging
+import os
import re
import sys
from logging import Logger, StreamHandler
-from logging.handlers import SysLogHandler
import discord.ext.commands.view
from logmatic import JsonFormatter
@@ -30,30 +30,56 @@ Logger.trace = monkeypatch_trace
# Set up logging
logging_handlers = []
+# We can't import this yet, so we have to define it ourselves
+DEBUG_MODE = True if 'local' in os.environ.get("SITE_URL", "local") else False
-logging_handlers.append(StreamHandler(stream=sys.stderr))
-json_handler = logging.FileHandler(filename="log.json", mode="w")
-json_handler.formatter = JsonFormatter()
-logging_handlers.append(json_handler)
+if DEBUG_MODE:
+ logging_handlers.append(StreamHandler(stream=sys.stdout))
+
+ json_handler = logging.FileHandler(filename="log.json", mode="w")
+ json_handler.formatter = JsonFormatter()
+ logging_handlers.append(json_handler)
+else:
+ logging_handlers.append(logging.FileHandler(filename="log.txt", mode="w"))
+
+ json_handler = logging.StreamHandler(stream=sys.stdout)
+ json_handler.formatter = JsonFormatter()
+ logging_handlers.append(json_handler)
+
logging.basicConfig(
format="%(asctime)s pd.beardfist.com Bot: | %(name)30s | %(levelname)8s | %(message)s",
datefmt="%b %d %H:%M:%S",
- level=logging.TRACE,
+ level=logging.TRACE if DEBUG_MODE else logging.INFO,
handlers=logging_handlers
)
log = logging.getLogger(__name__)
-# We need to defer the import from `constants.py`
-# because otherwise the logging config would not be applied
-# to any logging done in the module.
-from bot.constants import Papertrail # noqa
-if Papertrail.address:
- papertrail_handler = SysLogHandler(address=(Papertrail.address, Papertrail.port))
- papertrail_handler.setLevel(logging.DEBUG)
- logging.getLogger('bot').addHandler(papertrail_handler)
+
+for key, value in logging.Logger.manager.loggerDict.items():
+ # Force all existing loggers to the correct level and handlers
+ # This happens long before we instantiate our loggers, so
+ # those should still have the expected level
+
+ if key == "bot":
+ continue
+
+ if not isinstance(value, logging.Logger):
+ # There might be some logging.PlaceHolder objects in there
+ continue
+
+ if DEBUG_MODE:
+ value.setLevel(logging.DEBUG)
+ else:
+ value.setLevel(logging.INFO)
+
+ for handler in value.handlers.copy():
+ value.removeHandler(handler)
+
+ for handler in logging_handlers:
+ value.addHandler(handler)
# Silence discord and websockets
diff --git a/bot/__main__.py b/bot/__main__.py
index b1e9c61fa..d04be725c 100644
--- a/bot/__main__.py
+++ b/bot/__main__.py
@@ -7,6 +7,7 @@ from discord.ext.commands import Bot, when_mentioned_or
from bot.constants import Bot as BotConfig, ClickUp
from bot.formatter import Formatter
+from bot.utils.service_discovery import wait_for_rmq
log = logging.getLogger(__name__)
@@ -36,13 +37,19 @@ bot.http_session = ClientSession(
)
)
+log.info("Waiting for RabbitMQ...")
+has_rmq = wait_for_rmq()
+
+if has_rmq:
+ log.info("RabbitMQ found")
+else:
+ log.warning("Timed out while waiting for RabbitMQ")
+
# Internal/debug
bot.load_extension("bot.cogs.logging")
-bot.load_extension("bot.cogs.rmq")
bot.load_extension("bot.cogs.security")
bot.load_extension("bot.cogs.events")
-
# Commands, etc
bot.load_extension("bot.cogs.bot")
bot.load_extension("bot.cogs.cogs")
@@ -64,6 +71,9 @@ bot.load_extension("bot.cogs.tags")
bot.load_extension("bot.cogs.verification")
bot.load_extension("bot.cogs.utils")
+if has_rmq:
+ bot.load_extension("bot.cogs.rmq")
+
bot.run(BotConfig.token)
bot.http_session.close() # Close the aiohttp session when the bot finishes running
diff --git a/bot/cogs/bot.py b/bot/cogs/bot.py
index 6d574d0b1..af9abd7cb 100644
--- a/bot/cogs/bot.py
+++ b/bot/cogs/bot.py
@@ -56,7 +56,7 @@ class Bot:
embed = Embed(
description="A utility bot designed just for the Python server! Try `bot.help()` for more info.",
- url="https://github.com/discord-python/bot"
+ url="https://gitlab.com/discord-python/projects/bot"
)
repo = Repo(".")
@@ -67,7 +67,7 @@ class Bot:
embed.set_author(
name="Python Bot",
- url="https://github.com/discord-python/bot",
+ url="https://gitlab.com/discord-python/projects/bot",
icon_url=URLs.bot_avatar
)
diff --git a/bot/cogs/cogs.py b/bot/cogs/cogs.py
index 51693f9ef..7eaf5005c 100644
--- a/bot/cogs/cogs.py
+++ b/bot/cogs/cogs.py
@@ -51,7 +51,7 @@ class Cogs:
embed.set_author(
name="Python Bot (Cogs)",
- url=URLs.github_bot_repo,
+ url=URLs.gitlab_bot_repo,
icon_url=URLs.bot_avatar
)
@@ -108,7 +108,7 @@ class Cogs:
embed.set_author(
name="Python Bot (Cogs)",
- url=URLs.github_bot_repo,
+ url=URLs.gitlab_bot_repo,
icon_url=URLs.bot_avatar
)
@@ -163,7 +163,7 @@ class Cogs:
embed.set_author(
name="Python Bot (Cogs)",
- url=URLs.github_bot_repo,
+ url=URLs.gitlab_bot_repo,
icon_url=URLs.bot_avatar
)
@@ -264,7 +264,7 @@ class Cogs:
embed.colour = Colour.blurple()
embed.set_author(
name="Python Bot (Cogs)",
- url=URLs.github_bot_repo,
+ url=URLs.gitlab_bot_repo,
icon_url=URLs.bot_avatar
)
diff --git a/bot/cogs/events.py b/bot/cogs/events.py
index 5345a63b0..53e58bd21 100644
--- a/bot/cogs/events.py
+++ b/bot/cogs/events.py
@@ -103,6 +103,7 @@ class Events:
roles = [str(r.id) for r in member.roles] # type: List[int]
users.append({
+ "avatar": member.avatar_url_as(),
"user_id": str(member.id),
"roles": roles,
"username": member.name,
@@ -142,7 +143,11 @@ class Events:
)
async def on_member_update(self, before: Member, after: Member):
- if before.roles == after.roles and before.name == after.name and before.discriminator == after.discriminator:
+ if (
+ before.roles == after.roles and
+ before.name == after.name and
+ before.discriminator == after.discriminator and
+ before.avatar == after.avatar):
return
before_role_names = [role.name for role in before.roles] # type: List[str]
@@ -152,6 +157,7 @@ class Events:
log.debug(f"{before.display_name} roles changing from {before_role_names} to {after_role_names}")
changes = await self.send_updated_users({
+ "avatar": after.avatar_url_as(),
"user_id": str(after.id),
"roles": role_ids,
"username": after.name,
@@ -164,6 +170,7 @@ class Events:
role_ids = [str(r.id) for r in member.roles] # type: List[str]
changes = await self.send_updated_users({
+ "avatar": member.avatar_url_as(),
"user_id": str(member.id),
"roles": role_ids,
"username": member.name,
diff --git a/bot/cogs/logging.py b/bot/cogs/logging.py
index 05fed7042..67f09dc8a 100644
--- a/bot/cogs/logging.py
+++ b/bot/cogs/logging.py
@@ -23,7 +23,7 @@ class Logging:
embed = Embed(description="Connected!")
embed.set_author(
name="Python Bot",
- url="https://github.com/discord-python/bot",
+ url="https://gitlab.com/discord-python/projects/bot",
icon_url="https://raw.githubusercontent.com/discord-python/branding/master/logos/logo_circle.png"
)
diff --git a/bot/cogs/snakes.py b/bot/cogs/snakes.py
index 78be08fab..ec32a119d 100644
--- a/bot/cogs/snakes.py
+++ b/bot/cogs/snakes.py
@@ -140,7 +140,7 @@ class Snakes:
More information can be found in the code-jam-1 repo.
- https://github.com/discord-python/code-jam-1
+ https://gitlab_bot_repo.com/discord-python/code-jams/code-jam-1
"""
wiki_brief = re.compile(r'(.*?)(=+ (.*?) =+)', flags=re.DOTALL)
@@ -981,7 +981,7 @@ class Snakes:
title="About the snake cog",
description=(
"The features in this cog were created by members of the community "
- "during our first ever [code jam event](https://github.com/discord-python/code-jam-1). \n\n"
+ "during our first ever [code jam event](https://gitlab.com/discord-python/code-jams/code-jam-1). \n\n"
"The event saw over 50 participants, who competed to write a discord bot cog with a snake theme over "
"48 hours. The staff then selected the best features from all the best teams, and made modifications "
"to ensure they would all work together before integrating them into the community bot.\n\n"
diff --git a/bot/constants.py b/bot/constants.py
index 50a316d26..eec150d1a 100644
--- a/bot/constants.py
+++ b/bot/constants.py
@@ -233,13 +233,6 @@ class ClickUp(metaclass=YAMLGetter):
key: str
-class Papertrail(metaclass=YAMLGetter):
- section = "papertrail"
-
- address: str
- port: str
-
-
class RabbitMQ(metaclass=YAMLGetter):
section = "rabbitmq"
@@ -263,7 +256,7 @@ class URLs(metaclass=YAMLGetter):
site_names_api: str
site_idioms_api: str
site_special_api: str
- github_bot_repo: str
+ gitlab_bot_repo: str
bot_avatar: str
omdb: str
diff --git a/bot/utils/service_discovery.py b/bot/utils/service_discovery.py
new file mode 100644
index 000000000..8d79096bd
--- /dev/null
+++ b/bot/utils/service_discovery.py
@@ -0,0 +1,22 @@
+import datetime
+import socket
+import time
+from contextlib import closing
+
+from bot.constants import RabbitMQ
+
+THIRTY_SECONDS = datetime.timedelta(seconds=30)
+
+
+def wait_for_rmq():
+ start = datetime.datetime.now()
+
+ while True:
+ if datetime.datetime.now() - start > THIRTY_SECONDS:
+ return False
+
+ with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
+ if sock.connect_ex((RabbitMQ.host, RabbitMQ.port)) == 0:
+ return True
+
+ time.sleep(0.5)
diff --git a/config-default.yml b/config-default.yml
index e10150617..eb0f86d2f 100644
--- a/config-default.yml
+++ b/config-default.yml
@@ -55,11 +55,6 @@ clickup:
key: !ENV 'CLICKUP_KEY'
-papertrail:
- address: !ENV 'PAPERTRAIL_ADDRESS'
- port: !ENV 'PAPERTRAIL_PORT'
-
-
rabbitmq:
host: "pdrmq"
port: 5672
@@ -80,6 +75,6 @@ urls:
site_names_api: 'https://api.pythondiscord.com/bot/snake_names'
site_idioms_api: 'https://api.pythondiscord.com/bot/snake_idioms'
site_special_api: 'https://api.pythondiscord.com/bot/special_snakes'
- github_bot_repo: 'https://github.com/discord-python/bot'
+ gitlab_bot_repo: 'https://gitlab.com/discord-python/projects/bot'
bot_avatar: 'https://raw.githubusercontent.com/discord-python/branding/master/logos/logo_circle/logo_circle.png'
omdb: 'http://omdbapi.com'
diff --git a/docker/ci.Dockerfile b/docker/ci.Dockerfile
new file mode 100644
index 000000000..fd7e25239
--- /dev/null
+++ b/docker/ci.Dockerfile
@@ -0,0 +1,20 @@
+FROM python:3.6-alpine3.7
+
+RUN apk add --update docker \
+ curl \
+ tini \
+ build-base \
+ libffi-dev \
+ zlib \
+ jpeg-dev \
+ libxml2 libxml2-dev libxslt-dev \
+ zlib-dev \
+ freetype-dev
+
+RUN pip install pipenv
+
+ENV LIBRARY_PATH=/lib:/usr/lib
+ENV PIPENV_VENV_IN_PROJECT=1
+ENV PIPENV_IGNORE_VIRTUALENVS=1
+ENV PIPENV_NOSPIN=1
+ENV PIPENV_HIDE_EMOJIS=1
diff --git a/scripts/deploy.sh b/scripts/deploy.sh
index d8a5a0176..50ec87f59 100644
--- a/scripts/deploy.sh
+++ b/scripts/deploy.sh
@@ -1,7 +1,7 @@
#!/bin/bash
# Build and deploy on master branch
-if [[ $TRAVIS_BRANCH == 'master' && $TRAVIS_PULL_REQUEST == 'false' ]]; then
+if [[ $CI_COMMIT_REF_SLUG == 'master' ]]; then
echo "Connecting to docker hub"
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin