Merge pull request #132 from pvizeli/mariadb

Mariadb
This commit is contained in:
Pascal Vizeli
2017-06-27 22:53:59 +02:00
committed by GitHub
3 changed files with 113 additions and 0 deletions

13
mariadb/Dockerfile Normal file
View File

@@ -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" ]

31
mariadb/config.json Normal file
View File

@@ -0,0 +1,31 @@
{
"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",
"ports": {
"3306/tcp": 3306
},
"options": {
"databases": ["homeassistant"],
"logins": [
{"username": "hass", "host": "localhost", "password": null}
],
"rights": [
{"username": "hass", "host": "localhost", "database": "homeassistant", "grant": "ALL PRIVILEGES ON"}
]
},
"schema": {
"databases": ["str"],
"logins": [
{"username": "str", "host": "str", "password": "str"}
],
"rights": [
{"username": "str", "host": "str", "database": "str", "grant": "str"}
]
},
"image": "homeassistant/{arch}-addon-mariadb"
}

69
mariadb/run.sh Normal file
View File

@@ -0,0 +1,69 @@
#!/bin/bash
set -e
CONFIG_PATH=/data/options.json
MARIADB_DATA=/data/databases
DATABASES=$(jq --raw-output ".databases[]" $CONFIG_PATH)
LOGINS=$(jq --raw-output '.logins | length' $CONFIG_PATH)
RIGHTS=$(jq --raw-output '.rights | length' $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
echo "[INFO] Init custom database"
for line in $DATABASES; do
echo "[INFO] Create database $line"
mysql -e "CREATE DATABASE $line;" 2> /dev/null || true
done
# Init logins
echo "[INFO] Init/Update users"
for (( i=0; i < "$LOGINS"; i++ )); do
USERNAME=$(jq --raw-output ".logins[$i].username" $CONFIG_PATH)
PASSWORD=$(jq --raw-output ".logins[$i].password" $CONFIG_PATH)
HOST=$(jq --raw-output ".logins[$i].host" $CONFIG_PATH)
if mysql -e "SET PASSWORD FOR '$USERNAME'@'$HOST' = PASSWORD('$PASSWORD');" 2> /dev/null; then
echo "[INFO] Update user $USERNAME"
else
echo "[INFO] Create user $USERNAME"
mysql -e "CREATE USER '$USERNAME'@'$HOST' IDENTIFIED BY '$PASSWORD';" 2> /dev/null || true
fi
done
# Init rights
echo "[INFO] Init/Update rights"
for (( i=0; i < "$RIGHTS"; i++ )); do
USERNAME=$(jq --raw-output ".rights[$i].username" $CONFIG_PATH)
HOST=$(jq --raw-output ".rights[$i].host" $CONFIG_PATH)
DATABASE=$(jq --raw-output ".rights[$i].database" $CONFIG_PATH)
GRANT=$(jq --raw-output ".rights[$i].grant" $CONFIG_PATH)
echo "[INFO] Alter rights for $USERNAME@$HOST - $DATABASE"
mysql -e "GRANT $GRANT $DATABASE.* TO '$USERNAME'@'$HOST';" 2> /dev/null || true
done
# Register stop
function stop_mariadb() {
mysqladmin shutdown
}
trap "stop_mariadb" SIGTERM SIGHUP
wait "$MARIADB_PID"