Skip to content
This repository has been archived by the owner. It is now read-only.

initial commit #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python:3.9.7
ENV PYTHONUNBUFFERED=1
WORKDIR /app
# Copy python dependecies file
COPY requirements.txt .
RUN pip install -r requirements.txt
EXPOSE 8001
COPY . /app/

CMD ["python3", "main.py"]
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Stream Disk Usage Clean Up
Clean up older screenshots from Stream folder when disk is getting used up
> Ensure you are using the default image format:
> `image_format = $(camera)_screenshots/%y-%m-%d/%H-%M-%S.%f.jpg`

## Setup

1. Build the image
```bash
docker build --tag platerecognizer/stream-cleaner .
```

2. Run Image
```bash
docker run --rm -t -v /tmp/stream-template-0:/user-data/ -e INTERVAL=3 -e PERCENTAGE=90 -e LOGGING=DEBUG platerecognizer/stream-cleaner

```

3. Configuration Parameters

| Arg | Default | Description |
| --- | ----------- | --------- |
| -v | **Required** | Stream folder, `config.ini` location |
| -e | INTERVAL=30 | Interval between usage checks. In seconds |
| -e | PERCENTAGE=90 | Screenshots are deleted when percentage is passed |
| -e | LOGGING=INFO | Enable more verbose logging with `DEBUG` |


67 changes: 67 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import logging
import os
import shutil
import time
import sys
from configobj import ConfigObj


LOG_LEVEL = os.environ.get('LOGGING', 'INFO').upper()

logging.basicConfig(
stream=sys.stdout,
level=LOG_LEVEL,
style='{',
format="{asctime} {levelname} {name} {threadName} : {message}")

lgr = logging.getLogger(__name__)


CONFIG_FILE = '/user-data/config.ini'


def cleanup(cameras: list):
"""
Delete camera screenshots dirs
"""

for camera in cameras:
lgr.info(f'Cleaning up Camera: {camera}')
screenshots_dir = f'/user-data/{camera}_screenshots'
try:
shutil.rmtree(screenshots_dir)
except OSError as e:
lgr.error(f"Error Deleting: {e.filename} - {e.strerror}. Skipped.")



def disk_check(limit: int):
"""
Check disk usage as a percentage
"""

total, used, free = shutil.disk_usage(__file__)
lgr.debug(f'Total: {total}, Used: {used}, Free: {free} ')
usage = used/total*100

lgr.debug(f'Usage :{usage}%')

return usage > limit



if __name__ == '__main__':
interval = int(os.getenv('INTERVAL', 30))
percentage = int(os.getenv('PERCENTAGE', 90))

if not os.path.exists(CONFIG_FILE):
lgr.error('config.ini not found, did you forget to mount the stream volume?')
exit(1)

config = ConfigObj('config.ini')
cameras = config['cameras'].sections
while True:
disk_full = disk_check(percentage)
if disk_full:
cleanup(cameras)
time.sleep(interval)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
configobj==5.0.6