From 28b3d10ef1758b8ad5869d6da7594241c596a728 Mon Sep 17 00:00:00 2001 From: Nathan W Date: Sun, 3 Dec 2023 14:38:28 -0700 Subject: [PATCH] Initial commit --- .github/workflows/latest.yml | 41 ++++++++++++++++ .gitignore | 1 + Dockerfile | 8 ++++ README.md | 92 +++++++++++++++++++++++++++++++++++- docker-compose.ofelia.yml | 26 ++++++++++ docker-compose.yml | 13 +++++ entry.sh | 69 +++++++++++++++++++++++++++ 7 files changed, 249 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/latest.yml create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 docker-compose.ofelia.yml create mode 100644 docker-compose.yml create mode 100755 entry.sh diff --git a/.github/workflows/latest.yml b/.github/workflows/latest.yml new file mode 100644 index 0000000..c22adcb --- /dev/null +++ b/.github/workflows/latest.yml @@ -0,0 +1,41 @@ +name: Build latest image + +on: + push: + branches: + - main + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Log in to the Container registry + uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push Docker image + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1269488 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +data diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f921344 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM python:latest + +RUN git clone https://github.com/daniel-widrick/zap2it-GuideScraping.git /zap2it + +ADD entry.sh /entry.sh +RUN chmod 755 /entry.sh /zap2it/zap2it-GuideScrape.py + +CMD ["/entry.sh"] diff --git a/README.md b/README.md index 72d8773..746f84e 100644 --- a/README.md +++ b/README.md @@ -1 +1,91 @@ -# zap2it \ No newline at end of file +# zap2it + +This is a docker image for running [daniel-widrick/zap2it-GuideScraping](https://github.com/daniel-widrick/zap2it-GuideScraping) in an automated way. + +## Features +* Running of zap2it-GuideScrape.py +* Docker Compose examples +* Removal of historical xmlguide files +* Healthcheck.io integration +* Run once, or forever based on sleep time + +## Environment Variables Options +* CONFIGFILE: [string] + * Location of your config file. + * default: `/data/zap2itconfig.ini` +* OUTPUTFILE: [string] + * Location where you want the output file to be placed. + * default: `/data/xmlguide.xmltv` +* HEALTHCHECK_URL: [string] + * URL to ping when starting and ending the run. + * default: `undefined` +* SLEEPTIME: [int] + * Number of seconds to sleep before running again. + * default: `undefined` + +## Running + +### Only Once + +#### docker cli +``` bash +docker run + -v $(PWD)/data:/data + -e CONFIGFILE=/data/zap2itconfig.ini + -e OUTPUTFILE=/data/xmlguide.xmltv + -e HEALTHCHECK_URL=https://hc-ping.com/UUID + --user 1000:1000 + ghcr.io/itsamenathan/zap2it:main +``` +#### docker-compose.yml + +``` yml +version: '3' +services: + zap2it: + image: ghcr.io/itsamenathan/zap2it:main + volumes: + - ./data:/data + environment: + CONFIGFILE: "/data/zap2itconfig.ini" + OUTPUTFILE: "/data/xmlguide.xmltv" + HEALTHCHECK_URL: "https://hc-ping.com/UUID" + user: "1000:1000" +``` + +### Running Continually + +The only difference is adding the `SLEEPTIME` variable. + +#### docker cli +``` bash +docker run + -v $(PWD)/data:/data + -e CONFIGFILE=/data/zap2itconfig.ini + -e OUTPUTFILE=/data/xmlguide.xmltv + -e HEALTHCHECK_URL=https://hc-ping.com/UUID + -e SLEEPTIME=43200 + --user 1000:1000 + ghcr.io/itsamenathan/zap2it:main +``` +#### docker-compose.yml + +``` yml +version: '3' +services: + zap2it: + image: ghcr.io/itsamenathan/zap2it:main + volumes: + - ./data:/data + environment: + CONFIGFILE: "/data/zap2itconfig.ini" + OUTPUTFILE: "/data/xmlguide.xmltv" + HEALTHCHECK_URL: "https://hc-ping.com/UUID" + SLEEPTIME: 43200 + user: "1000:1000" +``` + +### Running with [ofelia](https://github.com/mcuadros/ofelia) + +Ofelia can run a docker container on a schedule for you. +Check out the [docker-compose.ofelia.yml](docker-compose.ofelia.yml) for reference. \ No newline at end of file diff --git a/docker-compose.ofelia.yml b/docker-compose.ofelia.yml new file mode 100644 index 0000000..8e67344 --- /dev/null +++ b/docker-compose.ofelia.yml @@ -0,0 +1,26 @@ +version: '3' + +services: + zap2it: + container_name: ghcr.io/itsamenathan/zap2it:main + image: zap2it + volumes: + - ./data:/data + environment: + CONFIGFILE: "/data/zap2itconfig.ini" + OUTPUTFILE: "/data/xmlguide.xmltv" + HEALTHCHECK_URL: "https://hc-ping.com/UUID" # CHANGE ME + user: "1000:1000" + + ofelia: + image: mcuadros/ofelia:latest + restart: always + depends_on: + - zap2it + command: daemon --docker + volumes: + - /var/run/docker.sock:/var/run/docker.sock:ro + labels: + ofelia.enabled: "true" + ofelia.job-run.zap2it.schedule: "@every 12h" + ofelia.job-run.zap2it.Container: "zap2it" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..2861aa1 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,13 @@ +version: '3' + +services: + zap2it: + container_name: zap2it + image: ghcr.io/itsamenathan/zap2it:main + volumes: + - ./data:/data + environment: + CONFIGFILE: "/data/zap2itconfig.ini" + OUTPUTFILE: "/data/xmlguide.xmltv" + HEALTHCHECK_URL: "https://hc-ping.com/UUID" # CHANGE ME + user: "1000:1000" diff --git a/entry.sh b/entry.sh new file mode 100755 index 0000000..0b1ec6a --- /dev/null +++ b/entry.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +config_file="${CONFIGFILE:-/data/zap2itconfig.ini}" +output_file="${OUTPUTFILE:-/data/xmlguide.xmltv}" +output_dir="$(dirname "$output_file")" +date_format="%Y-%m-%d %H:%M:%S %Z" + +# Ping a healthcheck.io endpoint +healthcheck_io() { + [ "$1" ] && local url_path="/${1}" || local url_path="" + if [ "$HEALTHCHECK_URL" ]; then + curl -fsS -m 10 --retry 5 -o /dev/null "$HEALTHCHECK_URL$url_path" + fi +} + +# Validate some checks before running main +validate() { + # Check output dir + if [ ! -d "$output_dir" ] || [ ! -w "$output_dir" ]; then + echo "Directory does not exist or is not writable...exiting" + exit 1 + fi + + # Check for config file + if [ ! -f "$config_file" ]; then + echo "Can't find $config_file...exiting" + exit 1 + fi +} + +zap2it() { + echo "Started: $(date +"$date_format")" + + # Run script + python /zap2it/zap2it-GuideScrape.py \ + --configfile "$config_file" \ + --outputfile "$output_file" \ + --language "${LANGUAGE:-en}" + + echo "Finished: $(date +"$date_format")" + + # remove historical files + rm "$output_dir"/xmlguide.2*.xmltv +} + +main() { + # Loop if sleeptime is defined + if [ "$SLEEPTIME" ]; then + while true; do + echo "Running zap2it" + zap2it + + echo "Next: $(date +"$date_format" -d "+$SLEEPTIME seconds")" + + # sleep until next run + sleep "$SLEEPTIME" + done + else + echo "Running zap2it" + zap2it + fi + +} + +# Run the scripts functions +healthcheck_io "start" +validate +main +healthcheck_io