Files
jhonny-editor/admin/deploy.command
T
João Henrique b541f502ba feat: initial commit - Jhonny Editor
- Adicionado estrutura completa do projeto
- Configurado MCP server para Premiere Pro
- Adicionado documentação e skills
- Configurado Gitignore para o projeto
2026-09-08 09:59:31 -04:00

129 lines
4.1 KiB
Bash
Executable File

#!/bin/zsh
# ============================================================
# OpenCut Ronald - admin/deploy.command
#
# DEPLOY: garante o backend OpenCut no ar em http://127.0.0.1:5679
# e sobe a ponte de live-updates (WebSocket) que o painel consome.
#
# E este o script que o painel do Premiere dispara quando pede
# "iniciar backend" (via ~/.opencut/launcher.command). Por isso ele
# nao pergunta nada e nao mexe no git: roda sozinho e sai.
#
# Uso:
# admin/deploy.command sobe (ou recupera) o backend
# OPENCUT_RESTART=1 admin/deploy.command reinicia o backend atual
# OPENCUT_NO_OPEN=1 admin/deploy.command nao abre o navegador
# ============================================================
set -u
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
CODE="$ROOT/code"
START_SCRIPT="$CODE/start-opencut.sh"
RUNTIME_DIR="$CODE/.opencut-runtime"
LOG="$RUNTIME_DIR/server.log"
URL="http://127.0.0.1:5679"
HEALTH="$URL/health"
RESTART="${OPENCUT_RESTART:-0}"
notify() { osascript -e "display notification \"$1\" with title \"OpenCut\"" 2>/dev/null || true; }
open_ui() { [ "${OPENCUT_NO_OPEN:-0}" = "1" ] || open "$URL" 2>/dev/null || true; }
server_ok() { curl -s -m 5 "$HEALTH" 2>/dev/null | grep -q '"status":"ok"'; }
stop_opencut_pids() {
local signal="$1"
local pid
for pid in ${(f)PGREP}; do
[ -n "$pid" ] && kill "$signal" "$pid" 2>/dev/null || true
done
}
# ------------------------------------------------------------
# Ponte de live-updates (WebSocket)
#
# O backend nao sobe a ponte sozinho: ela so existe apos um POST em
# /ws/start, e esse endpoint exige o token CSRF que o /health entrega.
# Sem este passo o painel abre mostrando "Bridge stopped" e o usuario
# precisa clicar em "Start" na mao a cada boot.
# ------------------------------------------------------------
start_bridge() {
if curl -s -m 5 "$URL/ws/status" 2>/dev/null | grep -q '"running":true'; then
echo "-> ponte de live-updates ja ativa"
return 0
fi
local token
token="$(curl -s -m 5 -H "Origin: $URL" "$HEALTH" 2>/dev/null \
| /usr/bin/python3 -c 'import sys,json
try: print(json.load(sys.stdin).get("csrf_token") or "")
except Exception: print("")' 2>/dev/null)"
if [ -z "$token" ]; then
echo "-> Aviso: /health nao devolveu token CSRF; ponte nao iniciada"
return 1
fi
if curl -s -m 10 -X POST "$URL/ws/start" \
-H "Content-Type: application/json" \
-H "X-OpenCut-Token: $token" \
-H "Origin: $URL" -d '{}' 2>/dev/null | grep -q '"running":true'; then
echo "-> ponte de live-updates ativa"
return 0
fi
echo "-> Aviso: a ponte de live-updates nao subiu (veja $LOG)"
return 1
}
# 1) Ja esta no ar?
if server_ok && [ "$RESTART" != "1" ]; then
echo "-> backend ja rodando em $URL"
start_bridge
notify "Backend ja esta rodando em $URL"
open_ui
exit 0
fi
if [ "$RESTART" = "1" ]; then
echo "-> reinicio solicitado; substituindo o backend atual"
fi
# 2) Processo antigo travado? Derruba antes de subir outro.
PGREP=$(pgrep -f 'opencut.server' || true)
if [ -n "$PGREP" ]; then
notify "Backend travado detectado - reiniciando..."
echo "Encerrando processo(s) antigo(s): $PGREP"
stop_opencut_pids TERM
sleep 3
PGREP=$(pgrep -f 'opencut.server' || true)
[ -n "$PGREP" ] && stop_opencut_pids KILL
sleep 1
fi
# 3) Sobe pelo start-opencut.sh (venv, modelos, ffmpeg e tmp no Merongo)
if [ ! -x "$START_SCRIPT" ]; then
echo "Erro: $START_SCRIPT nao encontrado ou sem permissao de execucao" >&2
notify "Falha: start-opencut.sh ausente"
exit 1
fi
mkdir -p "$RUNTIME_DIR"
mkdir -p "/Volumes/Merongo/SISTEMAS/OPENCUT-MEDIA/tmp"
nohup "$START_SCRIPT" > "$LOG" 2>&1 &
NEW_PID=$!
echo "Backend iniciado (PID $NEW_PID). Aguardando health check..."
# 4) Espera ate 60s pelo health check
for i in {1..30}; do
if server_ok; then
start_bridge
notify "Backend pronto em $URL"
open_ui
exit 0
fi
if ! kill -0 $NEW_PID 2>/dev/null; then
break
fi
sleep 2
done
echo "Falha ao iniciar. Log em $LOG" >&2
notify "Falha ao iniciar o backend. Veja $LOG"
tail -30 "$LOG"
exit 1