Skip to content

Commit

Permalink
docker container + data move prep
Browse files Browse the repository at this point in the history
  • Loading branch information
AEnterprise committed Mar 1, 2021
1 parent ec4758e commit 0fc830a
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 102 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!requirements.txt
!GearBot/*
!lang/*
!template.json
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM python:3.9
WORKDIR /GearBot
COPY requirements.txt ./
RUN pip3 install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "./GearBot/GearBot.py"]
2 changes: 1 addition & 1 deletion GearBot/Cogs/AntiSpam.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ async def ban_punishment(self, v: Violation):


async def censor_detector(self):
# reciever taks for someone gets censored
# reciever task for someone gets censored
while self.running:
try:
message = None
Expand Down
3 changes: 2 additions & 1 deletion GearBot/Cogs/PromMonitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ async def create_site(self):

runner = web.AppRunner(metrics_app)
await self.bot.loop.create_task(runner.setup())
site = web.TCPSite(runner, 'localhost', 8090 + self.bot.cluster)
site = web.TCPSite(runner, host='0.0.0.0', port=8090)

await site.start()

self.metric_server = site
Expand Down
48 changes: 46 additions & 2 deletions GearBot/GearBot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# force it to use v6 instead of v7
import asyncio

import discord.http
discord.http.Route.BASE = 'https://discordapp.com/api/v6'

discord.http.Route.BASE = 'http://http-proxy/api/v6'

import os
from argparse import ArgumentParser
Expand All @@ -9,10 +12,30 @@
from Bot.GearBot import GearBot
from Util import Configuration, GearbotLogging
from discord import Intents, MemberCacheFlags
from kubernetes import client, config

def prefix_callable(bot, message):
return TheRealGearBot.prefix_callable(bot, message)

async def node_init(generation, resource_version):
from database import DatabaseConnector
from database.DatabaseConnector import Node
await DatabaseConnector.init()
hostname = os.uname()[1]
GearbotLogging.info(f"GearBot clusternode {hostname} (generation {generation}). Trying to figure out where i fit in")
existing = await Node.filter(hostname=hostname, generation=generation).get_or_none()
if existing is None:
count = 0
while count < 100:

try:
await Node.create(hostname=hostname, generation=generation, resource_version=resource_version, shard=count)
return count
except Exception as ex:
GearbotLogging.exception("did something go wrong?", ex)
count += 1
else:
return existing.shard

if __name__ == '__main__':
parser = ArgumentParser()
Expand All @@ -34,6 +57,9 @@ def prefix_callable(bot, message):
else:
token = input("Please enter your Discord token: ")

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

args = {
"command_prefix": prefix_callable,
"case_insensitive": True,
Expand Down Expand Up @@ -69,12 +95,30 @@ def prefix_callable(bot, message):
"cluster": offset,
"shard_ids": [*range(offset * num_shards, (offset * num_shards) + num_shards)]
})
elif os.environ['namespace']:
GearbotLogging.info("Determining scaling information from kubernetes ...")
namespace = os.environ['namespace']
config.load_incluster_config()
kubeclient = client.AppsV1Api()
deployment = kubeclient.read_namespaced_deployment("gearbot", "gearbot")
print(deployment)
cluster = loop.run_until_complete(node_init(deployment.status.observed_generation, deployment.metadata.annotations["deployment.kubernetes.io/revision"]))
num_clusters = deployment.spec.replicas
args.update({
"shard_count": num_clusters,
"cluster": cluster,
"shard_ids": [cluster]
})




gearbot = GearBot(**args)

gearbot.remove_command("help")
GearbotLogging.info("Ready to go, spinning up the gears")
GearbotLogging.info(f"Ready to go, spinning up as instance {args['cluster'] + 1}/{args['shard_count']}")
gearbot.run(token)
GearbotLogging.info("GearBot shutting down, cleaning up")
gearbot.database_connection.close()
GearbotLogging.info("Cleanup complete")

4 changes: 2 additions & 2 deletions GearBot/Util/Configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def move_keys(config, section, *keys):
async def initialize(bot: commands.Bot):
global CONFIG_VERSION, BOT, TEMPLATE
BOT = bot
TEMPLATE = Utils.fetch_from_disk("config/template")
TEMPLATE = Utils.fetch_from_disk("template")
CONFIG_VERSION = TEMPLATE["VERSION"]
GearbotLogging.info(f"Current template config version: {CONFIG_VERSION}")
# GearbotLogging.info(f"Loading configurations for {len(bot.guilds)} guilds.")
Expand All @@ -415,7 +415,7 @@ def load_config(guild):
SERVER_CONFIGS[guild] = update_config(guild, config)
if len(config.keys()) is 0:
GearbotLogging.info(f"No config available for {guild}, creating a blank one.")
SERVER_CONFIGS[guild] = Utils.fetch_from_disk("config/template")
SERVER_CONFIGS[guild] = Utils.fetch_from_disk("template")
save(guild)
validate_config(guild)
Features.check_server(guild)
Expand Down
11 changes: 9 additions & 2 deletions GearBot/database/DatabaseConnector.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from tortoise.models import Model
from tortoise import fields, Tortoise

from Util import Configuration
from Util import Configuration, GearbotLogging


class LoggedMessage(Model):
Expand Down Expand Up @@ -76,10 +76,17 @@ class RaidAction(Model):
action = fields.CharField(max_length=20)
infraction = fields.ForeignKeyField("models.Infraction", related_name="RaiderAction", source_field="infraction_id", null=True)

class Node(Model):
hostname = fields.CharField(max_length=50, pk=True)
generation = fields.IntField()
shard = fields.IntField()
resource_version = fields.CharField(max_length=50)


async def init():
GearbotLogging.info("Connecting to the database...")
await Tortoise.init(
db_url=f"mysql://{Configuration.get_master_var('DATABASE_USER')}:{Configuration.get_master_var('DATABASE_PASS')}@{Configuration.get_master_var('DATABASE_HOST')}:{Configuration.get_master_var('DATABASE_PORT')}/{Configuration.get_master_var('DATABASE_NAME')}",
db_url=Configuration.get_master_var('DATABASE'),
modules={"models": ["database.DatabaseConnector"]}
)
await Tortoise.generate_schemas()
2 changes: 1 addition & 1 deletion config/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*.json
!template.json
!../template.json
!*.example
91 changes: 0 additions & 91 deletions config/template.json

This file was deleted.

Loading

0 comments on commit 0fc830a

Please sign in to comment.