File size: 8,960 Bytes
b77958b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<#
.SYNOPSIS
    Apply a SmartClass deployment bundle to an edge node.

.DESCRIPTION
    Extracts a deployment bundle ZIP, validates checksums, and deploys
    FAISS index, student map, config, and optional model files to the
    appropriate directories on the edge node.

.PARAMETER BundleZip
    Path to the deployment bundle ZIP file.

.PARAMETER SectionKey
    Section identifier (e.g., AIML-3-A).

.PARAMETER DataDir
    Root data directory on the edge node (default: data).

.PARAMETER BackupExisting
    Create backup of existing files before overwriting (default: true).

.EXAMPLE
    .\apply_deploy_bundle.ps1 -BundleZip data\deploy\latest_bundle.zip -SectionKey AIML-3-A -DataDir data
#>

param(
    [Parameter(Mandatory=$true)]
    [string]$BundleZip,

    [Parameter(Mandatory=$true)]
    [string]$SectionKey,

    [string]$DataDir = "data",

    [bool]$BackupExisting = $true
)

$ErrorActionPreference = "Stop"

# ─── Helper Functions ────────────────────────────────────────────────
function Write-Status($message) {
    Write-Host "  [INFO] $message" -ForegroundColor Cyan
}

function Write-Success($message) {
    Write-Host "  [OK]   $message" -ForegroundColor Green
}

function Write-Warn($message) {
    Write-Host "  [WARN] $message" -ForegroundColor Yellow
}

function Write-Fail($message) {
    Write-Host "  [FAIL] $message" -ForegroundColor Red
}

function Get-FileChecksum($path) {
    $hash = Get-FileHash -Path $path -Algorithm SHA256
    return $hash.Hash.ToLower()
}

# ─── Main ────────────────────────────────────────────────────────────
Write-Host ""
Write-Host "╔══════════════════════════════════════════════════════════╗" -ForegroundColor Blue
Write-Host "β•‘       SmartClass Edge Node - Bundle Deployment          β•‘" -ForegroundColor Blue
Write-Host "β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•" -ForegroundColor Blue
Write-Host ""

# Validate input
if (-not (Test-Path $BundleZip)) {
    Write-Fail "Bundle ZIP not found: $BundleZip"
    exit 1
}

Write-Status "Section: $SectionKey"
Write-Status "Bundle: $BundleZip"
Write-Status "Data directory: $DataDir"
Write-Host ""

# ─── Step 1: Extract bundle to temp directory ────────────────────────
Write-Host "Step 1: Extracting bundle..." -ForegroundColor White
$tempDir = Join-Path $env:TEMP "smartclass_bundle_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
Expand-Archive -Path $BundleZip -DestinationPath $tempDir -Force
Write-Success "Extracted to: $tempDir"

# ─── Step 2: Validate manifest ──────────────────────────────────────
Write-Host "Step 2: Validating manifest..." -ForegroundColor White
$manifestPath = Join-Path $tempDir "manifest.json"
if (-not (Test-Path $manifestPath)) {
    Write-Fail "manifest.json not found in bundle!"
    Remove-Item -Recurse -Force $tempDir
    exit 1
}

$manifest = Get-Content $manifestPath | ConvertFrom-Json
Write-Status "Bundle version: $($manifest.version)"
Write-Status "Created: $($manifest.created_at)"
Write-Status "Section: $($manifest.section_key)"
Write-Status "Students: $($manifest.student_count)"

if ($manifest.section_key -ne $SectionKey) {
    Write-Warn "Bundle section ($($manifest.section_key)) doesn't match target ($SectionKey)"
    $confirm = Read-Host "Continue anyway? (y/N)"
    if ($confirm -ne "y") {
        Remove-Item -Recurse -Force $tempDir
        exit 1
    }
}

# ─── Step 3: Validate checksums ─────────────────────────────────────
Write-Host "Step 3: Validating checksums..." -ForegroundColor White
$checksumFailed = $false
foreach ($file in $manifest.files.PSObject.Properties) {
    $filePath = Join-Path $tempDir $file.Name
    if (Test-Path $filePath) {
        $actualChecksum = Get-FileChecksum $filePath
        $expectedChecksum = $file.Value.checksum
        if ($actualChecksum -eq $expectedChecksum) {
            Write-Success "$($file.Name) - checksum OK"
        } else {
            Write-Fail "$($file.Name) - checksum MISMATCH"
            $checksumFailed = $true
        }
    } else {
        Write-Fail "$($file.Name) - FILE MISSING"
        $checksumFailed = $true
    }
}

if ($checksumFailed) {
    Write-Fail "Checksum validation failed! Aborting deployment."
    Remove-Item -Recurse -Force $tempDir
    exit 1
}

# ─── Step 4: Backup existing files ──────────────────────────────────
if ($BackupExisting) {
    Write-Host "Step 4: Backing up existing files..." -ForegroundColor White
    $backupDir = Join-Path $DataDir "backups" "$(Get-Date -Format 'yyyyMMdd_HHmmss')"
    New-Item -ItemType Directory -Path $backupDir -Force | Out-Null

    $existingIndex = Join-Path $DataDir "faiss_indices" "$SectionKey.index"
    $existingMap = Join-Path $DataDir "student_maps" "${SectionKey}_map.json"

    if (Test-Path $existingIndex) {
        Copy-Item $existingIndex $backupDir
        Write-Status "Backed up FAISS index"
    }
    if (Test-Path $existingMap) {
        Copy-Item $existingMap $backupDir
        Write-Status "Backed up student map"
    }
    Write-Success "Backup saved to: $backupDir"
} else {
    Write-Host "Step 4: Skipping backup (disabled)" -ForegroundColor White
}

# ─── Step 5: Deploy files ───────────────────────────────────────────
Write-Host "Step 5: Deploying files..." -ForegroundColor White

# FAISS index
$faissDir = Join-Path $DataDir "faiss_indices"
New-Item -ItemType Directory -Path $faissDir -Force | Out-Null
$faissSource = Join-Path $tempDir "faiss_index.bin"
if (Test-Path $faissSource) {
    Copy-Item $faissSource (Join-Path $faissDir "$SectionKey.index") -Force
    Write-Success "FAISS index deployed"
}

# Student map
$mapDir = Join-Path $DataDir "student_maps"
New-Item -ItemType Directory -Path $mapDir -Force | Out-Null
$mapSource = Join-Path $tempDir "student_map.json"
if (Test-Path $mapSource) {
    Copy-Item $mapSource (Join-Path $mapDir "${SectionKey}_map.json") -Force
    Write-Success "Student map deployed"
}

# Edge config
$configDir = Join-Path $DataDir ".." "config"
New-Item -ItemType Directory -Path $configDir -Force | Out-Null
$configSource = Join-Path $tempDir "edge_config.yaml"
if (Test-Path $configSource) {
    Copy-Item $configSource (Join-Path $configDir "edge_config.yaml") -Force
    Write-Success "Edge config deployed"
}

# Model files (if included)
$modelsSource = Join-Path $tempDir "models"
if (Test-Path $modelsSource) {
    $modelsDir = Join-Path $DataDir ".." "models"
    New-Item -ItemType Directory -Path $modelsDir -Force | Out-Null
    Copy-Item "$modelsSource\*" $modelsDir -Recurse -Force
    $modelCount = (Get-ChildItem $modelsSource -File).Count
    Write-Success "Deployed $modelCount model files"
}

# ─── Step 6: Cleanup ────────────────────────────────────────────────
Write-Host "Step 6: Cleanup..." -ForegroundColor White
Remove-Item -Recurse -Force $tempDir
Write-Success "Temporary files removed"

# ─── Done ────────────────────────────────────────────────────────────
Write-Host ""
Write-Host "╔══════════════════════════════════════════════════════════╗" -ForegroundColor Green
Write-Host "β•‘            βœ… Bundle deployed successfully!              β•‘" -ForegroundColor Green
Write-Host "β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•" -ForegroundColor Green
Write-Host ""
Write-Status "Section: $SectionKey"
Write-Status "Students: $($manifest.student_count)"
Write-Host ""
Write-Host "  Next steps:" -ForegroundColor White
Write-Host "    1. Restart the edge service: docker restart smartclass-edge" -ForegroundColor Gray
Write-Host "    2. Check logs: docker logs -f smartclass-edge" -ForegroundColor Gray
Write-Host "    3. Verify metrics: curl http://localhost:9100/metrics" -ForegroundColor Gray
Write-Host ""