diff --git a/nix/mariadb-up-mac.nix b/nix/mariadb-up-mac.nix index d513a4807..8d1b0e56e 100644 --- a/nix/mariadb-up-mac.nix +++ b/nix/mariadb-up-mac.nix @@ -1,126 +1,128 @@ { lib , gnused , openssl , mariadb , writeShellApplication , writeTextFile }: let # Use small script executed by bash to have a normal shell environment. mariadb-entrypoint = writeShellApplication { name = "mariadb-init"; text = '' MARIADB_DIR=''${XDG_DATA_HOME:-$HOME/.local/share}/MariaDB echo "View MariaDB logs: tail -f $MARIADB_DIR/logs" >&2 echo "Kill MariaDB server: pkill mariadbd" >&2 # Explicitly close fd3 to prevent `direnv` from hanging # (https://linear.app/comm/issue/ENG-3254/remove-wait-logic-in-nix-develop) exec 3>&- # 'exec' allows for us to replace bash process with MariaDB exec "${mariadb}/bin/mariadbd" \ --socket "$MARIADB_DIR"/mysql.sock \ --datadir "$MARIADB_DIR" \ + --innodb-ft-min-token-size=1 \ + --innodb-ft-enable-stopword=0 \ &> "$MARIADB_DIR"/logs ''; }; mariadb-version = let versions = lib.versions; in "${versions.major mariadb.version}.${versions.minor mariadb.version}"; # Small boiler-plate text file for us to write for keyserver db_config_template = writeTextFile { name = "db-config"; text = '' { "host": "localhost", "user": "comm", "password": "PASS", "database": "comm", "dbType": "mariadb${mariadb-version}" } ''; }; # writeShellApplication is a "writer helper" which # will create a shellchecked executable shell script located in $out/bin/ # This shell script will be used to allow for impure+stateful actions in writeShellApplication { name = "mariadb-up"; text = '' # "$HOME/Library/Application Support/" is the canonical path to use # on darwin for storing user data for installed applications. # However, mysql and mariadb don't quote paths in the mariadbd script, # so use XDG conventions and hope $HOME doesn't have a space. MARIADB_DATA_HOME="''${XDG_DATA_HOME:-$HOME/.local/share}/MariaDB" MARIADB_PIDFILE="$MARIADB_DATA_HOME"/mariadb.pid export MYSQL_UNIX_PORT="$MARIADB_DATA_HOME"/mysql.sock if [[ ! -d "$MARIADB_DATA_HOME"/mysql ]]; then # mysql directory should exist if MariaDB has been initialized echo "Initializing MariaDB database at $MARIADB_DATA_HOME" >&2 "${lib.getBin mariadb}/bin/mariadb-install-db" \ --datadir="$MARIADB_DATA_HOME" \ --auth-root-authentication-method=socket fi "${../scripts/start_comm_daemon.sh}" \ mariadbd \ MariaDB \ "${mariadb-entrypoint}/bin/mariadb-init" \ "$MARIADB_PIDFILE" if [[ ! -S "$MYSQL_UNIX_PORT" ]]; then echo "Waiting for MariaDB to come up" while [[ ! -S "$MYSQL_UNIX_PORT" ]]; do sleep 1; done fi # Assume this was run from git repository PRJ_ROOT=$(git rev-parse --show-toplevel) KEYSERVER_DB_CONFIG="$PRJ_ROOT"/keyserver/secrets/db_config.json # Check if database exists commDBCount=$("${lib.getBin mariadb}/bin/mariadb" -u "$USER" \ -Bse "SELECT COUNT(1) FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'comm';" ) if [[ "$commDBCount" -eq 0 ]]; then "${lib.getBin mariadb}/bin/mariadb" -u "$USER" \ -Bse "CREATE DATABASE comm" fi # Initialize comm user, database, and secrets file for MariaDB # Connecting through socket doesn't require a password userCount=$("${lib.getBin mariadb}/bin/mariadb" -u "$USER" \ -Bse "SELECT COUNT(1) FROM mysql.user WHERE user = 'comm';" ) if [[ "$userCount" -eq 0 ]]; then echo "Creating comm user" >&2 "${lib.getBin mariadb}/bin/mariadb" -u "$USER" \ -Bse "CREATE USER comm@localhost; GRANT ALL ON "'comm.*'" TO comm@localhost;" fi if [[ ! -f "$KEYSERVER_DB_CONFIG" ]]; then echo "Writing connection information to $KEYSERVER_DB_CONFIG" >&2 mkdir -p "$(dirname "$KEYSERVER_DB_CONFIG")" PASS=$("${lib.getBin openssl}/bin/openssl" rand -hex 6) "${lib.getBin mariadb}/bin/mariadb" -u "$USER" \ -Bse "ALTER USER comm@localhost IDENTIFIED BY '$PASS'" # It's very difficult to write json from bash, just copy a nix # file then use sed to subsitute cp "${db_config_template}" "$KEYSERVER_DB_CONFIG" chmod +w "$KEYSERVER_DB_CONFIG" # Nix files are read-only "${gnused}/bin/sed" -i -e "s|PASS|$PASS|g" "$KEYSERVER_DB_CONFIG" fi # Explicitly exit this script so the parent shell can determine # when it's safe to return control of terminal to user exit 0 ''; } diff --git a/scripts/comm-dev.sh b/scripts/comm-dev.sh index f2cd3593b..9197d0d80 100755 --- a/scripts/comm-dev.sh +++ b/scripts/comm-dev.sh @@ -1,71 +1,100 @@ #! /usr/bin/env bash # This is an entry for common development workflows like starting and stopping # expensive services set -euo pipefail COMM_ROOT="$(git rev-parse --show-toplevel)" log() { echo "$@" >&2 } usage() { echo "Comm Development" echo "" echo "Commands:" echo " services - start or stop development services" + echo " db - restart MariaDB server" echo "" exit 1 } services_usage() { echo "Comm Development Services" echo "" echo "Commands:" echo " restart - restart services" echo " start - start localstack and rabbitmq" echo " stop - stop localstack and rabbitmq" echo "" exit 1 } services_command() { case "$1" in restart) "$0" services stop || true "$0" services start ;; start) nix run "$COMM_ROOT"#rabbitmq-up nix run "$COMM_ROOT"#localstack-up ;; stop) log "Stopping services" nix run "$COMM_ROOT"#localstack-down pkill rabbitmq-server beam.smp ;; *) log "$(basename "$0"): unknown services option '$1'" services_usage exit 1 ;; esac } +db_usage() { + echo "Comm MariaDB Server" + echo "" + echo "Commands:" + echo " restart - restart MariaDB server" + echo "" + + exit 1 +} + +db_command() { + case "$1" in + restart) + pkill mariadbd + nix run .#mariadb-up + ;; + *) + log "$(basename "$0"): unknown db option '$1'" + db_usage + exit 1 + ;; + esac +} + case "$1" in -h|--help) usage ;; services) shift services_command "$@" ;; + db) + shift + db_command "$@" + ;; *) log "$(basename "$0"): unknown option '$1'" usage ;; esac