Skip to content

Commit

Permalink
Merge pull request #83 from Drazzilb08/patch-1
Browse files Browse the repository at this point in the history
Patch 1
  • Loading branch information
Drazzilb08 authored Feb 6, 2024
2 parents d27d250 + 1334a2f commit f018f46
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 112 deletions.
3 changes: 1 addition & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

# Allow files and directories
!/main.py
!/start.sh
!/Pipfile
!/Pipfile.lock
!/modules
!/util
!/scripts
!/config
!/VERSION

!/root

# Ignore unnecessary files inside allowed directories
# This should go after the allowed directories
Expand Down
94 changes: 34 additions & 60 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
# Stage 1: Create an intermediate image for installing pipenv and converting Pipfile to requirements.txt
FROM python:3.11-slim as pipenv
FROM hotio/base:alpinevpn

# Copy Pipfile and Pipfile.lock to the intermediate image
COPY Pipfile Pipfile.lock ./
# Set working directory
WORKDIR /app

# Copy to the working directory
COPY . .

# Install Python
RUN apk add --no-cache python3

# Install pip3
RUN apk add --no-cache py3-pip

# Install pipenv and use it to generate requirements.txt
RUN pip3 install --no-cache-dir --upgrade pipenv; \
pipenv requirements > requirements.txt

# Debugging: Display the contents of requirements.txt
RUN cat requirements.txt
# Install gcc for building Python dependencies; install app dependencies
RUN apk update; \
apk install -y gcc; \
pip3 install --no-cache-dir -r requirements.txt

# Stage 2: Create an intermediate image for installing Python dependencies from requirements.txt
FROM python:3.11-slim as python-reqs
# Install wget, curl, unzip, p7zip-full, tzdata, vim
RUN apk add wget curl unzip tzdata && \
apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community/ --allow-untrusted p7zip

# Copy requirements.txt from the pipenv stage to the intermediate image
COPY --from=pipenv /requirements.txt requirements.txt
# # Install rclone
# RUN curl https://rclone.org/install.sh | bash

# Install gcc for building Python dependencies; install app dependencies
RUN apt-get update; \
apt-get install -y gcc; \
pip3 install --no-cache-dir -r requirements.txt
# # Install rclone dependencies
# RUN apk add --no-cache ca-certificates fuse

# Stage 3: Create the final image with the application and rclone setup
FROM python:3.11-slim
# # Test rclone installation
# RUN rclone --version

# # Install docker-cli
# RUN apk add --no-cache docker-cli

# Metadata and labels
LABEL maintainer="Drazzilb" \
Expand All @@ -32,51 +44,13 @@ LABEL maintainer="Drazzilb" \
org.opencontainers.image.authors="Drazzilb" \
org.opencontainers.image.title="userScripts"

# Set working directory and copy Python packages from the python-reqs stage
WORKDIR /app

COPY --from=python-reqs /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages

# Set script environment variables
ENV XDG_CONFIG_HOME=/config
ENV US_CONFIG=/config/config.yml
ENV US_LOGS=/config/logs
ENV CONFIG_DIR=/config
ENV DATA_DIR=/data
ENV LOG_DIR=/config/logs
ENV TZ=America/Los_Angeles

# Delete unnecessary setup files
RUN set -eux; \
rm -f Pipfile Pipfile.lock; \
groupadd -g 99 dockeruser; \
useradd -u 100 -g 99 dockeruser; \
apt-get update; \
apt-get install -y --no-install-recommends wget curl unzip p7zip-full tzdata vim;

# Install rclone dependencies and rclone
RUN curl https://rclone.org/install.sh | bash && \
apt-get remove -y curl && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Test rclone installation
RUN rclone --version

# Install Docker CLI inside the container
RUN apt-get update; \
apt-get install -y --no-install-recommends docker-cli; \
apt-get clean; \
rm -rf /var/lib/apt/lists/*

# Share the Docker socket with the container
VOLUME /var/run/docker.sock

# Test rclone installation
RUN rclone --version

# Copy the application source code into the container
COPY . .

# Give permissions to all files under /app/scripts
RUN chmod -R 777 /app/scripts
VOLUME [ "/config" ]
VOLUME [ "/data" ]

CMD ["python", "main.py"]
ENTRYPOINT ["bash", "./start.sh"]
COPY root/ /
3 changes: 2 additions & 1 deletion config/config.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ schedule:
border_replacerr:
health_checkarr:
labelarr:
nohl:
nohl:
sync_gdrive:
poster_cleanarr:
poster_renamerr:
queinatorr:
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"upgradinatorr",
"unmatched_assets",
]



def run_module(module_name, logger):
Expand Down
2 changes: 1 addition & 1 deletion modules/poster_cleanarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def main():

# Check if assets exist, log and exit if not found
if not all(assets_dict.values()):
logger.error("No assets found, Check asset_folders setting in your config. Exiting.")
logger.error("No assets found, Please double check your settings. Exiting...")
sys.exit()

# Check if media exists, log and exit if not found
Expand Down
4 changes: 2 additions & 2 deletions modules/unmatched_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def match_assets(assets_dict, media_dict, ignore_root_folders):
if media_type in media_dict and media_type in assets_dict:
# Iterate through each media data in the media dictionary of the current type
for media_data in tqdm(media_dict[media_type], desc=f"Matching {media_type}", unit="media", total=len(media_dict[media_type]), leave=True, disable=None):
# Check if the media is released, ended, or continuing
if media_type in ['series', 'movies'] and not media_data['status'] in ['released', 'ended', 'continuing']:
# Check if the media is released, ended, or continuing or not monitored
if media_type in ['series', 'movies'] and not media_data['status'] in ['released', 'ended', 'continuing'] or media_type in ['series', 'movies'] and not media_data['monitored']:
continue
if media_type == "collections":
location = media_data['location']
Expand Down
Empty file.
34 changes: 34 additions & 0 deletions root/etc/s6-overlay/s6-rc.d/init-setup-app/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/command/with-contenv bash
# shellcheck shell=bash

umask "${UMASK}"

echo "
------------------------------------------------
Starting userScripts
PUID: ${PUID}
PGID: ${PGID}
UMASK: ${UMASK}
CONFIG_DIR: ${CONFIG_DIR}
DATA_DIR: ${DATA_DIR}
APP_DIR: ${APP_DIR}
------------------------------------------------
"

if [[ ! -f "${CONFIG_DIR}/config.yml" || ! -f "${CONFIG_DIR}/config.sample.yml" ]]; then
echo "Config file not found. Copying config.sample.yml to ${CONFIG_DIR}"
cp /app/config/config.sample.yml "${CONFIG_DIR}"
fi
if [[ ! -f "${CONFIG_DIR}/backup-appdata.conf" || ! -f "${CONFIG_DIR}/backup-appdata-example.conf" ]]; then
echo "Backup appdata config file not found. Copying backup-appdata-example.conf to ${CONFIG_DIR}"
cp /app/config/backup-appdata-example.conf "${CONFIG_DIR}"
fi
if [[ ! -f "${CONFIG_DIR}/backup-plex.conf" || ! -f "${CONFIG_DIR}/backup-plex-example.conf" ]]; then
echo "Backup plex config file not found. Copying backup-plex-example.conf to ${CONFIG_DIR}"
cp /app/config/backup-plex-example.conf "${CONFIG_DIR}"
fi

echo "Setting permissions for ${CONFIG_DIR} and ${DATA_DIR} to ${PUID}:${PGID}"
chown hotio:hotio -R "${CONFIG_DIR}"
chown hotio:hotio -R "${DATA_DIR}"
chown hotio:hotio -R "${APP_DIR}"
1 change: 1 addition & 0 deletions root/etc/s6-overlay/s6-rc.d/init-setup-app/type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oneshot
1 change: 1 addition & 0 deletions root/etc/s6-overlay/s6-rc.d/init-setup-app/up
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/etc/s6-overlay/s6-rc.d/init-setup-app/run
Empty file.
11 changes: 11 additions & 0 deletions root/etc/s6-overlay/s6-rc.d/service-userscripts/finish
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/command/with-contenv bash
# shellcheck shell=bash

if test "$1" -eq 256 ; then
e=$((128 + $2))
else
e="$1"
fi

echo "$e" > /run/s6-linux-init-container-results/exitcode
/run/s6/basedir/bin/halt
7 changes: 7 additions & 0 deletions root/etc/s6-overlay/s6-rc.d/service-userscripts/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/command/with-contenv bash
# shellcheck shell=bash

umask "${UMASK}"

# shellcheck disable=SC2086
exec s6-setuidgid hotio python3 "${APP_DIR}/main.py"
1 change: 1 addition & 0 deletions root/etc/s6-overlay/s6-rc.d/service-userscripts/type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
longrun
Empty file.
Empty file.
36 changes: 0 additions & 36 deletions start.sh

This file was deleted.

16 changes: 10 additions & 6 deletions util/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@
import os
from pathlib import Path
from util.logger import setup_logger
from util.utility import *
import time
logger = setup_logger("info", "main")


def is_docker():
cgroup = Path('/proc/self/cgroup')
return Path('/.dockerenv').is_file() or (cgroup.is_file() and 'docker' in cgroup.read_text())
logger = setup_logger("info", "main")

# Set the config file path
if is_docker():
config_file_path = os.getenv('US_CONFIG', '/config/config.yml')
# Set the config path
config_path = os.getenv('CONFIG_DIR', '/config')
# Set the config file path
config_file_path = os.path.join(config_path, "config.yml")
else:
# Set the config file path
config_file_path = os.path.join(pathlib.Path(__file__).parents[1], "config/config.yml")


# Wait for the config file to be created
while not os.path.isfile(config_file_path):
logger.info(f"Config file not found. Retrying in 60 seconds...")
time.sleep(60)


class Config:
"""
A class to represent the config file
Expand Down
6 changes: 3 additions & 3 deletions util/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import logging.handlers
import pathlib

from util.utility import is_docker
import logging
import logging.handlers
import os
Expand All @@ -24,8 +24,8 @@ def setup_logger(log_level, script_name):
A logger object for logging messages.
"""

if 'HOSTNAME' in os.environ: # Heuristic to check for a container environment
log_dir = os.getenv(f'US_LOGS/{script_name}', f'/config/logs/{script_name}')
if is_docker():
log_dir = os.getenv(f'LOG_DIR/{script_name}', f'/config/logs/{script_name}')
else:
log_dir = f"{os.path.join(pathlib.Path(__file__).parents[1], 'logs', script_name)}"

Expand Down
7 changes: 7 additions & 0 deletions util/utility.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import re
import os
import json
from pathlib import Path
import shutil

try:
import html
Expand Down Expand Up @@ -667,3 +669,8 @@ def validate(config, script_config, logger):
else:
return True

def is_docker():
cgroup = Path('/proc/self/cgroup')
return Path('/.dockerenv').is_file() or (cgroup.is_file() and 'docker' in cgroup.read_text())


0 comments on commit f018f46

Please sign in to comment.