diff --git a/.travis.yml b/.travis.yml index a42996d..8c791e0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,4 +29,4 @@ branches: - master script: - - ./lint.sh --sh + - ./dev/util/shellcheck.sh diff --git a/Dockerfile b/Dockerfile index 61ecd60..3b1a02a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,16 @@ -ARG pg_alpine_branch -FROM alpine:${pg_alpine_branch} +ARG alpine_version +FROM alpine:${alpine_version} -ARG pg_alpine_branch -ARG pg_version +ARG alpine_version +ARG pg_full_version #-------------------------------------------------------------------------------- # Install dependencies #-------------------------------------------------------------------------------- # "postgresql" is required for "pg_restore" -# "python" is required for "aws-cli" #-------------------------------------------------------------------------------- -RUN echo "http://dl-cdn.alpinelinux.org/alpine/v${pg_alpine_branch}/main" >> /etc/apk/repositories - -RUN apk --no-cache add dumb-init postgresql=${pg_version} python py-pip && \ - pip install awscli && \ - apk --purge -v del py-pip +RUN echo "http://dl-cdn.alpinelinux.org/alpine/v${alpine_version}/main" >> /etc/apk/repositories +RUN apk --no-cache --update add dumb-init postgresql=${pg_full_version} aws-cli #-------------------------------------------------------------------------------- # Set script permissions and create required directories diff --git a/README.md b/README.md index f1bc80f..44f871f 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,25 @@ Cron based download from s3 and database restore. +## Build + +`./build_push.sh [-p , --package ]` + +`./build_push.sh -p 12.4-3.12` + +### Package files + +Each package file represents a release for a particular `postgres` branch. + +The contents of the latest package file may look like this: + +``` +ALPINE_VERSION='3.12' +PG_BASE_VERSION='12' +PG_FULL_VERSION='12.4' +PG_LATEST=true +``` + ## Usage Typically this image is instantiated as a container among many others and would have the responsibility of getting downloading a dump file from s3 and restoring a database at a particular time of day. diff --git a/action.sh b/action.sh index b6bfbfe..53bcf7c 100755 --- a/action.sh +++ b/action.sh @@ -100,10 +100,10 @@ if [ -n "$PRE_RESTORE_PSQL" ]; then fi if [ -n "$SCHEMA" ]; then - printf '%s' " pg_restore --jobs $(grep -c ^processor /proc/cpuinfo) --schema $SCHEMA --no-owner -d /cache/${dump_file}" + printf '\n%s' " pg_restore --jobs $(grep -c ^processor /proc/cpuinfo) --schema $SCHEMA --no-owner -d /cache/${dump_file}" pg_restore --jobs "$(grep -c ^processor /proc/cpuinfo)" --schema "$SCHEMA" --no-owner -d "${DATABASE_URL}" "/cache/${dump_file}" else - printf '%s' " pg_restore --jobs $(grep -c ^processor /proc/cpuinfo) --no-owner -d /cache/${dump_file}" + printf '\n%s' " pg_restore --jobs $(grep -c ^processor /proc/cpuinfo) --no-owner -d /cache/${dump_file}" pg_restore --jobs "$(grep -c ^processor /proc/cpuinfo)" --no-owner -d "${DATABASE_URL}" "/cache/${dump_file}" fi diff --git a/build_push.sh b/build_push.sh index adc4f26..b7047f2 100755 --- a/build_push.sh +++ b/build_push.sh @@ -1,21 +1,56 @@ #!/usr/bin/env sh -builds=$(echo ' -9.6.10 9.6.10-r0 3.6 -9.6 9.6.10-r0 3.6 -9 9.6.10-r0 3.6 -latest 9.6.10-r0 3.6 -' | grep -v '^#' | tr -s ' ') - -# shellcheck disable=SC2039 -IFS=$'\n' -for build in $builds; do - tag=$(echo "${build}" | cut -d ' ' -f 1 ) - pg_version=$(echo "${build}" | cut -d ' ' -f 2) - pg_alpine_version=$(echo "${build}" | cut -d ' ' -f 3) - - echo docker build --tag bluedrop360/postgres-restore-from-s3:"${tag}" --build-arg pg_version="${pg_version}" --build-arg pg_alpine_branch="${pg_alpine_version}" . - eval docker build --tag bluedrop360/postgres-restore-from-s3:"${tag}" --build-arg pg_version="${pg_version}" --build-arg pg_alpine_branch="${pg_alpine_version}" . - echo docker push bluedrop360/postgres-restore-from-s3:"${tag}" - eval docker push bluedrop360/postgres-restore-from-s3:"${tag}" +#------------------------------------------------------------------------------------ +# Loop over arguments +#------------------------------------------------------------------------------------ +for arg in "$@"; do + # [ -p | --package ] + if [ -n "${package_flag}" ]; then + package_flag='' + package="${arg}" + fi + + if [ "${arg}" = "-p" ] || [ "${arg}" = "--package" ]; then + package_flag=true + fi +done + +#------------------------------------------------------------------------------------ +# Exit on error +#------------------------------------------------------------------------------------ +if [ -z "${package}" ]; then + echo '> Package file not specified: [-p , --package ]' + exit 127 +fi + +if [ -f "./package/${package}.env" ]; then + . "./package/${package}.env" +else + echo "> Package file not found: './package/${package}.env'" + exit 127 +fi + +builds=\ +"${PG_FULL_VERSION}_${PG_FULL_VERSION}-r0_${ALPINE_VERSION}",\ +"${PG_BASE_VERSION}_${PG_FULL_VERSION}-r0_${ALPINE_VERSION}" + +if [ "${PG_LATEST:-'false'}" = 'true' ]; then + builds="${builds}","latest_${PG_FULL_VERSION}-r0_${ALPINE_VERSION}" +fi + +echo $builds | tr ',' '\n' | while read build; do + ALPINE_VERSION=$(echo "${build}" | cut -d '_' -f 3) + pg_restore_from_s3_tag=$(echo "${build}" | cut -d '_' -f 1 ) + PG_FULL_VERSION=$(echo "${build}" | cut -d '_' -f 2) + + echo "" + echo "--------------------------------" + echo "POSTGRES-RESTORE-FROM-S3 TAG: ${pg_restore_from_s3_tag}" + echo "POSTGRES PACKAGE VERSION: ${PG_FULL_VERSION}" + echo "--------------------------------" + + echo docker build --tag bluedrop360/postgres-restore-from-s3:$pg_restore_from_s3_tag --build-arg pg_full_version=$PG_FULL_VERSION --build-arg alpine_version="${ALPINE_VERSION}" . + eval docker build --tag bluedrop360/postgres-restore-from-s3:$pg_restore_from_s3_tag --build-arg pg_full_version=$PG_FULL_VERSION --build-arg alpine_version="${ALPINE_VERSION}" . || exit 1 + echo docker push bluedrop360/postgres-restore-from-s3:$pg_restore_from_s3_tag + eval docker push bluedrop360/postgres-restore-from-s3:$pg_restore_from_s3_tag || exit 1 done diff --git a/dev/util/shellcheck.sh b/dev/util/shellcheck.sh new file mode 100755 index 0000000..af7e20d --- /dev/null +++ b/dev/util/shellcheck.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env sh + +printf '\n%s\n' "Checking shell scripts..." + +SHELLCHECK_OPTS="" + +RUN_SHELLCHECK="shellcheck ${ALLOW_EXTERNAL_SOURCE:-} ${SHELLCHECK_OPTS} {} +" +eval "find ./*.sh -type f -exec ${RUN_SHELLCHECK}" diff --git a/docker-compose.yml b/docker-compose.yml index 70c2799..8d2bc99 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,14 +11,14 @@ version: '3' services: postgres-restore-from-s3: - image: postgres-restore-from-s3:9.6.10 + image: postgres-restore-from-s3:11.7 network_mode: 'host' build: context: ./ dockerfile: ./Dockerfile args: - pg_alpine_branch: '3.6' - pg_version: '9.6.10-r0' + alpine_version: '3.12' + pg_full_version: '12.4-r0' environment: AWS_BUCKET: AWS_REGION: diff --git a/lint.sh b/lint.sh deleted file mode 100644 index be6fb52..0000000 --- a/lint.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env sh - -ALLOW_EXTERNAL_SOURCE="${ALLOW_EXTERNAL_SOURCE:-}" - -if [ "${1}" = "--yml" ]; then - printf '\n%s\n' "Checking '.travis.yml'..." - travis lint ./.travis.yml -fi - -if [ "${1}" = "--sh" ]; then - printf '\n%s\n' "Checking shell scripts..." - - SHELLCHECK_OPTS="" - - RUN_SHELLCHECK="shellcheck ${ALLOW_EXTERNAL_SOURCE} ${SHELLCHECK_OPTS} {} +" - eval "find ./*.sh -type f -exec ${RUN_SHELLCHECK}" -fi diff --git a/package/10.12-3.8.env b/package/10.12-3.8.env new file mode 100644 index 0000000..c1f48eb --- /dev/null +++ b/package/10.12-3.8.env @@ -0,0 +1,3 @@ +ALPINE_VERSION='3.8' +PG_BASE_VERSION='10' +PG_FULL_VERSION='10.12' diff --git a/package/11.7-3.9.env b/package/11.7-3.9.env new file mode 100644 index 0000000..970ec5b --- /dev/null +++ b/package/11.7-3.9.env @@ -0,0 +1,4 @@ +ALPINE_VERSION='3.9' +PG_LATEST=false +PG_BASE_VERSION='11' +PG_FULL_VERSION='11.7' diff --git a/package/12.4-3.12.env b/package/12.4-3.12.env new file mode 100644 index 0000000..806b28e --- /dev/null +++ b/package/12.4-3.12.env @@ -0,0 +1,4 @@ +ALPINE_VERSION='3.12' +PG_LATEST=false +PG_BASE_VERSION='12' +PG_FULL_VERSION='12.4' diff --git a/package/9.6.13-3.6.env b/package/9.6.13-3.6.env new file mode 100644 index 0000000..9ee2ce9 --- /dev/null +++ b/package/9.6.13-3.6.env @@ -0,0 +1,3 @@ +ALPINE_VERSION='3.6' +PG_BASE_VERSION='9' +PG_FULL_VERSION='9.6.13'