# Run Deployment - Execution Guide

## ✅ Scripts Validated and Ready

All scripts have been validated:
- ✓ Syntax OK
- ✓ Executable permissions set
- ✓ Dependencies present
- ✓ Help/usage messages working

## Quick Start

### Step 1: Copy Scripts to Proxmox Host

**From your local machine:**

```bash
cd /home/intlc/projects/proxmox
./scripts/copy-scripts-to-proxmox.sh
```

This copies all deployment scripts to the Proxmox host at `/opt/smom-dbis-138-proxmox/scripts/`.

### Step 2: Run Deployment on Proxmox Host

**SSH to Proxmox host and execute:**

```bash
# 1. SSH to Proxmox host
ssh root@192.168.11.10

# 2. Navigate to deployment directory
cd /opt/smom-dbis-138-proxmox

# 3. Run complete deployment
sudo ./scripts/deployment/deploy-validated-set.sh \
    --source-project /home/intlc/projects/smom-dbis-138
```

**Note**: The source project path must be accessible from the Proxmox host. If the Proxmox host is remote, ensure:
- The directory is mounted/shared, OR
- Configuration files are copied separately to the Proxmox host
```

## Execution Options

### Option 1: Complete Deployment (First Time)

Deploys everything from scratch:

```bash
sudo ./scripts/deployment/deploy-validated-set.sh \
    --source-project /path/to/smom-dbis-138
```

**What it does:**
1. Deploys containers
2. Copies configuration files
3. Bootstraps network
4. Validates deployment

### Option 2: Bootstrap Existing Containers

If containers are already deployed:

```bash
sudo ./scripts/network/bootstrap-network.sh
```

Or using the main script:

```bash
sudo ./scripts/deployment/deploy-validated-set.sh \
    --skip-deployment \
    --skip-config \
    --source-project /path/to/smom-dbis-138
```

### Option 3: Validate Only

Just validate the current deployment:

```bash
sudo ./scripts/validation/validate-validator-set.sh
```

### Option 4: Check Node Health

Check health of a specific node:

```bash
# Human-readable output
sudo ./scripts/health/check-node-health.sh 1000

# JSON output (for automation)
sudo ./scripts/health/check-node-health.sh 1000 --json
```

## Expected Output

### Successful Deployment

```
=========================================
Deploy Validated Set - Script-Based Approach
=========================================

=== Pre-Deployment Validation ===
[✓] Prerequisites checked

=========================================
Phase 1: Deploy Containers
=========================================
[INFO] Deploying Besu nodes...
[✓] Besu nodes deployed

=========================================
Phase 2: Copy Configuration Files
=========================================
[INFO] Copying Besu configuration files...
[✓] Configuration files copied

=========================================
Phase 3: Bootstrap Network
=========================================
[INFO] Bootstrapping network...
[INFO] Collecting enodes from validators...
[✓] Network bootstrapped

=========================================
Phase 4: Validate Deployment
=========================================
[INFO] Validating validator set...
[✓] All validators validated successfully!

=========================================
[✓] Deployment Complete!
=========================================
```

## Monitoring During Execution

### Watch Logs in Real-Time

```bash
# In another terminal, watch the log file
tail -f /opt/smom-dbis-138-proxmox/logs/deploy-validated-set-*.log
```

### Check Container Status

```bash
# List all containers
pct list | grep -E "1000|1001|1002|1003|1004|1500|1501|1502|1503|2500|2501|2502"

# Check specific container
pct status 1000
```

### Monitor Service Logs

```bash
# Watch Besu service logs
pct exec 1000 -- journalctl -u besu-validator -f
```

## Troubleshooting

### If Deployment Fails

1. **Check the log file:**
   ```bash
   tail -100 /opt/smom-dbis-138-proxmox/logs/deploy-validated-set-*.log
   ```

2. **Check container status:**
   ```bash
   pct list
   ```

3. **Check service status:**
   ```bash
   pct exec <vmid> -- systemctl status besu-validator
   ```

4. **Review error messages** in the script output

### Common Issues

**Issue: Containers not starting**
- Check resources (RAM, disk)
- Check OS template availability
- Review container logs

**Issue: Configuration copy fails**
- Verify source project path is correct
- Check source files exist
- Verify containers are running

**Issue: Bootstrap fails**
- Ensure containers are running
- Check P2P port (30303) is accessible
- Verify enode extraction works

**Issue: Validation fails**
- Check validator keys exist
- Verify configuration files are present
- Check services are running

## Post-Deployment Verification

After successful deployment, verify:

```bash
# 1. Check all services are running
for vmid in 1000 1001 1002 1003 1004 1500 1501 1502 1503 2500 2501 2502; do
    echo "=== Container $vmid ==="
    pct exec $vmid -- systemctl status besu-validator besu-sentry besu-rpc --no-pager 2>/dev/null | head -5
done

# 2. Check consensus (block production)
pct exec 2500 -- curl -s -X POST \
    -H "Content-Type: application/json" \
    -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' \
    http://localhost:8545 | python3 -m json.tool

# 3. Check peer connections
pct exec 2500 -- curl -s -X POST \
    -H "Content-Type: application/json" \
    -d '{"jsonrpc":"2.0","method":"admin_peers","params":[],"id":1}' \
    http://localhost:8545 | python3 -m json.tool
```

## Success Criteria

Deployment is successful when:
- ✓ All containers are running
- ✓ All services are active
- ✓ Network is bootstrapped (static-nodes.json deployed)
- ✓ Validators are validated
- ✓ Consensus is active (blocks being produced)
- ✓ Nodes can connect to peers

## Next Steps

After successful deployment:
1. Set up monitoring
2. Configure backups
3. Document node endpoints
4. Set up alerting
5. Plan maintenance schedule
