# Agente de Infraestrutura - Cloudflare & Coolify

Este documento contém os tokens e APIs necessárias para gerenciar a infraestrutura.

## Tokens

### Azure DevOps
- **Arquivo**: `~/.ssh/azure_devops_auth.json`
- **Organization**: CN-Squad
- **Project**: Invista FIDC - Nexus
- **PAT**: Token de acesso pessoal para API Azure DevOps

#### Azure DevOps API

```bash
# Configurar PAT
export AZDO_PAT=$(cat ~/.ssh/azure_devops_auth.json | jq -r '.pat')
export AZDO_ORG="CN-Squad"
export AZDO_PROJECT="Invista FIDC - Nexus"

# Listar builds
curl -s -u ":$AZDO_PAT" "https://dev.azure.com/$AZDO_ORG/$AZDO_PROJECT/_apis/build/builds?api-version=7.0" | jq '.value[] | {id, buildNumber, result, status}'

# Ver logs de um build (substituir BUILD_ID e LOG_ID)
curl -s -u ":$AZDO_PAT" "https://dev.azure.com/$AZDO_ORG/_apis/build/builds/BUILD_ID/logs/LOG_ID"

# Obter timeline do build
curl -s -u ":$AZDO_PAT" "https://dev.azure.com/$AZDO_ORG/_apis/build/builds/BUILD_ID/timeline"

# Listar variable groups
curl -s -u ":$AZDO_PAT" "https://dev.azure.com/$AZDO_ORG/$AZDO_PROJECT/_apis/distributedtask/variablegroups?api-version=7.0"

# Triggerar build
curl -s -X POST -u ":$AZDO_PAT" \
  -H "Content-Type: application/json" \
  "https://dev.azure.com/$AZDO_ORG/$AZDO_PROJECT/_apis/build/builds?api-version=7.0" \
  -d '{"definition": {"id": 37}, "sourceBranch": "refs/heads/devops"}'
```

### OCI (Oracle Cloud Infrastructure)
- **Arquivo**: `~/.ssh/azure_devops_auth.json` (mesmo arquivo, contém PAT)
- **Region**: `sa-saopaulo-1`
- **Namespace**: `grbb7qzeuoag`
- **Variable Group**: `oci-terraform`

#### Compartimentos OCI

| Ambiente | Compartment OCID |
|----------|------------------|
| Dev | `ocid1.compartment.oc1..aaaaaaaa76x3nykkjwvctpr6px34dysu3pbg7p62h2r65fegt7fvbrioll3a` |
| HML | `ocid1.compartment.oc1..aaaaaaaawc3hj6isbiibbiuokijw7le72ga3qa3qyblsdhrkj5gdygskzmwq` |
| PROD | `ocid1.compartment.oc1..aaaaaaaazjxk5dkwleol3hjq7huojevotvnjhvy5ngro4jtwcqx2fn2ujara` |

#### OCI CLI (local)

```bash
# Verificar namespace
oci os ns get

# Listar buckets (dev)
oci os bucket list --compartment-id "ocid1.compartment.oc1..aaaaaaaa76x3nykkjwvctpr6px34dysu3pbg7p62h2r65fegt7fvbrioll3a"

# Listar objetos no bucket
oci os object list --namespace-name "grbb7qzeuoag" --bucket-name "mfe-user-dev"

# Criar bucket
oci os bucket create \
  --namespace-name "grbb7qzeuoag" \
  --name "mfe-nome-dev" \
  --compartment-id "ocid1.compartment.oc1..aaaaaaaa76x3nykkjwvctpr6px34dysu3pbg7p62h2r65fegt7fvbrioll3a"

# Upload arquivo
oci os object put \
  --namespace-name "grbb7qzeuoag" \
  --bucket-name "mfe-user-dev" \
  --file ./dist/browser/index.html \
  --name "index.html"
```

### Cloudflare
- **Arquivo**: `~/.ssh/cloudflare-token`
- **Email**: yamamoto@rede5.com.br
- **Zone ID (gohorsejobs.com)**: `5e7e9286849525abf7f30b451b7964ac`

### Coolify (Redbull)
- **Arquivo**: `~/.ssh/coolify-redbull-token`
- **URL**: https://redbull.rede5.com.br/api/v1
- **Server UUID**: `m844o4gkwkwcc0k48swgs8c8`

## Comandos Rápidos

### Cloudflare DNS

```bash
# Configurar variáveis
export CF_AUTH_EMAIL="yamamoto@rede5.com.br"
export CF_AUTH_KEY="5dcfd89a9d4ec330dede0d4074a518f26818e"
export CF_ZONE_ID="5e7e9286849525abf7f30b451b7964ac"

# Listar registros DNS
curl -s -H "X-Auth-Email: $CF_AUTH_EMAIL" -H "X-Auth-Key: $CF_AUTH_KEY" \
  "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records"

# Buscar por subdomínio
curl -s -H "X-Auth-Email: $CF_AUTH_EMAIL" -H "X-Auth-Key: $CF_AUTH_KEY" \
  "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records?name=gohorsejobs.com" | grep -o '"name":"[^"]*"'

# Criar A record
curl -s -X POST -H "X-Auth-Email: $CF_AUTH_EMAIL" -H "X-Auth-Key: $CF_AUTH_KEY" \
  -H "Content-Type: application/json" \
  "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records" \
  -d '{"type":"A","name":"novo.gohorsejobs.com","content":"185.194.141.70","proxied":false}'

# Atualizar DNS record (precisa do ID)
curl -s -X PUT -H "X-Auth-Email: $CF_AUTH_EMAIL" -H "X-Auth-Key: $CF_AUTH_KEY" \
  -H "Content-Type: application/json" \
  "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/<RECORD_ID>" \
  -d '{"type":"A","name":"nome.gohorsejobs.com","content":"NOVO_IP","proxied":false}'

# Deletar DNS record
curl -s -X DELETE -H "X-Auth-Email: $CF_AUTH_EMAIL" -H "X-Auth-Key: $CF_AUTH_KEY" \
  "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/<RECORD_ID>"

# Limpar cache
curl -s -X DELETE -H "X-Auth-Email: $CF_AUTH_EMAIL" -H "X-Auth-Key: $CF_AUTH_KEY" \
  -H "Content-Type: application/json" \
  "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/purge_cache" \
  -d '{"purge_everything":true}'
```

### Coolify API

```bash
# Configurar variáveis
export COOLIFY_TOKEN="tjaxz6z6osQUz6whMvNMw23kcBqgToQAvMjSLos347164da9"
export COOLIFY_URL="https://redbull.rede5.com.br/api/v1"

# Listar aplicações
curl -s -H "Authorization: Bearer $COOLIFY_TOKEN" "$COOLIFY_URL/applications"

# Ver aplicação específica
curl -s -H "Authorization: Bearer $COOLIFY_TOKEN" "$COOLIFY_URL/applications/<UUID>"

# Deploy aplicação
curl -s -H "Authorization: Bearer $COOLIFY_TOKEN" "$COOLIFY_URL/deploy?uuid=<UUID>"

# Atualizar domínio (importante: usar http:// ou https://)
curl -s -X PATCH -H "Authorization: Bearer $COOLIFY_TOKEN" \
  -H "Content-Type: application/json" \
  "$COOLIFY_URL/applications/<UUID>" \
  -d '{"domains":"http://novo-dominio.gohorsejobs.com","instant_deploy":true}'

# Ver domínios do servidor
curl -s -H "Authorization: Bearer $COOLIFY_TOKEN" \
  "$COOLIFY_URL/servers/m844o4gkwkwcc0k48swgs8c8/domains"

# Listar deployments
curl -s -H "Authorization: Bearer $COOLIFY_TOKEN" "$COOLIFY_URL/deployments"

# Ver status de deployment
curl -s -H "Authorization: Bearer $COOLIFY_TOKEN" \
  "$COOLIFY_URL/deployments/<DEPLOYMENT_UUID>"

# Listar databases
curl -s -H "Authorization: Bearer $COOLIFY_TOKEN" "$COOLIFY_URL/databases"
```

## UUIDs GoHorseJobs (Redbull Coolify)

| Serviço | UUID |
|---------|------|
| Backend | `iw4sow8s0kkg4cccsk08gsoo` |
| Frontend | `ao8g40scws0w4cgo8coc8o40` |
| Backoffice | `hg48wkw4wggwsswcwc8sooo4` |
| Seeder | `q4w48gos8cgssso00o8w8gck` |
| Database | `bgws48os8wgwk08o48wg8k80` |

## Fluxo Padrão: Criar Novo Subdomínio

1. **Criar DNS no Cloudflare:**
```bash
curl -s -X POST -H "X-Auth-Email: $CF_AUTH_EMAIL" -H "X-Auth-Key: $CF_AUTH_KEY" \
  -H "Content-Type: application/json" \
  "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records" \
  -d '{"type":"A","name":"nome.gohorsejobs.com","content":"185.194.141.70","proxied":false}'
```

2. **Atualizar domínio no Coolify:**
```bash
curl -s -X PATCH -H "Authorization: Bearer $COOLIFY_TOKEN" \
  -H "Content-Type: application/json" \
  "$COOLIFY_URL/applications/<UUID>" \
  -d '{"domains":"http://nome.gohorsejobs.com","instant_deploy":true}'
```

## Referências

- [Cloudflare API Docs](https://api.cloudflare.com/)
- [Coolify API Docs](https://coolify.io/docs/api-reference/authorization)
- Documentação: `../gohorsejobs/docs/DEVOPS.md`

---

## Rodizios Daqui (Coolify - Redbull)

### URLs
- Frontend: https://rodiziosdaqui.rede5.com.br
- Backend: https://api.rodiziosdaqui.rede5.com.br
- Backoffice: https://backoffice.rodiziosdaqui.rede5.com.br
- Seeder: https://seeder.rodiziosdaqui.rede5.com.br

### UUIDs Coolify

| Serviço | UUID |
|---------|------|
| Backend | ecso4woo4woo0ks84888k8ck |
| Frontend | l4g4cc884gc04kg8s480gs48 |
| Backoffice | s4kskw08400wcw8g40ossggo |
| Seeder | ko0kkw4kw8g80scksg8sk0wc |

### Deploy

```bash
# Deploy all
curl -s -H "Authorization: Bearer $(cat ~/.ssh/coolify-redbull-token)" "https://redbull.rede5.com.br/api/v1/deploy?uuid=ecso4woo4woo0ks84888k8ck" # Backend
curl -s -H "Authorization: Bearer $(cat ~/.ssh/coolify-redbull-token)" "https://redbull.rede5.com.br/api/v1/deploy?uuid=l4g4cc884gc04kg8s480gs48" # Frontend
curl -s -H "Authorization: Bearer $(cat ~/.ssh/coolify-redbull-token)" "https://redbull.rede5.com.br/api/v1/deploy?uuid=s4kskw08400wcw8g40ossggo" # Backoffice
curl -s -H "Authorization: Bearer $(cat ~/.ssh/coolify-redbull-token)" "https://redbull.rede5.com.br/api/v1/deploy?uuid=ko0kkw4kw8g80scksg8sk0wc" # Seeder
```
