-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_remote_module.ps1
More file actions
372 lines (313 loc) · 13.2 KB
/
ssh_remote_module.ps1
File metadata and controls
372 lines (313 loc) · 13.2 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# ============================================================================
# CodeReady v2.2 — SSH Remote Execution Module (PowerShell)
# Add these functions to codeready.ps1
# ============================================================================
# --- Remote Configuration ---
$script:RemoteMode = $false
$script:RemoteHost = ""
$script:RemoteUser = ""
$script:RemotePort = 22
$script:RemoteKey = ""
$script:RemoteAuth = "key" # key | agent | password
# --- Target Selection --------------------------------------------------------
function Select-Target {
Write-Host ""
Write-Host " === Target Machine ===" -ForegroundColor Cyan
Write-Host " 1) This machine (localhost)" -ForegroundColor Green -NoNewline
Write-Host " <- default" -ForegroundColor DarkGray
Write-Host " 2) Remote machine (SSH)" -ForegroundColor Green
Write-Host ""
$choice = Read-Host " Select target [1]"
if ([string]::IsNullOrWhiteSpace($choice)) { $choice = "1" }
switch ($choice) {
"1" {
$script:RemoteMode = $false
Write-Host " [OK] Target: localhost" -ForegroundColor Green
}
"2" {
$script:RemoteMode = $true
Configure-Remote
}
default {
$script:RemoteMode = $false
Write-Host " [OK] Target: localhost" -ForegroundColor Green
}
}
}
# --- Remote Configuration Wizard ---------------------------------------------
function Configure-Remote {
Write-Host ""
Write-Host " === Remote SSH Configuration ===" -ForegroundColor Cyan
Write-Host ""
# Check for saved hosts
$configDir = Join-Path $env:USERPROFILE ".codeready"
$hostsFile = Join-Path $configDir "remote-hosts.json"
if (Test-Path $hostsFile) {
try {
$savedHosts = Get-Content $hostsFile -Raw | ConvertFrom-Json
if ($savedHosts.Count -gt 0) {
Write-Host " Saved hosts:" -ForegroundColor Yellow
for ($i = 0; $i -lt $savedHosts.Count; $i++) {
$h = $savedHosts[$i]
$label = if ($h.label) { $h.label } else { $h.host }
$port = if ($h.port) { $h.port } else { 22 }
Write-Host " $($i+1)) $label ($($h.user)@$($h.host):$port)" -ForegroundColor White
}
Write-Host " $($savedHosts.Count + 1)) New connection" -ForegroundColor White
Write-Host ""
$hostChoice = Read-Host " Select host"
$idx = 0
if ([int]::TryParse($hostChoice, [ref]$idx) -and $idx -ge 1 -and $idx -le $savedHosts.Count) {
$h = $savedHosts[$idx - 1]
$script:RemoteHost = $h.host
$script:RemoteUser = $h.user
$script:RemotePort = if ($h.port) { $h.port } else { 22 }
$script:RemoteKey = if ($h.key) { $h.key } else { "" }
Write-Host " [OK] Loaded: $($script:RemoteUser)@$($script:RemoteHost):$($script:RemotePort)" -ForegroundColor Green
Test-RemoteConnection
return
}
}
} catch {
# Invalid JSON, continue to new connection
}
}
# New connection
$script:RemoteHost = Read-Host " Hostname or IP"
if ([string]::IsNullOrWhiteSpace($script:RemoteHost)) {
Write-Host " [ERR] No hostname provided, falling back to localhost" -ForegroundColor Red
$script:RemoteMode = $false
return
}
$userInput = Read-Host " Username [$env:USERNAME]"
$script:RemoteUser = if ([string]::IsNullOrWhiteSpace($userInput)) { $env:USERNAME } else { $userInput }
$portInput = Read-Host " Port [22]"
$script:RemotePort = if ([string]::IsNullOrWhiteSpace($portInput)) { 22 } else { [int]$portInput }
Write-Host ""
Write-Host " Authentication:" -ForegroundColor Cyan
Write-Host " 1) SSH key (default)" -ForegroundColor White
Write-Host " 2) SSH agent" -ForegroundColor White
Write-Host " 3) Password" -ForegroundColor White
$authChoice = Read-Host " Auth method [1]"
if ([string]::IsNullOrWhiteSpace($authChoice)) { $authChoice = "1" }
switch ($authChoice) {
"1" {
$defaultKey = Join-Path $env:USERPROFILE ".ssh\id_ed25519"
if (!(Test-Path $defaultKey)) {
$defaultKey = Join-Path $env:USERPROFILE ".ssh\id_rsa"
}
$keyInput = Read-Host " Key path [$defaultKey]"
$script:RemoteKey = if ([string]::IsNullOrWhiteSpace($keyInput)) { $defaultKey } else { $keyInput }
if (!(Test-Path $script:RemoteKey)) {
Write-Host " [ERR] Key file not found: $($script:RemoteKey)" -ForegroundColor Red
$script:RemoteMode = $false
return
}
$script:RemoteAuth = "key"
}
"2" {
$script:RemoteAuth = "agent"
}
"3" {
$script:RemoteAuth = "password"
# Password will be prompted by ssh.exe interactively
}
}
if (Test-RemoteConnection) {
$saveAns = Read-Host " Save this host for future use? [Y/n]"
if ([string]::IsNullOrWhiteSpace($saveAns) -or $saveAns -match "^[Yy]$") {
$label = Read-Host " Label (e.g. 'dev-server')"
Save-RemoteHost -Label $label
}
}
}
# --- Build SSH Command -------------------------------------------------------
function Get-SshCommand {
param([string]$Command)
# Windows 10+ has built-in ssh.exe (OpenSSH)
$sshExe = "ssh"
if (!(Get-Command ssh -ErrorAction SilentlyContinue)) {
# Fallback to plink (PuTTY)
if (Get-Command plink -ErrorAction SilentlyContinue) {
$sshExe = "plink"
} else {
Write-Host " [ERR] No SSH client found. Install OpenSSH or PuTTY." -ForegroundColor Red
return $null
}
}
$args = @(
"-o", "StrictHostKeyChecking=accept-new",
"-o", "ConnectTimeout=10",
"-p", $script:RemotePort
)
if ($script:RemoteAuth -eq "key" -and $script:RemoteKey) {
$args += @("-i", $script:RemoteKey)
}
$target = "$($script:RemoteUser)@$($script:RemoteHost)"
return @{
Exe = $sshExe
Args = $args + @($target, $Command)
Target = $target
}
}
# --- Test Remote Connection --------------------------------------------------
function Test-RemoteConnection {
Write-Host " [..] Testing SSH connection to $($script:RemoteUser)@$($script:RemoteHost):$($script:RemotePort)..." -ForegroundColor Cyan
$cmd = Get-SshCommand -Command "echo CodeReady-SSH-OK && uname -s"
if ($null -eq $cmd) { return $false }
try {
$result = & $cmd.Exe $cmd.Args 2>&1 | Out-String
if ($result -match "CodeReady-SSH-OK") {
Write-Host " [OK] SSH connection successful" -ForegroundColor Green
if ($result -match "Linux|Darwin") {
$os = if ($result -match "Darwin") { "macOS" } else { "Linux" }
Write-Host " [..] Remote OS: $os" -ForegroundColor Cyan
}
return $true
}
} catch {
# Connection failed
}
Write-Host " [ERR] SSH connection failed. Check credentials." -ForegroundColor Red
$script:RemoteMode = $false
return $false
}
# --- Save Remote Host --------------------------------------------------------
function Save-RemoteHost {
param([string]$Label = "")
$configDir = Join-Path $env:USERPROFILE ".codeready"
$hostsFile = Join-Path $configDir "remote-hosts.json"
if (!(Test-Path $configDir)) { New-Item -ItemType Directory -Path $configDir -Force | Out-Null }
$hosts = @()
if (Test-Path $hostsFile) {
try {
$hosts = @(Get-Content $hostsFile -Raw | ConvertFrom-Json)
} catch { $hosts = @() }
}
# Remove duplicate
$hosts = @($hosts | Where-Object {
-not ($_.host -eq $script:RemoteHost -and $_.user -eq $script:RemoteUser -and $_.port -eq $script:RemotePort)
})
$newHost = @{
label = if ($Label) { $Label } else { $script:RemoteHost }
host = $script:RemoteHost
user = $script:RemoteUser
port = $script:RemotePort
key = $script:RemoteKey
}
$hosts += $newHost
$hosts | ConvertTo-Json -Depth 3 | Set-Content $hostsFile -Encoding UTF8
Write-Host " [OK] Host saved: $($newHost.label)" -ForegroundColor Green
}
# --- Remote Bootstrap (copy codeready.sh to remote) -------------------------
function Invoke-RemoteBootstrap {
Write-Host " [..] Bootstrapping remote machine..." -ForegroundColor Cyan
# Create temp dir on remote
$cmd = Get-SshCommand -Command "mktemp -d /tmp/codeready.XXXXXX"
$remoteTmp = (& $cmd.Exe $cmd.Args 2>$null).Trim()
if ([string]::IsNullOrWhiteSpace($remoteTmp)) {
Write-Host " [ERR] Failed to create temp dir on remote" -ForegroundColor Red
return $null
}
# Copy codeready.sh via scp
$scriptDir = Split-Path -Parent $PSCommandPath
$shScript = Join-Path $scriptDir "codeready.sh"
if (!(Test-Path $shScript)) {
Write-Host " [ERR] codeready.sh not found at $shScript" -ForegroundColor Red
return $null
}
Write-Host " [..] Copying codeready.sh to remote..." -ForegroundColor Cyan
$scpArgs = @(
"-o", "StrictHostKeyChecking=accept-new",
"-P", $script:RemotePort
)
if ($script:RemoteAuth -eq "key" -and $script:RemoteKey) {
$scpArgs += @("-i", $script:RemoteKey)
}
$scpArgs += @($shScript, "$($script:RemoteUser)@$($script:RemoteHost):$remoteTmp/codeready.sh")
& scp $scpArgs 2>$null
# Make executable
$cmd = Get-SshCommand -Command "chmod +x $remoteTmp/codeready.sh"
& $cmd.Exe $cmd.Args 2>$null
Write-Host " [OK] Bootstrap complete" -ForegroundColor Green
return $remoteTmp
}
# --- Remote Exec (live streaming) --------------------------------------------
function Invoke-RemoteExec {
param([string]$Command)
$cmd = Get-SshCommand -Command $Command
# Stream output live
$process = Start-Process -FilePath $cmd.Exe -ArgumentList ($cmd.Args -join " ") `
-NoNewWindow -PassThru -Wait -RedirectStandardOutput "NUL"
# Actually, for live streaming we need a different approach
& $cmd.Exe $cmd.Args 2>&1 | ForEach-Object { Write-Host $_ }
return $LASTEXITCODE
}
# --- Remote Scan / Install / Profile ----------------------------------------
function Invoke-RemoteScan {
$remoteTmp = Invoke-RemoteBootstrap
if ($null -eq $remoteTmp) { return }
Write-Host ""
Write-Host " --- Remote Scan Output ---" -ForegroundColor DarkGray
Invoke-RemoteExec -Command "sudo bash $remoteTmp/codeready.sh --scan"
Write-Host " --- End Remote Output ---" -ForegroundColor DarkGray
# Cleanup
$cmd = Get-SshCommand -Command "rm -rf $remoteTmp"
& $cmd.Exe $cmd.Args 2>$null
}
function Invoke-RemoteInstall {
param([string]$Items)
$remoteTmp = Invoke-RemoteBootstrap
if ($null -eq $remoteTmp) { return }
Write-Host ""
Write-Host " --- Remote Install Output ---" -ForegroundColor DarkGray
Invoke-RemoteExec -Command "sudo bash $remoteTmp/codeready.sh --install '$Items'"
Write-Host " --- End Remote Output ---" -ForegroundColor DarkGray
$cmd = Get-SshCommand -Command "rm -rf $remoteTmp"
& $cmd.Exe $cmd.Args 2>$null
}
function Invoke-RemoteProfile {
param([int]$ProfileNum)
$remoteTmp = Invoke-RemoteBootstrap
if ($null -eq $remoteTmp) { return }
Write-Host ""
Write-Host " --- Remote Profile Output ---" -ForegroundColor DarkGray
Invoke-RemoteExec -Command "sudo bash $remoteTmp/codeready.sh --profile $ProfileNum"
Write-Host " --- End Remote Output ---" -ForegroundColor DarkGray
$cmd = Get-SshCommand -Command "rm -rf $remoteTmp"
& $cmd.Exe $cmd.Args 2>$null
}
# --- Updated Banner ----------------------------------------------------------
function Show-Banner {
Write-Host ""
Write-Host " =====================================================" -ForegroundColor Cyan
Write-Host " CodeReady -- Developer Environment Setup v2.2.0" -ForegroundColor Green
Write-Host " =====================================================" -ForegroundColor Cyan
if ($script:RemoteMode) {
Write-Host " TARGET: $($script:RemoteUser)@$($script:RemoteHost):$($script:RemotePort)" -ForegroundColor Yellow
Write-Host " Mode: SSH Remote" -ForegroundColor Yellow
} else {
Write-Host " TARGET: localhost" -ForegroundColor Green
Write-Host " Mode: Local" -ForegroundColor Green
}
Write-Host " =====================================================" -ForegroundColor Cyan
Write-Host ""
}
# --- CLI Parameter Support ---------------------------------------------------
# Add to param() block at top of codeready.ps1:
#
# param(
# [string]$Remote = "",
# [string]$RemoteUser = "",
# [int]$RemotePort = 22,
# [string]$RemoteKey = "",
# [switch]$Scan,
# [string]$Install = "",
# [int]$Profile = 0,
# [switch]$LocalOnly
# )
#
# Usage:
# .\codeready.ps1 -Remote "192.168.1.50" -RemoteUser "deploy" -Profile 5
# .\codeready.ps1 -Remote "myserver.com" -Scan
# .\codeready.ps1 -Remote "dev-box" -RemoteKey "~/.ssh/id_ed25519" -Install "python,nodejs"