Skip to content

Commit

Permalink
V2 (#22)
Browse files Browse the repository at this point in the history
* Promote the `dump` command to a first class CLI
* Remove unused images
* Migrate to Github Actions

---------

Co-authored-by: Nick Schuch <nick@myschuch.com>
  • Loading branch information
nickschuch and nickschuch authored Sep 13, 2023
1 parent 95e46c4 commit 6952378
Show file tree
Hide file tree
Showing 746 changed files with 244,138 additions and 14,908 deletions.
110 changes: 0 additions & 110 deletions .circleci/config.yml

This file was deleted.

16 changes: 16 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Lint

on:
push

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.21'
- uses: golangci/golangci-lint-action@v3
with:
args: --timeout=5m
42 changes: 42 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Create and publish a Docker image

on:
push:
branches: ['main']

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest

permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- 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: Build and push Docker image
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
14 changes: 14 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Test

on:
push

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.21'
- run: go test ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
/bin
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:1.21 as build

WORKDIR /go/src/github.com/skpr/mtk
COPY . /go/src/github.com/skpr/mtk

ENV CGO_ENABLED=0

RUN go build -o bin/mtk -ldflags='-extldflags "-static"' github.com/skpr/mtk/cmd/mtk

FROM alpine:3.18

COPY --from=build /go/src/github.com/skpr/mtk/bin/mtk /usr/local/bin/mtk

ENTRYPOINT ["/usr/local/bin/mtk"]
49 changes: 14 additions & 35 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,45 +1,24 @@
#!/usr/bin/make -f

IMAGE_REPO_BASE=skpr/mtk
ARCH=amd64
VERSION_TAG=v2-latest
export CGO_ENABLED=0

define build_image
docker build --build-arg ARCH=${ARCH} -t ${IMAGE_REPO_BASE}-${1}:${VERSION_TAG}-${ARCH} ${1}
endef

define test_image
container-structure-test test --image ${IMAGE_REPO_BASE}-${1}:${VERSION_TAG}-${ARCH} --config ${1}/tests.yml
endef

define push_image
docker push ${IMAGE_REPO_BASE}-${1}:${VERSION_TAG}-${ARCH}
endef

define manifest
$(eval IMAGE=${IMAGE_REPO_BASE}-${1}:${VERSION_TAG})
docker manifest create ${IMAGE} --amend ${IMAGE}-arm64 --amend ${IMAGE}-amd64
docker manifest push ${IMAGE}
define build_step
GOOS=$(1) GOARCH=$(2) go build -o bin/mtk-$(1)-$(2) -ldflags='-extldflags "-static"' github.com/skpr/mtk/cmd/mtk
endef

# Builds the project.
build:
$(call build_image,build)
$(call build_image,mysql)
$(call build_image,dump)

test:
$(call test_image,build)
$(call test_image,mysql)
$(call test_image,dump)
$(call build_step,linux,amd64)
$(call build_step,linux,arm64)
$(call build_step,darwin,amd64)
$(call build_step,darwin,arm64)

push:
$(call push_image,build)
$(call push_image,mysql)
$(call push_image,dump)
# Run all lint checking with exit codes for CI.
lint:
golint -set_exit_status `go list ./... | grep -v /vendor/`

manifest:
$(call manifest,build)
$(call manifest,mysql)
$(call manifest,dump)
# Run tests with coverage reporting.
test:
go test -cover ./...

.PHONY: *
70 changes: 40 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@ MySQL Toolkit

Toolkit for exporting, sanitizing and packaging MySQL database.

## Scenario
## The Goal of this Project

The following example scenario showcase multiple _mtk_ tools:
To provide developers with the tools to share MySQL database dumps in a safe and repeatable manner.

* [dump](/dump)
* [build](/build)
## Example

In the following scenario we will be dumping a sanitized version of the database and packaging the image into a container image which can then be consumed by developers using Docker Compose.
The following example scenario will showcase how developers can use mtk to dump and package a MySQL image database.

The benefits for this approach are:
### Dump a Sanitized Database with MTK

* **Safe** - The database are sanitized for safety.
* **Repeatable** - Images can be recreated very quickly given they are packaged as an image.
* **Easy** - Integrates with Docker Compose.
**Configuration File**

### Dump
Prior to packaging, developers should assess the data that is stored within the database and determine what data should be sanitized or dropped.

**View the rules**

The following rules cover some common Drupal 7/8 scenarios were data should be sanitized or dropped.
The following mtk configuration file cover some of common Drupal 7/8 data which should be sanitized or dropped.

```bash
$ cat mtk.yml
Expand Down Expand Up @@ -59,33 +54,48 @@ ignore:

**Dump the database**

The following command will dump a sanitized version of the database using the below environment variables.
Now that you have a configuration file, it is time to dump the database.

The following command will dump a MySQL database using the configuration file created in the previous step.

```bash
$ export MTK_DUMP_CONFIG=mtk.yml
$ export MTK_DUMP_HOSTNAME=127.0.0.1
$ export MTK_DUMP_USERNAME=root
$ export MTK_DUMP_PASSWORD=password
$ export MTK_DUMP_DATABASE=test
$ export MTK_HOSTNAME=127.0.0.1
$ export MTK_USERNAME=root
$ export MTK_PASSWORD=password
$ export MTK_CONFIG=mtk.yml

$ mtk-dump test > db.sql
```

### Build a Database Image using Docker

Next we can build a database image using the database dump created by the step prior.

$ mtk-dump > db.sql
First, create a Dockerfile which will import the sanitized database dump.

In this example we are using MySQL images from our [image repository](https://github.com/skpr/image-mysql).

```dockerfile
FROM docker.io/skpr/mysql:8.x-v3-latest

ADD db.sql /tmp/db.sql
RUN database-import local local local /tmp/db.sql
```

### Build
Next, build the image with Docker.

```bash
$ docker run -it -v $HOME/.docker:/kaniko/.docker \
-v $(pwd):/workspace \
skpr/mtk-build:latest --context=/workspace \
--dockerfile=/Dockerfile \
--single-snapshot \
--verbosity fatal \
--destination=docker.io/my/image:latest \
--destination=docker.io/my/image:$(date +%F)
docker build -t docker.io/my/database:latest .
```

Hooray! You have successfully packaged a sanitized MySQL database image!

### Integrate

The database image can then be integrated into local development workflows using Docker Compose (or similar).

Below is an example of how this can be configured.

```bash
$ cat docker-compose.yml

Expand All @@ -96,5 +106,5 @@ services:

# Services used as part of the local development environment.
mysql:
image: docker.io/my/image:latest
image: docker.io/my/database:latest
```
2 changes: 0 additions & 2 deletions build/Dockerfile

This file was deleted.

21 changes: 0 additions & 21 deletions build/README.md

This file was deleted.

Loading

0 comments on commit 6952378

Please sign in to comment.