# ROA2WEB Linux Deployment

Deploy ROA2WEB to Windows IIS production server from Linux/LXC (claude-agent).

## Quick Deploy (TLDR)

```bash
# Din orice director ROA2WEB (main, worktree, sau branch):
# Exemple locații posibile:
#   /workspace/roa2web                           # clone principal
#   /workspace/.worktrees/roa2web/fix/fix-bon    # worktree pentru fix
#   /workspace/.worktrees/roa2web/feature/xxx    # worktree pentru feature

# Deploy complet (frontend + backend)
./deployment/linux/deploy.sh

# Doar frontend
./deployment/linux/deploy.sh frontend

# Doar backend
./deployment/linux/deploy.sh backend

# Test conexiune SSH
./deployment/linux/deploy.sh test
```

**După deploy**: Serverul face auto-deploy în maxim 5 minute (scheduled task).

## Current Setup (claude-agent LXC)

SSH-ul este **deja configurat** pe acest LXC:

```bash
# Verificare conexiune (ar trebui să funcționeze direct)
ssh roa2web-prod "echo OK"
```

**Configurare existentă** (`~/.ssh/config`):
```
Host roa2web-prod
    HostName 10.0.20.36
    Port 22122
    User romfast
    IdentityFile ~/.ssh/roa2web_deploy
```

## Prerequisites

1. **Node.js 16+** - pentru build frontend
2. **SSH access** - deja configurat pe claude-agent LXC

## First-Time Setup (doar dacă SSH nu funcționează)

### 1. Generate SSH Key

```bash
ssh-keygen -t ed25519 -f ~/.ssh/roa2web_deploy -C "roa2web-deploy-lxc"
```

### 2. Configure SSH Host

Add to `~/.ssh/config`:

```
Host roa2web-prod
    HostName 10.0.20.36
    Port 22122
    User romfast
    IdentityFile ~/.ssh/roa2web_deploy
    IdentitiesOnly yes
    StrictHostKeyChecking accept-new
```

### 3. Add Public Key to Server

Copy the content of `~/.ssh/roa2web_deploy.pub`:

```bash
cat ~/.ssh/roa2web_deploy.pub
```

On Windows server (via RDP or SSH):
1. Connect: `ssh romfast@10.0.20.36 -p 22122`
2. Add key to `C:\Users\romfast\.ssh\authorized_keys`

### 4. Test Connection

```bash
ssh roa2web-prod "echo OK"
```

## Usage

```bash
cd /workspace/.worktrees/roa2web/fix/fix-bon

# Full deployment (frontend + backend)
./deployment/linux/deploy.sh

# Frontend only
./deployment/linux/deploy.sh frontend

# Backend only
./deployment/linux/deploy.sh backend

# Test SSH connection
./deployment/linux/deploy.sh test
```

## How It Works

```
[LXC] npm run build → dist/
         ↓
      Package: deploy-package-TIMESTAMP/
         ├── frontend/
         ├── backend/
         ├── shared/
         └── scripts/
         ↓
      SCP → C:\Temp\deploy-TIMESTAMP\
         ↓
[Server] Check-And-Deploy.ps1 (scheduled task, every 5 min)
         ↓
      Auto-deploy to C:\inetpub\wwwroot\roa2web\
```

## Server Configuration

| Setting | Value |
|---------|-------|
| Host | 10.0.20.36 |
| SSH Port | 22122 |
| User | romfast |
| Remote Path | C:\Temp |
| Install Path | C:\inetpub\wwwroot\roa2web |
| Logs Path | C:\inetpub\wwwroot\roa2web\logs |
| **Scripts Path** | **C:\TEMP\ROA2WEB-Scripts** |

### Permanent Scripts Location

Scripturile de deploy (`Check-And-Deploy.ps1`, `ROA2WEB-Console.ps1`) rulează din:
```
C:\TEMP\ROA2WEB-Scripts\
```

**IMPORTANT**: Când modifici scripturile, trebuie să le copiezi și aici:
```bash
# După deploy.sh, copiază scripturile actualizate în locația permanentă:
ssh roa2web-prod "powershell -Command \"Copy-Item -Path 'C:\\Temp\\deploy-*\\scripts\\*.ps1' -Destination 'C:\\TEMP\\ROA2WEB-Scripts\\' -Force\""
```

### What Deploy Preserves

Deploy-ul păstrează automat (NU le șterge):
- **`.env`** - Configurația mediului (credențiale, setări)
- **`data/`** - Directorul cu baze de date SQLite:
  - `data/receipts/receipts_prod.db` - Bonuri fiscale
  - `data/telegram/telegram_prod.db` - Sesiuni Telegram
  - `data/cache/` - Cache SQLite
  - `data/receipts/uploads/` - Fișiere uploadate

## Production Logs

```bash
# View backend stderr (errors)
ssh roa2web-prod "powershell -Command \"Get-Content 'C:\\inetpub\\wwwroot\\roa2web\\logs\\backend-stderr.log' -Tail 100\""

# View backend stdout (info logs)
ssh roa2web-prod "powershell -Command \"Get-Content 'C:\\inetpub\\wwwroot\\roa2web\\logs\\backend-stdout.log' -Tail 100\""

# Filter OCR errors
ssh roa2web-prod "powershell -Command \"Get-Content 'C:\\inetpub\\wwwroot\\roa2web\\logs\\backend-stderr.log' -Tail 200\"" | grep -i ocr
```

## Troubleshooting

### SSH Connection Failed

```bash
# Check SSH config
cat ~/.ssh/config

# Test with verbose output
ssh -v roa2web-prod "echo test"

# Check if key exists
ls -la ~/.ssh/roa2web_deploy*
```

### Build Failed

```bash
# Check Node.js version
node --version

# Reinstall dependencies
rm -rf node_modules
npm install
```

### Transfer Failed

```bash
# Test SCP manually
scp test.txt roa2web-prod:C:/Temp/

# Check Windows firewall (port 22122)
```

## Related Documentation

- [Windows Deployment](../windows/docs/WINDOWS_DEPLOYMENT.md)
- [Two-Tier IIS Architecture](../windows/docs/TWO-TIER-IIS-DEPLOYMENT.md)
- [ROA2WEB Console](../windows/scripts/ROA2WEB-Console.ps1)
