vost commited on
Commit
cd07f3b
·
verified ·
1 Parent(s): b5f6901

Upload 4 files

Browse files
Guardians/HW-Guardian/Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # HW-Guardian/Dockerfile
2
+ FROM python:3.12-slim
3
+
4
+ # Instala dependências de sistema para o psutil e ferramentas de monitoramento
5
+ RUN apt-get update && apt-get install -y \
6
+ gcc \
7
+ python3-dev \
8
+ && rm -rf /var/lib/apt/lists/*
9
+
10
+ RUN pip install --no-cache-dir psutil
11
+
12
+ WORKDIR /app
13
+ COPY guardian_logic.py .
14
+
15
+ # O script roda como root para ter acesso aos sensores no /sys
16
+ CMD ["python", "guardian_logic.py"]
Guardians/HW-Guardian/docker-compose.yaml ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AI-LAB/Guardians/HW-Guardian/docker-compose.yaml
2
+ services:
3
+ hw-guardian:
4
+ build: .
5
+ container_name: hw-guardian
6
+ privileged: true
7
+ volumes:
8
+ - Guardians-Share:/mnt/flags
9
+ - /sys:/sys:ro # Acesso aos sensores térmicos
10
+ restart: always
11
+
12
+ volumes:
13
+ Guardians-Share:
14
+ external: true # Criaremos manualmente ou via script principal
Guardians/HW-Guardian/guardian_logic.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import psutil
2
+ import time
3
+ import os
4
+
5
+ # Configurações Hardcoded (Rigidez e Estaticidade)
6
+ FLAGS_PATH = "/mnt/flags"
7
+ CHECK_INTERVAL = 5 # segundos
8
+ RAM_CHECK_INTERVAL = 10 # segundos
9
+
10
+ def get_cpu_temp():
11
+ # Tenta ler das zonas térmicas padrão do kernel Linux (WSL2/Linux)
12
+ try:
13
+ # Busca a temperatura mais alta entre as zonas disponíveis
14
+ temps = []
15
+ for i in range(10): # Checa até 10 zonas possíveis
16
+ path = f"/sys/class/thermal/thermal_zone{i}/temp"
17
+ if os.path.exists(path):
18
+ with open(path, "r") as f:
19
+ temps.append(int(f.read().strip()) / 1000)
20
+ return max(temps) if temps else 0
21
+ except:
22
+ return 0
23
+
24
+ def set_flag(state):
25
+ # Limpa flags antigas e define a nova
26
+ for f in ["NORMAL", "HIGH", "CRÍTICO", "OVERLOAD", "STANDBY"]:
27
+ p = os.path.join(FLAGS_PATH, f"STATE_{f}.txt")
28
+ if os.path.exists(p): os.remove(p)
29
+
30
+ with open(os.path.join(FLAGS_PATH, f"STATE_{state}.txt"), "w") as f:
31
+ f.write(f"TIMESTAMP: {time.ctime()}\nTEMP: {get_cpu_temp()}C\nRAM: {psutil.virtual_memory().percent}%")
32
+
33
+ def monitor():
34
+ overload_start = None
35
+ critico_start = None
36
+
37
+ while True:
38
+ temp = get_cpu_temp()
39
+ ram = psutil.virtual_memory().percent
40
+
41
+ # LOGICA DE ESTADOS
42
+ if temp > 95 or ram > 95:
43
+ if not overload_start: overload_start = time.time()
44
+ if time.time() - overload_start > 10:
45
+ set_flag("OVERLOAD")
46
+ elif temp > 90 or ram > 90:
47
+ if not critico_start: critico_start = time.time()
48
+ if time.time() - critico_start > 20:
49
+ set_flag("CRÍTICO")
50
+ elif temp > 82 or ram > 82:
51
+ set_flag("HIGH")
52
+ overload_start = critico_start = None
53
+ elif temp > 65:
54
+ set_flag("STANDBY")
55
+ overload_start = critico_start = None
56
+ else:
57
+ set_flag("NORMAL")
58
+ overload_start = critico_start = None
59
+
60
+ time.sleep(CHECK_INTERVAL)
61
+
62
+ if __name__ == "__main__":
63
+ monitor()
boot.ps1 CHANGED
@@ -1,12 +1,13 @@
1
- # --- AI-LAB Windows Smart Agent (Self-Updating) ---
2
- # Este script gerencia sua própria atualização, a pasta C:\AI-LAB e o atalho na Área de Trabalho.
 
3
  $ErrorActionPreference = "Stop"
4
  $INSTALL_PATH = "C:\AI-LAB"
5
  $SCRIPT_NAME = "boot.ps1"
6
  $FULL_SCRIPT_PATH = Join-Path $INSTALL_PATH $SCRIPT_NAME
7
  $REPO_RAW_URL = "https://huggingface.co/vost/pub-scripts/raw/main/"
8
 
9
- # 1. Garantir Privilégios e Pastas Estruturais
10
  if (-not (Test-Path $INSTALL_PATH)) {
11
  New-Item -Path $INSTALL_PATH -ItemType Directory -Force | Out-Null
12
  }
@@ -14,89 +15,87 @@ Set-Location $INSTALL_PATH
14
 
15
  # 2. Verificação do Docker (Winget)
16
  if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
17
- # Solicita admin apenas se precisar instalar o Docker
18
  if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
19
- Write-Host "Docker não encontrado. Solicitando permissões de Administrador para instalação..." -ForegroundColor Cyan
20
  Start-Process powershell.exe -ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
21
  exit
22
  }
23
-
24
- Write-Host "Docker não encontrado. Iniciando instalação..." -ForegroundColor Yellow
25
  winget install --id Docker.DockerDesktop --silent --accept-package-agreements
26
- Write-Host "Por favor, REINICIE o computador e clique no atalho AI-LAB na Área de Trabalho." -ForegroundColor Red
27
  exit
28
  }
29
 
 
30
  function Sync-RemoteFile {
31
  param (
32
  [Parameter(Mandatory=$true)][string]$FileName,
33
  [Parameter(Mandatory=$true)][string]$DestinationPath,
34
  [switch]$RestartOnUpdate
35
  )
36
-
37
  $RemoteUrl = "$REPO_RAW_URL$FileName"
38
  $TempFile = Join-Path $env:TEMP "ai-lab-$FileName"
39
-
40
  try {
41
  if (Test-Path $DestinationPath) {
42
  Invoke-WebRequest -Uri $RemoteUrl -OutFile $TempFile -UseBasicParsing
43
-
44
  $localHash = (Get-FileHash $DestinationPath -Algorithm SHA256).Hash
45
  $remoteHash = (Get-FileHash $TempFile -Algorithm SHA256).Hash
46
 
47
  if ($localHash -ne $remoteHash) {
48
- Write-Host "Nova versão de [$FileName] detectada! Atualizando..." -ForegroundColor Magenta
49
  Move-Item -Path $TempFile -Destination $DestinationPath -Force
50
  if ($RestartOnUpdate) {
51
- Write-Host "Reiniciando script para aplicar alterações..." -ForegroundColor Cyan
52
  Start-Process powershell.exe -ArgumentList "-ExecutionPolicy Bypass -File `"$DestinationPath`""
53
  exit
54
  }
55
- } else {
56
- Write-Host "[$FileName] já está na versão mais recente." -ForegroundColor Green
57
  }
58
  } else {
59
- Write-Host "Baixando versão inicial de [$FileName]..." -ForegroundColor Cyan
 
60
  Invoke-WebRequest -Uri $RemoteUrl -OutFile $DestinationPath -UseBasicParsing
61
  }
62
- } catch {
63
- Write-Warning "Falha ao sincronizar [$FileName]. Usando versão local se disponível."
64
- } finally {
65
- if (Test-Path $TempFile) { Remove-Item $TempFile -ErrorAction SilentlyContinue }
66
- }
67
  }
68
 
69
- # 3. Lógica de Auto-Atualização com Re-execução
70
- Write-Host "--- Verificando atualizações do Agente ---" -ForegroundColor Cyan
71
  Sync-RemoteFile -FileName $SCRIPT_NAME -DestinationPath $FULL_SCRIPT_PATH -RestartOnUpdate
72
 
73
- # 4. Solicita o Token (Uma única vez)
74
- if (-not $env:HF_TOKEN) {
75
  $token = Read-Host "Insira seu HuggingFace Token (Read)"
76
  [Environment]::SetEnvironmentVariable("HF_TOKEN", $token, "User")
77
  }
78
 
79
- # 5. Sincronização do docker-compose.yaml
80
- Write-Host "--- Sincronizando Infraestrutura (YAML) ---" -ForegroundColor Cyan
81
- Sync-RemoteFile -FileName "docker-compose.yaml" -DestinationPath (Join-Path $INSTALL_PATH "docker-compose.yaml")
 
 
 
 
 
82
 
83
- # 6. Execução Final do Docker
84
- Write-Host "--- Iniciando containers do Laboratório ---" -ForegroundColor Green
85
- docker compose up -d
 
 
 
 
 
 
 
86
 
87
- # 7. Criação do Atalho na Área de Trabalho
88
  $ShortcutPath = Join-Path ([Environment]::GetFolderPath("Desktop")) "AI-LAB.lnk"
89
  if (-not (Test-Path $ShortcutPath)) {
90
- Write-Host "Criando atalho na Área de Trabalho..." -ForegroundColor Gray
91
  $WshShell = New-Object -ComObject WScript.Shell
92
  $Shortcut = $WshShell.CreateShortcut($ShortcutPath)
93
  $Shortcut.TargetPath = "powershell.exe"
94
  $Shortcut.Arguments = "-ExecutionPolicy Bypass -File `"$FULL_SCRIPT_PATH`""
95
  $Shortcut.WorkingDirectory = $INSTALL_PATH
96
- $Shortcut.IconLocation = "powershell.exe,0" # Ícone do PowerShell
97
  $Shortcut.Save()
98
  }
99
 
100
- Write-Host "`n[SUCESSO] Seu AI-LAB está rodando e sincronizado." -ForegroundColor Green
101
- Write-Host "Pressione qualquer tecla para sair..."
102
  $null = [System.Console]::ReadKey()
 
1
+ # --- AI-LAB Windows Smart Start UP ---
2
+ # Inicia os Containers e sincroniza o ambiente
3
+
4
  $ErrorActionPreference = "Stop"
5
  $INSTALL_PATH = "C:\AI-LAB"
6
  $SCRIPT_NAME = "boot.ps1"
7
  $FULL_SCRIPT_PATH = Join-Path $INSTALL_PATH $SCRIPT_NAME
8
  $REPO_RAW_URL = "https://huggingface.co/vost/pub-scripts/raw/main/"
9
 
10
+ # 1. Garantir Pastas Estruturais
11
  if (-not (Test-Path $INSTALL_PATH)) {
12
  New-Item -Path $INSTALL_PATH -ItemType Directory -Force | Out-Null
13
  }
 
15
 
16
  # 2. Verificação do Docker (Winget)
17
  if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
 
18
  if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
19
+ Write-Host "Docker não encontrado. Solicitando permissões de Administrador..." -ForegroundColor Cyan
20
  Start-Process powershell.exe -ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
21
  exit
22
  }
23
+ Write-Host "Instalando Docker Desktop..." -ForegroundColor Yellow
 
24
  winget install --id Docker.DockerDesktop --silent --accept-package-agreements
25
+ Write-Host "REINICIE o computador e execute o AI-LAB novamente." -ForegroundColor Red
26
  exit
27
  }
28
 
29
+ # --- FUNÇÃO DE SINCRONIZAÇÃO ---
30
  function Sync-RemoteFile {
31
  param (
32
  [Parameter(Mandatory=$true)][string]$FileName,
33
  [Parameter(Mandatory=$true)][string]$DestinationPath,
34
  [switch]$RestartOnUpdate
35
  )
 
36
  $RemoteUrl = "$REPO_RAW_URL$FileName"
37
  $TempFile = Join-Path $env:TEMP "ai-lab-$FileName"
 
38
  try {
39
  if (Test-Path $DestinationPath) {
40
  Invoke-WebRequest -Uri $RemoteUrl -OutFile $TempFile -UseBasicParsing
 
41
  $localHash = (Get-FileHash $DestinationPath -Algorithm SHA256).Hash
42
  $remoteHash = (Get-FileHash $TempFile -Algorithm SHA256).Hash
43
 
44
  if ($localHash -ne $remoteHash) {
45
+ Write-Host "Atualizando [$FileName]..." -ForegroundColor Magenta
46
  Move-Item -Path $TempFile -Destination $DestinationPath -Force
47
  if ($RestartOnUpdate) {
 
48
  Start-Process powershell.exe -ArgumentList "-ExecutionPolicy Bypass -File `"$DestinationPath`""
49
  exit
50
  }
 
 
51
  }
52
  } else {
53
+ Write-Host "Baixando [$FileName]..." -ForegroundColor Cyan
54
+ New-Item -ItemType File -Path $DestinationPath -Force | Out-Null
55
  Invoke-WebRequest -Uri $RemoteUrl -OutFile $DestinationPath -UseBasicParsing
56
  }
57
+ } catch { Write-Warning "Falha ao sincronizar [$FileName]." }
 
 
 
 
58
  }
59
 
60
+ # 3. Auto-Atualização do Agente
 
61
  Sync-RemoteFile -FileName $SCRIPT_NAME -DestinationPath $FULL_SCRIPT_PATH -RestartOnUpdate
62
 
63
+ # 4. Token HuggingFace (Persistente no Usuário)
64
+ if (-not [Environment]::GetEnvironmentVariable("HF_TOKEN", "User")) {
65
  $token = Read-Host "Insira seu HuggingFace Token (Read)"
66
  [Environment]::SetEnvironmentVariable("HF_TOKEN", $token, "User")
67
  }
68
 
69
+ # --- 5. PREPARAÇÃO DO AMBIENTE DOCKER ---
70
+ Write-Host "--- Preparando Infraestrutura ---" -ForegroundColor Cyan
71
+
72
+ # Garante que o volume compartilhado exista ANTES do compose up
73
+ if (-not (docker volume ls -q -f name=Guardians-Share)) {
74
+ Write-Host "Criando Volume Compartilhado (Guardians-Share)..." -ForegroundColor Gray
75
+ docker volume create Guardians-Share
76
+ }
77
 
78
+ # 6. Execução do HW-Guardian
79
+ Write-Host "--- Iniciando HW-Guardian ---" -ForegroundColor Green
80
+ # Corrigido: 'docker build' em vez de 'built' e apontando para o contexto correto
81
+ $GUARDIAN_DIR = "$INSTALL_PATH\AI-LAB\Guardians\HW-Guardian"
82
+ if (Test-Path $GUARDIAN_DIR) {
83
+ docker build -t hw-guardian-base "$GUARDIAN_DIR" --no-cache
84
+ docker compose -f "$GUARDIAN_DIR\docker-compose.yaml" up -d
85
+ } else {
86
+ Write-Warning "Pasta do HW-Guardian não encontrada em $GUARDIAN_DIR"
87
+ }
88
 
89
+ # 7. Atalho na Área de Trabalho
90
  $ShortcutPath = Join-Path ([Environment]::GetFolderPath("Desktop")) "AI-LAB.lnk"
91
  if (-not (Test-Path $ShortcutPath)) {
 
92
  $WshShell = New-Object -ComObject WScript.Shell
93
  $Shortcut = $WshShell.CreateShortcut($ShortcutPath)
94
  $Shortcut.TargetPath = "powershell.exe"
95
  $Shortcut.Arguments = "-ExecutionPolicy Bypass -File `"$FULL_SCRIPT_PATH`""
96
  $Shortcut.WorkingDirectory = $INSTALL_PATH
 
97
  $Shortcut.Save()
98
  }
99
 
100
+ Write-Host "`n[SUCESSO] AI-LAB Ativo. Pressione qualquer tecla..." -ForegroundColor Green
 
101
  $null = [System.Console]::ReadKey()