From 06432696a5e82b147089a43714f09bce68c65b1d Mon Sep 17 00:00:00 2001 From: Anthony Thomas Date: Thu, 28 Mar 2024 10:01:42 -0400 Subject: [PATCH] Add image we can use through k8s/Argo to set up the database (#73) * create a database-builder image * add rootless option to db/setup.sh * require a specific debug flag to print db vals --- .github/workflows/build-db-latest.yml | 45 +++++++++++++++++++++++++++ db/Dockerfile | 5 +++ db/setup.sh | 29 ++++++++++------- docker-compose.yml | 6 ++-- 4 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/build-db-latest.yml create mode 100644 db/Dockerfile diff --git a/.github/workflows/build-db-latest.yml b/.github/workflows/build-db-latest.yml new file mode 100644 index 00000000..340ad8ed --- /dev/null +++ b/.github/workflows/build-db-latest.yml @@ -0,0 +1,45 @@ +--- +name: Build Lauth database builder +#description: Builds an image which can initialize the lauth database + +on: + workflow_dispatch: + push: + branches: [ main ] + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Checkout + uses: actions/checkout@v4 + + - name: Build image and push to GitHub Container Registry + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: 'ghcr.io/mlibrary/lauth/db-builder:${{ github.sha }}, ghcr.io/mlibrary/lauth/db-builder:latest' + file: db/Dockerfile + platforms: linux/amd64, linux/arm64 + + - name: Image digest + run: | + echo '${{ steps.docker_build.outputs.digest }}' + diff --git a/db/Dockerfile b/db/Dockerfile new file mode 100644 index 00000000..0817b303 --- /dev/null +++ b/db/Dockerfile @@ -0,0 +1,5 @@ +FROM bitnami/mariadb + +COPY ./db /sql + +EXPOSE 3306 diff --git a/db/setup.sh b/db/setup.sh index 83631ca0..e70fd35f 100755 --- a/db/setup.sh +++ b/db/setup.sh @@ -2,7 +2,7 @@ directory=$(dirname "$0") -while getopts u:p:h:P:a option +while getopts u:p:h:P:arb option do case "${option}" in @@ -11,6 +11,8 @@ do h)h=${OPTARG};; P)P=${OPTARG};; a)all="true";; + r)root="false";; + b)debug="true";; esac done @@ -23,18 +25,23 @@ password="${p:=}" host="${h:=localhost}" port="${P:=3306}" -echo "directory : $directory" -echo " user : $user" -echo " password : $password" -echo " host : $host" -echo " port : $port" -echo " database : $database" -echo " add data : ${all:=false}" -echo "" +if [[ $debug == "true" ]]; then + echo "directory : $directory" + echo " user : $user" + echo " password : $password" + echo " host : $host" + echo " port : $port" + echo " database : $database" + echo " add data : ${all:=false}" + echo " use root : ${root:=true}" + echo "" +fi -mariadb -u root --host=$host --port=$port -e "CREATE DATABASE ${database} DEFAULT CHARACTER SET utf8" -mariadb -u root --host=$host --port=$port -e "GRANT ALL ON ${database}.* TO ${user}@'%' IDENTIFIED by '${password}'" +if [[ $root == "true" ]]; then + mariadb -u root --host=$host --port=$port -e "CREATE DATABASE ${database} DEFAULT CHARACTER SET utf8" + mariadb -u root --host=$host --port=$port -e "GRANT ALL ON ${database}.* TO ${user}@'%' IDENTIFIED by '${password}'" +fi mariadb --user=$user --host=$host --port=$port --password=$password $database < "$directory/tables.sql" diff --git a/docker-compose.yml b/docker-compose.yml index 0e80b104..edb887fa 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -173,9 +173,9 @@ services: command: - /bin/bash - "-c" - - "/sql/setup.sh -u lauth -p lauth -h db.lauth.local lauth_development && \ - /sql/setup.sh -u lauth -p lauth -h db.lauth.local lauth_test && \ - /sql/setup.sh -u lauth -p lauth -h db.lauth.local -a lauth_demo" + - "/sql/setup.sh -u lauth -p lauth -h db.lauth.local -b lauth_development && \ + /sql/setup.sh -u lauth -p lauth -h db.lauth.local -b lauth_test && \ + /sql/setup.sh -u lauth -p lauth -h db.lauth.local -a -b lauth_demo" volumes: mariadb_data: