Skip to content

Commit

Permalink
- Reverted back to Python 3.12, due to a known issue with Python 3.13…
Browse files Browse the repository at this point in the history
… (This bot needs to stay with 3.12 for now): "Use of deprecated module audioop": Rapptz/discord.py#9477 (#11)

- Have setup.sh wipe out the old .venv folder before creating it again from scratch - This helps with major upgrades of Python
- Unpinned the Python dependencies - This will help cope with random dependabot security pull requests
- Use python development dependencies (ie. black, pylint) if specified by requirements-dev.txt
- If we detect a running systemd or docker bot instance, do a restart instead of a start
- Standardized naming of systemd service references to use underscores only, rather than a mix of dashes and underscores
- Parameterized naming of systemd service and Docker container in the install scripts

Co-authored-by: Tom Zacharoff <warelock2000@protonmail.com>
  • Loading branch information
warelock2 and Tom Zacharoff committed Jun 28, 2024
1 parent 38017ea commit 25425aa
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 43 deletions.
11 changes: 9 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Use Python 3.12 image as the base
FROM python:3.13.0b1-slim
# Use Python image as the base
#
# NOTE: This needs to stay at 3.12, as 3.13 has dependency issues
# with wheel
#
# "Use of deprecated module audioop"
# https://github.com/Rapptz/discord.py/issues/9477
#
FROM python:3.12

# Set the working directory inside the container
WORKDIR /app
Expand Down
File renamed without changes.
36 changes: 22 additions & 14 deletions install_and_start_with_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# This script installs the Discord bot as a Docker container. It expects
# that Docker is installed and configured correctly on the system.

CONTAINER_NAME="discord_google_calendar_bot"

# Verify that Docker is installed
if [ ! $(command -v docker) ]; then
echo "Please install Docker first."
Expand All @@ -21,28 +23,34 @@ cat ~/.aws/config ~/.aws/credentials | grep = | grep -v output | \
chmod 600 ~/.aws/docker_env.conf

# Build the Docker image
docker build -t discord_google_calendar_bot .
docker build -t $CONTAINER_NAME .

# Start the bot as a Docker container
docker run --name="discord_google_calendar_bot" \
--env-file=$HOME/.aws/docker_env.conf -d discord_google_calendar_bot
if docker inspect --format '{{.State.Running}}' "$CONTAINER_NAME" 2>/dev/null | grep -q 'true'; then
printf "Restarting existing Docker bot $CONTAINER_NAME: "
docker restart $CONTAINER_NAME
else
printf "Starting $CONTAINER_NAME as a Docker container: "
docker run --name="$CONTAINER_NAME" \
--env-file=$HOME/.aws/docker_env.conf -d $CONTAINER_NAME
fi

# Print instructions for manipulating the bot
echo ""
echo "The bot has been started as a Docker container:"
echo "The bot is now running as a Docker container: "
echo ""
docker ps | grep discord_google_calendar_bot
docker ps | grep $CONTAINER_NAME
echo ""
echo "Other operational things you can do with the container:"
echo "Other operational things you can do with the bot:"
echo ""
echo "docker start discord_google_calendar_bot"
echo "docker stop discord_google_calendar_bot"
echo "docker restart discord_google_calendar_bot"
echo "docker logs discord_google_calendar_bot -f"
echo "docker start $CONTAINER_NAME"
echo "docker stop $CONTAINER_NAME"
echo "docker restart $CONTAINER_NAME"
echo "docker logs $CONTAINER_NAME -f"
echo ""
echo "To stop the bot and remove the docker container and image completely:"
echo "To stop the bot, removing the docker container and image completely:"
echo ""
echo "docker stop discord_google_calendar_bot"
echo "docker rm discord_google_calendar_bot"
echo "docker image rm discord_google_calendar_bot"
echo "docker stop $CONTAINER_NAME"
echo "docker rm $CONTAINER_NAME"
echo "docker image rm $CONTAINER_NAME"
echo ""
53 changes: 31 additions & 22 deletions install_and_start_with_systemd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
# This script installs the Discord bot as a systemd service.

# Variables
INSTALL_DIR=/opt/discord_google_calendar_bot
USERNAME="discord_google_calendar_bot"
GROUPNAME="discord_google_calendar_bot"
SYSTEMD_SERVICE="discord_google_calendar_bot"
INSTALL_DIR=/opt/$SYSTEMD_SERVICE
USERNAME="$SYSTEMD_SERVICE"
GROUPNAME="$SYSTEMD_SERVICE"

# Verify that the AWS CLI is installed and properly configured
if [ ! -d ~/.aws ]; then
Expand All @@ -24,37 +25,45 @@ sudo useradd -r -m -g $GROUPNAME -s /usr/sbin/nologin $USERNAME
sudo passwd -l $USERNAME

sudo mkdir -p $INSTALL_DIR
sudo cp -rp .venv run_discord_google_calendar_bot.sh ~/.aws/docker_env.conf \
discord_google_calendar_bot.py $INSTALL_DIR
sudo cp -p discord-google-calendar-bot.service /etc/systemd/system/
sudo cp -rp .venv run_$SYSTEMD_SERVICE.sh ~/.aws/docker_env.conf \
$SYSTEMD_SERVICE.py $INSTALL_DIR
sudo cp -p $SYSTEMD_SERVICE.service /etc/systemd/system/
sudo chown -R $USERNAME:$GROUPNAME $INSTALL_DIR
sudo chmod 700 $INSTALL_DIR/
sudo chmod 700 $INSTALL_DIR/.venv
sudo chmod 400 $INSTALL_DIR/docker_env.conf
sudo chmod 400 $INSTALL_DIR/discord_google_calendar_bot.py
sudo chmod 700 $INSTALL_DIR/run_discord_google_calendar_bot.sh
sudo chmod 400 $INSTALL_DIR/$SYSTEMD_SERVICE.py
sudo chmod 700 $INSTALL_DIR/run_$SYSTEMD_SERVICE.sh
sudo systemctl daemon-reload
sudo systemctl enable discord-google-calendar-bot
sudo systemctl start discord-google-calendar-bot
sudo systemctl enable $SYSTEMD_SERVICE

if systemctl is-active --quiet "$SYSTEMD_SERVICE"; then
echo "Restart running service '$SYSTEMD_SERVICE'"
sudo systemctl restart $SYSTEMD_SERVICE
else
echo "Start service '$SYSTEMD_SERVICE'"
sudo systemctl start $SYSTEMD_SERVICE
fi

# Print instructions for manipulating the bot
echo ""
echo "The bot has been started as a systemd service:"
echo "The bot is running as a systemd service:"
echo ""
SYSTEMD_PAGER=cat systemctl status discord-google-calendar-bot
SYSTEMD_PAGER=cat systemctl status $SYSTEMD_SERVICE
echo ""
echo "Other operational things you can do with the container:"
echo "Other operational things you can do with the bot:"
echo ""
echo "sudo systemctl start discord-google-calendar-bot"
echo "sudo systemctl stop discord-google-calendar-bot"
echo "sudo systemctl restart discord-google-calendar-bot"
echo "sudo journalctl -fu discord-google-calendar-bot"
echo "sudo systemctl start $SYSTEMD_SERVICE"
echo "sudo systemctl stop $SYSTEMD_SERVICE"
echo "sudo systemctl restart $SYSTEMD_SERVICE"
echo "sudo systemctl status $SYSTEMD_SERVICE"
echo "sudo journalctl -fu $SYSTEMD_SERVICE"
echo ""
echo "To stop the bot and remove it completely:"
echo ""
echo "sudo systemctl stop discord-google-calendar-bot"
echo "sudo systemctl disable discord-google-calendar-bot"
echo "sudo rm -rf /opt/discord_google_calendar_bot"
echo "sudo rm /etc/systemd/system/discord-google-calendar-bot.service"
echo "sudo userdel -r discord_google_calendar_bot"
echo "sudo systemctl stop $SYSTEMD_SERVICE"
echo "sudo systemctl disable $SYSTEMD_SERVICE"
echo "sudo rm -rf /opt/$SYSTEMD_SERVICE"
echo "sudo rm /etc/systemd/system/$SYSTEMD_SERVICE.service"
echo "sudo userdel -r $SYSTEMD_SERVICE"
echo ""
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
boto3==1.34.132
discord.py==2.4.0
google==3.0.0
google-api-python-client==2.134.0
boto3
discord.py
google
google-api-python-client
5 changes: 4 additions & 1 deletion setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ install_if_not_exists python$PYTHON_VERSION
install_if_not_exists python${PYTHON_VERSION}-venv
install_if_not_exists python3-pip

#
[ -d .venv ] && rm -rf .venv

# Create a virtual environment using the specified Python version
python$PYTHON_VERSION -m venv .venv

Expand All @@ -34,7 +37,7 @@ source .venv/bin/activate
pip install --upgrade pip

# Install required Python packages from requirements.txt
pip install -r requirements.txt
[ -f requirements-dev.txt ] && pip install -r requirements-dev.txt || pip install -r requirements.txt

# Deactivate the virtual environment
deactivate
Expand Down

0 comments on commit 25425aa

Please sign in to comment.