Skip to content

Commit

Permalink
Add image we can use through k8s/Argo to set up the database (#73)
Browse files Browse the repository at this point in the history
* create a database-builder image

* add rootless option to db/setup.sh

* require a specific debug flag to print db vals
  • Loading branch information
antmoth authored Mar 28, 2024
1 parent 25ce33f commit 0643269
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 14 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/build-db-latest.yml
Original file line number Diff line number Diff line change
@@ -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 }}'
5 changes: 5 additions & 0 deletions db/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM bitnami/mariadb

COPY ./db /sql

EXPOSE 3306
29 changes: 18 additions & 11 deletions db/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -11,6 +11,8 @@ do
h)h=${OPTARG};;
P)P=${OPTARG};;
a)all="true";;
r)root="false";;
b)debug="true";;
esac
done

Expand All @@ -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"

Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 0643269

Please sign in to comment.