142 lines
5.6 KiB
PowerShell
142 lines
5.6 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Instala en el equipo actual la infraestructura global de OmniRoute + OpenCode.
|
|
|
|
.DESCRIPTION
|
|
1. Copia el plugin global `omniroute-bootstrap.ts` a la carpeta de plugins de OpenCode
|
|
(~/.config/opencode/plugins/). Asi OpenCode relanza OmniRoute al arrancar, en
|
|
cualquier repositorio.
|
|
2. Registra/actualiza la tarea programada `OmniRoute Watchdog` apuntando al vigilante
|
|
canonico de este repo (INFRAESTRUCTURA).
|
|
3. Arranca la tarea.
|
|
|
|
No requiere administrador. Idempotente: se puede re-ejecutar tras un git pull.
|
|
|
|
.NOTES
|
|
Repo: INFRAESTRUCTURA.
|
|
#>
|
|
[CmdletBinding()]
|
|
param(
|
|
[switch]$NoStart
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
$RepoRoot = Split-Path -Parent $PSScriptRoot
|
|
$TaskName = 'OmniRoute Watchdog'
|
|
$PwshExe = Join-Path $env:SystemRoot 'System32\WindowsPowerShell\v1.0\powershell.exe'
|
|
$Watchdog = Join-Path $RepoRoot 'omniroute\omniroute_watchdog.ps1'
|
|
$PluginSrc = Join-Path $RepoRoot 'opencode\omniroute-bootstrap.ts'
|
|
$PluginDir = Join-Path $env:USERPROFILE '.config\opencode\plugins'
|
|
$PluginDst = Join-Path $PluginDir 'omniroute-bootstrap.ts'
|
|
$UserId = "$env:COMPUTERNAME\$env:USERNAME"
|
|
|
|
# Monitor de estado (Telegram + correo + Jira)
|
|
$TaskMonitor = 'Monitor Estado'
|
|
$WatchdogMonitor = Join-Path $RepoRoot 'scripts\monitor_estado_watchdog.ps1'
|
|
$PluginSrcMon = Join-Path $RepoRoot 'opencode\monitor-estado-bootstrap.ts'
|
|
$PluginDstMon = Join-Path $PluginDir 'monitor-estado-bootstrap.ts'
|
|
$PluginSrcAuto = Join-Path $RepoRoot 'opencode\omniroute-autostart.ts'
|
|
$PluginDstAuto = Join-Path $PluginDir 'omniroute-autostart.ts'
|
|
|
|
Write-Host '== Instalando infraestructura global (OmniRoute + OpenCode) ==' -ForegroundColor Cyan
|
|
|
|
# 1) Plugin global de OpenCode ---------------------------------------------
|
|
if (-not (Test-Path $PluginDir)) { New-Item -ItemType Directory -Force -Path $PluginDir | Out-Null }
|
|
Copy-Item -LiteralPath $PluginSrc -Destination $PluginDst -Force
|
|
Write-Host " - Plugin global instalado en $PluginDst" -ForegroundColor Green
|
|
|
|
Copy-Item -LiteralPath $PluginSrcMon -Destination $PluginDstMon -Force
|
|
Write-Host " - Plugin global instalado en $PluginDstMon" -ForegroundColor Green
|
|
|
|
if (Test-Path $PluginSrcAuto) {
|
|
Copy-Item -LiteralPath $PluginSrcAuto -Destination $PluginDstAuto -Force
|
|
Write-Host " - Plugin global instalado en $PluginDstAuto" -ForegroundColor Green
|
|
}
|
|
|
|
# 2) Tarea programada ------------------------------------------------------
|
|
if (-not (Test-Path $Watchdog)) { throw "No se encuentra el vigilante: $Watchdog" }
|
|
|
|
if (Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue) {
|
|
Write-Host ' - Eliminando tarea anterior...' -ForegroundColor Yellow
|
|
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
|
|
}
|
|
|
|
$Action = New-ScheduledTaskAction `
|
|
-Execute $PwshExe `
|
|
-Argument "-NoProfile -NonInteractive -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$Watchdog`"" `
|
|
-WorkingDirectory $env:USERPROFILE
|
|
|
|
$Trigger = New-ScheduledTaskTrigger -AtLogOn -User $env:USERNAME
|
|
|
|
$Settings = New-ScheduledTaskSettingsSet `
|
|
-AllowStartIfOnBatteries `
|
|
-DontStopIfGoingOnBatteries `
|
|
-StartWhenAvailable `
|
|
-RestartCount 999 `
|
|
-RestartInterval (New-TimeSpan -Minutes 1) `
|
|
-MultipleInstances IgnoreNew
|
|
$Settings.ExecutionTimeLimit = 'PT0S'
|
|
$Settings.Hidden = $true
|
|
|
|
$Principal = New-ScheduledTaskPrincipal -UserId $UserId -LogonType Interactive -RunLevel Limited
|
|
|
|
Register-ScheduledTask `
|
|
-TaskName $TaskName `
|
|
-Action $Action `
|
|
-Trigger $Trigger `
|
|
-Settings $Settings `
|
|
-Principal $Principal `
|
|
-Description 'Arranca y vigila OmniRoute (localhost:20128) al iniciar sesion. Autocuracion.' `
|
|
-Force | Out-Null
|
|
|
|
Write-Host " - Tarea '$TaskName' registrada -> $Watchdog" -ForegroundColor Green
|
|
|
|
# 2b) Tarea programada del Monitor de estado --------------------------------
|
|
if (-not (Test-Path $WatchdogMonitor)) { throw "No se encuentra el vigilante: $WatchdogMonitor" }
|
|
|
|
if (Get-ScheduledTask -TaskName $TaskMonitor -ErrorAction SilentlyContinue) {
|
|
Write-Host ' - Eliminando tarea anterior del monitor...' -ForegroundColor Yellow
|
|
Unregister-ScheduledTask -TaskName $TaskMonitor -Confirm:$false
|
|
}
|
|
|
|
$ActionMon = New-ScheduledTaskAction `
|
|
-Execute $PwshExe `
|
|
-Argument "-NoProfile -NonInteractive -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$WatchdogMonitor`" -Once" `
|
|
-WorkingDirectory $env:USERPROFILE
|
|
|
|
$SettingsMon = New-ScheduledTaskSettingsSet `
|
|
-AllowStartIfOnBatteries `
|
|
-DontStopIfGoingOnBatteries `
|
|
-StartWhenAvailable `
|
|
-MultipleInstances IgnoreNew
|
|
$SettingsMon.ExecutionTimeLimit = 'PT0S'
|
|
$SettingsMon.Hidden = $true
|
|
|
|
Register-ScheduledTask `
|
|
-TaskName $TaskMonitor `
|
|
-Action $ActionMon `
|
|
-Trigger $Trigger `
|
|
-Settings $SettingsMon `
|
|
-Principal $Principal `
|
|
-Description 'Arranca el Monitor de estado (Telegram + correo + Jira) al iniciar sesion.' `
|
|
-Force | Out-Null
|
|
|
|
Write-Host " - Tarea '$TaskMonitor' registrada -> $WatchdogMonitor" -ForegroundColor Green
|
|
|
|
# 3) Arrancar --------------------------------------------------------------
|
|
if (-not $NoStart) {
|
|
Start-ScheduledTask -TaskName $TaskName
|
|
Start-ScheduledTask -TaskName $TaskMonitor
|
|
Write-Host ' - Vigilantes arrancados.' -ForegroundColor Yellow
|
|
}
|
|
|
|
# 4) Watchers de infraestructura (Telegram + recordatorios) -----------------
|
|
Write-Host ''
|
|
& (Join-Path $PSScriptRoot 'instalar_watchers.ps1') -NoStart:$NoStart
|
|
|
|
Write-Host ''
|
|
Write-Host 'Listo. Comprobar:' -ForegroundColor Cyan
|
|
Write-Host " Get-ScheduledTask -TaskName '$TaskName' | Get-ScheduledTaskInfo"
|
|
Write-Host " Get-Content `"$env:USERPROFILE\.omniroute\watchdog.log`" -Tail 20"
|