Skip to content

Commit

Permalink
Merge pull request #5642 from mailcow/staging
Browse files Browse the repository at this point in the history
2024-01
  • Loading branch information
DerLinkman authored Jan 17, 2024
2 parents cb0b023 + 90a7cff commit b5db5dd
Show file tree
Hide file tree
Showing 47 changed files with 1,333 additions and 347 deletions.
5 changes: 3 additions & 2 deletions data/Dockerfiles/acme/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
FROM alpine:3.17
FROM alpine:3.19

LABEL maintainer "The Infrastructure Company GmbH <[email protected]>"
LABEL maintainer "The Infrastructure Company GmbH GmbH <[email protected]>"

ARG PIP_BREAK_SYSTEM_PACKAGES=1
RUN apk upgrade --no-cache \
&& apk add --update --no-cache \
bash \
Expand Down
10 changes: 7 additions & 3 deletions data/Dockerfiles/clamd/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
FROM clamav/clamav:1.0.3_base
FROM alpine:3.19

LABEL maintainer "The Infrastructure Company GmbH <[email protected]>"
LABEL maintainer "The Infrastructure Company GmbH GmbH <[email protected]>"

RUN apk upgrade --no-cache \
&& apk add --update --no-cache \
rsync \
clamav \
bind-tools \
bash
bash \
tini

# init
COPY clamd.sh /clamd.sh
RUN chmod +x /sbin/tini

# healthcheck
COPY healthcheck.sh /healthcheck.sh
COPY clamdcheck.sh /usr/local/bin
RUN chmod +x /healthcheck.sh
RUN chmod +x /usr/local/bin/clamdcheck.sh
HEALTHCHECK --start-period=6m CMD "/healthcheck.sh"

ENTRYPOINT []
Expand Down
14 changes: 14 additions & 0 deletions data/Dockerfiles/clamd/clamdcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

set -eu

if [ "${CLAMAV_NO_CLAMD:-}" != "false" ]; then
if [ "$(echo "PING" | nc localhost 3310)" != "PONG" ]; then
echo "ERROR: Unable to contact server"
exit 1
fi

echo "Clamd is up"
fi

exit 0
10 changes: 6 additions & 4 deletions data/Dockerfiles/dockerapi/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
FROM alpine:3.17
FROM alpine:3.19

LABEL maintainer "The Infrastructure Company GmbH <[email protected]>"
LABEL maintainer "The Infrastructure Company GmbH GmbH <[email protected]>"

ARG PIP_BREAK_SYSTEM_PACKAGES=1
WORKDIR /app

RUN apk add --update --no-cache python3 \
py3-pip \
openssl \
tzdata \
py3-psutil \
py3-redis \
py3-async-timeout \
&& pip3 install --upgrade pip \
fastapi \
uvicorn \
aiodocker \
docker \
aioredis
docker
RUN mkdir /app/modules

COPY docker-entrypoint.sh /app/
Expand Down
99 changes: 50 additions & 49 deletions data/Dockerfiles/dockerapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,63 @@
import uuid
import async_timeout
import asyncio
import aioredis
import aiodocker
import docker
import logging
from logging.config import dictConfig
from fastapi import FastAPI, Response, Request
from modules.DockerApi import DockerApi
from redis import asyncio as aioredis
from contextlib import asynccontextmanager

dockerapi = None
app = FastAPI()

@asynccontextmanager
async def lifespan(app: FastAPI):
global dockerapi

# Initialize a custom logger
logger = logging.getLogger("dockerapi")
logger.setLevel(logging.INFO)
# Configure the logger to output logs to the terminal
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
formatter = logging.Formatter("%(levelname)s: %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)

logger.info("Init APP")

# Init redis client
if os.environ['REDIS_SLAVEOF_IP'] != "":
redis_client = redis = await aioredis.from_url(f"redis://{os.environ['REDIS_SLAVEOF_IP']}:{os.environ['REDIS_SLAVEOF_PORT']}/0")
else:
redis_client = redis = await aioredis.from_url("redis://redis-mailcow:6379/0")

# Init docker clients
sync_docker_client = docker.DockerClient(base_url='unix://var/run/docker.sock', version='auto')
async_docker_client = aiodocker.Docker(url='unix:///var/run/docker.sock')

dockerapi = DockerApi(redis_client, sync_docker_client, async_docker_client, logger)

logger.info("Subscribe to redis channel")
# Subscribe to redis channel
dockerapi.pubsub = redis.pubsub()
await dockerapi.pubsub.subscribe("MC_CHANNEL")
asyncio.create_task(handle_pubsub_messages(dockerapi.pubsub))


yield

# Close docker connections
dockerapi.sync_docker_client.close()
await dockerapi.async_docker_client.close()

# Close redis
await dockerapi.pubsub.unsubscribe("MC_CHANNEL")
await dockerapi.redis_client.close()

app = FastAPI(lifespan=lifespan)

# Define Routes
@app.get("/host/stats")
Expand Down Expand Up @@ -144,53 +191,7 @@ async def post_container_update_stats(container_id : str):

stats = json.loads(await dockerapi.redis_client.get(container_id + '_stats'))
return Response(content=json.dumps(stats, indent=4), media_type="application/json")

# Events
@app.on_event("startup")
async def startup_event():
global dockerapi

# Initialize a custom logger
logger = logging.getLogger("dockerapi")
logger.setLevel(logging.INFO)
# Configure the logger to output logs to the terminal
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
formatter = logging.Formatter("%(levelname)s: %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)

logger.info("Init APP")

# Init redis client
if os.environ['REDIS_SLAVEOF_IP'] != "":
redis_client = redis = await aioredis.from_url(f"redis://{os.environ['REDIS_SLAVEOF_IP']}:{os.environ['REDIS_SLAVEOF_PORT']}/0")
else:
redis_client = redis = await aioredis.from_url("redis://redis-mailcow:6379/0")

# Init docker clients
sync_docker_client = docker.DockerClient(base_url='unix://var/run/docker.sock', version='auto')
async_docker_client = aiodocker.Docker(url='unix:///var/run/docker.sock')

dockerapi = DockerApi(redis_client, sync_docker_client, async_docker_client, logger)

logger.info("Subscribe to redis channel")
# Subscribe to redis channel
dockerapi.pubsub = redis.pubsub()
await dockerapi.pubsub.subscribe("MC_CHANNEL")
asyncio.create_task(handle_pubsub_messages(dockerapi.pubsub))

@app.on_event("shutdown")
async def shutdown_event():
global dockerapi

# Close docker connections
dockerapi.sync_docker_client.close()
await dockerapi.async_docker_client.close()

# Close redis
await dockerapi.pubsub.unsubscribe("MC_CHANNEL")
await dockerapi.redis_client.close()


# PubSub Handler
async def handle_pubsub_messages(channel: aioredis.client.PubSub):
Expand Down
Loading

0 comments on commit b5db5dd

Please sign in to comment.