#!/bin/bash # Script to change MQTT Admin Password # Usage: ./change-mqtt-admin-password.sh set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Check if password argument is provided if [ -z "$1" ]; then echo -e "${RED}Error: No password provided${NC}" echo "Usage: $0 " exit 1 fi NEW_PASSWORD="$1" # Validate password (minimum 8 characters recommended) if [ ${#NEW_PASSWORD} -lt 8 ]; then echo -e "${YELLOW}Warning: Password is shorter than 8 characters${NC}" read -p "Continue anyway? (y/n) " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then exit 1 fi fi echo -e "${GREEN}Starting MQTT Admin Password Change...${NC}" echo # Step 1: Update .env file echo -e "${YELLOW}Step 1: Updating .env file...${NC}" if [ ! -f .env ]; then echo -e "${RED}Error: .env file not found${NC}" exit 1 fi # Check if MQTT_ADMIN_PASSWORD exists in .env if grep -q "^MQTT_ADMIN_PASSWORD=" .env; then # Update existing password sed -i "s/^MQTT_ADMIN_PASSWORD=.*/MQTT_ADMIN_PASSWORD=${NEW_PASSWORD}/" .env echo -e "${GREEN}✓ Updated MQTT_ADMIN_PASSWORD in .env${NC}" else # Add password if it doesn't exist echo "MQTT_ADMIN_PASSWORD=${NEW_PASSWORD}" >> .env echo -e "${GREEN}✓ Added MQTT_ADMIN_PASSWORD to .env${NC}" fi echo # Step 2: Restart containers echo -e "${YELLOW}Step 2: Restarting containers...${NC}" docker compose restart echo -e "${GREEN}✓ Containers restarted${NC}" echo # Step 3: Wait for app to be ready echo -e "${YELLOW}Step 3: Waiting for app to be ready...${NC}" sleep 5 echo -e "${GREEN}✓ App should be ready${NC}" echo # Step 4: Trigger MQTT configuration sync echo -e "${YELLOW}Step 4: Triggering MQTT configuration sync...${NC}" # Create a temporary TypeScript script that calls syncMosquittoConfig cat > /tmp/sync-mqtt.ts << 'EOF' import { syncMosquittoConfig } from './lib/mosquitto-sync'; async function main() { try { console.log('Starting MQTT configuration sync...'); const result = await syncMosquittoConfig(); if (result.success) { console.log('✓ Success:', result.message); process.exit(0); } else { console.error('✗ Failed:', result.message); process.exit(1); } } catch (error) { console.error('✗ Error:', error instanceof Error ? error.message : String(error)); process.exit(1); } } main(); EOF # Copy script to container docker cp /tmp/sync-mqtt.ts location-tracker:/app/sync-mqtt.ts # Execute with tsx (suppressing npm warnings) if docker exec location-tracker npx --yes tsx /app/sync-mqtt.ts 2>&1 | grep -v "npm warn"; then echo -e "${GREEN}✓ MQTT configuration synced successfully${NC}" else echo -e "${RED}✗ Sync failed, but password is updated in .env${NC}" echo -e "${YELLOW}You can manually trigger sync from the Admin Panel or restart Mosquitto${NC}" fi # Cleanup docker exec location-tracker rm -f /app/sync-mqtt.ts rm -f /tmp/sync-mqtt.ts echo echo -e "${GREEN}========================================${NC}" echo -e "${GREEN}MQTT Admin Password Change Complete!${NC}" echo -e "${GREEN}========================================${NC}" echo echo -e "New password: ${YELLOW}${NEW_PASSWORD}${NC}" echo echo -e "${YELLOW}Note:${NC} All MQTT connections using the old admin password will need to reconnect." echo