Files
docker-buckets/setup-cloudflare-r2.sh
2024-11-19 14:27:20 -03:00

114 lines
3.0 KiB
Bash
Executable File

#! /bin/sh
#
# Connect to a Cloudflare's R2 storage using mochoa/s3fs
#
# Run this script on the Docker HOST to bind buckets using "linode-bucket"
# -- Or any alias you give with "--alias" option -- driver name.
# Please refer to https://hub.docker.com/r/mochoa/s3fs-volume-plugin.
# You may bind to different buckets using different aliases.
#
# parse args
while test $# -ne 0; do
if test -n "$(echo $1 | grep "=")"; then
key=${1%=*};
value=${1#*=};
else
key=$1;
value=$2
shift
fi
if test "${key}" = "-k" || test "${key}" = "--key"; then
ACCESS_KEY="${value}";
elif test "${key}" = "-s" || test "${key}" = "--secret"; then
ACCESS_SECRET="${value}";
elif test "${key}" = "-l" || test "${key}" = "--location"; then
ACCOUNT_ID="${value}";
elif test "${key}" = "-i" || test "${key}" = "--account-id"; then
ACCOUNT_ID="${value}";
elif test "${key}" = "-a" || test "${key}" = "--alias"; then
PLUGIN_ALIAS="${value}";
elif test "${key}" = "-u" || test "${key}" = "--user"; then
if id "${value}" > /dev/null 2>&1; then
USER_ID="$(id -u "${value}")";
else
USER_ID="$(id -u)";
fi
elif test "${key}" = "-g" || test "${key}" = "--group"; then
if id "${value}" > /dev/null 2>&1; then
GROUP_ID="$(id -g "${value}")";
else
GROUP_ID="$(id -g)";
fi
else
echo "Invalid \"${key}\" option";
exit 1;
fi
shift
done;
if test -z "${PLUGIN_ALIAS}"; then
PLUGIN_ALIAS='r2storage';
fi
if test -z "${ACCOUNT_ID}" || test -z "${ACCOUNT_ID}"; then
echo "You must provide the \"--account-id\" of the R2 bucket";
exit 1;
fi
if test -z "${ACCESS_KEY}" || test -z "${ACCESS_SECRET}"; then
echo "You must provide both \"--key\" and \"--secret\" options";
exit 1;
fi
# test plugin
PLUGIN_ALIAS="${PLUGIN_ALIAS}";
PLUGIN_STATUS="$(docker plugin list \
| grep "${PLUGIN_ALIAS}" \
| rev \
| cut -d " " -f 1 \
| rev \
)";
# install s3fs-volume-plugin if not installed and disable it
if test -z "${PLUGIN_STATUS}"; then
docker plugin install \
mochoa/s3fs-volume-plugin \
--alias "${PLUGIN_ALIAS}" \
--grant-all-permissions \
--disable;
fi
# disable plugin to allow update settings
docker volume ls | while read volume_info; do
volume_driver="$(echo "${volume_info}" | xargs | cut -d " " -f 1)"
volume_name="$(echo "${volume_info}" | xargs | cut -d " " -f 2)"
if test -n "$(echo $volume_driver | grep "${PLUGIN_ALIAS}")"; then
docker volume rm "${volume_name}" > /dev/null;
fi
done
if test "${PLUGIN_STATUS}" = "true"; then
docker plugin disable "${PLUGIN_ALIAS}" \
> /dev/null \
|| exit 1;
fi
# set service
SERVICE_DOMAIN="${SERVICE_DOMAIN:-r2.cloudflarestorage.com}";
URL="https://${ACCOUNT_ID}.${SERVICE_DOMAIN}";
DEFAULT_S3FSOPTS="url=${URL}/";
# set credentials
docker plugin set \
"${PLUGIN_ALIAS}" AWSACCESSKEYID="${ACCESS_KEY}";
docker plugin set \
"${PLUGIN_ALIAS}" AWSSECRETACCESSKEY="${ACCESS_SECRET}";
# set permissions
DEFAULT_S3FSOPTS="uid=33,gid=33,allow_other,${DEFAULT_S3FSOPTS}";
docker plugin set "${PLUGIN_ALIAS}" DEFAULT_S3FSOPTS="${DEFAULT_S3FSOPTS}";
# enable plugin
docker plugin enable "${PLUGIN_ALIAS}";