Files
azerothcore-wotlk/apps/startup-scripts/src/migrate-registry.sh
Yehonal de98f42411 feat(Service Manager): add service registry custom dir and restore functionality (#22589)
This pull request introduces significant enhancements to the service management system by adding a service registry with features like automatic tracking, reboot persistence, and restoration of missing services. 

The goal of this PR is to allow the user to store the service configuration files into an arbitrary directory, in this way they can be easily tracked, versioned, and replicated across different environments

It also includes a migration script to transition from the legacy service configuration format to the new registry-based system. Below is a summary of the most important changes:

### Service Registry and Management Enhancements:
1. **Service Registry Integration**:
   - Added a comprehensive service registry system to track all created services, enabling features like cross-reboot persistence and restoration of missing services (`apps/startup-scripts/src/service-manager.sh`). [[1]](diffhunk://#diff-31edfed7f73d0647a5fc96ce74c249e025e884cd1fe06621cb78eb4a381464f9R41-R229) [[2]](diffhunk://#diff-31edfed7f73d0647a5fc96ce74c249e025e884cd1fe06621cb78eb4a381464f9R273)
   - Introduced commands for managing the registry, such as `restore` for recreating missing services and `list` for viewing registered services. [[1]](diffhunk://#diff-31edfed7f73d0647a5fc96ce74c249e025e884cd1fe06621cb78eb4a381464f9R273) [[2]](diffhunk://#diff-31edfed7f73d0647a5fc96ce74c249e025e884cd1fe06621cb78eb4a381464f9R332-R334) [[3]](diffhunk://#diff-31edfed7f73d0647a5fc96ce74c249e025e884cd1fe06621cb78eb4a381464f9R346-L172)

2. **PM2 Persistence**:
   - Enhanced PM2 integration to automatically configure startup persistence across reboots using `pm2 startup` and `pm2 save` after service creation.

### Migration and Compatibility:
3. **Migration Script**:
   - Added a `migrate-registry.sh` script to convert legacy service configurations into the new registry format. It ensures compatibility while preserving existing service information (`apps/startup-scripts/src/migrate-registry.sh`).

### Documentation Updates:
4. **Updated README**:
   - Expanded documentation in `README.md` to explain the new service registry features, including usage examples, custom configuration directories, and migration instructions. [[1]](diffhunk://#diff-0917b2888cc9b16539173f318b77773d08f7bf360579b68b9710a96ca2bcbb64L387-R468) [[2]](diffhunk://#diff-0917b2888cc9b16539173f318b77773d08f7bf360579b68b9710a96ca2bcbb64R613-R626)

### Configuration Improvements:
5. **Custom Configuration Directories**:
   - Added support for overriding the default configuration directory for service registry and files using the `AC_SERVICE_CONFIG_DIR` environment variable. [[1]](diffhunk://#diff-31edfed7f73d0647a5fc96ce74c249e025e884cd1fe06621cb78eb4a381464f9L14-R15) [[2]](diffhunk://#diff-31edfed7f73d0647a5fc96ce74c249e025e884cd1fe06621cb78eb4a381464f9R346-L172)

These changes significantly improve the usability, reliability, and maintainability of the service management system, especially for setups requiring persistence and multi-project configurations.
2025-08-25 20:25:17 +02:00

145 lines
5.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# One-time migration script for service registry
# Converts old format to new format
set -euo pipefail # Strict error handling
CONFIG_DIR="${AC_SERVICE_CONFIG_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/azerothcore/services}"
REGISTRY_FILE="$CONFIG_DIR/service_registry.json"
BACKUP_FILE="$CONFIG_DIR/service_registry.json.backup"
# Colors
readonly YELLOW='\033[1;33m'
readonly GREEN='\033[0;32m'
readonly RED='\033[0;31m'
readonly BLUE='\033[0;34m'
readonly NC='\033[0m'
echo -e "${BLUE}AzerothCore Service Registry Migration Tool${NC}"
echo "=============================================="
# Check dependencies
if ! command -v jq >/dev/null 2>&1; then
echo -e "${RED}Error: jq is required but not installed. Please install jq package.${NC}"
exit 1
fi
# Create config directory if it doesn't exist
mkdir -p "$CONFIG_DIR"
# Check if registry exists
if [ ! -f "$REGISTRY_FILE" ]; then
echo -e "${YELLOW}No registry file found. Nothing to migrate.${NC}"
exit 0
fi
# Validate JSON format
if ! jq empty "$REGISTRY_FILE" >/dev/null 2>&1; then
echo -e "${RED}Error: Registry file contains invalid JSON.${NC}"
echo "Please check the file: $REGISTRY_FILE"
exit 1
fi
# Check if it's already new format
if jq -e 'type == "array" and (length == 0 or .[0] | has("bin_path"))' "$REGISTRY_FILE" >/dev/null 2>&1; then
echo -e "${GREEN}Registry is already in new format. No migration needed.${NC}"
exit 0
fi
# Check if it's old format
if ! jq -e 'type == "array" and (length == 0 or .[0] | has("config"))' "$REGISTRY_FILE" >/dev/null 2>&1; then
echo -e "${YELLOW}Registry format not recognized. Manual review needed.${NC}"
echo "Current registry content:"
cat "$REGISTRY_FILE"
exit 1
fi
echo -e "${YELLOW}Old format detected. Starting migration...${NC}"
# Create backup
if ! cp "$REGISTRY_FILE" "$BACKUP_FILE"; then
echo -e "${RED}Error: Failed to create backup file.${NC}"
exit 1
fi
echo -e "${BLUE}Backup created: $BACKUP_FILE${NC}"
# Convert to new format
echo "[]" > "$REGISTRY_FILE.new"
services_migrated=0
while IFS= read -r service; do
if [ -n "$service" ] && [ "$service" != "null" ]; then
name=$(echo "$service" | jq -r '.name // ""')
provider=$(echo "$service" | jq -r '.provider // ""')
type=$(echo "$service" | jq -r '.type // ""')
config=$(echo "$service" | jq -r '.config // ""')
# Validate required fields
if [ -z "$name" ] || [ -z "$provider" ] || [ -z "$type" ]; then
echo -e "${YELLOW}Skipping invalid service entry: $service${NC}"
continue
fi
echo -e "${YELLOW}Migrating service: $name${NC}"
# Create new format entry with all required fields
new_entry=$(jq -n \
--arg name "$name" \
--arg provider "$provider" \
--arg type "$type" \
--arg bin_path "unknown" \
--arg args "" \
--arg created "$(date -Iseconds)" \
--arg status "migrated" \
--arg systemd_type "--user" \
--arg restart_policy "always" \
--arg session_manager "none" \
--arg gdb_enabled "0" \
--arg pm2_opts "" \
--arg server_config "" \
--arg legacy_config "$config" \
'{
name: $name,
provider: $provider,
type: $type,
bin_path: $bin_path,
args: $args,
created: $created,
status: $status,
systemd_type: $systemd_type,
restart_policy: $restart_policy,
session_manager: $session_manager,
gdb_enabled: $gdb_enabled,
pm2_opts: $pm2_opts,
server_config: $server_config,
legacy_config: $legacy_config
}')
# Add to new registry with error checking
if ! jq --argjson entry "$new_entry" '. += [$entry]' "$REGISTRY_FILE.new" > "$REGISTRY_FILE.new.tmp"; then
echo -e "${RED}Error: Failed to add service $name to new registry${NC}"
rm -f "$REGISTRY_FILE.new" "$REGISTRY_FILE.new.tmp"
exit 1
fi
mv "$REGISTRY_FILE.new.tmp" "$REGISTRY_FILE.new"
services_migrated=$((services_migrated + 1))
fi
done < <(jq -c '.[]?' "$BACKUP_FILE" 2>/dev/null || echo "")
# Replace old registry with new one
if ! mv "$REGISTRY_FILE.new" "$REGISTRY_FILE"; then
echo -e "${RED}Error: Failed to replace old registry with new one${NC}"
exit 1
fi
echo -e "${GREEN}Migration completed successfully!${NC}"
echo -e "${BLUE}Services migrated: $services_migrated${NC}"
echo -e "${BLUE}Use 'service-manager.sh restore' to review and update services.${NC}"
echo -e "${YELLOW}Note: Migrated services have bin_path='unknown' and need manual recreation.${NC}"
echo ""
echo -e "${BLUE}To recreate services, use commands like:${NC}"
echo " ./service-manager.sh create auth authserver --provider pm2 --bin-path /path/to/your/bin"
echo " ./service-manager.sh create world worldserver --provider systemd --bin-path /path/to/your/bin"