-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterra-node-windows.ps1
More file actions
327 lines (282 loc) · 12.6 KB
/
terra-node-windows.ps1
File metadata and controls
327 lines (282 loc) · 12.6 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# Terra Node Setup Script for Windows
# Usage:
# PowerShell -ExecutionPolicy Bypass -File terra-node-windows.ps1 -ClientId "your_id" -ClientPassword "your_password"
# Or one-liner:
# iwr -useb https://raw.githubusercontent.com/AI-Decenter/Agent-Node/main/terra-node-windows.ps1 | iex
param(
[Parameter(Mandatory=$false)]
[string]$ClientId = $env:CLIENT_ID,
[Parameter(Mandatory=$false)]
[string]$ClientPassword = $env:CLIENT_PASSWORD,
[Parameter(Mandatory=$false)]
[string]$DownloadUrl = "https://github.com/AI-Decenter/Agent-Node/releases/latest/download/agent-node-windows-x86_64.exe"
)
# Require Administrator privileges
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Host ""
Write-Host "========================================" -ForegroundColor Red
Write-Host " ADMINISTRATOR PRIVILEGES REQUIRED" -ForegroundColor Red
Write-Host "========================================" -ForegroundColor Red
Write-Host ""
Write-Host "Please run PowerShell as Administrator:" -ForegroundColor Yellow
Write-Host " 1. Right-click PowerShell" -ForegroundColor Cyan
Write-Host " 2. Select 'Run as Administrator'" -ForegroundColor Cyan
Write-Host ""
Write-Host "Then run the command again." -ForegroundColor Yellow
Write-Host ""
Start-Sleep -Seconds 3
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " TERRA NODE INSTALLER" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# Enhanced logging functions
function Write-Log {
param([string]$Message)
Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $Message" -ForegroundColor Green
[Console]::Out.Flush()
}
function Write-ErrorLog {
param([string]$Message)
Write-Host "[ERROR] $Message" -ForegroundColor Red
[Console]::Out.Flush()
exit 1
}
# Validate required parameters - prompt if missing
if ([string]::IsNullOrWhiteSpace($ClientId)) {
Write-Host "Client ID not provided." -ForegroundColor Yellow
$ClientId = Read-Host "Please enter your Client ID"
if ([string]::IsNullOrWhiteSpace($ClientId)) {
Write-ErrorLog "Client ID is required. Usage: .\terra-node-windows.ps1 -ClientId 'your_id' -ClientPassword 'your_password'"
}
}
if ([string]::IsNullOrWhiteSpace($ClientPassword)) {
Write-Host "Client Password not provided." -ForegroundColor Yellow
$SecurePassword = Read-Host "Please enter your Client Password" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
$ClientPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR)
if ([string]::IsNullOrWhiteSpace($ClientPassword)) {
Write-ErrorLog "Client password is required. Usage: .\terra-node-windows.ps1 -ClientId 'your_id' -ClientPassword 'your_password'"
}
}
# Get public IP
try {
$PublicIP = (Invoke-WebRequest -Uri "https://icanhazip.com" -UseBasicParsing -TimeoutSec 5).Content.Trim()
$ClientWithIP = "${ClientId}_windows_${PublicIP}"
} catch {
$ClientWithIP = "${ClientId}_windows"
}
Write-Log "Installing Terra Node (ID: $ClientWithIP)"
# Create setup directory
$SetupDir = "C:\ProgramData\TerraNode"
Write-Log "Creating setup directory: $SetupDir"
if (-not (Test-Path $SetupDir)) {
New-Item -ItemType Directory -Path $SetupDir -Force | Out-Null
}
# Download node binary
$AgentPath = Join-Path $SetupDir "terra-agent.exe"
# Check if file exists and force delete it
if (Test-Path $AgentPath) {
Write-Log "Existing terra-agent.exe found, force removing..."
try {
# Stop the service first if it's running
$Service = Get-Service -Name "TerraNode" -ErrorAction SilentlyContinue
if ($Service -and $Service.Status -eq "Running") {
Write-Log "Stopping TerraNode service to release file lock..."
Stop-Service -Name "TerraNode" -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2
}
# Force delete with multiple attempts
$Attempts = 0
$MaxAttempts = 3
$Deleted = $false
while (-not $Deleted -and $Attempts -lt $MaxAttempts) {
$Attempts++
try {
Remove-Item -Path $AgentPath -Force -ErrorAction Stop
$Deleted = $true
Write-Log "Old file deleted successfully"
} catch {
if ($Attempts -lt $MaxAttempts) {
Write-Log "Deletion attempt $Attempts failed, retrying..."
Start-Sleep -Seconds 1
} else {
# Last resort: try to rename and delete
Write-Log "Using alternative deletion method..."
$BackupPath = "$AgentPath.old"
try {
if (Test-Path $BackupPath) { Remove-Item -Path $BackupPath -Force }
Move-Item -Path $AgentPath -Destination $BackupPath -Force
Remove-Item -Path $BackupPath -Force
$Deleted = $true
Write-Log "Old file deleted using alternative method"
} catch {
Write-ErrorLog "Failed to delete existing file after multiple attempts: $_. Please manually delete $AgentPath and run the script again."
}
}
}
}
} catch {
Write-ErrorLog "Failed to prepare for file deletion: $_"
}
}
Write-Log "Downloading terra-agent ...."
# Configure TLS and security protocols
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13
try {
# Try with different methods
Write-Log "Attempting download method 1 (Invoke-WebRequest)..."
$ProgressPreference = 'SilentlyContinue' # Speed up download
Invoke-WebRequest -Uri $DownloadUrl -OutFile $AgentPath -UseBasicParsing -TimeoutSec 300
Write-Log "Download completed successfully"
} catch {
Write-Log "Method 1 failed, trying method 2 (WebClient)..."
try {
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($DownloadUrl, $AgentPath)
Write-Log "Download completed successfully using WebClient"
} catch {
Write-Log "Method 2 failed, trying method 3 (BITS)..."
try {
Import-Module BitsTransfer
Start-BitsTransfer -Source $DownloadUrl -Destination $AgentPath
Write-Log "Download completed successfully using BITS"
} catch {
Write-ErrorLog "All download methods failed. URL: $DownloadUrl. Last Error: $_. Please check: 1) File exists at URL 2) Internet connection 3) Firewall/Proxy settings"
}
}
}
# Create config file
$ConfigPath = Join-Path $SetupDir "config.toml"
$ConfigContent = @"
host = "127.0.0.1"
port = 8379
storage_path = "terra_data"
engine = "rwlock"
sync_interval_seconds = 30
[replication]
enabled = true
mqtt_broker = "emqx.decenter.ai"
mqtt_port = 1883
topic_prefix = "terra_kv"
client_id = "$ClientWithIP"
client_password = "$ClientPassword"
"@
Set-Content -Path $ConfigPath -Value $ConfigContent -Encoding UTF8
# Create NSSM service (Windows Service Wrapper)
Write-Log "Setting up Windows Service..."
# Check if service already exists
$ServiceName = "TerraNode"
$ExistingService = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if ($ExistingService) {
Write-Log "Stopping existing service..."
Stop-Service -Name $ServiceName -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2
Write-Log "Removing existing service..."
sc.exe delete $ServiceName | Out-Null
Start-Sleep -Seconds 2
}
# Download and setup NSSM (Non-Sucking Service Manager) for better service management
Write-Log "Downloading NSSM (Service Manager)..."
$NssmZip = Join-Path $env:TEMP "nssm.zip"
$NssmDir = Join-Path $SetupDir "nssm"
try {
Invoke-WebRequest -Uri "https://nssm.cc/release/nssm-2.24.zip" -OutFile $NssmZip -UseBasicParsing
Expand-Archive -Path $NssmZip -DestinationPath $env:TEMP -Force
# Copy appropriate NSSM version (64-bit or 32-bit)
if ([Environment]::Is64BitOperatingSystem) {
$NssmExe = Join-Path $SetupDir "nssm.exe"
Copy-Item "$env:TEMP\nssm-2.24\win64\nssm.exe" -Destination $NssmExe -Force
} else {
$NssmExe = Join-Path $SetupDir "nssm.exe"
Copy-Item "$env:TEMP\nssm-2.24\win32\nssm.exe" -Destination $NssmExe -Force
}
Write-Log "NSSM installed successfully"
} catch {
Write-Log "NSSM download failed, using built-in service creation"
$NssmExe = $null
}
# Create service
Write-Log "Creating Windows service..."
if ($NssmExe -and (Test-Path $NssmExe)) {
# Use NSSM for better service management
Write-Log "Using NSSM for service creation..."
& $NssmExe install $ServiceName "`"$AgentPath`"" --config "`"$ConfigPath`"" 2>&1 | Out-Null
& $NssmExe set $ServiceName DisplayName "Terra Node Service" 2>&1 | Out-Null
& $NssmExe set $ServiceName Description "Terra distributed key-value store node running in background" 2>&1 | Out-Null
& $NssmExe set $ServiceName Start SERVICE_AUTO_START 2>&1 | Out-Null
& $NssmExe set $ServiceName AppDirectory "`"$SetupDir`"" 2>&1 | Out-Null
& $NssmExe set $ServiceName AppStdout "`"$SetupDir\service.log`"" 2>&1 | Out-Null
& $NssmExe set $ServiceName AppStderr "`"$SetupDir\service-error.log`"" 2>&1 | Out-Null
& $NssmExe set $ServiceName AppRotateFiles 1 2>&1 | Out-Null
& $NssmExe set $ServiceName AppRotateBytes 1048576 2>&1 | Out-Null
Write-Log "Service created with NSSM"
} else {
# Fallback to built-in service creation
Write-Log "Using built-in service creation..."
$ServiceParams = @{
Name = $ServiceName
BinaryPathName = "`"$AgentPath`" --config `"$ConfigPath`""
DisplayName = "Terra Node Service"
Description = "Terra distributed key-value store node running in background"
StartupType = "Automatic"
}
New-Service @ServiceParams | Out-Null
}
# Configure service recovery options
sc.exe failure $ServiceName reset= 86400 actions= restart/5000/restart/5000/restart/5000 | Out-Null
# Set service to run in background (no console window)
sc.exe config $ServiceName type= own | Out-Null
# Start the service
Write-Log "Starting Terra Node service..."
try {
Start-Service -Name $ServiceName
Start-Sleep -Seconds 3
$Service = Get-Service -Name $ServiceName
if ($Service.Status -eq "Running") {
Write-Log "Terra Node service started successfully"
$ServiceStatus = "Running"
} else {
Write-Log "Warning: Service status is $($Service.Status)"
$ServiceStatus = $Service.Status
}
} catch {
Write-Log "Warning: Failed to start service. Error: $_"
$ServiceStatus = "Failed"
}
# Create firewall rule for port 8379 (optional)
Write-Log "Configuring firewall..."
try {
$FirewallRule = Get-NetFirewallRule -DisplayName "Terra Node" -ErrorAction SilentlyContinue
if (-not $FirewallRule) {
New-NetFirewallRule -DisplayName "Terra Node" -Direction Inbound -Protocol TCP -LocalPort 8379 -Action Allow | Out-Null
Write-Log "Firewall rule created for port 8379"
}
} catch {
Write-Log "Warning: Could not create firewall rule. You may need to configure it manually."
}
# Display installation summary
Write-Log "Installation completed!"
Write-Host ""
Write-Host "Terra Node Details:" -ForegroundColor Cyan
Write-Host " Client ID: $ClientWithIP" -ForegroundColor White
Write-Host " Setup Directory: $SetupDir" -ForegroundColor White
Write-Host " Service Status: $ServiceStatus" -ForegroundColor White
Write-Host " Service Type: Background (No Console Window)" -ForegroundColor White
Write-Host ""
Write-Host "Service Management Commands:" -ForegroundColor Cyan
Write-Host " Check status: Get-Service -Name TerraNode" -ForegroundColor Yellow
Write-Host " Stop service: Stop-Service -Name TerraNode" -ForegroundColor Yellow
Write-Host " Start service: Start-Service -Name TerraNode" -ForegroundColor Yellow
Write-Host " Restart service: Restart-Service -Name TerraNode" -ForegroundColor Yellow
Write-Host ""
Write-Host "Log Files:" -ForegroundColor Cyan
Write-Host " Standard output: $SetupDir\service.log" -ForegroundColor Yellow
Write-Host " Error output: $SetupDir\service-error.log" -ForegroundColor Yellow
Write-Host ""
Write-Host "Configuration file: $ConfigPath" -ForegroundColor Cyan
Write-Host "Executable: $AgentPath" -ForegroundColor Cyan
Write-Host ""
[Console]::Out.Flush()