From e56136104e5c87ba483a8d87afeed6e153205379 Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Fri, 23 Aug 2024 12:55:16 -0400 Subject: [PATCH 1/6] Add dockerfile and cron setup for sldl with lsio cron --- Dockerfile | 28 +++++++++++++++++++ .../init-mod-universal-crontab-config | 0 .../root/etc/s6-overlay/s6-rc.d/svc-cron/run | 4 +++ .../root/etc/s6-overlay/s6-rc.d/svc-cron/type | 1 + .../root/etc/s6-overlay/s6-rc.d/svc-cron/up | 1 + .../s6-rc.d/user/contents.d/svc-cron | 0 6 files changed, 34 insertions(+) create mode 100644 Dockerfile create mode 100644 docker/root/etc/s6-overlay/s6-rc.d/svc-cron/dependencies.d/init-mod-universal-crontab-config create mode 100755 docker/root/etc/s6-overlay/s6-rc.d/svc-cron/run create mode 100644 docker/root/etc/s6-overlay/s6-rc.d/svc-cron/type create mode 100644 docker/root/etc/s6-overlay/s6-rc.d/svc-cron/up create mode 100644 docker/root/etc/s6-overlay/s6-rc.d/user/contents.d/svc-cron diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..93c601b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +FROM ghcr.io/linuxserver/baseimage-alpine:3.20 + +ENV TZ=Etc/GMT + +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + +RUN \ + echo "**** install packages ****" && \ + apk --no-cache add \ + binutils-gold \ + openssl \ + zlib \ + libstdc++ \ + dotnet6-sdk \ + aspnetcore6-runtime \ + bash + +ENV DOCKER_MODS=linuxserver/mods:universal-cron + +COPY docker/root/ / + +WORKDIR /app + +COPY --chown=abc:abc . /app + +RUN rm -rf /app/docker + +RUN dotnet publish -c Release -r linux-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true \ No newline at end of file diff --git a/docker/root/etc/s6-overlay/s6-rc.d/svc-cron/dependencies.d/init-mod-universal-crontab-config b/docker/root/etc/s6-overlay/s6-rc.d/svc-cron/dependencies.d/init-mod-universal-crontab-config new file mode 100644 index 0000000..e69de29 diff --git a/docker/root/etc/s6-overlay/s6-rc.d/svc-cron/run b/docker/root/etc/s6-overlay/s6-rc.d/svc-cron/run new file mode 100755 index 0000000..075a118 --- /dev/null +++ b/docker/root/etc/s6-overlay/s6-rc.d/svc-cron/run @@ -0,0 +1,4 @@ +#!/usr/bin/with-contenv bash + +echo -e "\nStarting cron" +/usr/sbin/crond -b -S -l 0 -L /config/cron.log \ No newline at end of file diff --git a/docker/root/etc/s6-overlay/s6-rc.d/svc-cron/type b/docker/root/etc/s6-overlay/s6-rc.d/svc-cron/type new file mode 100644 index 0000000..3d92b15 --- /dev/null +++ b/docker/root/etc/s6-overlay/s6-rc.d/svc-cron/type @@ -0,0 +1 @@ +oneshot \ No newline at end of file diff --git a/docker/root/etc/s6-overlay/s6-rc.d/svc-cron/up b/docker/root/etc/s6-overlay/s6-rc.d/svc-cron/up new file mode 100644 index 0000000..bb88ca2 --- /dev/null +++ b/docker/root/etc/s6-overlay/s6-rc.d/svc-cron/up @@ -0,0 +1 @@ +/etc/s6-overlay/s6-rc.d/svc-cron/run diff --git a/docker/root/etc/s6-overlay/s6-rc.d/user/contents.d/svc-cron b/docker/root/etc/s6-overlay/s6-rc.d/user/contents.d/svc-cron new file mode 100644 index 0000000..e69de29 From 222c750bfb90beb10d323719d674a567e62948ca Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Fri, 23 Aug 2024 13:34:38 -0400 Subject: [PATCH 2/6] Use musl release and refactor dockerfile stages --- Dockerfile | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/Dockerfile b/Dockerfile index 93c601b..8bd4c97 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,28 +1,40 @@ -FROM ghcr.io/linuxserver/baseimage-alpine:3.20 +FROM ghcr.io/linuxserver/baseimage-alpine:3.20 as base + +FROM base as build + +RUN \ + echo "**** install build packages ****" && \ + apk --no-cache add \ + binutils-gold \ + openssl \ + zlib \ + libstdc++ \ + dotnet6-sdk + +WORKDIR /app + +COPY --chown=root:root . /app + +RUN dotnet publish -c Release -r linux-musl-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true \ + && rm -f slsk-batchdl/bin/Release/net6.0/osx-x64/publish/*.pdb + +FROM base as app ENV TZ=Etc/GMT RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone RUN \ - echo "**** install packages ****" && \ + echo "**** install runtime packages ****" && \ apk --no-cache add \ - binutils-gold \ - openssl \ - zlib \ - libstdc++ \ - dotnet6-sdk \ - aspnetcore6-runtime \ - bash + dotnet6-runtime && \ + echo "**** cleanup ****" && \ + rm -rf \ + /root/.cache \ + /tmp/* ENV DOCKER_MODS=linuxserver/mods:universal-cron COPY docker/root/ / -WORKDIR /app - -COPY --chown=abc:abc . /app - -RUN rm -rf /app/docker - -RUN dotnet publish -c Release -r linux-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true \ No newline at end of file +COPY --from=build /app/slsk-batchdl/bin/Release/net6.0/linux-musl-x64/publish/* /usr/bin/ \ No newline at end of file From 2381faf80c0282353106ceca906e22e965824db2 Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Fri, 23 Aug 2024 14:03:43 -0400 Subject: [PATCH 3/6] Enable cross-platform dockerfile --- Dockerfile | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8bd4c97..5709cd6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,9 @@ FROM ghcr.io/linuxserver/baseimage-alpine:3.20 as base FROM base as build +ARG TARGETPLATFORM +ARG DOCKER_ARCH + RUN \ echo "**** install build packages ****" && \ apk --no-cache add \ @@ -15,8 +18,10 @@ WORKDIR /app COPY --chown=root:root . /app -RUN dotnet publish -c Release -r linux-musl-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true \ - && rm -f slsk-batchdl/bin/Release/net6.0/osx-x64/publish/*.pdb +RUN if [ "$DOCKER_ARCH" = "amd64" ] || [ "$TARGETPLATFORM" = "linux/amd64" ]; then export DN_RUNTIME=linux-musl-x64; echo 'Building x64'; fi \ + && if [ "$DOCKER_ARCH" = "arm64" ] || [ "$TARGETPLATFORM" = "linux/arm64" ]; then export DN_RUNTIME=linux-musl-arm64; echo 'Build ARM'; fi \ + && dotnet publish -c Release -r "$DN_RUNTIME" -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true -o build \ + && rm -f build/*.pdb FROM base as app @@ -37,4 +42,4 @@ ENV DOCKER_MODS=linuxserver/mods:universal-cron COPY docker/root/ / -COPY --from=build /app/slsk-batchdl/bin/Release/net6.0/linux-musl-x64/publish/* /usr/bin/ \ No newline at end of file +COPY --from=build /app/build/* /usr/bin/ \ No newline at end of file From 24a770967100dea0d4600f242047856515b6fece Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Fri, 23 Aug 2024 14:10:56 -0400 Subject: [PATCH 4/6] Add github action for publishing image --- .github/workflows/publishImage.yml | 81 ++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 .github/workflows/publishImage.yml diff --git a/.github/workflows/publishImage.yml b/.github/workflows/publishImage.yml new file mode 100644 index 0000000..8aa3b28 --- /dev/null +++ b/.github/workflows/publishImage.yml @@ -0,0 +1,81 @@ +name: Publish Docker image to Dockerhub + +on: + workflow_dispatch: + push: +# uncomment to auto build when commits are pushed to listed branches +# branches: +# - 'master' + tags: + - '*.*.*' + # don't trigger if just updating docs + paths-ignore: + - 'README.md' + - '.github/**' + +jobs: + push_to_registry: + name: Build and push container images + if: ${{ github.event_name != 'pull_request' && (vars.GHCR_IMAGE_NAME != '' || vars.DOCKERHUB_IMAGE_NAME != '' ) }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + include: + - dockerfile: ./Dockerfile + suffix: '' + platforms: 'linux/amd64,linux/arm64' + # https://docs.github.com/en/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token + permissions: + packages: write + contents: read + steps: + - name: Check out the repo + uses: actions/checkout@v4 + +# uncomment to enable dockerhub + +# - name: Log in to Docker Hub +# uses: docker/login-action@v3 +# with: +# username: ${{ secrets.DOCKER_USERNAME }} +# password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + # add to images list to enable dockerhub ${{ vars.DOCKERHUB_IMAGE_NAME }} + images: | + ${{ vars.GHCR_IMAGE_NAME }} + # generate Docker tags based on the following events/attributes + # https://github.com/docker/metadata-action/issues/247#issuecomment-1511259674 for NOT is default branch, eventually + tags: | + type=raw,value=latest,enable={{is_default_branch}},suffix=${{ matrix.suffix }} + type=ref,event=branch,enable=${{ !endsWith(github.ref, 'master') }},suffix=${{ matrix.suffix }} + type=semver,pattern={{version}},suffix=${{ matrix.suffix }} + flavor: | + latest=false + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + file: ${{ matrix.dockerfile }} + push: ${{ !env.ACT}} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + platforms: ${{ matrix.platforms }} From 121e813dcf80aef8961aec06dc99921277384ba8 Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Fri, 23 Aug 2024 14:37:04 -0400 Subject: [PATCH 5/6] Add docs for docker --- README.md | 53 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 11 ++++++++++ 2 files changed, 64 insertions(+) create mode 100644 docker-compose.yml diff --git a/README.md b/README.md index f5c8f80..9b2021a 100644 --- a/README.md +++ b/README.md @@ -531,3 +531,56 @@ sldl "artist=MC MENTAL" --aggregate --album ## Notes - For macOS builds you can use publish.sh to build the app. Download dotnet from https://dotnet.microsoft.com/en-us/download/dotnet/6.0, then run `chmod +x publish.sh && sh publish.sh`. For intel macs, uncomment the x64 and comment the arm64 section in publish.sh. - `--display single` and especially `double` can cause the printed lines to be duplicated or overwritten on some configurations. Use `simple` if that's an issue. + +## Docker + +A docker container for running `sldl` can be built from this repository. The image supports linux x86/ARM. + +To build and start container: + +```shell +clone https://github.com/fiso64/slsk-batchdl +cd slsk-batchdl +docker compose up -d +``` + +`exec` into the container to start using `sldl`: + +```shell +docker compose exec sldl sh +sldl --help +``` + +The compose stack mounts two directories relative to where `docker-compose.yml` is located which can be used for file management: + +* `/config` (at `./config` on host) - put your `sldl.conf` [configuration](#configuration-) in this directory and then use `sldl -c /config ...` to use your configuration in the container +* `/data` (at `./data` on host) - use as the download directory IE `sldl -p /data ...` + +### File Permissions + +If you are running Docker on a **Linux Host** you should specify `user:group` permissions of the user who owns the **configuration and data directory** on the host to avoid [docker file permission problems.](https://ikriv.com/blog/?p=4698) These can be specified using the [environmental variables **PUID** and **PGID**.](https://docs.linuxserver.io/general/understanding-puid-and-pgid) + +To get the UID and GID for the current user run these commands from a terminal: + +* `id -u` -- prints UID +* `id -g` -- prints GID + +Replace these with the corresponding variable (`PUID` `PGID`) in `docker-compose.yml`. + + +### Cron + +One or more `sldl` commands can be run on a schedule using [cron](https://en.wikipedia.org/wiki/Cron) built into the container. + +To create a schedule make a new file on the host `./config/crontabs/abc` and use it with the standard [crontab](https://en.wikipedia.org/wiki/Cron#Overview) syntax. + +Make sure to restart the container after any changes to the cron file are made. + +Example => Run `sldl` every Sunday at 1am, search for all tracks from the specified Spotify playlist + +``` +# min hour day month weekday command +0 1 * * 0 sldl https://open.spotify.com/playlist/6sf1WR5grXGJ6dET -c /config -p /data" +``` + +[crontab.guru](https://crontab.guru/) could be used to help with the scheduling expression. \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..774b625 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +services: + sldl: + build: + context: . + environment: + - TZ=Etc/GMT # Specify timezone from TZ Database name found here https://en.wikipedia.org/wiki/List_of_tz_database_time_zones + #- PUID=1000 # recommended if running docker on linux host, see main README Docker setup instructions + #- PGID=1000 # recommended if running docker on linux host, see main README Docker setup instructions + volumes: + - ./config:/config + - ./data:/data \ No newline at end of file From f7be57fed982f5ddecc879606cf4bb2967018f1e Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Fri, 23 Aug 2024 15:03:23 -0400 Subject: [PATCH 6/6] More accurate cron example --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9b2021a..1bef7b8 100644 --- a/README.md +++ b/README.md @@ -576,11 +576,11 @@ To create a schedule make a new file on the host `./config/crontabs/abc` and use Make sure to restart the container after any changes to the cron file are made. -Example => Run `sldl` every Sunday at 1am, search for all tracks from the specified Spotify playlist +Example => Run `sldl` every Sunday at 1am, search for missing tracks from the specified Spotify playlist ``` # min hour day month weekday command -0 1 * * 0 sldl https://open.spotify.com/playlist/6sf1WR5grXGJ6dET -c /config -p /data" +0 1 * * 0 sldl https://open.spotify.com/playlist/6sf1WR5grXGJ6dET -c /config -p /data --music-dir /data --skip-existing" ``` [crontab.guru](https://crontab.guru/) could be used to help with the scheduling expression. \ No newline at end of file