| 1 |
#!/usr/bin/env sh |
| 2 |
# |
| 3 |
# Onionprobe standalone monitoring node manager |
| 4 |
# |
| 5 |
# Copyright (C) 2024 The Tor Project, Inc. |
| 6 |
# |
| 7 |
# This program is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published |
| 9 |
# by the Free Software Foundation, either version 3 of the License, |
| 10 |
# or any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
# Parameters |
| 21 |
BASENAME="`basename $0`" |
| 22 |
DIRNAME="`dirname $0`" |
| 23 |
ACTION="$1" |
| 24 |
BASEPATH="$DIRNAME" |
| 25 |
CONTAINER_RUNTIME="${CONTAINER_RUNTIME:-docker}" |
| 26 |
COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-onionprobe}" |
| 27 |
|
| 28 |
# Load the environment file if available |
| 29 |
if [ -f "$BASEPATH/.env" ]; then |
| 30 |
. $BASEPATH/.env |
| 31 |
fi |
| 32 |
|
| 33 |
# We need to be explicity on the project name since Podman Compose |
| 34 |
# does not honor this paramenter on .env files, as of 2024-10-15: |
| 35 |
# https://github.com/containers/podman-compose/issues/475 |
| 36 |
COMPOSE_CMD="${CONTAINER_RUNTIME}-compose -p ${COMPOSE_PROJECT_NAME}" |
| 37 |
|
| 38 |
# Status |
| 39 |
onionprobe_monitor_ps() { |
| 40 |
${COMPOSE_CMD} ps $* |
| 41 |
} |
| 42 |
|
| 43 |
# Watch services |
| 44 |
onionprobe_monitor_watch() { |
| 45 |
watch ${COMPOSE_CMD} ps $* |
| 46 |
} |
| 47 |
|
| 48 |
# Pull images |
| 49 |
onionprobe_monitor_pull() { |
| 50 |
${COMPOSE_CMD} pull $* |
| 51 |
} |
| 52 |
|
| 53 |
# Build services |
| 54 |
onionprobe_monitor_build() { |
| 55 |
${COMPOSE_CMD} build |
| 56 |
} |
| 57 |
|
| 58 |
# Bring services up |
| 59 |
onionprobe_monitor_up() { |
| 60 |
onionprobe_monitor_pull --quiet |
| 61 |
onionprobe_monitor_build |
| 62 |
${COMPOSE_CMD} up -d --remove-orphans |
| 63 |
} |
| 64 |
|
| 65 |
# Alias for "up" |
| 66 |
onionprobe_monitor_run() { |
| 67 |
onionprobe_monitor_up $* |
| 68 |
} |
| 69 |
|
| 70 |
# Alias for "up" |
| 71 |
onionprobe_monitor_start() { |
| 72 |
onionprobe_monitor_up $* |
| 73 |
} |
| 74 |
|
| 75 |
# Bring services up |
| 76 |
onionprobe_monitor_down() { |
| 77 |
${COMPOSE_CMD} down $* |
| 78 |
} |
| 79 |
|
| 80 |
# Stop services |
| 81 |
onionprobe_monitor_stop() { |
| 82 |
${COMPOSE_CMD} stop $* |
| 83 |
} |
| 84 |
|
| 85 |
# Restart services |
| 86 |
onionprobe_monitor_restart() { |
| 87 |
onionprobe_monitor_stop $* |
| 88 |
onionprobe_monitor_up |
| 89 |
} |
| 90 |
|
| 91 |
# Reload services |
| 92 |
onionprobe_monitor_reload() { |
| 93 |
onionprobe_monitor_up |
| 94 |
} |
| 95 |
|
| 96 |
# Show service log |
| 97 |
onionprobe_monitor_logs() { |
| 98 |
${COMPOSE_CMD} logs -f |
| 99 |
} |
| 100 |
|
| 101 |
# Get the hostname the existing services |
| 102 |
onionprobe_monitor_hostnames() { |
| 103 |
for service in prometheus alertmanager grafana onionprobe; do |
| 104 |
echo -n "${service}: " |
| 105 |
|
| 106 |
${CONTAINER_RUNTIME} exec -ti ${COMPOSE_PROJECT_NAME}_tor_1 \ |
| 107 |
cat /var/lib/tor/${service}/hostname |
| 108 |
done |
| 109 |
} |
| 110 |
|
| 111 |
# Generate restricted discovery (aka client authorization) keys |
| 112 |
onionprobe_monitor_genkeys() { |
| 113 |
${CONTAINER_RUNTIME} exec -ti ${COMPOSE_PROJECT_NAME}_tor_1 \ |
| 114 |
/usr/local/bin/generate-auth-keys-for-all-onion-services $* |
| 115 |
} |
| 116 |
|
| 117 |
# Show restricted discovery (aka client authorization) keys |
| 118 |
onionprobe_monitor_showkeys() { |
| 119 |
local user="${1:-admin}" |
| 120 |
|
| 121 |
for service in prometheus alertmanager grafana onionprobe; do |
| 122 |
echo -n "${user}@${service}: " |
| 123 |
|
| 124 |
${CONTAINER_RUNTIME} exec -ti ${COMPOSE_PROJECT_NAME}_tor_1 \ |
| 125 |
test -f /var/lib/tor/${service}/authorized_clients/${user}.priv && \ |
| 126 |
${CONTAINER_RUNTIME} exec -ti ${COMPOSE_PROJECT_NAME}_tor_1 \ |
| 127 |
cat /var/lib/tor/${service}/authorized_clients/${user}.priv |
| 128 |
done |
| 129 |
} |
| 130 |
|
| 131 |
# Remove restricted discovery (aka client authorization) keys |
| 132 |
onionprobe_monitor_removekeys() { |
| 133 |
local user="${1:-admin}" |
| 134 |
|
| 135 |
for service in prometheus alertmanager grafana onionprobe; do |
| 136 |
echo -n "${user}@${service}: " |
| 137 |
|
| 138 |
${CONTAINER_RUNTIME} exec -ti ${COMPOSE_PROJECT_NAME}_tor_1 \ |
| 139 |
rm -f /var/lib/tor/${service}/authorized_clients/${user}.* |
| 140 |
done |
| 141 |
} |
| 142 |
|
| 143 |
# Execute a command inside a container |
| 144 |
onionprobe_monitor_exec() { |
| 145 |
local container="${1}" |
| 146 |
local command="${2:-/bin/sh}" |
| 147 |
local instance="${INSTANCE:-1}" |
| 148 |
|
| 149 |
if [ -z "$container" ]; then |
| 150 |
echo "usage: $BASENAME exec <container> <command> [args]" |
| 151 |
exit 1 |
| 152 |
fi |
| 153 |
|
| 154 |
if [ ! -z "$2" ]; then |
| 155 |
shift 2 |
| 156 |
elif [ ! -z "$1" ]; then |
| 157 |
shift |
| 158 |
fi |
| 159 |
|
| 160 |
${CONTAINER_RUNTIME} exec -ti ${COMPOSE_PROJECT_NAME}_${container}_${instance} ${command} $* |
| 161 |
} |
| 162 |
|
| 163 |
# Alias for "exec" |
| 164 |
onionprobe_monitor_shell() { |
| 165 |
onionprobe_monitor_exec $* |
| 166 |
} |
| 167 |
|
| 168 |
# Usage |
| 169 |
onionprobe_monitor_usage() { |
| 170 |
echo "usage: ${BASENAME} <action> [args]" |
| 171 |
echo "" |
| 172 |
echo "available actions:" |
| 173 |
echo "" |
| 174 |
grep "^onionprobe_monitor_" $0 | cut -d ' ' -f 1 | \ |
| 175 |
sed -e 's/onionprobe_monitor_//' -e 's/()//' | \ |
| 176 |
sort | xargs -L 6 | column -t -c 6 | sed -e 's/^/\t/' |
| 177 |
echo "" |
| 178 |
echo "Check documentation for details: https://onionservices.torproject.org/apps/web/onionprobe/standalone/" |
| 179 |
} |
| 180 |
|
| 181 |
# Dispatch |
| 182 |
if type onionprobe_monitor_${ACTION} 2> /dev/null | grep -q "onionprobe_monitor_$ACTION "; then |
| 183 |
shift |
| 184 |
onionprobe_monitor_${ACTION} $* |
| 185 |
else |
| 186 |
onionprobe_monitor_usage |
| 187 |
fi |