Skip to content

Commit

Permalink
Update mariadb add-on to use Bashio home-assistant#727 (home-assistan…
Browse files Browse the repository at this point in the history
…t#744)

* Update mariadb add-on to use Bashio home-assistant#727

* Update log entries and loops

* Update change log and version

* Fixing database setup and some wording updates

* Database check replaced with bashio based check
  • Loading branch information
Zapfmeister authored and frenck committed Oct 7, 2019
1 parent c59cbcd commit c7a017d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
3 changes: 3 additions & 0 deletions mariadb/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.3
- Update from bash to bashio

## 1.2
- Change the way to migrate data

Expand Down
2 changes: 1 addition & 1 deletion mariadb/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "MariaDB",
"version": "1.2",
"version": "1.3",
"slug": "mariadb",
"description": "An SQL database server",
"url": "https://home-assistant.io/addons/mariadb/",
Expand Down
51 changes: 23 additions & 28 deletions mariadb/run.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
#!/bin/bash
#!/usr/bin/env bashio
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"
if ! bashio::fs.directory_exists "${MARIADB_DATA}"; then
bashio::log.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"
bashio::log.info "Using existing mariadb initial system"
fi

# Start mariadb
echo "[INFO] Start MariaDB"
bashio::log.info "Starting MariaDB"
mysqld_safe --datadir="$MARIADB_DATA" --user=root --skip-log-bin < /dev/null &
MARIADB_PID=$!

Expand All @@ -26,42 +21,42 @@ while ! mysql -e "" 2> /dev/null; do
sleep 1
done

echo "[INFO] Check data integrity and fix corruptions"
bashio::log.info "Check data integrity and fix corruptions"
mysqlcheck --no-defaults --check-upgrade --auto-repair --databases mysql --skip-write-binlog > /dev/null || true
mysqlcheck --no-defaults --all-databases --fix-db-names --fix-table-names --skip-write-binlog > /dev/null || true
mysqlcheck --no-defaults --check-upgrade --all-databases --auto-repair --skip-write-binlog > /dev/null || true

# Init databases
echo "[INFO] Init custom database"
for line in $DATABASES; do
echo "[INFO] Create database $line"
bashio::log.info "Init custom database"
for line in $(bashio::config "databases"); do
bashio::log.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)
bashio::log.info "Init/Update users"
for login in $(bashio::config "logins|keys"); do
USERNAME=$(bashio::config "logins[${login}].username")
PASSWORD=$(bashio::config "logins[${login}].password")
HOST=$(bashio::config "logins[${login}].host")

if mysql -e "SET PASSWORD FOR '$USERNAME'@'$HOST' = PASSWORD('$PASSWORD');" 2> /dev/null; then
echo "[INFO] Update user $USERNAME"
bashio::log.info "Update user $USERNAME"
else
echo "[INFO] Create user $USERNAME"
bashio::log.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)
bashio::log.info "Init/Update rights"
for right in $(bashio::config "rights|keys"); do
USERNAME=$(bashio::config "rights[${right}].username")
HOST=$(bashio::config "rights[${right}].host")
DATABASE=$(bashio::config "rights[${right}].database")
GRANT=$(bashio::config "rights[${right}].grant")

echo "[INFO] Alter rights for $USERNAME@$HOST - $DATABASE"
bashio::log.info "Alter rights for $USERNAME@$HOST - $DATABASE"
mysql -e "GRANT $GRANT $DATABASE.* TO '$USERNAME'@'$HOST';" 2> /dev/null || true
done

Expand Down

0 comments on commit c7a017d

Please sign in to comment.