Skip to content

Commit

Permalink
feat: update scripts to support project types, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janosmiko committed Aug 23, 2024
1 parent 02ac8a9 commit 6f19213
Show file tree
Hide file tree
Showing 22 changed files with 163 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/*
.vscode/*
tests/output/**
55 changes: 49 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,65 @@
.DEFAULT_GOAL = help

SHELL = bash
project = docker-toolbox
PROJECT = docker-toolbox
GIT_AUTHOR = janosmiko
MAKEFLAGS += --always-make
EXPORT_COMMAND = docker run --rm -v $(PWD)/images/default/rootfs/usr/local/bin:/usr/local/bin -v $(PWD)/tests/output/export:/data -v $(PWD)/tests/export/$(PROJECT_TYPE):/app rewardenv/$(PROJECT):alpine-latest
IMPORT_COMMAND = docker run --rm -v $(PWD)/images/default/rootfs/usr/local/bin:/usr/local/bin -v $(PWD)/tests/import:/data -v $(PWD)/tests/output/import/$(PROJECT_TYPE):/app rewardenv/$(PROJECT):alpine-latest

help: ## Outputs this help screen
@grep -E '(^[\/a-zA-Z0-9_-]+:.*?##.*$$)|(^##)' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}{printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}' | sed -e 's/\[32m##/[33m/'

.PHONY: build-alpine build-debian build-ubuntu

build-alpine: ## Build the default alpine image
docker build -t rewardenv/$(project):alpine-latest -f images/default/alpine/Dockerfile images/default
build build-alpine: ## Build the default alpine image
docker build -t rewardenv/$(PROJECT):alpine-latest -f images/default/alpine/Dockerfile images/default

build-debian: ## Build the default debian image
docker build -t rewardenv/$(project):debian-bookworm-slim -f images/default/debian/Dockerfile images/default
docker build -t rewardenv/$(PROJECT):debian-bookworm-slim -f images/default/debian/Dockerfile images/default

build-ubuntu: ## Build the default ubuntu image
docker build -t rewardenv/$(project):ubuntu-jammy -f images/default/ubuntu/Dockerfile images/default
docker build -t rewardenv/$(PROJECT):ubuntu-jammy -f images/default/ubuntu/Dockerfile images/default

build-all: build-alpine build-debian build-ubuntu ## Build all default images

test-magento-export: ## Test the magento export image
$(eval PROJECT_TYPE := magento)
$(EXPORT_COMMAND) "/usr/local/bin/export-media.sh --gzip --project-type $(PROJECT_TYPE) --source-dir /app --target-dir /data --target-filename $(PROJECT_TYPE)-media"

test-shopware-export: ## Test the shopware export image
$(eval PROJECT_TYPE := shopware)
$(EXPORT_COMMAND) "/usr/local/bin/export-media.sh --gzip --project-type $(PROJECT_TYPE) --source-dir /app --target-dir /data --target-filename $(PROJECT_TYPE)-media"

test-wordpress-export: ## Test the wordpress export image
$(eval PROJECT_TYPE := wordpress)
$(EXPORT_COMMAND) "/usr/local/bin/export-media.sh --gzip --project-type $(PROJECT_TYPE) --source-dir /app --target-dir /data --target-filename $(PROJECT_TYPE)-media"

test-magento-import: ## Test the magento import image
$(eval PROJECT_TYPE := magento)
$(IMPORT_COMMAND) "/usr/local/bin/import-media.sh --project-type $(PROJECT_TYPE) --target-dir /app --source-file /data/$(PROJECT_TYPE)-media.tgz"

test-shopware-import: ## Test the magento import image
$(eval PROJECT_TYPE := shopware)
$(IMPORT_COMMAND) "/usr/local/bin/import-media.sh --project-type $(PROJECT_TYPE) --target-dir /app --source-file /data/$(PROJECT_TYPE)-media.tgz"

test-wordpress-import: ## Test the magento import image
$(eval PROJECT_TYPE := wordpress)
$(IMPORT_COMMAND) "/usr/local/bin/import-media.sh --project-type $(PROJECT_TYPE) --target-dir /app --source-file /data/$(PROJECT_TYPE)-media.tgz"

test-magento-export-stripped: ## Test the magento export image
$(eval PROJECT_TYPE := magento)
$(EXPORT_COMMAND) "/usr/local/bin/export-media.sh --gzip --source-dir /app/pub --compress-targets media --target-dir /data --target-filename $(PROJECT_TYPE)-media-stripped"

test-magento-import-stripped: ## Test the magento import image
$(eval PROJECT_TYPE := magento)
$(IMPORT_COMMAND) "/usr/local/bin/import-media.sh --project-type $(PROJECT_TYPE) --target-dir /app --source-file /data/$(PROJECT_TYPE)-media-stripped.tgz"

test-shopware-export-stripped: ## Test the magento export image
$(eval PROJECT_TYPE := shopware)
$(EXPORT_COMMAND) "/usr/local/bin/export-media.sh --gzip --source-dir /app/public --compress-targets media,thumbnail,sitemap --target-dir /data --target-filename $(PROJECT_TYPE)-media-stripped"

test-shopware-import-stripped: ## Test the magento import image
$(eval PROJECT_TYPE := shopware)
$(IMPORT_COMMAND) "/usr/local/bin/import-media.sh --project-type $(PROJECT_TYPE) --target-dir /app --source-file /data/$(PROJECT_TYPE)-media-stripped.tgz"

build-all: build-alpine build-debian build-ubuntu ## Build all default images
6 changes: 3 additions & 3 deletions images/default/rootfs/usr/local/bin/baseurl-update.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env bash
[ "$DEBUG" = "true" ] && set -x
set -uo pipefail
[ "${DEBUG:-false}" = "true" ] && set -x
set -eEuo pipefail

log() {
[ "${SILENT}" != "true" ] && echo -e "INFO: $*"
[ "${SILENT:-false}" != "true" ] && echo -e "INFO: $*"
}

error() {
Expand Down
6 changes: 3 additions & 3 deletions images/default/rootfs/usr/local/bin/export-db.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env bash
[ "$DEBUG" = "true" ] && set -x
[ "${DEBUG:-false}" = "true" ] && set -x
set -eEuo pipefail

log() {
[ "${SILENT}" != "true" ] && echo -e "INFO: $*"
[ "${SILENT:-false}" != "true" ] && echo -e "INFO: $*"
}

error() {
Expand All @@ -22,7 +22,7 @@ trap 'error status code: $? line: ${LINENO}' ERR

usage() {
error "$(cat <<EOF
usage: $0 options media-dump.tar.gz
usage: $0 options
Options:
-s, --quiet: suppress messages
-g, --gz, --gzip: use gzip compression
Expand Down
62 changes: 51 additions & 11 deletions images/default/rootfs/usr/local/bin/export-media.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env bash
[ "$DEBUG" = "true" ] && set -x
[ "${DEBUG:-false}" = "true" ] && set -x
set -eEuo pipefail

log() {
[ "${SILENT}" != "true" ] && echo -e "INFO: $*"
[ "${SILENT:-false}" != "true" ] && echo -e "INFO: $*"
}

error() {
Expand All @@ -22,16 +22,18 @@ trap 'error status code: $? line: ${LINENO}' ERR

usage() {
error "$(cat <<EOF
usage: $0 options media-dump.tar.gz
usage: $0 option
Options:
-s, --quiet: suppress messages
-z, --zip: use zip compression
-g, --gz, --gzip: use gzip compression
-b, --bz2, --bzip2: use gzip compression
-x, --xz: use xz compression
--source-dir: target directory to compress (default: data)
--source-dir: source directory (default: /data)
--target-dir: backup will be created to this directory
--target-filename: the backup will be saved using this name (without extension)
--compress-targets: comma separated target directories to compress (default: pub/media)
--project-type: the type of the project (default: '')
EOF
)"
}
Expand All @@ -40,9 +42,13 @@ EOF
SILENT="${SILENT:-false}"
TARGET_DIR="${TARGET_DIR:-/data}"
TARGET_FILENAME="${TARGET_FILENAME:-media}"
SOURCE_DIR="${SOURCE_DIR:-/media}"
SOURCE_DIR="${SOURCE_DIR:-.}"
COMPRESS_TARGETS="${COMPRESS_TARGETS:-pub/media}"
PROJECT_TYPE="${PROJECT_TYPE:-}"
FILE_EXTENSION=".tgz"
COMPRESS_COMMAND="tar -zcvf"
COMPRESS_COMMAND="tar --ignore-failed-read -zcvf"

echo "Args: $*"

while test $# -gt 0; do
case "$1" in
Expand All @@ -56,17 +62,17 @@ while test $# -gt 0; do
;;
-b|--bz2|--bzip2)
FILE_EXTENSION=".tbz2"
COMPRESS_COMMAND="tar -jcvf"
COMPRESS_COMMAND="tar --ignore-failed-read -jcvf"
shift
;;
-g|--gz|--gzip)
FILE_EXTENSION=".tgz"
COMPRESS_COMMAND="tar -zcvf"
COMPRESS_COMMAND="tar --ignore-failed-read -zcvf"
shift
;;
-x|--xz)
FILE_EXTENSION=".txz"
COMPRESS_COMMAND="tar -Jcvf"
COMPRESS_COMMAND="tar --ignore-failed-read -Jcvf"
shift
;;
-s|--quiet)
Expand All @@ -77,6 +83,14 @@ while test $# -gt 0; do
SOURCE_DIR="$2"
shift 2
;;
--source-dir)
SOURCE_DIR="$2"
shift 2
;;
--compress-targets)
COMPRESS_TARGETS="$2"
shift 2
;;
--target-dir)
TARGET_DIR="$2"
shift 2
Expand All @@ -85,17 +99,43 @@ while test $# -gt 0; do
TARGET_FILENAME="$2"
shift 2
;;
--project-type)
PROJECT_TYPE="$2"
shift 2
;;
*)
break
;;
esac
done

if [ -n "${PROJECT_TYPE}" ]; then
log "Project type is: ${PROJECT_TYPE}"

case "${PROJECT_TYPE}" in
magento)
COMPRESS_TARGETS="pub/media,var/import,var/export,var/importexport"
log "Compress targets: ${COMPRESS_TARGETS}"
;;
shopware)
COMPRESS_TARGETS="public/media,public/sitemap,public/thumbnail,files"
log "Compress targets: ${COMPRESS_TARGETS}"
;;
wordpress)
COMPRESS_TARGETS="wp-content/uploads"
log "Compress targets: ${COMPRESS_TARGETS}"
;;
*)
error "Unknown project type: ${PROJECT_TYPE}"
;;
esac
fi

export_media() {
log "Compressing file: ${TARGET_DIR}/${TARGET_FILENAME}${FILE_EXTENSION} from ${SOURCE_DIR}"
log "Compressing file: ${TARGET_DIR}/${TARGET_FILENAME}${FILE_EXTENSION} from ${SOURCE_DIR} compress targets: ${COMPRESS_TARGETS//,/ }"
mkdir -p "${TARGET_DIR}"
cd "${SOURCE_DIR}"
eval "${COMPRESS_COMMAND} ${TARGET_DIR}/${TARGET_FILENAME}${FILE_EXTENSION} ."
eval "${COMPRESS_COMMAND} ${TARGET_DIR}/${TARGET_FILENAME}${FILE_EXTENSION} ${COMPRESS_TARGETS//,/ }"
}

export_media
Expand Down
6 changes: 3 additions & 3 deletions images/default/rootfs/usr/local/bin/import-db.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env bash
[ "$DEBUG" = "true" ] && set -x
[ "${DEBUG:-false}" = "true" ] && set -x
set -eEuo pipefail

log() {
[ "${SILENT}" != "true" ] && echo -e "INFO: $*"
[ "${SILENT:-false}" != "true" ] && echo -e "INFO: $*"
}

error() {
Expand All @@ -22,7 +22,7 @@ trap 'error status code: $? line: ${LINENO}' ERR

usage() {
error "$(cat <<EOF
usage: $0 options db-dump.sql
usage: $0 options
Options:
-s, --quiet: suppress messages
--source-file: the file that should be imported
Expand Down
65 changes: 53 additions & 12 deletions images/default/rootfs/usr/local/bin/import-media.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env bash
[ "$DEBUG" = "true" ] && set -x
[ "${DEBUG:-false}" = "true" ] && set -x
set -eEuo pipefail

log() {
[ "${SILENT}" != "true" ] && echo -e "INFO: $*"
[ "${SILENT:-false}" != "true" ] && echo -e "INFO: $*"
}

error() {
Expand All @@ -22,12 +22,14 @@ trap 'error status code: $? line: ${LINENO}' ERR

usage() {
error "$(cat <<EOF
usage: $0 options media-dump.tar.gz
usage: $0 options
Options:
-s, --quiet: suppress messages
--source-file: the file that should be imported
--target-dir: target directory for extraction (default: data)
--search-for: search for directory in archive (default: "")
--target-subdir: target subdirectory for extraction (default: '')
--search-for: search for this directory in the archive for media (default: '')
--project-type: the type of the project (default: '')
--cleanup: remove everything from the target directory before importing
--fix-permission: change owner and group after import
--owner: change owner of files to this after the import (default: 1000)
Expand All @@ -40,8 +42,10 @@ EOF
COMPRESSION="${COMPRESSION:-gz}"
UNCOMPRESS_COMMAND="${UNCOMPRESS_COMMAND:-gzip -c}"
SILENT="${SILENT:-false}"
PROJECT_TYPE="${PROJECT_TYPE:-}"
SOURCE_FILE="${SOURCE_FILE:-}"
TARGET_DIR="${TARGET_DIR:-data}"
TARGET_SUBDIR="${TARGET_SUBDIR:-}"
SEARCH_FOR="${SEARCH_FOR:-}"
CLEANUP="${CLEANUP:-false}"
FIX_PERMISSIONS="${FIX_PERMISSIONS:-false}"
Expand All @@ -62,6 +66,14 @@ while test $# -gt 0; do
TARGET_DIR="$2"
shift 2
;;
--project-type)
PROJECT_TYPE="$2"
shift 2
;;
--target-subdir)
TARGET_SUBDIR="$2"
shift 2
;;
--search-for)
SEARCH_FOR="$2"
shift 2
Expand Down Expand Up @@ -120,26 +132,51 @@ else
error "Bad media format! Accepted formats: *.txz, *.tar.xz, *.tar.gz, *.tgz, *.tar.bz2, *.tbz2, *.zip"
fi

if [ -n "${PROJECT_TYPE}" ]; then
log "Project type is: ${PROJECT_TYPE}"

case "${PROJECT_TYPE}" in
magento)
SEARCH_FOR="media"
TARGET_SUBDIR="pub"
log "Searching for media in: ${SEARCH_FOR}"
;;
shopware)
SEARCH_FOR="media"
TARGET_SUBDIR="public"
log "Searching for media in: ${SEARCH_FOR}"
;;
wordpress)
SEARCH_FOR="uploads"
TARGET_SUBDIR="wp-content"
log "Searching for media in: ${SEARCH_FOR}"
;;
*)
error "Unknown project type: ${PROJECT_TYPE}"
;;
esac
fi

case $COMPRESSION in
gz|gzip)
CHECK_COMMAND="tar tf ${SOURCE_FILE} ./${SEARCH_FOR} 2>/dev/null | wc -l"
CHECK_COMMAND="tar tf ${SOURCE_FILE} ${SEARCH_FOR} 2>/dev/null | wc -l"
UNCOMPRESS_COMMAND="tar zxvf ${SOURCE_FILE}"
UNCOMPRESS_COMMAND_STRIP="tar zxvf ${SOURCE_FILE} --strip-components=2 ./${SEARCH_FOR}"
UNCOMPRESS_COMMAND_STRIPPED="mkdir -p ${TARGET_SUBDIR} && tar zxvf ${SOURCE_FILE} -C ${TARGET_SUBDIR} ${SEARCH_FOR}"
;;
bz2)
CHECK_COMMAND="tar tf ${SOURCE_FILE} ./${SEARCH_FOR} 2>/dev/null | wc -l"
CHECK_COMMAND="tar tf ${SOURCE_FILE} ${SEARCH_FOR} 2>/dev/null | wc -l"
UNCOMPRESS_COMMAND="tar jxvf ${SOURCE_FILE}"
UNCOMPRESS_COMMAND_STRIP="tar jxvf ${SOURCE_FILE} --strip-components=2 ./${SEARCH_FOR}"
UNCOMPRESS_COMMAND_STRIPPED="mkdir -p ${TARGET_SUBDIR} && tar jxvf ${SOURCE_FILE} -C ${TARGET_SUBDIR} ${SEARCH_FOR}"
;;
zip)
CHECK_COMMAND="unzip -l ${SOURCE_FILE} | tail -n +4 | awk '{print \$4}' | grep \"^${SEARCH_FOR}/$\""
UNCOMPRESS_COMMAND='unzip -o ${SOURCE_FILE}'
UNCOMPRESS_COMMAND_STRIP='TMPDIR="$(mktemp -d)" && unzip -o -d ${TMPDIR} ${SOURCE_FILE} && rsync -Par ${TMPDIR}/${SEARCH_FOR}/ ./'
UNCOMPRESS_COMMAND_STRIPPED='mkdir -p ${TARGET_SUBDIR} && unzip -o -d ${TARGET_SUBDIR} ${SOURCE_FILE}'
;;
xz)
CHECK_COMMAND="tar tf ${SOURCE_FILE} ./${SEARCH_FOR} 2>/dev/null | wc -l"
CHECK_COMMAND="tar tf ${SOURCE_FILE} ${SEARCH_FOR} 2>/dev/null | wc -l"
UNCOMPRESS_COMMAND="tar Jxvf ${SOURCE_FILE}"
UNCOMPRESS_COMMAND_STRIP="tar Jxvf ${SOURCE_FILE} --strip-components=2 ./${SEARCH_FOR}"
UNCOMPRESS_COMMAND_STRIPPED="mkdir -p ${TARGET_SUBDIR} && tar Jxvf ${SOURCE_FILE} -C ${TARGET_SUBDIR} ${SEARCH_FOR}"
;;
unknown)
;;
Expand All @@ -161,14 +198,18 @@ import_media() {
SEARCH_CONTAINS=false
# shellcheck disable=SC2086
if [ -n "${SEARCH_FOR}" ]; then
log "Checking if the archive contains ${SEARCH_FOR} directory"
if [ "$(eval "${CHECK_COMMAND}")" -gt 0 ]; then
log "The archive contains ${SEARCH_FOR} directory"
SEARCH_CONTAINS=true
fi
fi

if [ "${SEARCH_CONTAINS}" = "true" ]; then
eval "${UNCOMPRESS_COMMAND_STRIP}"
log "Extracting ${SEARCH_FOR} directory from the archive"
eval "${UNCOMPRESS_COMMAND_STRIPPED}"
else
log "Extracting the whole archive"
eval "${UNCOMPRESS_COMMAND}"
fi
fi
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Binary file added tests/import/magento-media-stripped.tgz
Binary file not shown.
Binary file added tests/import/magento-media.tgz
Binary file not shown.
Binary file added tests/import/shopware-media-stripped.tgz
Binary file not shown.
Binary file added tests/import/shopware-media.tgz
Binary file not shown.
Binary file added tests/import/wordpress-media.tgz
Binary file not shown.

0 comments on commit 6f19213

Please sign in to comment.