From 036ddc7aa667ed72c2996ff1668d6423560a4168 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Wed, 16 Aug 2023 17:43:30 +1000 Subject: [PATCH] bot: Initial add --- .gitea/workflows/build.yml | 27 +++++++++++++++++++++++++- README.md | 18 ++++++++++++++++++ discord-bot/Dockerfile | 6 ++++++ discord-bot/bot.py | 37 ++++++++++++++++++++++++++++++++++++ discord-bot/requirements.txt | 3 +++ 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 discord-bot/Dockerfile create mode 100644 discord-bot/bot.py create mode 100644 discord-bot/requirements.txt diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index bf59720..7620559 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -26,4 +26,29 @@ jobs: docker tag hnshosting-master:$tag_num git.woodburn.au/nathanwoodburn/hnshosting-master:$tag_num docker push git.woodburn.au/nathanwoodburn/hnshosting-master:$tag_num docker tag hnshosting-master:$tag_num git.woodburn.au/nathanwoodburn/hnshosting-master:latest - docker push git.woodburn.au/nathanwoodburn/hnshosting-master:latest \ No newline at end of file + docker push git.woodburn.au/nathanwoodburn/hnshosting-master:latest + + Build Bot: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Install Docker + run : | + apt-get install ca-certificates curl gnupg + install -m 0755 -d /etc/apt/keyrings + curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg + chmod a+r /etc/apt/keyrings/docker.gpg + echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null + apt-get update + apt-get install docker-ce-cli -y + - name: Build Docker image + run : | + cd discord-bot + echo "${{ secrets.DOCKERGIT_TOKEN }}" | docker login git.woodburn.au -u nathanwoodburn --password-stdin + tag_num=$(git rev-parse --short HEAD) + docker build -t hnshosting-bot:$tag_num . + docker tag hnshosting-bot:$tag_num git.woodburn.au/nathanwoodburn/hnshosting-bot:$tag_num + docker push git.woodburn.au/nathanwoodburn/hnshosting-bot:$tag_num + docker tag hnshosting-bot:$tag_num git.woodburn.au/nathanwoodburn/hnshosting-bot:latest + docker push git.woodburn.au/nathanwoodburn/hnshosting-bot:latest \ No newline at end of file diff --git a/README.md b/README.md index abc22f5..82c1270 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,17 @@ Then there is the worker server which is the server that will be used to host th This is done to make it easier to manage multiple wordpress sites on multiple servers. +## Overview + +The master server will be used to manage the worker servers. +The worker servers will be used to host the wordpress sites. +The bot will be used to provide an easier way to manage the master server. + +## Usage + +!TODO + + ## Master server install Docker is the easiest way to install the master server. @@ -36,4 +47,11 @@ Add worker to master server: ``` curl -X POST http://master-server-ip:5000/add-worker?worker=worker-name&ip=worker-server-ip -H "key: api-key" +``` + +## Discord bot install + +Docker install +``` +docker run -d -p 5000:5000 -e MASTER_IP= -e DISCORD_TOKEN= -e LICENCE-API=your-api-key -e WORKER_KEY=your-api-key --name hnshosting-bot git.woodburn.au/nathanwoodburn/hnshosting-bot:latest ``` \ No newline at end of file diff --git a/discord-bot/Dockerfile b/discord-bot/Dockerfile new file mode 100644 index 0000000..a225ff3 --- /dev/null +++ b/discord-bot/Dockerfile @@ -0,0 +1,6 @@ +FROM python:3.10-bullseye +COPY requirements.txt /app/ +WORKDIR /app +RUN pip install -r requirements.txt +COPY . . +CMD ["python3", "bot.py"] \ No newline at end of file diff --git a/discord-bot/bot.py b/discord-bot/bot.py new file mode 100644 index 0000000..e9efb25 --- /dev/null +++ b/discord-bot/bot.py @@ -0,0 +1,37 @@ +import os +from dotenv import load_dotenv +import discord +from discord import app_commands +import requests + +load_dotenv() +TOKEN = os.getenv('DISCORD_TOKEN') +ADMINID = 0 +Master_IP = os.getenv('MASTER_IP') + +intents = discord.Intents.default() +client = discord.Client(intents=intents) +tree = app_commands.CommandTree(client) + +@tree.command(name="addworker", description="Adds a worker to the master server") +async def addworker(ctx, ip: str, name: str): + if ctx.author.id == ADMINID: + r = requests.get(f"http://{Master_IP}:5000/add-worker?worker={name}&ip={ip}") + if r.status_code == 200: + await ctx.response.send_message(f"Worker {name} added to the master server",ephemeral=True) + else: + await ctx.response.send_message(f"Error adding worker {name} to the master server\n" + r.text,ephemeral=True) + else: + await ctx.response.send_message("You do not have permission to use this command",ephemeral=True) + + + +# When the bot is ready +@client.event +async def on_ready(): + global ADMINID + ADMINID = client.application.owner.id + await tree.sync() + await client.loop.create_task(client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="over HNSHosting wordpress"))) + +client.run(TOKEN) \ No newline at end of file diff --git a/discord-bot/requirements.txt b/discord-bot/requirements.txt new file mode 100644 index 0000000..ce61fe9 --- /dev/null +++ b/discord-bot/requirements.txt @@ -0,0 +1,3 @@ +discord.py +python-dotenv +requests \ No newline at end of file