Skip to content

Commit

Permalink
Updated to the latest version of scaffold and added code coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Oct 14, 2023
1 parent 079336e commit 81ecfb0
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 568 deletions.
22 changes: 17 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
version: 2
version: 2.1

orbs:
codecov: codecov/[email protected]

jobs:
build:
docker:
Expand All @@ -11,7 +15,9 @@ jobs:
- checkout
- run:
name: Lint scripts
command: shellcheck seed-db.sh
command: |
shfmt -i 2 -ci -s -d seed-db.sh tests/bats/*.bash tests/bats/*.bats
shellcheck seed-db.sh tests/bats/*.bash tests/bats/*.bats
- setup_remote_docker
- run:
name: Create Docker builder and install emulators
Expand All @@ -26,17 +32,23 @@ jobs:
- run:
name: Run Bats tests
command: |
npm --prefix tests/bats ci
echo "${DOCKER_PASS}" | docker login --username "${DOCKER_USER}" --password-stdin
bats tests/bats/data.bats --tap
kcov --include-pattern=.sh,.bash --bash-parse-files-in-dir=. --exclude-pattern=vendor,node_modules,coverage $(pwd)/coverage tests/bats/node_modules/.bin/bats tests/bats
- store_artifacts:
path: coverage
- codecov/upload:
# @see https://github.com/codecov/codecov-circleci-orb/issues/158
xtra_args: "--nonZero -s coverage"
- run:
name: Deploy image
command: |
echo "${DOCKER_PASS}" | docker login --username "${DOCKER_USER}" --password-stdin
echo "${DOCKER_PASS}" | docker login --username "${DOCKER_USER}" --password-stdin
if [ -n "${CIRCLE_TAG}" ]; then
export TAG="${CIRCLE_TAG}"
elif [ "${CIRCLE_BRANCH}" != "main" ]; then
export TAG="$(echo ${CIRCLE_BRANCH} | sed 's/[^a-zA-Z0-9]/-/g')"
fi
fi
DOCKER_BUILDKIT=1 docker buildx build --no-cache --platform "${BUILDX_PLATFORMS}" --tag drevops/mariadb-drupal-data:${TAG:-canary} --push .
workflows:
Expand Down
25 changes: 25 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This is the top-most .editorconfig file; do not search in parent directories.
root = true

# All files.
[*]
end_of_line = LF
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true



[*.{json,lock}]
indent_size = 4

[*.{yml,yaml}]
indent_size = 2

[*.{sh,bash,bats}]
indent_size = 2

[*.xml]
indent_size = 4
2 changes: 1 addition & 1 deletion .github/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ template: |
$CHANGES
**Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...$RESOLVED_VERSION
**Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...$NEXT_MINOR_VERSION
$CONTRIBUTORS
4 changes: 3 additions & 1 deletion .github/workflows/auto-assign-pr-author.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ name: 'Auto Author Assign'

on:
pull_request_target:
types: [opened, reopened]
types:
- opened
- reopened

permissions:
pull-requests: write
Expand Down
32 changes: 0 additions & 32 deletions .github/workflows/draft-release.yml

This file was deleted.

22 changes: 22 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Release

on:
push:
tags:
- '*'
branches:
- main

permissions:
contents: write

jobs:
release-drafter:
permissions:
contents: write
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: release-drafter/release-drafter@v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# To ignore OS temporary files use global .gitignore
# https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files#configuring-ignored-files-for-all-repositories-on-your-computer

.data
.logs
coverage
3 changes: 2 additions & 1 deletion 9999-mariadb-init.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# This file is minimally modified to be easily updatable from the upstream.
# @see https://github.com/uselagoon/lagoon-images/blob/main/images/mariadb/entrypoints/9999-mariadb-init.bash

# LCOV_EXCL_START
set -eo pipefail

# Locations
Expand Down Expand Up @@ -167,3 +167,4 @@ EOF
echo "done, now starting daemon"

fi
# LCOV_EXCL_END
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
# MariaDB Database data container
Allows capturing database data as a Docker layer.
<p align="center">
<a href="" rel="noopener">
<img width=200px height=200px src="https://placehold.jp/000000/ffffff/200x200.png?text=Mariadb+Drupal+Data&css=%7B%22border-radius%22%3A%22%20100px%22%7D" alt="Mariadb Drupal data logo"></a>
</p>

<h1 align="center">MariaDB data container for Drupal with database captured as Docker layers.</h1>

<div align="center">

[![GitHub Issues](https://img.shields.io/github/issues/drevops/mariadb-drupal-data.svg)](https://github.com/drevops/mariadb-drupal-data/issues)
[![GitHub Pull Requests](https://img.shields.io/github/issues-pr/drevops/mariadb-drupal-data.svg)](https://github.com/drevops/mariadb-drupal-data/pulls)
[![CircleCI](https://circleci.com/gh/drevops/mariadb-drupal-data.svg?style=shield)](https://circleci.com/gh/drevops/mariadb-drupal-data)
[![codecov](https://codecov.io/gh/drevops/mariadb-drupal-data/graph/badge.svg?token=JYSIXUF6QX)](https://codecov.io/gh/drevops/mariadb-drupal-data)
![GitHub release (latest by date)](https://img.shields.io/github/v/release/drevops/mariadb-drupal-data)
![LICENSE](https://img.shields.io/github/license/drevops/mariadb-drupal-data)
![Renovate](https://img.shields.io/badge/renovate-enabled-green?logo=renovatebot)

</div>

## How it works

Usually, MariaDB uses data directory specified as a Docker volume that is
mounted onto host: this allows retaining data after container restarts.

The MariaDB image in this project uses custom location `/usr/lib/db-data` (not
The MariaDB image in this project uses custom location `/usr/lib/db-data` (not
a Docker volume) to store expanded database files. These files then can be
captured as a Docker layer and stored as an image to docker registry.
captured as a Docker layer and stored as an image to docker registry.

Image consumers download the image and start containers with instantaneously
Image consumers download the image and start containers with instantaneously
available data (no time-consuming database imports required).

Technically, majority of the functionality is relying on upstream [`amazeeio/mariadb-drupal`](https://github.com/uselagoon/lagoon-images/blob/main/images/mariadb-drupal/10.4.Dockerfile) Docker image.
[Entrypoint script](entrypoint.bash) had to be copied from [upstream script](https://github.com/uselagoon/lagoon-images/blob/main/images/mariadb/entrypoints/9999-mariadb-init.bash) and adjusted to support custom data directory.
Technically, majority of the functionality is relying on upstream [`amazeeio/mariadb-drupal`](https://github.com/uselagoon/lagoon-images/blob/main/images/mariadb-drupal/10.4.Dockerfile) Docker image.
[Entrypoint script](entrypoint.bash) had to be copied from [upstream script](https://github.com/uselagoon/lagoon-images/blob/main/images/mariadb/entrypoints/9999-mariadb-init.bash) and adjusted to support custom data directory.

## Use case

Expand All @@ -29,10 +41,10 @@ Drupal website with a large database.
3. CI process tags and pushes image to the Docker registry.
4. Website developers pull the latest image from the registry and build site locally.
OR
Subsequent CI builds pull the latest image from the registry and build site.
Subsequent CI builds pull the latest image from the registry and build site.

When required, website developers restart docker stack locally with an already
imported database, which saves a significant amount of time for database
When required, website developers restart docker stack locally with an already
imported database, which saves a significant amount of time for database
imports.

## Seeding image with your database
Expand All @@ -51,7 +63,7 @@ In some cases, shell may report platform incorrectly. Run with forced platform:
### Running tests

bats tests/bats/data.bats --tap
# or
# or
DOCKER_DEFAULT_PLATFORM=linux/amd64 bats --tap tests/bats/data.bats

### Publishing
Expand Down
24 changes: 12 additions & 12 deletions seed-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ note() { printf " %s\n" "$1"; }
[ "${BASE_IMAGE##*/}" = "$BASE_IMAGE" ] && fail "${BASE_IMAGE} should be in a format myorg/myimage." && exit 1
[ "${DST_IMAGE##*/}" = "$DST_IMAGE" ] && fail "${DST_IMAGE} should be in a format myorg/myimage." && exit 1

log_container(){
log_container() {
mkdir -p "${LOG_DIR}" >/dev/null
docker logs "${1}" >> "${LOG_DIR}/${2:-}${1}.log" 2>&1
docker logs "${1}" >>"${LOG_DIR}/${2:-}${1}.log" 2>&1
}

wait_for_db_service(){
wait_for_db_service() {
echo -n " Waiting for the service to become ready."
docker exec --user 1000 -i "${1}" sh -c "until nc -z localhost 3306; do sleep 1; echo -n .; done; echo"
log_container "${cid}"
pass "MYSQL is running."
}

assert_db_system_tables_present(){
assert_db_system_tables_present() {
if docker exec --user 1000 "${1}" /usr/bin/mysql -e "show tables from information_schema;" | grep -q user_variables; then
pass "Database system tables present."
else
Expand All @@ -81,7 +81,7 @@ assert_db_system_tables_present(){
fi
}

assert_db_was_imported(){
assert_db_was_imported() {
if docker exec --user 1000 "${1}" /usr/bin/mysql -e "show tables;" | grep -q users; then
pass "Imported database exists."
else
Expand All @@ -90,35 +90,35 @@ assert_db_was_imported(){
fi
}

start_container(){
start_container() {
task "Start container from the image ${1}"
cid=$(docker run -d --rm "${1}" 2>"$LOG_DIR"/container-start.log)
cat "${LOG_DIR}"/container-start.log >> "$LOG_DIR/${cid}.log" && rm "${LOG_DIR}"/container-start.log || true
cat "${LOG_DIR}"/container-start.log >>"$LOG_DIR/${cid}.log" && rm "${LOG_DIR}"/container-start.log || true
pass "Started container ${cid}"
wait_for_db_service "${cid}"
assert_db_system_tables_present "${cid}"
}

get_started_container_id(){
get_started_container_id() {
docker ps -q --filter ancestor="${1}" --filter status=running | head -n 1
}

stop_container(){
stop_container() {
task "Stop and removing container ${1}"
# Log container output before stopping it into a separate log file for debugging.
log_container "${1}" "stopped-"
docker stop "${1}" > /dev/null
docker stop "${1}" >/dev/null
pass "Stopped and removed container ${1}"
}

# ------------------------------------------------------------------------------

info "Started database seeding."

rm -Rf "${LOG_DIR}" > /dev/null
rm -Rf "${LOG_DIR}" >/dev/null
mkdir -p "${LOG_DIR}" >/dev/null

rm -Rf "${TMP_DATA_DIR}" > /dev/null
rm -Rf "${TMP_DATA_DIR}" >/dev/null
mkdir -p "${TMP_DATA_DIR}" >/dev/null

if [ "$(uname -m)" = "arm64" ]; then
Expand Down
2 changes: 2 additions & 0 deletions tests/bats/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/coverage
Loading

0 comments on commit 81ecfb0

Please sign in to comment.