From 75527f2e00d87601e5166206aa0506994ade7c7c Mon Sep 17 00:00:00 2001 From: Paul Gabriel Date: Mon, 18 Dec 2023 21:39:09 +0100 Subject: [PATCH] feat: s3cmd Docker image --- .dockerignore | 1 + .github/workflows/main.yml | 38 ++++++++++++++++ .gitignore | 53 +---------------------- Dockerfile | 17 ++++++++ README.md | 13 +++++- entrypoint.sh | 88 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 157 insertions(+), 53 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/main.yml create mode 100644 Dockerfile create mode 100755 entrypoint.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.DS_Store diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..cf4c6d9 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,38 @@ +name: main + +on: + push: + branches: + - main + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + main: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + - name: Run Buildx + run: | + docker buildx build \ + --push \ + --platform=linux/amd64,linux/arm64,linux/armhf \ + -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest -f ./Dockerfile ./ diff --git a/.gitignore b/.gitignore index c6127b3..e43b0f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,52 +1 @@ -# Prerequisites -*.d - -# Object files -*.o -*.ko -*.obj -*.elf - -# Linker output -*.ilk -*.map -*.exp - -# Precompiled Headers -*.gch -*.pch - -# Libraries -*.lib -*.a -*.la -*.lo - -# Shared objects (inc. Windows DLLs) -*.dll -*.so -*.so.* -*.dylib - -# Executables -*.exe -*.out -*.app -*.i*86 -*.x86_64 -*.hex - -# Debug files -*.dSYM/ -*.su -*.idb -*.pdb - -# Kernel Module Compile Results -*.mod* -*.cmd -.tmp_versions/ -modules.order -Module.symvers -Mkfile.old -dkms.conf +.DS_Store diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..74dc766 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM alpine:3 + +LABEL org.opencontainers.image.source = "https://github.com/dataforgoodfr/d4g-s3cmd" +LABEL org.opencontainers.image.authors = "Data For Good" + +RUN apk update +RUN apk add python3 py-pip py-setuptools git ca-certificates + +RUN git clone https://github.com/s3tools/s3cmd.git /opt/s3cmd +RUN ln -s /opt/s3cmd/s3cmd /usr/bin/s3cmd + +WORKDIR /opt + +ADD ./entrypoint.sh /opt/entrypoint.sh + +WORKDIR / +CMD ["/opt/entrypoint.sh"] diff --git a/README.md b/README.md index 1131fd0..acfb3a7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,13 @@ # d4g-s3cmd -s3cmd Dockerizing for dataforgood + +This repository is an attempt at creating a standard Docker image for our use of `s3cmd` at @dataforgoodfr. + +The result is a lightweight image that can be configured fully with environment variables. +We use Scaleway so some configuration is tailored to their platform (notably the `host_base` and `host_bucket` configurations). + +## Usage +Usage is documented in-script, to display the help menu use + +``` +docker run -it --rm ghcr.io/dataforgoodfr/d4g-s3cmd:latest help +``` diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..0792920 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,88 @@ +#!/bin/sh -xe + +# +# main entry point to run s3cmd +# +S3CMD_PATH=/opt/s3cmd/s3cmd +usage() { + cat < \\ + -e SECRET_KEY= \\ + -e S3_PATH="s3:///" \\ + -e HOST_BASE="fr-par.scw.cloud" \\ + -e BUCKET_REGION="fr-par" \\ + ghcr.io/dataforgoodfr/d4g-s3cmd:latest \\ + [push|pull|help] + +This script will push or pull any directory mapped to /opt/local to/from an S3 bucket. +This image has been built with Scaleway's object storage in-mind, some configurations might +be compatible with that platform only. + +Supported environment variables configurations : +ACCESS_KEY : AWS access key (Required) +SECRET_KEY : AWS secret key (Required) +S3_PATH : S3 path to sync to/from (Required) +HOST_BASE : AWS host base (Optional) +BUCKET_REGION : AWS bucket region (Optional) +EOF + exit 1 +} + +setup_colors() { + if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then + # shellcheck disable=SC2034 + NOCOLOR='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m' + else + # shellcheck disable=SC2034 + NOCOLOR='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW='' + fi +} + +info() { + echo -e "${GREEN}$*${NOCOLOR}" +} + +error() { + echo -e "${RED}$*${NOCOLOR}" +} + +debug() { + if [ "$DEBUG" == 'true' ]; then + echo -e "$1" + fi +} + +setup_colors +if ( $1 == "help" ); then + usage +fi + +for $VAR in ACCESS_KEY SECRET_KEY S3_PATH; do + if [ -z "${!VAR}" ]; then + error "$VAR not set." + usage + fi +done + +echo "" >> /.s3cfg +echo "use_https = True" >> /.s3cfg +echo "access_key = ${ACCESS_KEY}" >> /.s3cfg +echo "secret_key = ${SECRET_KEY}" >> /.s3cfg +if [ -z "${HOST_BASE}" ]; then + echo "host_base = ${HOST_BASE}" >> /.s3cfg +fi +if [ -z "${HOST_BUCKET}" ]; then + echo "host_bucket = ${HOST_BUCKET}" >> /.s3cfg +fi +if [ -z "${BUCKET_REGION}" ]; then + echo "bucket_location = ${BUCKET_REGION}" >> /.s3cfg +fi + +if ( $1 == "push" ); then + ${S3CMD_PATH} --config=/.s3cfg sync /opt/local ${S3_PATH} +fi + +if ( $1 == "pull" ); then + ${S3CMD_PATH} --config=/.s3cfg sync /opt/local/ ${S3_PATH} +fi