| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # |
| 3 | # Matrix E2E Test Environment Setup Script |
| 4 | # |
| 5 | # This script performs all the steps required to set up the Matrix Synapse server |
| 6 | # for end-to-end testing of Shoutrrr's Matrix service. |
| 7 | # |
| 8 | # Usage: |
| 9 | # ./setup.sh [OPTIONS] [COMMAND] |
| 10 | # |
| 11 | # Commands: |
| 12 | # generate-config Generate the Synapse server configuration |
| 13 | # start-server Start the Matrix Synapse server using docker compose |
| 14 | # create-user Create the admin test user |
| 15 | # create-room Create the test room with alias #test:localhost |
| 16 | # setup-all Run all setup steps in order (default) |
| 17 | # |
| 18 | # Options: |
| 19 | # --help, -h Show this help message |
| 20 | # --verbose, -v Enable verbose output |
| 21 | # |
| 22 | # Environment: |
| 23 | # The script automatically loads .env file from its directory if present. |
| 24 | # Required variables for create-user: |
| 25 | # None (uses defaults: admin/admin) |
| 26 | # Required variables for create-room: |
| 27 | # SHOUTRRR_MATRIX_HOST |
| 28 | # SHOUTRRR_MATRIX_USER |
| 29 | # SHOUTRRR_MATRIX_PASSWORD |
| 30 | # SHOUTRRR_MATRIX_ROOM (optional, defaults to #test:localhost) |
| 31 | # SHOUTRRR_MATRIX_DISABLE_TLS (optional) |
| 32 | # |
| 33 | |
| 34 | set -euo pipefail |
| 35 | |
| 36 | # ============================================================================= |
| 37 | # Configuration and Constants |
| 38 | # ============================================================================= |
| 39 | |
| 40 | # Color codes for output |
| 41 | readonly RED='\033[0;31m' |
| 42 | readonly GREEN='\033[0;32m' |
| 43 | readonly YELLOW='\033[1;33m' |
| 44 | readonly BLUE='\033[0;34m' |
| 45 | readonly NC='\033[0m' # No Color |
| 46 | |
| 47 | # Script configuration |
| 48 | SCRIPT_DIR="$(cd "$(dirnamescip-shell system . . dirname. "$0")" && pwdscip-shell system . . pwd.)" |
| 49 | DATA_DIR="${SCRIPT_DIR}/data" |
| 50 | ENV_FILE="${SCRIPT_DIR}/.env" |
| 51 | ELEMENT_ENV_FILE="${SCRIPT_DIR}/element.env" |
| 52 | COMPOSE_FILE="${SCRIPT_DIR}/docker-compose.yaml" |
| 53 | |
| 54 | # Default values |
| 55 | DEFAULT_MATRIX_USER="admin" |
| 56 | DEFAULT_MATRIX_PASSWORD="admin" |
| 57 | DEFAULT_MATRIX_HOST="localhost:8008" |
| 58 | DEFAULT_MATRIX_ROOM="#test:localhost" |
| 59 | |
| 60 | # Rate limiting configuration for E2E testing |
| 61 | RATE_LIMIT_CONFIG='# Rate limiting configuration - effectively disabled for E2E testing |
| 62 | # Using very high values to allow rapid test execution without rate limits. |
| 63 | # See: https://element-hq.github.io/synapse/latest/usage/configuration/config_documentation.html#ratelimiting |
| 64 | rc_message: |
| 65 | per_second: 1000 |
| 66 | burst_count: 10000 |
| 67 | |
| 68 | rc_registration: |
| 69 | per_second: 1000 |
| 70 | burst_count: 10000 |
| 71 | |
| 72 | rc_login: |
| 73 | address: |
| 74 | per_second: 1000 |
| 75 | burst_count: 10000 |
| 76 | account: |
| 77 | per_second: 1000 |
| 78 | burst_count: 10000 |
| 79 | failed_attempts: |
| 80 | per_second: 1000 |
| 81 | burst_count: 10000 |
| 82 | |
| 83 | rc_joins: |
| 84 | local: |
| 85 | per_second: 1000 |
| 86 | burst_count: 10000 |
| 87 | remote: |
| 88 | per_second: 1000 |
| 89 | burst_count: 10000' |
| 90 | |
| 91 | # Verbose mode |
| 92 | VERBOSE=false |
| 93 | |
| 94 | # Docker compose command (detected in check_requirements) |
| 95 | COMPOSE_CMD="" |
| 96 | |
| 97 | # ============================================================================= |
| 98 | # Helper Functions |
| 99 | # ============================================================================= |
| 100 | |
| 101 | # Print an error message and exit |
| 102 | errorscip-shell shell shell-project 0.0.0 error().() { |
| 103 | echoscip-shell system . . echo. -e "${RED}[ERROR]${NC} $1" >&2 |
| 104 | exit 1 |
| 105 | } |
| 106 | |
| 107 | # Print an info message |
| 108 | infoscip-shell shell shell-project 0.0.0 info().() { |
| 109 | echoscip-shell system . . echo. -e "${GREEN}[INFO]${NC} $1" |
| 110 | } |
| 111 | |
| 112 | # Print a warning message |
| 113 | warnscip-shell shell shell-project 0.0.0 warn().() { |
| 114 | echoscip-shell system . . echo. -e "${YELLOW}[WARN]${NC} $1" |
| 115 | } |
| 116 | |
| 117 | # Print a debug message (only in verbose mode) |
| 118 | debugscip-shell shell shell-project 0.0.0 debug().() { |
| 119 | if [[ "$VERBOSE" == "true" ]]; then |
| 120 | echoscip-shell system . . echo. -e "${BLUE}[DEBUG]${NC} $1" |
| 121 | fi |
| 122 | } |
| 123 | |
| 124 | # Print usage information |
| 125 | usagescip-shell shell shell-project 0.0.0 usage().() { |
| 126 | echoscip-shell system . . echo. "Matrix E2E Test Environment Setup Script" |
| 127 | echoscip-shell system . . echo. "" |
| 128 | echoscip-shell system . . echo. "This script performs all the steps required to set up the Matrix Synapse server" |
| 129 | echoscip-shell system . . echo. "for end-to-end testing of Shoutrrr's Matrix service." |
| 130 | echoscip-shell system . . echo. "" |
| 131 | echoscip-shell system . . echo. "Usage:" |
| 132 | echoscip-shell system . . echo. " ./setup.sh [OPTIONS] [COMMAND]" |
| 133 | echoscip-shell system . . echo. "" |
| 134 | echoscip-shell system . . echo. "Commands:" |
| 135 | echoscip-shell system . . echo. " generate-config Generate the Synapse server configuration" |
| 136 | echoscip-shell system . . echo. " start-server Start the Matrix Synapse server using docker compose" |
| 137 | echoscip-shell system . . echo. " create-user Create the admin test user" |
| 138 | echoscip-shell system . . echo. " create-room Create the test room with alias #test:localhost" |
| 139 | echoscip-shell system . . echo. " setup-all Run all setup steps in order (default)" |
| 140 | echoscip-shell system . . echo. "" |
| 141 | echoscip-shell system . . echo. "Options:" |
| 142 | echoscip-shell system . . echo. " --help, -h Show this help message" |
| 143 | echoscip-shell system . . echo. " --verbose, -v Enable verbose output" |
| 144 | echoscip-shell system . . echo. "" |
| 145 | echoscip-shell system . . echo. "Environment:" |
| 146 | echoscip-shell system . . echo. " The script automatically loads .env file from its directory if present." |
| 147 | echoscip-shell system . . echo. " Required variables for create-user:" |
| 148 | echoscip-shell system . . echo. " None (uses defaults: admin/admin)" |
| 149 | echoscip-shell system . . echo. " Required variables for create-room:" |
| 150 | echoscip-shell system . . echo. " SHOUTRRR_MATRIX_HOST" |
| 151 | echoscip-shell system . . echo. " SHOUTRRR_MATRIX_USER" |
| 152 | echoscip-shell system . . echo. " SHOUTRRR_MATRIX_PASSWORD" |
| 153 | echoscip-shell system . . echo. " SHOUTRRR_MATRIX_ROOM (optional, defaults to #test:localhost)" |
| 154 | echoscip-shell system . . echo. " SHOUTRRR_MATRIX_DISABLE_TLS (optional)" |
| 155 | echoscip-shell system . . echo. "" |
| 156 | echoscip-shell system . . echo. "Examples:" |
| 157 | echoscip-shell system . . echo. " ./setup.sh # Run all setup steps" |
| 158 | echoscip-shell system . . echo. " ./setup.sh generate-config # Generate configuration only" |
| 159 | echoscip-shell system . . echo. " ./setup.sh start-server # Start the server only" |
| 160 | echoscip-shell system . . echo. " ./setup.sh --verbose setup-all # Run with verbose output" |
| 161 | } |
| 162 | |
| 163 | # Load environment variables from .env file |
| 164 | load_env_filescip-shell shell shell-project 0.0.0 load_env_file().() { |
| 165 | if [[ -f "$ENV_FILE" ]]; then |
| 166 | debugscip-shell shell shell-project 0.0.0 debug(). "Loading environment variables from ${ENV_FILE}" |
| 167 | set -a |
| 168 | source "$ENV_FILE" |
| 169 | set +a |
| 170 | infoscip-shell shell shell-project 0.0.0 info(). "Loaded environment variables from ${ENV_FILE}" |
| 171 | else |
| 172 | debugscip-shell shell shell-project 0.0.0 debug(). "No .env file found at ${ENV_FILE}" |
| 173 | fi |
| 174 | } |
| 175 | |
| 176 | # Check if required commands are available |
| 177 | check_requirementsscip-shell shell shell-project 0.0.0 check_requirements().() { |
| 178 | local missing_cmds=() |
| 179 | |
| 180 | # Check for docker |
| 181 | if ! command -v docker &> /dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 182 | missing_cmds+=("docker") |
| 183 | fi |
| 184 | |
| 185 | # Check for docker compose (plugin or standalone) |
| 186 | if docker compose version &> /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 187 | COMPOSE_CMD="docker compose" |
| 188 | elif command -v docker-compose &> /dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 189 | COMPOSE_CMD="docker-compose" |
| 190 | else |
| 191 | missing_cmds+=("docker-compose") |
| 192 | fi |
| 193 | |
| 194 | # Check for curl |
| 195 | if ! command -v curl &> /dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 196 | missing_cmds+=("curl") |
| 197 | fi |
| 198 | |
| 199 | # Check for jq |
| 200 | if ! command -v jq &> /dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 201 | missing_cmds+=("jq") |
| 202 | fi |
| 203 | |
| 204 | if [[ ${#missing_cmds[@]} -gt 0 ]]; then |
| 205 | errorscip-shell shell shell-project 0.0.0 error(). "Missing required commands: ${missing_cmds[*]}" |
| 206 | fi |
| 207 | |
| 208 | debugscip-shell shell shell-project 0.0.0 debug(). "All required commands are available" |
| 209 | debugscip-shell shell shell-project 0.0.0 debug(). "Using compose command: ${COMPOSE_CMD}" |
| 210 | } |
| 211 | |
| 212 | # Wait for the Matrix server to be ready |
| 213 | wait_for_serverscip-shell shell shell-project 0.0.0 wait_for_server().() { |
| 214 | local base="$1" |
| 215 | local max_attempts="${2:-30}" |
| 216 | local attempt=1 |
| 217 | |
| 218 | infoscip-shell shell shell-project 0.0.0 info(). "Waiting for Matrix server at ${base} to be ready..." |
| 219 | |
| 220 | while [[ $attempt -le $max_attempts ]]; do |
| 221 | if curlscip-shell system . . curl. -s -o /dev/nullscip-shell filesystem . . `/dev/null`. -w "%{http_code}" "${base}/_matrix/client/versions" 2>/dev/nullscip-shell filesystem . . `/dev/null`. | grepscip-shell system . . grep. -q "200"; then |
| 222 | infoscip-shell shell shell-project 0.0.0 info(). "Matrix server is ready!" |
| 223 | return 0 |
| 224 | fi |
| 225 | |
| 226 | debugscip-shell shell shell-project 0.0.0 debug(). "Attempt ${attempt}/${max_attempts}: Server not ready yet..." |
| 227 | sleepscip-shell system . . sleep. 2 |
| 228 | ((attempt++)) |
| 229 | done |
| 230 | |
| 231 | errorscip-shell shell shell-project 0.0.0 error(). "Matrix server did not become ready after ${max_attempts} attempts" |
| 232 | } |
| 233 | |
| 234 | # ============================================================================= |
| 235 | # Setup Functions |
| 236 | # ============================================================================= |
| 237 | |
| 238 | # Generate the Synapse server configuration |
| 239 | # This creates the necessary configuration files in the data directory |
| 240 | generate_configscip-shell shell shell-project 0.0.0 generate_config().() { |
| 241 | infoscip-shell shell shell-project 0.0.0 info(). "Generating Matrix Synapse configuration..." |
| 242 | |
| 243 | # Check for element.env file |
| 244 | if [[ ! -f "$ELEMENT_ENV_FILE" ]]; then |
| 245 | warnscip-shell shell shell-project 0.0.0 warn(). "element.env not found at ${ELEMENT_ENV_FILE}" |
| 246 | infoscip-shell shell shell-project 0.0.0 info(). "Creating default element.env file..." |
| 247 | |
| 248 | catscip-shell system . . cat. > "$ELEMENT_ENV_FILE" << 'EOF' |
| 249 | # Matrix Synapse configuration |
| 250 | # Server name |
| 251 | SYNAPSE_SERVER_NAME=localhost |
| 252 | # Enable registration |
| 253 | SYNAPSE_ALLOW_GUEST_ACCESS=true |
| 254 | # Enable registration without verification |
| 255 | SYNAPSE_ALLOW_INSECURE_REGISTRATION_TOKEN=true |
| 256 | EOF |
| 257 | fi |
| 258 | |
| 259 | # Create data directory if it doesn't exist |
| 260 | if [[ ! -d "$DATA_DIR" ]]; then |
| 261 | mkdirscip-shell system . . mkdir. -p "$DATA_DIR" |
| 262 | debugscip-shell shell shell-project 0.0.0 debug(). "Created data directory at ${DATA_DIR}" |
| 263 | fi |
| 264 | |
| 265 | # Run the Synapse generate command |
| 266 | infoscip-shell shell shell-project 0.0.0 info(). "Running Synapse configuration generator..." |
| 267 | docker run --rm \ |
| 268 | -u "$(idscip-shell system . . id. -u):$(idscip-shell system . . id. -g)" \ |
| 269 | -v "${DATA_DIR}:/data" \ |
| 270 | --env-file "$ELEMENT_ENV_FILE" \ |
| 271 | matrixdotorg/synapse:latest generate |
| 272 | |
| 273 | if [[ $? -eq 0 ]]; then |
| 274 | infoscip-shell shell shell-project 0.0.0 info(). "Configuration generated successfully in ${DATA_DIR}" |
| 275 | else |
| 276 | errorscip-shell shell shell-project 0.0.0 error(). "Failed to generate configuration" |
| 277 | fi |
| 278 | |
| 279 | # Add rate limiting configuration to homeserver.yaml |
| 280 | # This must be done before starting the server to ensure the configuration takes effect |
| 281 | local homeserver_yaml="${DATA_DIR}/homeserver.yaml" |
| 282 | if [[ -f "$homeserver_yaml" ]]; then |
| 283 | if ! grepscip-shell system . . grep. -q "^rc_message:" "$homeserver_yaml" 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 284 | infoscip-shell shell shell-project 0.0.0 info(). "Adding rate limiting config to homeserver.yaml..." |
| 285 | echoscip-shell system . . echo. "$RATE_LIMIT_CONFIG" >> "$homeserver_yaml" |
| 286 | infoscip-shell shell shell-project 0.0.0 info(). "Rate limiting configuration added" |
| 287 | else |
| 288 | debugscip-shell shell shell-project 0.0.0 debug(). "Rate limiting config already present in homeserver.yaml" |
| 289 | fi |
| 290 | fi |
| 291 | } |
| 292 | |
| 293 | # Disable rate limiting in the Synapse configuration via Admin API |
| 294 | # This uses the Synapse Admin API to override rate limiting per user |
| 295 | disable_rate_limitingscip-shell shell shell-project 0.0.0 disable_rate_limiting().() { |
| 296 | # Load environment variables |
| 297 | load_env_filescip-shell shell shell-project 0.0.0 load_env_file(). |
| 298 | |
| 299 | # Get configuration from environment variables |
| 300 | local host="${SHOUTRRR_MATRIX_HOST:-${DEFAULT_MATRIX_HOST}}" |
| 301 | local user="${SHOUTRRR_MATRIX_USER:-${DEFAULT_MATRIX_USER}}" |
| 302 | local password="${SHOUTRRR_MATRIX_PASSWORD:-${DEFAULT_MATRIX_PASSWORD}}" |
| 303 | |
| 304 | # Validate required variables |
| 305 | if [[ -z "$host" ]]; then |
| 306 | errorscip-shell shell shell-project 0.0.0 error(). "SHOUTRRR_MATRIX_HOST is not set" |
| 307 | fi |
| 308 | if [[ -z "$user" ]]; then |
| 309 | errorscip-shell shell shell-project 0.0.0 error(). "SHOUTRRR_MATRIX_USER is not set" |
| 310 | fi |
| 311 | if [[ -z "$password" ]]; then |
| 312 | errorscip-shell shell shell-project 0.0.0 error(). "SHOUTRRR_MATRIX_PASSWORD is not set" |
| 313 | fi |
| 314 | |
| 315 | local homeserver_yaml="${DATA_DIR}/homeserver.yaml" |
| 316 | |
| 317 | infoscip-shell shell shell-project 0.0.0 info(). "Disabling rate limiting for user '${user}' at ${host}..." |
| 318 | |
| 319 | # First, disable rate limiting in the global config (if not already done) |
| 320 | if [[ -f "$homeserver_yaml" ]]; then |
| 321 | if ! grepscip-shell system . . grep. -q "^rc_message:" "$homeserver_yaml" 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 322 | infoscip-shell shell shell-project 0.0.0 info(). "Adding rate limiting config to homeserver.yaml..." |
| 323 | echoscip-shell system . . echo. "$RATE_LIMIT_CONFIG" >> "$homeserver_yaml" |
| 324 | else |
| 325 | debugscip-shell shell shell-project 0.0.0 debug(). "Rate limiting config already present in homeserver.yaml" |
| 326 | fi |
| 327 | fi |
| 328 | |
| 329 | # Get base URL using the helper function (handles TLS setting) |
| 330 | local base_url |
| 331 | base_url=$(get_base_url "$host") |
| 332 | |
| 333 | # Wait for server to be ready |
| 334 | wait_for_serverscip-shell shell shell-project 0.0.0 wait_for_server(). "$base_url" 30 |
| 335 | |
| 336 | infoscip-shell shell shell-project 0.0.0 info(). "Using base URL: ${base_url}" |
| 337 | |
| 338 | # Login to get access token using the existing login function |
| 339 | local admin_token |
| 340 | admin_token=$(loginscip-shell system . . login. "$base_url" "$user" "$password") |
| 341 | |
| 342 | # Now disable rate limiting for the user via Admin API |
| 343 | infoscip-shell shell shell-project 0.0.0 info(). "Disabling rate limit for user via Admin API..." |
| 344 | local response |
| 345 | response=$( |
| 346 | curlscip-shell system . . curl. -s -X POST "${base_url}/_synapse/admin/v1/users/@${user}:localhost/override_ratelimit" \ |
| 347 | -H "Content-Type: application/json" \ |
| 348 | -H "Authorization: Bearer ${admin_token}" \ |
| 349 | -d '{ |
| 350 | "messages_per_second": 0, |
| 351 | "burst_count": 0 |
| 352 | }' 2>&1 |
| 353 | ) || errorscip-shell shell shell-project 0.0.0 error(). "Failed to disable rate limiting for user" |
| 354 | |
| 355 | # Check for errors in response |
| 356 | local error_msg |
| 357 | error_msg=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.errcode // empty' 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.) |
| 358 | |
| 359 | if [[ -n "$error_msg" && "$error_msg" != "null" ]]; then |
| 360 | local error_desc |
| 361 | error_desc=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.error // "Unknown error"' 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.) |
| 362 | errorscip-shell shell shell-project 0.0.0 error(). "Failed to disable rate limiting: ${error_msg} - ${error_desc}" |
| 363 | fi |
| 364 | |
| 365 | infoscip-shell shell shell-project 0.0.0 info(). "Rate limit override response for ${user}: $response" |
| 366 | |
| 367 | # Also disable for testuser if it exists |
| 368 | infoscip-shell shell shell-project 0.0.0 info(). "Disabling rate limit for testuser via Admin API..." |
| 369 | response=$( |
| 370 | curlscip-shell system . . curl. -s -X POST "${base_url}/_synapse/admin/v1/users/@testuser:localhost/override_ratelimit" \ |
| 371 | -H "Content-Type: application/json" \ |
| 372 | -H "Authorization: Bearer ${admin_token}" \ |
| 373 | -d '{ |
| 374 | "messages_per_second": 0, |
| 375 | "burst_count": 0 |
| 376 | }' 2>&1 |
| 377 | ) || warnscip-shell shell shell-project 0.0.0 warn(). "Failed to disable rate limiting for testuser (user may not exist)" |
| 378 | |
| 379 | # Check for errors - but don't fail if testuser doesn't exist |
| 380 | error_msg=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.errcode // empty' 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.) |
| 381 | |
| 382 | if [[ -n "$error_msg" && "$error_msg" != "null" ]]; then |
| 383 | # Only warn if it's not a "not found" error |
| 384 | local error_desc |
| 385 | error_desc=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.error // "Unknown error"' 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.) |
| 386 | if [[ "$error_msg" == "M_NOT_FOUND" ]]; then |
| 387 | infoscip-shell shell shell-project 0.0.0 info(). "testuser does not exist, skipping rate limit override" |
| 388 | else |
| 389 | warnscip-shell shell shell-project 0.0.0 warn(). "Failed to disable rate limiting for testuser: ${error_msg} - ${error_desc}" |
| 390 | fi |
| 391 | else |
| 392 | infoscip-shell shell shell-project 0.0.0 info(). "Rate limit override response for testuser: $response" |
| 393 | fi |
| 394 | |
| 395 | infoscip-shell shell shell-project 0.0.0 info(). "Rate limiting disabled successfully via Admin API" |
| 396 | } |
| 397 | |
| 398 | # Start the Matrix Synapse server using docker compose |
| 399 | start_serverscip-shell shell shell-project 0.0.0 start_server().() { |
| 400 | infoscip-shell shell shell-project 0.0.0 info(). "Starting Matrix Synapse server..." |
| 401 | |
| 402 | # Check if docker compose file exists |
| 403 | if [[ ! -f "$COMPOSE_FILE" ]]; then |
| 404 | errorscip-shell shell shell-project 0.0.0 error(). "Docker compose file not found at ${COMPOSE_FILE}" |
| 405 | fi |
| 406 | |
| 407 | # Ensure COMPOSE_CMD is set |
| 408 | if [[ -z "$COMPOSE_CMD" ]]; then |
| 409 | check_requirementsscip-shell shell shell-project 0.0.0 check_requirements(). |
| 410 | fi |
| 411 | |
| 412 | # Change to the script directory |
| 413 | cd "$SCRIPT_DIR" |
| 414 | |
| 415 | # Export UID and GID for docker-compose to use |
| 416 | # This ensures the container runs as the current host user |
| 417 | # Use _UID/_GID to avoid conflict with shell built-in readonly UID variable |
| 418 | local _user_uid _user_gid |
| 419 | _user_uid="${MATRIX_UID:-$(idscip-shell system . . id. -u)}" |
| 420 | _user_gid="${MATRIX_GID:-$(idscip-shell system . . id. -g)}" |
| 421 | export MATRIX_UID="$_user_uid" |
| 422 | export MATRIX_GID="$_user_gid" |
| 423 | |
| 424 | debugscip-shell shell shell-project 0.0.0 debug(). "Using MATRIX_UID=${MATRIX_UID} MATRIX_GID=${MATRIX_GID} for container" |
| 425 | |
| 426 | # Stop any existing container |
| 427 | if $COMPOSE_CMD ps --status running 2>/dev/nullscip-shell filesystem . . `/dev/null`. | grepscip-shell system . . grep. -q synapse; then |
| 428 | infoscip-shell shell shell-project 0.0.0 info(). "Stopping existing Synapse container..." |
| 429 | $COMPOSE_CMD down || truescip-shell system . . true. |
| 430 | fi |
| 431 | |
| 432 | # Start the server |
| 433 | $COMPOSE_CMD up -d |
| 434 | |
| 435 | if [[ $? -eq 0 ]]; then |
| 436 | infoscip-shell shell shell-project 0.0.0 info(). "Matrix Synapse server started successfully" |
| 437 | infoscip-shell shell shell-project 0.0.0 info(). "Server is available at http://localhost:8008" |
| 438 | infoscip-shell shell shell-project 0.0.0 info(). "Use 'docker logs synapse -f' to watch the logs" |
| 439 | else |
| 440 | errorscip-shell shell shell-project 0.0.0 error(). "Failed to start Matrix Synapse server" |
| 441 | fi |
| 442 | } |
| 443 | |
| 444 | # Create the admin test user |
| 445 | create_userscip-shell shell shell-project 0.0.0 create_user().() { |
| 446 | infoscip-shell shell shell-project 0.0.0 info(). "Creating admin test user..." |
| 447 | |
| 448 | # Load environment file before reading variables to ensure |
| 449 | # values from .env take precedence over hardcoded defaults. |
| 450 | load_env_filescip-shell shell shell-project 0.0.0 load_env_file(). |
| 451 | |
| 452 | # Default credentials |
| 453 | local user="${SHOUTRRR_MATRIX_USER:-${DEFAULT_MATRIX_USER}}" |
| 454 | local password="${SHOUTRRR_MATRIX_PASSWORD:-${DEFAULT_MATRIX_PASSWORD}}" |
| 455 | local server_url="${SHOUTRRR_MATRIX_HOST:-${DEFAULT_MATRIX_HOST}}" |
| 456 | |
| 457 | # Get base URL for server readiness check |
| 458 | local base_url |
| 459 | base_url=$(get_base_url "$server_url") |
| 460 | |
| 461 | # Wait for server to be ready |
| 462 | wait_for_serverscip-shell shell shell-project 0.0.0 wait_for_server(). "$base_url" 60 |
| 463 | |
| 464 | # Register the user using docker exec |
| 465 | # Wrap in if to prevent set -e from exiting before fallback check |
| 466 | infoscip-shell shell shell-project 0.0.0 info(). "Registering user '${user}'..." |
| 467 | |
| 468 | if docker exec synapse \ |
| 469 | register_new_matrix_user \ |
| 470 | -u "$user" \ |
| 471 | -p "$password" \ |
| 472 | -a \ |
| 473 | -c /data/homeserver.yamlscip-shell filesystem . . `/data/homeserver.yaml`. \ |
| 474 | "${base_url}"; then |
| 475 | infoscip-shell shell shell-project 0.0.0 info(). "User '${user}' created successfully" |
| 476 | else |
| 477 | # Check if user already exists |
| 478 | if docker exec synapse \ |
| 479 | register_new_matrix_user \ |
| 480 | -u "$user" \ |
| 481 | -p "$password" \ |
| 482 | -a \ |
| 483 | -c /data/homeserver.yamlscip-shell filesystem . . `/data/homeserver.yaml`. \ |
| 484 | "${base_url}" 2>&1 | grepscip-shell system . . grep. -q "already exists"; then |
| 485 | warnscip-shell shell shell-project 0.0.0 warn(). "User '${user}' already exists, continuing..." |
| 486 | else |
| 487 | errorscip-shell shell shell-project 0.0.0 error(). "Failed to create user '${user}'" |
| 488 | fi |
| 489 | fi |
| 490 | |
| 491 | # Persist resolved settings to ENV_FILE so downstream consumers |
| 492 | # (e.g., testing/e2e/matrix/matrix_suite_test.go) see the same values. |
| 493 | infoscip-shell shell shell-project 0.0.0 info(). "Persisting resolved settings to ${ENV_FILE}..." |
| 494 | { |
| 495 | echoscip-shell system . . echo. "SHOUTRRR_MATRIX_USER=${user}" |
| 496 | echoscip-shell system . . echo. "SHOUTRRR_MATRIX_PASSWORD=${password}" |
| 497 | echoscip-shell system . . echo. "SHOUTRRR_MATRIX_HOST=${server_url}" |
| 498 | } >> "$ENV_FILE" |
| 499 | |
| 500 | # Always persist the TLS setting - default to true (HTTP) for local development |
| 501 | # since Synapse runs on HTTP by default |
| 502 | local disable_tls="${SHOUTRRR_MATRIX_DISABLE_TLS:-true}" |
| 503 | echoscip-shell system . . echo. "SHOUTRRR_MATRIX_DISABLE_TLS=${disable_tls}" >> "$ENV_FILE" |
| 504 | |
| 505 | infoscip-shell shell shell-project 0.0.0 info(). "Resolved settings persisted to ${ENV_FILE}" |
| 506 | } |
| 507 | |
| 508 | # Get the base URL for Matrix API |
| 509 | # Defaults to http unless TLS is explicitly enabled |
| 510 | get_base_urlscip-shell shell shell-project 0.0.0 get_base_url().() { |
| 511 | local host="${1:-${DEFAULT_MATRIX_HOST}}" |
| 512 | local disable_tls="${SHOUTRRR_MATRIX_DISABLE_TLS:-}" |
| 513 | |
| 514 | if [[ "$disable_tls" == "false" ]]; then |
| 515 | echoscip-shell system . . echo. "https://${host}" |
| 516 | else |
| 517 | echoscip-shell system . . echo. "http://${host}" |
| 518 | fi |
| 519 | } |
| 520 | |
| 521 | # Login to Matrix and get access token |
| 522 | loginscip-shell shell shell-project 0.0.0 login().() { |
| 523 | local base_url="$1" |
| 524 | local user="$2" |
| 525 | local password="$3" |
| 526 | |
| 527 | debugscip-shell shell shell-project 0.0.0 debug(). "Logging in as ${user} to ${base_url}..." |
| 528 | |
| 529 | local response |
| 530 | response=$( |
| 531 | curlscip-shell system . . curl. -s -X POST "${base_url}/_matrix/client/v3/login" \ |
| 532 | -H "Content-Type: application/json" \ |
| 533 | --data "$(jq -n --arg user "$user" --arg password "$password" \ |
| 534 | '{type: "m.login.password", identifier: {type: "m.id.user", user: $user}, password: $password}')" \ |
| 535 | 2>&1 |
| 536 | ) || errorscip-shell shell shell-project 0.0.0 error(). "Failed to connect to Matrix server at ${base_url}" |
| 537 | |
| 538 | # Check for errors in response |
| 539 | local error_msg |
| 540 | error_msg=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.errcode // empty' 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.) |
| 541 | |
| 542 | if [[ -n "$error_msg" && "$error_msg" != "null" ]]; then |
| 543 | local error_desc |
| 544 | error_desc=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.error // "Unknown error"' 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.) |
| 545 | errorscip-shell shell shell-project 0.0.0 error(). "Login failed: ${error_msg} - ${error_desc}" |
| 546 | fi |
| 547 | |
| 548 | local access_token |
| 549 | access_token=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.access_token' 2>/dev/nullscip-shell filesystem . . `/dev/null`.) || errorscip-shell shell shell-project 0.0.0 error(). "Failed to parse login response" |
| 550 | |
| 551 | if [[ -z "$access_token" || "$access_token" == "null" ]]; then |
| 552 | errorscip-shell shell shell-project 0.0.0 error(). "No access token received from login" |
| 553 | fi |
| 554 | |
| 555 | debugscip-shell shell shell-project 0.0.0 debug(). "Login successful, token length: ${#access_token}" |
| 556 | echoscip-shell system . . echo. "$access_token" |
| 557 | } |
| 558 | |
| 559 | # Create the test room with alias #test:localhost |
| 560 | create_roomscip-shell shell shell-project 0.0.0 create_room().() { |
| 561 | infoscip-shell shell shell-project 0.0.0 info(). "Creating test room..." |
| 562 | |
| 563 | # Load environment variables |
| 564 | load_env_filescip-shell shell shell-project 0.0.0 load_env_file(). |
| 565 | |
| 566 | # Get configuration |
| 567 | local host="${SHOUTRRR_MATRIX_HOST:-${DEFAULT_MATRIX_HOST}}" |
| 568 | local user="${SHOUTRRR_MATRIX_USER:-${DEFAULT_MATRIX_USER}}" |
| 569 | local password="${SHOUTRRR_MATRIX_PASSWORD:-${DEFAULT_MATRIX_PASSWORD}}" |
| 570 | local room_alias="${SHOUTRRR_MATRIX_ROOM:-${DEFAULT_MATRIX_ROOM}}" |
| 571 | |
| 572 | # Validate required variables |
| 573 | if [[ -z "$host" ]]; then |
| 574 | errorscip-shell shell shell-project 0.0.0 error(). "SHOUTRRR_MATRIX_HOST is not set" |
| 575 | fi |
| 576 | if [[ -z "$user" ]]; then |
| 577 | errorscip-shell shell shell-project 0.0.0 error(). "SHOUTRRR_MATRIX_USER is not set" |
| 578 | fi |
| 579 | if [[ -z "$password" ]]; then |
| 580 | errorscip-shell shell shell-project 0.0.0 error(). "SHOUTRRR_MATRIX_PASSWORD is not set" |
| 581 | fi |
| 582 | |
| 583 | infoscip-shell shell shell-project 0.0.0 info(). "Using host: ${host}" |
| 584 | infoscip-shell shell shell-project 0.0.0 info(). "Using user: ${user}" |
| 585 | infoscip-shell shell shell-project 0.0.0 info(). "Using room: ${room_alias}" |
| 586 | |
| 587 | # Get base URL |
| 588 | local base_url |
| 589 | base_url=$(get_base_urlscip-shell shell shell-project 0.0.0 get_base_url(). "$host") |
| 590 | |
| 591 | # Wait for server to be ready |
| 592 | wait_for_serverscip-shell shell shell-project 0.0.0 wait_for_server(). "$base_url" 30 |
| 593 | |
| 594 | # Login to get access token |
| 595 | local access_token |
| 596 | access_token=$(loginscip-shell shell shell-project 0.0.0 login(). "$base_url" "$user" "$password") |
| 597 | |
| 598 | # Extract local part of the alias (e.g., "test" from "#test:localhost") |
| 599 | local local_alias |
| 600 | local_alias=$(echoscip-shell system . . echo. "$room_alias" | sedscip-shell system . . sed. 's/^#//' | cutscip-shell system . . cut. -d':' -f1) |
| 601 | |
| 602 | debugscip-shell shell shell-project 0.0.0 debug(). "Creating room with alias: ${local_alias}" |
| 603 | |
| 604 | # Create the room |
| 605 | local response |
| 606 | response=$( |
| 607 | curlscip-shell system . . curl. -s -X POST "${base_url}/_matrix/client/v3/createRoom" \ |
| 608 | -H "Content-Type: application/json" \ |
| 609 | -H "Authorization: Bearer ${access_token}" \ |
| 610 | -d "{ |
| 611 | \"room_alias_name\": \"${local_alias}\", |
| 612 | \"name\": \"Test Room\", |
| 613 | \"topic\": \"Test room for shoutrrr e2e tests\", |
| 614 | \"visibility\": \"public\" |
| 615 | }" 2>&1 |
| 616 | ) || errorscip-shell shell shell-project 0.0.0 error(). "Failed to create room" |
| 617 | |
| 618 | # Check for errors in response |
| 619 | local error_msg |
| 620 | error_msg=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.errcode // empty' 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.) |
| 621 | |
| 622 | if [[ -n "$error_msg" && "$error_msg" != "null" ]]; then |
| 623 | local error_desc |
| 624 | error_desc=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.error // "Unknown error"' 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.) |
| 625 | |
| 626 | # Room might already exist - that's okay |
| 627 | if [[ "$error_msg" == "ROOM_ALIAS_EXISTS" ]]; then |
| 628 | warnscip-shell shell shell-project 0.0.0 warn(). "Room alias already exists: ${room_alias}" |
| 629 | return 0 |
| 630 | fi |
| 631 | |
| 632 | errorscip-shell shell shell-project 0.0.0 error(). "Failed to create room: ${error_msg} - ${error_desc}" |
| 633 | fi |
| 634 | |
| 635 | local room_id |
| 636 | room_id=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.room_id' 2>/dev/nullscip-shell filesystem . . `/dev/null`.) || errorscip-shell shell shell-project 0.0.0 error(). "Failed to parse room creation response" |
| 637 | |
| 638 | if [[ -z "$room_id" || "$room_id" == "null" ]]; then |
| 639 | errorscip-shell shell shell-project 0.0.0 error(). "No room_id received from room creation" |
| 640 | fi |
| 641 | |
| 642 | infoscip-shell shell shell-project 0.0.0 info(). "Successfully created room: ${room_alias} (${room_id})" |
| 643 | |
| 644 | # Persist resolved room to ENV_FILE so downstream consumers see the same value. |
| 645 | echoscip-shell system . . echo. "SHOUTRRR_MATRIX_ROOM=${room_alias}" >> "$ENV_FILE" |
| 646 | infoscip-shell shell shell-project 0.0.0 info(). "Resolved room persisted to ${ENV_FILE}" |
| 647 | } |
| 648 | |
| 649 | # Run all setup steps in order |
| 650 | setup_allscip-shell shell shell-project 0.0.0 setup_all().() { |
| 651 | infoscip-shell shell shell-project 0.0.0 info(). "Starting complete Matrix E2E test environment setup..." |
| 652 | echoscip-shell system . . echo. "" |
| 653 | |
| 654 | # Check requirements |
| 655 | check_requirementsscip-shell shell shell-project 0.0.0 check_requirements(). |
| 656 | |
| 657 | # Step 1: Generate config |
| 658 | echoscip-shell system . . echo. "" |
| 659 | infoscip-shell shell shell-project 0.0.0 info(). "=== Step 1/5: Generating configuration ===" |
| 660 | generate_configscip-shell shell shell-project 0.0.0 generate_config(). |
| 661 | |
| 662 | # Step 2: Start server |
| 663 | echoscip-shell system . . echo. "" |
| 664 | infoscip-shell shell shell-project 0.0.0 info(). "=== Step 2/5: Starting server ===" |
| 665 | start_serverscip-shell shell shell-project 0.0.0 start_server(). |
| 666 | |
| 667 | # Step 3: Create user |
| 668 | echoscip-shell system . . echo. "" |
| 669 | infoscip-shell shell shell-project 0.0.0 info(). "=== Step 3/5: Creating test user ===" |
| 670 | create_userscip-shell shell shell-project 0.0.0 create_user(). |
| 671 | |
| 672 | # Step 4: Disable rate limiting (requires server and user to be ready) |
| 673 | echoscip-shell system . . echo. "" |
| 674 | infoscip-shell shell shell-project 0.0.0 info(). "=== Step 4/5: Disabling rate limiting ===" |
| 675 | disable_rate_limitingscip-shell shell shell-project 0.0.0 disable_rate_limiting(). |
| 676 | |
| 677 | # Step 5: Create room |
| 678 | echoscip-shell system . . echo. "" |
| 679 | infoscip-shell shell shell-project 0.0.0 info(). "=== Step 5/5: Creating test room ===" |
| 680 | create_roomscip-shell shell shell-project 0.0.0 create_room(). |
| 681 | |
| 682 | echoscip-shell system . . echo. "" |
| 683 | infoscip-shell shell shell-project 0.0.0 info(). "==========================================" |
| 684 | infoscip-shell shell shell-project 0.0.0 info(). "Matrix E2E test environment setup complete!" |
| 685 | infoscip-shell shell shell-project 0.0.0 info(). "==========================================" |
| 686 | infoscip-shell shell shell-project 0.0.0 info(). "" |
| 687 | infoscip-shell shell shell-project 0.0.0 info(). "You can now run the E2E tests with:" |
| 688 | infoscip-shell shell shell-project 0.0.0 info(). " go test -v ./testing/e2e/matrix/..." |
| 689 | infoscip-shell shell shell-project 0.0.0 info(). "" |
| 690 | infoscip-shell shell shell-project 0.0.0 info(). "To stop the server, run:" |
| 691 | infoscip-shell shell shell-project 0.0.0 info(). " docker compose -f ${COMPOSE_FILE} down" |
| 692 | } |
| 693 | |
| 694 | # ============================================================================= |
| 695 | # Main Entry Point |
| 696 | # ============================================================================= |
| 697 | |
| 698 | mainscip-shell shell shell-project 0.0.0 main().() { |
| 699 | # Parse command-line arguments |
| 700 | local command="setup-all" |
| 701 | |
| 702 | while [[ $# -gt 0 ]]; do |
| 703 | case "$1" in |
| 704 | --help|-h) |
| 705 | usagescip-shell shell shell-project 0.0.0 usage(). |
| 706 | exit 0 |
| 707 | ;; |
| 708 | --verbose|-v) |
| 709 | VERBOSE=true |
| 710 | shift |
| 711 | ;; |
| 712 | generate-config|start-server|create-user|create-room|setup-all|disable_rate_limiting) |
| 713 | command="$1" |
| 714 | shift |
| 715 | ;; |
| 716 | *) |
| 717 | errorscip-shell shell shell-project 0.0.0 error(). "Unknown option or command: $1" |
| 718 | ;; |
| 719 | esac |
| 720 | done |
| 721 | |
| 722 | # Execute the requested command |
| 723 | case "$command" in |
| 724 | generate-config) |
| 725 | generate_configscip-shell shell shell-project 0.0.0 generate_config(). |
| 726 | ;; |
| 727 | start-server) |
| 728 | start_serverscip-shell shell shell-project 0.0.0 start_server(). |
| 729 | ;; |
| 730 | create-user) |
| 731 | create_userscip-shell shell shell-project 0.0.0 create_user(). |
| 732 | ;; |
| 733 | create-room) |
| 734 | create_roomscip-shell shell shell-project 0.0.0 create_room(). |
| 735 | ;; |
| 736 | setup-all) |
| 737 | setup_allscip-shell shell shell-project 0.0.0 setup_all(). |
| 738 | ;; |
| 739 | disable_rate_limiting) |
| 740 | disable_rate_limitingscip-shell shell shell-project 0.0.0 disable_rate_limiting(). |
| 741 | ;; |
| 742 | esac |
| 743 | } |
| 744 | |
| 745 | # Run main function with all arguments |
| 746 | mainscip-shell shell shell-project 0.0.0 main(). "$@" |