diff --git a/mariadb/Dockerfile b/mariadb/Dockerfile new file mode 100644 index 0000000..712a98f --- /dev/null +++ b/mariadb/Dockerfile @@ -0,0 +1,13 @@ +FROM %%BASE_IMAGE%% + +# Add env +ENV LANG C.UTF-8 + +# Setup base +RUN apk add --no-cache jq mariadb mariadb-client + +# Copy data +COPY run.sh / +RUN chmod a+x /run.sh + +CMD [ "/run.sh" ] diff --git a/mariadb/config.json b/mariadb/config.json new file mode 100644 index 0000000..96eefb9 --- /dev/null +++ b/mariadb/config.json @@ -0,0 +1,19 @@ +{ + "name": "MariaDB", + "version": "0.1", + "slug": "mariadb", + "description": "MariaDB Server is one of the most popular database servers in the world.", + "url": "https://home-assistant.io/addons/mariadb/", + "startup": "before", + "boot": "auto", + "network": { + "tcp/3306": 3306 + }, + "options": { + "databases": ["homeassistant"] + }, + "schema": { + "databases": ["str"] + }, + "image": "homeassistant/{arch}-addon-mariadb" +} diff --git a/mariadb/run.sh b/mariadb/run.sh new file mode 100644 index 0000000..53390c0 --- /dev/null +++ b/mariadb/run.sh @@ -0,0 +1,32 @@ +#!/bin/bash +set -e + +CONFIG_PATH=/data/options.json +MARIADB_DATA=/data/databases + +DATABASES=$(jq --raw-output ".databases[]" $CONFIG_PATH) + +# Init mariadb +if [ ! -d "$MARIADB_DATA" ]; then + echo "[INFO] Create a new mariadb initial system" + mysql_install_db --user=root --datadir="$MARIADB_DATA" > /dev/null +else + echo "[INFO] Use exists mariadb initial system" +fi + +# Start mariadb +echo "[INFO] Start MariaDB" +mysqld_safe --datadir="$MARIADB_DATA" --user=root < /dev/null & +MARIADB_PID=$! + +# Wait until DB is running +while ! mysql -e 2> /dev/null; do + sleep 1 +done + +# Init databases +for line in $DATABASES; do + mysql -e "CREATE DATABASE $line;" 2> /dev/null || true +done + +wait "$MARIADB_PID"