generated from ThalesGroup/template-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.ps1
executable file
·310 lines (262 loc) · 10.8 KB
/
install.ps1
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
param(
[Parameter(Mandatory = $false)]
[switch]$Offline,
[Parameter(Mandatory = $false)]
[string]$OpenaiApiKey,
[Parameter(Mandatory = $false)]
[string]$AgiCredentials = "",
[Parameter(Mandatory = $false)]
[string]$InstallPath = (Get-Location).Path
)
if (-not $Offline) {
if (-not $OpenaiApiKey) {
throw "OpenaiApiKey is required when not in offline mode."
}
if (-not $AgiCredentials) {
throw "AgiCredentials is required when not in offline mode."
}
}
$LogDir = Join-Path $env:USERPROFILE "log\install_logs"
if (-not (Test-Path $LogDir)) { New-Item -ItemType Directory -Path $LogDir | Out-Null }
$LogFile = Join-Path $LogDir ("install_{0}.log" -f (Get-Date -Format "yyyyMMdd_HHmmss"))
Start-Transcript -Path $LogFile
Get-ChildItem -Recurse -Directory | Where-Object {
$_.Name -match '\.venv|uv.lock|build|dist|.*egg-info'
} | Remove-Item -Recurse -Force
# ================================
# Prevent Running as Administrator
# ================================
if ([Security.Principal.WindowsPrincipal]::new([Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "Error: This script should not be run as Administrator. Please run as a regular user." -ForegroundColor Red
Stop-Transcript
exit 1
}
# ================================
# Global Variables and Paths
# ================================
# AGI_INSTALL_PATH corresponds to $InstallPath.
$CurrentPath = (Get-Location).Path
$LocalDir = Join-Path $env:LOCALAPPDATA "agilab"
New-Item -ItemType Directory -Force -Path $LocalDir | Out-Null
$AgiPathFile = Join-Path $LocalDir ".agi-path"
$PYTHON_VERSION = "3.12"
# Define project directories (AGI_PROJECT_SRC is "$AgiDir\src")
$AgiProject = Join-Path $CurrentPath "src\agilab"
$FrameworkDir = Join-Path $AgiProject "fwk"
$AppsDir = Join-Path $AgiProject "apps"
Write-Host "Installation Directory: $InstallPath" -ForegroundColor Cyan
if (-not $Offline)
{
Write-Host "Selected user: $AgiCredentials" -ForegroundColor Yellow
Write-Host "OpenAI API Key: $OpenaiApiKey" -ForegroundColor Yellow
}
# ================================
# Utility Functions
# ================================
function Check-VisualStudio {
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
if (Test-Path $vswhere) {
$vs2022 = & $vswhere -version 17.0 -latest -products * -property installationPath
if ($vs2022) {
Write-Host "Visual Studio 2022 is installed at: $vs2022" -ForegroundColor Green
} else {
Write-Host "Visual Studio 2022 is not found." -ForegroundColor Red
exit 1
}
} else {
Write-Host "vswhere.exe not found. Cannot determine VS installation." -ForegroundColor Red
Write-Host "Please ensure Visual Studio 2022 is installed before continuing." -ForegroundColor Green
exit 1
}
}
function Check-Internet {
Write-Host "Checking internet connectivity..." -ForegroundColor Blue
try {
$response = Invoke-WebRequest -Uri "https://www.google.com" -Method Head -TimeoutSec 10
if ($response.StatusCode -eq 200) {
Write-Host "Internet connection is OK." -ForegroundColor Green
}
}
catch {
Write-Host "No internet connection detected. Abording." -ForegroundColor Red
Stop-Transcript
exit 1
}
Write-Host ""
}
function Install-Dependencies {
Write-Host "Installing system dependencies" -ForegroundColor Blue
Write-Host ""
$choice = Read-Host "Do you want to install system dependencies? (y/N)"
if ($choice -match "^[Yy]$") {
if (-not (Get-Command "uv" -ErrorAction SilentlyContinue))
{
Write-Host "Installing uv..." -ForegroundColor Blue
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
}
Write-Host "NOTE: Please install required dependencies manually or via your preferred package manager on Windows." -ForegroundColor Yellow
# Optionally, add code here to install dependencies using Chocolatey if desired.
}
Write-Host ""
}
function Choose-PythonVersion {
Write-Host "Choosing Python version..." -ForegroundColor Blue
$availablePythonVersions = uv python list | Where-Object { $_ -match $PYTHON_VERSION }
if (-not $availablePythonVersions) {
Write-Host "No matching Python versions found for '$PYTHON_VERSION'" -ForegroundColor Red
exit 1
}
$pythonArray = @()
foreach ($line in $availablePythonVersions) {
$pythonArray += $line
}
for ($i = 0; $i -lt $pythonArray.Count; $i++) {
if ($pythonArray[$i] -match $PYTHON_VERSION) {
Write-Host "$($i + 1) - $($pythonArray[$i])" -ForegroundColor Green
} else {
Write-Host "$($i + 1) - $($pythonArray[$i])"
}
}
do {
$selection = Read-Host "Enter the number of the Python version you want to use (default: 1)"
if ([string]::IsNullOrWhiteSpace($selection)) {
$selection = 1
}
$valid = $selection -as [int] -and $selection -ge 1 -and $selection -le $pythonArray.Count
if (-not $valid) {
Write-Host "Invalid selection. Please try again." -ForegroundColor Red
}
} while (-not $valid)
$chosenPython = ($pythonArray[$selection - 1] -split '\s+')[0]
$installedPythons = (uv python list --only-installed | ForEach-Object { ($_ -split '\s+')[0] })
if ($installedPythons -notcontains $chosenPython) {
Write-Host "Installing $chosenPython..." -ForegroundColor Yellow
uv python install $chosenPython
Write-Host "Python version ($chosenPython) is now installed." -ForegroundColor Green
} else {
Write-Host "Python version ($chosenPython) is already installed." -ForegroundColor Green
}
$env:PYTHON_VERSION = ($chosenPython -split '-')[1]
}
function Backup-AGIProject {
Write-Host "Backing Up Existing AGI Project (if any)" -ForegroundColor Blue
Write-Host ""
if ($InstallPath -eq $CurrentPath)
{
Write-Host "AGI project directory is 'src'; Skipping Backup." -ForegroundColor Yellow
return
}
if (Test-Path $CurrentPath) {
if (Test-Path (Join-Path $CurrentPath "zip-agi.py")) {
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
$backupFile = Join-Path $LocalDir ("{0}_{1}.zip" -f (Split-Path $AgiProject -Leaf), $timestamp)
Write-Host "Existing AGI project found at $AgiProject. Creating backup: $backupFile" -ForegroundColor Yellow
try {
# Use Compress-Archive as a backup mechanism
Compress-Archive -Path $AgiProject\* -DestinationPath $backupFile -Force
Write-Host "Backup created successfully at $backupFile." -ForegroundColor Green
if ((Split-Path $AgiProject -Leaf) -ne "agilab") {
Remove-Item -Recurse -Force $AgiProject
Write-Host "Existing AGI project directory removed." -ForegroundColor Green
}
else {
Write-Host "AGI project directory is 'src'; preserving it." -ForegroundColor Yellow
}
}
catch {
Write-Host "Error: Backup failed. Aborting installation." -ForegroundColor Red
Write-Host "Details: $($_.Exception.Message)" -ForegroundColor Red
Stop-Transcript
exit 1
}
}
else {
Write-Host "Existing AGI project found at $AgiProject but no zip-agi.py found. Skipping backup." -ForegroundColor Yellow
}
}
else {
Write-Host "No existing AGI project found at $AgiProject. Skipping backup." -ForegroundColor Yellow
}
Write-Host ""
}
function Copy-ProjectFiles {
if ($InstallPath -ne $CurrentPath) {
if (Test-Path "$CurrentPath/src") {
Write-Host "Copying project files to install directory..." -ForegroundColor Blue
New-Item -ItemType Directory -Force -Path $InstallPath | Out-Null
robocopy $CurrentPath $InstallPath /E /MIR /NFL /NDL /NJH /NJS | Out-Null
} else {
Write-Host "Source directory 'src' not found. Exiting." -ForegroundColor Red
exit 1
}
} else {
Write-Host "Using current directory as install directory; no copy needed." -ForegroundColor Yellow
}
"$InstallPath/src/agilab" | Set-Content -Encoding UTF8 -Path $AgiPathFile
[System.Environment]::SetEnvironmentVariable('AGI_ROOT', "$InstallPath/src/agilab", [System.EnvironmentVariableTarget]::User)
Write-Host "Installation root path has been exported as AGI_ROOT and written in $LocalDir" -ForegroundColor Green
}
function Update-Environment {
$envFile = Join-Path $LocalDir ".env"
if (Test-Path $envFile) {
Remove-Item $envFile
}
@"
OPENAI_API_KEY="$OpenaiApiKey"
CLUSTER_CREDENTIALS="$AgiCredentials"
AGI_PYTHON_VERSION="$env:PYTHON_VERSION"
"@ | Set-Content -Encoding UTF8 -Path $envFile
Write-Host "Environment updated in $envFile" -ForegroundColor Green
}
function Install-FrameworkApps {
$frameworkDir = Join-Path $InstallPath "src\agilab\fwk"
$appsDir = Join-Path $InstallPath "src\agilab\apps"
Write-Host "Installing Framework..." -ForegroundColor Blue
Push-Location $frameworkDir
& "./install.ps1" $frameworkDir
Pop-Location
Write-Host "Installing Apps..." -ForegroundColor Blue
Write-Host "$appsDir" -ForegroundColor Yellow
Push-Location $appsDir
& "./install.ps1" $appsDir "1"
Pop-Location
}
function Write-EnvValues {
$sharedDir = $env:LOCALAPPDATA
$sharedEnv = Join-Path $sharedDir "agilab\.env"
$sharedPath = Join-Path $sharedDir "agilab\.agi-path"
$agilabEnv = Join-Path $env:USERPROFILE ".agilab\.env"
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$username = $currentUser.Split('\')[-1]
if (-not (Test-Path $sharedEnv)) {
Write-Host "Error: $sharedEnv does not exist." -ForegroundColor Red
exit 1
}
# Append the shared env file content to agilab env file
$path = Get-Content $sharedPath
if (-not ($path -like "C:\*") -and ($username -like "T0*"))
{
$userDir = [Environment]::GetFolderPath('UserProfile')
$agilabEnv = Join-Path $userDir "MyApp/.agilab/.env"
}
Get-Content $sharedEnv | Out-File -Append -FilePath $agilabEnv -Encoding UTF8
Write-Host ".env file updated." -ForegroundColor Green
}
# ================================
# Main Flow
# ================================
if (-not $Offline)
{
Check-Internet
}
Check-VisualStudio
if (-not $Offline)
{
Install-Dependencies
}
Choose-PythonVersion
Backup-AGIProject
Copy-ProjectFiles
Update-Environment
Install-FrameworkApps
Write-EnvValues