Skip to content

Commit

Permalink
Merge pull request #77 from Dysta/docker/update-docker-environment
Browse files Browse the repository at this point in the history
Docker/update docker environment
  • Loading branch information
Dysta authored Jul 14, 2023
2 parents da50301 + ddcbab7 commit d53bbe7
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 27 deletions.
19 changes: 19 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM python:3.10-buster

WORKDIR /app

RUN apt-get update && \
apt-get install --no-install-recommends -y ffmpeg && \
apt-get clean && rm -rf /var/lib/apt/lists/*

RUN pip install --no-cache-dir -U poetry

COPY poetry.lock pyproject.toml ./

RUN poetry config virtualenvs.create false \
&& poetry install --no-interaction --no-ansi


COPY . ./

CMD ["poetry", "run", "task", "start"]
54 changes: 54 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-docker-compose
{
"name": "JukeBot dev container",

// Update the 'dockerComposeFile' list if you have more compose files or use different names.
// The .devcontainer/docker-compose.yml file contains any overrides you need/want to make.
"dockerComposeFile": [
"../docker-compose.yml",
"docker-compose.yml"
],

// The 'service' property is the name of the service for the container that VS Code should
// use. Update this value and .devcontainer/docker-compose.yml to the real service name.
"service": "jukebot",

// The optional 'workspaceFolder' property is the path VS Code should open by default when
// connected. This is typically a file mount in .devcontainer/docker-compose.yml
"workspaceFolder": "/app/${localWorkspaceFolderBasename}",

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Configure tool-specific properties.
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance",
"ms-python.black-formatter",
"ms-python.isort",
"njpwerner.autodocstring",
"aaron-bond.better-comments",
"donjayamanne.python-environment-manager",
"mhutchie.git-graph"
]
}
}

// Uncomment the next line to run commands after the container is created.
// "postCreateCommand": "poetry install"

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Uncomment the next line if you want start specific services in your Docker Compose config.
// "runServices": [],

// Uncomment the next line if you want to keep your containers running after VS Code shuts down.
// "shutdownAction": "none",

// Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "devcontainer"
}
26 changes: 26 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: '3'
services:
# Update this to the name of the service you want to work with in your docker-compose.yml file
jukebot:
# Uncomment if you want to override the service's Dockerfile to one in the .devcontainer
# folder. Note that the path of the Dockerfile and context is relative to the *primary*
# docker-compose.yml file (the first in the devcontainer.json "dockerComposeFile"
# array). The sample below assumes your primary file is in the root of your project.
#
build:
context: .
dockerfile: .devcontainer/Dockerfile

volumes:
# Update this to wherever you want VS Code to mount the folder of your project
- ..:/app:cached

# Uncomment the next four lines if you will use a ptrace-based debugger like C++, Go, and Rust.
# cap_add:
# - SYS_PTRACE
# security_opt:
# - seccomp:unconfined

# Overrides default command so things don't shut down after the process ends.
command: /bin/sh -c "while sleep 1000; do :; done"

24 changes: 24 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Ignore everything
*

# Allow directories
!/data
!/jukebot
!/logs

# Allow files
!/docker-compose.yml
!/Dockerfile
!/.dockerignore
!/poetry.lock
!/pyproject.toml
!/.env.example
!/README.md
!/LICENSE.md

# Ignore unnecessary files inside allowed directories
# This should go after the allowed directories
**/*~
**/*.log
**/.DS_Store
**/Thumbs.db
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dev or prod
ENVIRONMENT=dev
LOG_LEVEL=DEBUG
DISCORD_LOG_LEVEL=WARNING

BOT_TOKEN=your_token_here

BOT_TEST_GUILD_IDS="123456, 456789"
Expand Down
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM python:3.9-slim-buster
FROM python:3.10-slim-buster

WORKDIR /app/jukebot
WORKDIR /app

RUN apt-get update && \
apt-get install --no-install-recommends -y ffmpeg && \
Expand All @@ -10,7 +10,8 @@ RUN pip install --no-cache-dir -U poetry

COPY poetry.lock pyproject.toml ./

RUN poetry install --no-dev -E speed
RUN poetry config virtualenvs.create false \
&& poetry install --no-dev --no-interaction --no-ansi -E speed

COPY . ./

Expand Down
14 changes: 3 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,14 @@ version: "3"
services:

jukebot:
image: dysta/jukebot
build: .
container_name: jukebot-app
tty: true
deploy:
restart_policy:
condition: on-failure
max_attempts: 3
window: 20s
window: 15s
env_file:
- .env
environment:
LOGS_PATH: "/data/logs"
volumes:
- jukelogs:/data/logs


volumes:
jukelogs:

- .:/app:cached
4 changes: 2 additions & 2 deletions jukebot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def get_test_guilds_ids() -> Optional[List[int]]:
def main():
load_dotenv(".env")
logging.set_logging(
plogging.DEBUG,
plogging.getLevelName(os.environ["LOG_LEVEL"]),
intercept_disnake_log=True,
disnake_loglevel=plogging.WARNING,
disnake_loglevel=plogging.getLevelName(os.environ["DISCORD_LOG_LEVEL"]),
)

bot = JukeBot(
Expand Down
23 changes: 12 additions & 11 deletions jukebot/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datetime
import logging
import logging as plogging
import os
from contextlib import contextmanager

Expand All @@ -19,20 +20,23 @@ def set_logging(
disnake_loglevel: int = logging.INFO,
):
logger.info(f"Environment is set to '{os.environ['ENVIRONMENT']}'.")
logger.info(f"Jukebot log messages with level {jukebot_loglevel}.")
logger.info(f"Jukebot log messages with level {plogging.getLevelName(jukebot_loglevel)}.")

if intercept_disnake_log:
logger.info(f"Intercepting disnake log messages with level {disnake_loglevel}.")
logger.info(
f"Intercepting disnake log messages with level {plogging.getLevelName(disnake_loglevel)}."
)
logging.basicConfig(handlers=[InterceptHandler()], level=disnake_loglevel)

if os.environ["ENVIRONMENT"] == Environment.DEVELOPMENT:
pass
elif os.environ["ENVIRONMENT"] == Environment.PRODUCTION:
if not os.environ["ENVIRONMENT"] in list(Environment):
logger.critical(f"Unknown environment {os.environ['ENVIRONMENT']}.")
exit(1)

if os.environ["ENVIRONMENT"] == Environment.PRODUCTION:
logger.remove()
path: str = os.environ["LOGS_PATH"] if "LOGS_PATH" in os.environ else "./logs"
fmt = "{time:YYYY-MM-DD at HH:mm:ss} || {level} || {name} || {message}"
fmt = "{time:YYYY-MM-DD at HH:mm:ss} || {level} || {name} || {message}"
logger.add(
f"{path}/log-{datetime.datetime.now():%Y-%m-%d}.log",
f"./logs/log-{datetime.datetime.now():%Y-%m-%d}.log",
level=jukebot_loglevel,
format=fmt,
rotation="01:00",
Expand All @@ -41,9 +45,6 @@ def set_logging(
mode="w",
encoding="utf-8",
)
else:
logger.error(f"Unknown environment {os.environ['ENVIRONMENT']}.")
exit(1)


@contextmanager
Expand Down

0 comments on commit d53bbe7

Please sign in to comment.