diff --git a/scripts/bin/comm-dev b/scripts/bin/comm-dev new file mode 100755 index 000000000..e3bf1f24a --- /dev/null +++ b/scripts/bin/comm-dev @@ -0,0 +1,7 @@ +#! /usr/bin/env bash + +# This is a command entrypoint so that the majority of the bash +# code can be linted by shellcheck + +PRJ_ROOT=$(git rev-parse --show-toplevel) +"$PRJ_ROOT"/scripts/comm-dev.sh $@ diff --git a/scripts/comm-dev.sh b/scripts/comm-dev.sh new file mode 100755 index 000000000..e2cb8c396 --- /dev/null +++ b/scripts/comm-dev.sh @@ -0,0 +1,69 @@ +#! /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 "" + + exit 1 +} + +services_usage() { + echo "Comm Development Services" + echo "" + echo "Commands:" + echo " restart - restart services" + echo " start - start rabbitmq" + echo " stop - stop rabbitmq" + echo "" + + exit 1 +} + +services_command() { + case "$1" in + restart) + "$0" services stop || true + "$0" services start + ;; + start) + nix run "$COMM_ROOT"#rabbitmq-up + ;; + stop) + log "Stopping services" + pkill rabbitmq-server beam.smp + ;; + *) + log "$(basename "$0"): unknown services option '$1'" + services_usage + exit 1 + ;; + esac +} + +case "$1" in + -h|--help) + usage + ;; + services) + shift + services_command "$@" + ;; + *) + log "$(basename "$0"): unknown option '$1'" + usage + ;; +esac diff --git a/scripts/source_development_defaults.sh b/scripts/source_development_defaults.sh index b7bf915dc..30406a173 100644 --- a/scripts/source_development_defaults.sh +++ b/scripts/source_development_defaults.sh @@ -1,48 +1,52 @@ #!/usr/bin/env bash if [[ "$OSTYPE" == 'linux'* ]]; then export MYSQL_UNIX_PORT="${XDG_RUNTIME_DIR:-/run/user/$UID}/mysql.sock" export ANDROID_HOME="${ANDROID_HOME:-$HOME/Android/Sdk}" fi if [[ "$OSTYPE" == 'darwin'* ]]; then MARIADB_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/MariaDB" export MYSQL_UNIX_PORT="$MARIADB_DIR"/mysql.sock export ANDROID_HOME="${ANDROID_HOME:-$HOME/Library/Android/sdk}" fi # User defaults of # https://www.rabbitmq.com/configure.html#supported-environment-variables export RABBITMQ_NODENAME=comm export RABBITMQ_DEFAULT_PASS=comm export RABBITMQ_DEFAULT_USER=comm export RABBITMQ_HOME=${XDG_DATA_HOME:-$HOME/.local/share}/RabbitMQ export RABBITMQ_MNESIA_BASE=${RABBITMQ_HOME}/mnesia export RABBITMQ_LOG_BASE=${RABBITMQ_HOME}/logs export RABBITMQ_LOGS=${RABBITMQ_LOG_BASE}/comm.log export RABBITMQ_PLUGINS_EXPAND_DIR=${RABBITMQ_HOME}/plugins_expand export RABBITMQ_PID_FILE=${RABBITMQ_HOME}/rabbitmq.pid export PATH="$PATH":"$ANDROID_HOME"/emulator:"$ANDROID_HOME"/tools export PATH="$PATH":"$ANDROID_HOME"/tools/bin:"$ANDROID_HOME"/platform-tools # ANDROID_SDK_ROOT is deprecated, but it's still used by some tooling # such as sdkmanager. ANDROID_HOME is the new prefered env var. export ANDROID_SDK_ROOT="${ANDROID_SDK_ROOT:-$ANDROID_HOME}" export PATH="$PATH":./node_modules/.bin +# Development helpers +PRJ_ROOT="$(git rev-parse --show-toplevel)" +export PATH="$PATH":${PRJ_ROOT}/scripts/bin + # mysql2 package wants stable prefixes for temporary directory paths # 'nix develop' will set TMP and related variables to something different each # invocation export TMP=/tmp/app.comm export TEMP="$TMP" export TMPDIR="$TMP" export TEMPDIR="$TMP" mkdir -p "$TMP" # For cargo + rustup applications, ensure cargo user bin directory is on path if [[ ! "$PATH" =~ \.cargo/bin ]]; then export PATH="$PATH":${HOME}/.cargo/bin fi diff --git a/scripts/start_comm_daemon.sh b/scripts/start_comm_daemon.sh index 928144373..c5c1a5dd5 100755 --- a/scripts/start_comm_daemon.sh +++ b/scripts/start_comm_daemon.sh @@ -1,38 +1,39 @@ #!/usr/bin/env bash service_command="$1" # The command used to start the service (e.g. mariadbd) service_name="$2" # The official name of service (e.g. MariaDB) entrypoint="$3" # Command or script used to initiate service pidfile="$4" # Location of PID file # Check if service was already started set +e # allow for pgrep to not find matches # BSD pgrep doesn't have a "count" feature, use wc then trim whitespace service_count=$(pgrep "$service_command" | wc -l | xargs echo) set -euo pipefail if [[ "$service_count" -eq "0" ]]; then echo "Starting $service_name" # No service present, start our own # Launch in subshell so if the original terminal is closed, the process # will be inherited instead of also being forced closed + mkdir -p "$(dirname "${pidfile}")" ($entrypoint & echo "$!" > "$pidfile") elif [[ "$service_count" -eq "1" ]]; then # Check if it was started by this script running_pid="$(pgrep "$service_command")" if [[ ! -f "$pidfile" ]] || \ [[ "$(cat "$pidfile")" != "$running_pid" ]]; then echo "Existing $service_name instance found outside of nix environment" >&2 echo "Please stop existing services and attempt 'nix develop' again" >&2 exit 1 fi else echo "Many $service_name instances found outside of nix environment" >&2 echo "Please stop existing services and attempt 'nix develop' again" >&2 exit 1 fi