Skip to content

Commit

Permalink
Merge pull request #143 from somakeit/develop
Browse files Browse the repository at this point in the history
v1.1
  • Loading branch information
sjefferson99 authored Jun 13, 2024
2 parents 0fd0167 + 7e12e40 commit 9b6726e
Show file tree
Hide file tree
Showing 39 changed files with 1,091 additions and 399 deletions.
62 changes: 55 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
## Python Version
Python 3.12.3

## Docker deployment
### Installation
- Clone the repository to your target server host
- Install docker if not already present
- Set the slack tokens and DB Web UI Credentials as environment variables using either method below:
- Linux
- `export SLACK_APP_TOKEN=<app-token>`
- `export SLACK_BOT_TOKEN=<bot-token>`
- `export ME_CONFIG_BASICAUTH_USERNAME=<mongo-express-basicauth-username>`
- `export ME_CONFIG_BASICAUTH_PASSWORD=<mongo-express-basicauth-password>`
- .env File
- Set the environment variables (minimum of the slack tokens) using either method below. See [template.env](template.env) for all possible environment variables.
- `docker-compose` File - **Highest Precedence**
- Set the variables in your docker-compose file
- `.env` File
- Create a file called `.env` alongside the docker-compose.yml file (see `template.env` in the repo)
- Issue one of the following commands:
- Local Build: `docker compose up -d --build`
Expand All @@ -17,8 +17,52 @@


### Configuration

#### Network Ports
The host ports mapped for the slack server and webserver should be configured in the docker compose file, however it is also possible to override the ports in the server configs directly if you are not using docker.

#### External Config Files
Current files:
- `logging.json` (located at [smib/logging.json](smib/logging.json) in the repo)
- `.env`

This is mapped to `/app/config` in the container

> [!IMPORTANT]
> If you map `/app/config` to a host directory, then you *MUST* add the 2 external files to this location.
You can make this location accessible by Mapping the internal directory to a volume or bind mount in the docker compose file.

Linux:
```yaml
volumes:
- /etc/smib/:/app/config/
```
Windows:
```yaml
volumes:
- C:/smib/config:/app/config/
```
Local Development:
- Set the `_EXTERNAL_CONFIG_LOCATION` environment variable to the directory containing the External Config Files

#### Logging
Map the internal /app/logs directory to a volume or bind mount in the docker compose to store the logs outside the containers

Linux:
```yaml
volumes:
- /var/log/smib/slack/:/app/logs/
```

Windows:
```yaml
volumes:
- C:/smib/slack/logs:/app/logs/
```

## SMIBHID
[SMIBHID](smibhid/README.md) is the So Make It Bot Human Interface Device and definitely not a mispronunciation of any insults from a popular 90s documentary detailing the activites of the Jupiter Mining Core.

Expand All @@ -37,4 +81,8 @@ I think some form of backwards compatibility or similar functionality would be g

An [issue](https://github.com/somakeit/S.M.I.B./issues/83) has been created to track the progress and gather ideas.

## Version
When bumping the poetry version (in pyproject.toml), the `HID` class (part of SMIBHID) `version` attribute also needs manually updating.

> [!IMPORTANT]
> This version needs to match the release when it goes into the `master` branch.
13 changes: 5 additions & 8 deletions docker-compose-develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ services:
- WEBSOCKET_ALLOWED_HOSTS=smib-webserver,smib-webserver.smib-bridge-network
- MONGO_DB_HOST=smib-db

# Passed in from HOST
- SLACK_APP_TOKEN
- SLACK_BOT_TOKEN
networks:
- smib-bridge-network
command: "python -m smib.slack"
restart: unless-stopped

smib-webserver:
container_name: smib-webserver
Expand All @@ -32,19 +30,17 @@ services:
environment:
- WEBSOCKET_HOST=smib-slack

# Passed in from HOST
- SLACK_APP_TOKEN
- SLACK_BOT_TOKEN
networks:
- smib-bridge-network
command: "python -m smib.webserver"
restart: unless-stopped

smib-db:

# Specific version - latest that works on a pi
image: mongo:4.4.18
container_name: smib-db
restart: always
restart: unless-stopped
ports:
- 27017:27017
networks:
Expand All @@ -55,12 +51,13 @@ services:
container_name: smib-db-ui
depends_on:
- smib-db
restart: always
restart: unless-stopped
ports:
- 8082:8081
environment:
ME_CONFIG_MONGODB_URL: mongodb://smib-db:27017/
ME_CONFIG_BASICAUTH: true
ME_CONFIG_OPTIONS_READONLY: true

networks:
- smib-bridge-network
Expand Down
13 changes: 5 additions & 8 deletions docker-compose-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ services:
- WEBSOCKET_ALLOWED_HOSTS=smib-webserver,smib-webserver.smib-bridge-network
- MONGO_DB_HOST=smib-db

# Passed in from HOST
- SLACK_APP_TOKEN
- SLACK_BOT_TOKEN
networks:
- smib-bridge-network
command: "python -m smib.slack"
restart: unless-stopped

smib-webserver:
container_name: smib-webserver
Expand All @@ -32,19 +30,17 @@ services:
environment:
- WEBSOCKET_HOST=smib-slack

# Passed in from HOST
- SLACK_APP_TOKEN
- SLACK_BOT_TOKEN
networks:
- smib-bridge-network
command: "python -m smib.webserver"
restart: unless-stopped

smib-db:

# Specific version - latest that works on a pi
image: mongo:4.4.18
container_name: smib-db
restart: always
restart: unless-stopped
ports:
- 27017:27017
networks:
Expand All @@ -55,12 +51,13 @@ services:
container_name: smib-db-ui
depends_on:
- smib-db
restart: always
restart: unless-stopped
ports:
- 8082:8081
environment:
ME_CONFIG_MONGODB_URL: mongodb://smib-db:27017/
ME_CONFIG_BASICAUTH: true
ME_CONFIG_OPTIONS_READONLY: true

networks:
- smib-bridge-network
Expand Down
12 changes: 4 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ services:
- WEBSOCKET_ALLOWED_HOSTS=smib-webserver,smib-webserver.smib-bridge-network
- MONGO_DB_HOST=smib-db

# Passed in from HOST
- SLACK_APP_TOKEN
- SLACK_BOT_TOKEN
networks:
- smib-bridge-network
command: "python -m smib.slack"
restart: unless-stopped

smib-webserver:
container_name: smib-webserver
Expand All @@ -35,13 +33,14 @@ services:
networks:
- smib-bridge-network
command: "python -m smib.webserver"
restart: unless-stopped

smib-db:

# Specific version - latest that works on a pi
image: mongo:4.4.18
container_name: smib-db
restart: always
restart: unless-stopped
ports:
- 27017:27017
networks:
Expand All @@ -52,17 +51,14 @@ services:
container_name: smib-db-ui
depends_on:
- smib-db
restart: always
restart: unless-stopped
ports:
- 8082:8081
environment:
ME_CONFIG_MONGODB_URL: mongodb://smib-db:27017/
ME_CONFIG_BASICAUTH: true
ME_CONFIG_OPTIONS_READONLY: true

ME_CONFIG_BASICAUTH_USERNAME:
ME_CONFIG_BASICAUTH_PASSWORD:

networks:
- smib-bridge-network

Expand Down
7 changes: 3 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "smib"
version = "1.0.0"
version = "1.1.0"
description = ""
authors = ["sam57719 <[email protected]>"]
readme = "README.md"
Expand All @@ -17,12 +17,12 @@ websockets = "^12.0"
tornado = "^6.4"
injectable = "^3.4.7"
apscheduler = "^3.10.4"
fastapi = "^0.111.0"
uvicorn = "^0.29.0"
dataclasses-json = "^0.6.4"
aiohttp = "^3.9.5"
pymongo = "^4.7.0"
mogo = "^0.6.0"
fastapi = "^0.111.0"


[build-system]
Expand Down
15 changes: 12 additions & 3 deletions smib-fast.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Use an official Python 3.11 runtime as a base image
FROM python:3.11-buster as builder
FROM python:3.12.3-bullseye as builder

RUN pip install poetry==1.4.2

Expand All @@ -24,10 +24,10 @@ WORKDIR /app
COPY smib ./smib
COPY pyproject.toml poetry.lock README.md ./

RUN poetry install --without dev --no-root && rm -rf $POETRY_CACHE_DIR
RUN poetry install --without dev && rm -rf $POETRY_CACHE_DIR

# The runtime image, used to just run the code provided its virtual environment
FROM python:3.11-slim-buster as runtime
FROM python:3.12.3-slim-bullseye as runtime

ENV VIRTUAL_ENV=/app/.venv \
PATH="/app/.venv/bin:$PATH"
Expand All @@ -40,3 +40,12 @@ COPY --from=builder /etc/localtime /etc/localtime

WORKDIR /app
COPY smib ./smib

# Remove logging.json from container
RUN rm ./smib/logging.json

# Copy logging.json into correct container location
COPY smib/logging.json /app/config/logging.json

# Copy .env if it exists
COPY .env* /app/config/
16 changes: 9 additions & 7 deletions smib/common/config.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import json
import logging.config
from urllib.parse import urlparse
from decouple import config, Csv
import warnings
import os
from pathlib import Path
import smib
from urllib.parse import urlparse

from decouple import config, AutoConfig

import smib
from smib.common.utils import to_path

warnings.filterwarnings("ignore", category=RuntimeWarning)

ROOT_DIRECTORY = Path(smib.__file__).parent
EXTERNAL_CONFIG_LOCATION = config('_EXTERNAL_CONFIG_LOCATION', default='/app/config/', cast=Path)


config = AutoConfig(search_path=EXTERNAL_CONFIG_LOCATION)

APPLICATION_NAME = config('APPLICATION_NAME', default='S.M.I.B.')

WEBSOCKET_SCHEME = config('WEBSERVER_SCHEME', default='ws')
WEBSOCKET_HOST = config('WEBSOCKET_HOST', default='localhost')
WEBSOCKET_PORT = config('WEBSOCKET_PORT', default=4123, cast=int)
WEBSOCKET_PATH = config('WEBSOCKET_PATH', default='ws', cast=to_path)
WEBSOCKET_URL = urlparse(f"{WEBSOCKET_SCHEME}://{WEBSOCKET_HOST}:{WEBSOCKET_PORT}/{WEBSOCKET_PATH}")
WEBSOCKET_URL = urlparse(f"{WEBSOCKET_SCHEME}://{WEBSOCKET_HOST}:{WEBSOCKET_PORT}/{WEBSOCKET_PATH}")
25 changes: 20 additions & 5 deletions smib/common/logging_/setup.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
import inspect
import json
import logging
import logging.config
from pathlib import Path

from smib.common.config import ROOT_DIRECTORY
from smib.common.config import EXTERNAL_CONFIG_LOCATION
from smib.common.utils import get_module_name
from injectable import injectable_factory, load_injection_container, inject


def read_logging_json(path=ROOT_DIRECTORY / 'logging.json'):
def read_logging_json(path=EXTERNAL_CONFIG_LOCATION / 'logging.json'):
path = Path(path)

logging.basicConfig()
logging.info(f'Resolving logging.json to {path}')

if not (path.exists() and path.is_file()):
logging.warning(f'No logging json file found at {path}')
return None

with open(path, 'rt') as file:
config_file = json.load(file)
return config_file


def setup_logging(path=ROOT_DIRECTORY / 'logging.json'):
logging.config.dictConfig(read_logging_json(path))
def setup_logging(path=EXTERNAL_CONFIG_LOCATION / 'logging.json'):
try:
logging.config.dictConfig(read_logging_json(path))
except Exception as e:
logging.basicConfig()
logger = logging.getLogger('setup_logging')
logger.warning(e)


@injectable_factory(logging.Logger, qualifier="plugin_logger")
Expand Down
Loading

0 comments on commit 9b6726e

Please sign in to comment.