<# : @echo off setlocal rem I-MATH for Windows: the imath command (login, gpus, upload/download) + institute printers. rem Double-click to install / update. From a terminal: imath-windows.cmd uninstall set "IMATH_SELF=%~f0" set "IMATH_ARGS=%*" powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "iex ([IO.File]::ReadAllText($env:IMATH_SELF))" echo. pause exit /b #> # --------------------------------------------------------------------------- # Everything below is PowerShell (Windows PowerShell 5.1, built into Win10/11) # --------------------------------------------------------------------------- $ErrorActionPreference = 'Continue' $JumpHost = 'ssh.math.uzh.ch' $PrintHost = 'print22.math.uzh.ch' $LocalPort = 6631 $Dir = Join-Path $env:LOCALAPPDATA 'imath' $SshDir = Join-Path $env:USERPROFILE '.ssh' $Key = Join-Path $SshDir 'id_imath' $Cfg = Join-Path $SshDir 'config' $TunnelPs = Join-Path $Dir 'imath-printer-tunnel.ps1' $TunnelLog = Join-Path $Dir 'tunnel.log' $State = Join-Path $Dir 'printers.txt' $BinDir = Join-Path $Dir 'bin' $ImathPs = Join-Path $Dir 'imath.ps1' $EnvKey = 'HKCU:\Environment' $RunKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Run' $RunName = 'imath-printers' $MarkBegin = '# >>> imath (managed by imath-windows.cmd) >>>' $MarkEnd = '# <<< imath <<<' $Utf8 = New-Object Text.UTF8Encoding($false) # no BOM: OpenSSH chokes on it $Utf8Bom = New-Object Text.UTF8Encoding($true) # .ps1 files: PowerShell 5.1 needs the BOM for non-ASCII paths function Say ($m, $c = 'Gray') { Write-Host " $m" -ForegroundColor $c } function Step($m) { Write-Host ''; Write-Host "== $m" -ForegroundColor Cyan } function Fail($m) { Write-Host ''; Write-Host " $m" -ForegroundColor Red; exit 1 } function Get-Ssh { $p = Join-Path $env:WINDIR 'System32\OpenSSH\ssh.exe' if (Test-Path $p) { return $p } $c = Get-Command ssh.exe -ErrorAction SilentlyContinue if ($c) { return $c.Source } return $null } # Runs an (ASCII-only) PowerShell snippet as administrator in its own window. # Returns the lines the snippet wrote to $log, or $null if elevation was refused. function Invoke-Admin([string]$Title, [string]$Body) { $id = [guid]::NewGuid().ToString('N') $f = Join-Path $env:TEMP "imath-admin-$id.ps1" $log = Join-Path $env:TEMP "imath-admin-$id.log" $script = @" `$ErrorActionPreference = 'Continue' `$log = '$log' function Out-Log(`$m) { Write-Host `$m; Add-Content -LiteralPath `$log -Value `$m } `$Host.UI.RawUI.WindowTitle = '$Title' $Body "@ [IO.File]::WriteAllText($f, $script, $Utf8Bom) try { Start-Process powershell.exe -Verb RunAs -Wait ` -ArgumentList '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', "`"$f`"" | Out-Null } catch { Remove-Item $f -ErrorAction SilentlyContinue return $null } $r = @(); if (Test-Path $log) { $r = @(Get-Content -LiteralPath $log) } Remove-Item $f, $log -ErrorAction SilentlyContinue return ,$r } function Stop-Tunnel { $ps = @(Get-CimInstance Win32_Process -Filter "Name='powershell.exe'" -ErrorAction SilentlyContinue | Where-Object { $_.CommandLine -like '*imath-printer-tunnel*' }) foreach ($p in $ps) { Get-CimInstance Win32_Process -Filter "ParentProcessId=$($p.ProcessId)" -ErrorAction SilentlyContinue | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue } Stop-Process -Id $p.ProcessId -Force -ErrorAction SilentlyContinue } } function Remove-ConfigBlock([string]$text) { $pattern = '(?s)\r?\n?' + [regex]::Escape($MarkBegin) + '.*?' + [regex]::Escape($MarkEnd) + '\r?\n?' return [regex]::Replace($text, $pattern, "`r`n").Trim() } function Get-InstalledPrinters { if (Test-Path $State) { return @(Get-Content $State | Where-Object { $_ -match '^[A-Za-z0-9._-]+$' }) } return @() } function Get-UserPath { (Get-Item $EnvKey).GetValue('Path', '', 'DoNotExpandEnvironmentNames') } function Set-UserPath([string]$p) { Set-ItemProperty -Path $EnvKey -Name Path -Value $p -Type ExpandString # tell Explorer & new terminals; writing any user variable broadcasts the change [Environment]::SetEnvironmentVariable('IMATH_HOME', $Dir, 'User') } function Get-ProfilePaths { $docs = [Environment]::GetFolderPath('MyDocuments') @((Join-Path $docs 'WindowsPowerShell\profile.ps1'), (Join-Path $docs 'PowerShell\profile.ps1')) } function Test-Port { $c = New-Object Net.Sockets.TcpClient try { $c.Connect('127.0.0.1', $LocalPort); return $true } catch { return $false } finally { $c.Close() } } # =========================================================================== # Uninstall # =========================================================================== if ($env:IMATH_ARGS -match '(?i)uninstall') { Step 'Removing I-MATH printers and tunnel' $names = Get-InstalledPrinters if ($names.Count) { $list = ($names | ForEach-Object { "'$_'" }) -join ',' $r = Invoke-Admin 'I-MATH: removing printers' @" foreach (`$n in @($list)) { if (Get-Printer -Name `$n -ErrorAction SilentlyContinue) { Remove-Printer -Name `$n -ErrorAction SilentlyContinue; Out-Log "removed `$n" } } "@ if ($null -eq $r) { Say 'Administrator rights were refused - printers are still installed.' Yellow } else { Say ("{0} printer(s) removed" -f @($r | Where-Object { $_ -like 'removed*' }).Count) Green } } Stop-Tunnel Remove-ItemProperty -Path $RunKey -Name $RunName -ErrorAction SilentlyContinue Remove-Item $TunnelPs, $TunnelLog, $State, $ImathPs -ErrorAction SilentlyContinue Remove-Item $BinDir, (Join-Path $Dir 'nodes'), (Join-Path $Dir 'gpu-nodes') -Recurse -Force -ErrorAction SilentlyContinue $p = @((Get-UserPath) -split ';' | Where-Object { $_ -and $_ -ne $BinDir }) -join ';' Set-UserPath $p [Environment]::SetEnvironmentVariable('IMATH_HOME', $null, 'User') foreach ($pf in Get-ProfilePaths) { if (Test-Path $pf) { [IO.File]::WriteAllText($pf, (Remove-ConfigBlock ([IO.File]::ReadAllText($pf))) + "`r`n", $Utf8Bom) } } if (Test-Path $Cfg) { [IO.File]::WriteAllText($Cfg, (Remove-ConfigBlock ([IO.File]::ReadAllText($Cfg))) + "`r`n", $Utf8) } Say 'The imath command, tunnel, autostart and the "imath" SSH entry are gone.' Green Say "Kept: your key $Key (and its copy on the server)." Say 'Delete it there with: ssh @ssh.math.uzh.ch and edit ~/.ssh/authorized_keys' exit 0 } # =========================================================================== # Install / update # =========================================================================== Write-Host '' Write-Host ' I-MATH for Windows - imath command + institute printers' -ForegroundColor White Write-Host ' -------------------------------------------------------' # --- 1. OpenSSH client ------------------------------------------------------- Step 'OpenSSH client' $Ssh = Get-Ssh if (-not $Ssh) { Say 'Not installed - installing the Windows "OpenSSH Client" feature (needs admin)...' Yellow $r = Invoke-Admin 'I-MATH: installing OpenSSH' @" `$c = Get-WindowsCapability -Online -Name 'OpenSSH.Client*' | Select-Object -First 1 if (`$c) { Add-WindowsCapability -Online -Name `$c.Name | Out-Null; Out-Log 'done' } "@ $Ssh = Get-Ssh if (-not $Ssh) { Fail 'OpenSSH could not be installed. Settings > System > Optional features > "OpenSSH Client", then run this again.' } } $SshKeygen = Join-Path (Split-Path $Ssh) 'ssh-keygen.exe' Say "ok ($Ssh)" Green # --- 2. Username ------------------------------------------------------------- Step 'I-MATH account' $cfgText = '' if (Test-Path $Cfg) { $cfgText = [IO.File]::ReadAllText($Cfg) } $default = '' if ($cfgText -match ('(?s)' + [regex]::Escape($MarkBegin) + '.*?\n\s*User\s+(\S+)')) { $default = $Matches[1] } do { $prompt = ' I-MATH username'; if ($default) { $prompt += " [$default]" } $User = (Read-Host $prompt).Trim() if (-not $User) { $User = $default } } until ($User -match '^[a-z][a-z0-9._-]*$') # --- 3. Key ------------------------------------------------------------------ Step 'SSH key' if (-not (Test-Path $SshDir)) { New-Item -ItemType Directory -Path $SshDir | Out-Null } if (Test-Path $Key) { Say "using existing $Key" Green } else { # No passphrase: the printer tunnel runs in the background and cannot ask for one. $empty = if ($PSVersionTable.PSVersion.Major -ge 7) { '' } else { '""' } & $SshKeygen -q -t ed25519 -f $Key -N $empty -C "imath-windows-$env:COMPUTERNAME" | Out-Null if (-not (Test-Path $Key)) { Fail 'ssh-keygen failed.' } Say "created $Key" Green } # --- 4. ~/.ssh/config -------------------------------------------------------- Step 'SSH shortcut "imath"' $block = @" $MarkBegin Host imath HostName $JumpHost User $User IdentityFile ~/.ssh/id_imath IdentitiesOnly yes ServerAliveInterval 30 ServerAliveCountMax 3 StrictHostKeyChecking accept-new $MarkEnd "@ $rest = Remove-ConfigBlock $cfgText if ($rest -match '(?im)^\s*Host\s+(.*\s)?imath(\s|$)') { Say 'Note: your ssh config already has its own "Host imath" entry above ours;' Yellow Say 'ssh uses the first match, so check that it points to ssh.math.uzh.ch.' Yellow } $new = if ($rest) { $rest + "`r`n`r`n" + $block } else { $block } [IO.File]::WriteAllText($Cfg, ($new -replace "`r?`n", "`r`n") + "`r`n", $Utf8) Say "ok (ssh imath -> $User@$JumpHost)" Green # --- 5. Key on the server ---------------------------------------------------- Step 'Login without password' function Test-Login { $out = & $Ssh -o BatchMode=yes -o ConnectTimeout=15 imath true 2>&1 $script:LoginErr = ($out | ForEach-Object { "$_" }) -join ' ' return ($LASTEXITCODE -eq 0) } if (Test-Login) { Say 'key already accepted' Green } else { if ($LoginErr -match 'timed out|Could not resolve|Network is unreachable|Connection refused|No route') { Fail "Cannot reach $JumpHost ($LoginErr). Some networks block SSH - try eduroam or the UZH VPN." } $pub = ([IO.File]::ReadAllText("$Key.pub")).Trim() $body = $pub.Split(' ')[1] $remote = "umask 077; mkdir -p ~/.ssh; touch ~/.ssh/authorized_keys; " + "grep -qF '$body' ~/.ssh/authorized_keys || printf '%s\n' '$pub' >> ~/.ssh/authorized_keys" Say 'Enter your I-MATH password once (and the 2FA code if asked).' Yellow & $Ssh -o PubkeyAuthentication=no -o PreferredAuthentications=keyboard-interactive,password imath $remote if (-not (Test-Login)) { Fail "Key login still fails: $LoginErr" } Say 'key installed on the server' Green } # --- 6. The imath command ---------------------------------------------------- Step 'imath command' if (-not (Test-Path $BinDir)) { New-Item -ItemType Directory -Path $BinDir -Force | Out-Null } $imathSrc = @' # imath for Windows - generated by imath-windows.cmd; do not edit, it gets overwritten. # Remove with: imath-windows.cmd uninstall $ErrorActionPreference = 'Continue' $IMATH_USER = '__USER__' $Ssh = '__SSH__' $Scp = Join-Path (Split-Path $Ssh) 'scp.exe' $Key = Join-Path $env:USERPROFILE '.ssh\id_imath' $NodeList = '/usr/sepp/etc/compute_hosts.lst' $Reserve = 'https://made-fb1.math.uzh.ch' $Base = Join-Path $env:LOCALAPPDATA 'imath' $Cache = Join-Path $Base 'nodes' $GpuCache = Join-Path $Base 'gpu-nodes' $Fanout = 8 # parallel SSH connections; the jump host's MaxStartups is 10:30:100 $NodeOpts = @('-J', 'imath', '-i', $Key, '-o', 'IdentitiesOnly=yes', '-o', 'StrictHostKeyChecking=accept-new') $Inv = [Globalization.CultureInfo]::InvariantCulture $script:Code = 0 # --- building blocks ------------------------------------------------------- function Warn([string]$m) { Write-Host $m -ForegroundColor Yellow } function Fail([string]$m) { Warn $m; $script:Code = 1 } # Remote commands travel base64-encoded: Windows PowerShell 5.1 mangles # double quotes in arguments to native programs, base64 has none. function ConvertTo-Remote([string]$s) { $b = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(($s -replace "`r", ''))) "echo $b | base64 -d | bash" } # Single-quote for the far side: a path like "parent's" would otherwise # tear a remote mkdir in half. function Quote-Sh([string]$s) { "'" + ($s -replace "'", "'\''") + "'" } function Join-Args([string[]]$a) { ($a | ForEach-Object { if ($_ -match '[\s"]') { '"' + ($_ -replace '"', '\"') + '"' } else { $_ } }) -join ' ' } function New-TempDir { $d = Join-Path $env:TEMP ('imath.' + [guid]::NewGuid().ToString('N').Substring(0, 8)) New-Item -ItemType Directory -Path $d | Out-Null $d } # Everything runs through the jump host, so that is the one place a strange # network can break. Probe it and say what actually went wrong. function Test-Jump { $out = @(& $Ssh -n -o BatchMode=yes -o ConnectTimeout=10 imath true 2>&1 | ForEach-Object { "$_" }) if ($LASTEXITCODE -eq 0) { return $true } if (($out -join "`n") -match 'Permission denied|Too many authentication failures|passphrase') { return $true # the network is fine, it is the key: let the real call do the asking } $why = @($out | Where-Object { $_.Trim() }) | Select-Object -Last 1 if (-not $why) { $why = 'no reason given' } Write-Host '' Write-Host ' The jump host ssh.math.uzh.ch is not reachable from this network.' -ForegroundColor Red Write-Host " $why" Write-Host ' imath needs one outgoing SSH connection on port 22. Some phone hotspots,' Write-Host ' hotel and guest networks block it -- there only the UZH VPN gets you through.' Write-Host '' return $false } # Run a command on a node. No prompts, stdin detached. function Invoke-Node([string]$h, [string]$cmd) { & $Ssh -n @NodeOpts -o BatchMode=yes -o ConnectTimeout=15 "$IMATH_USER@$h" (ConvertTo-Remote $cmd) 2>$null } # Run one remote command on every node, at most $Fanout at a time. # Output of node X lands in $dir\X. function Invoke-Fanout([string[]]$nodes, [string]$cmd, [string]$dir) { $rc = ConvertTo-Remote $cmd $run = @() foreach ($h in $nodes) { $a = @('-n') + $NodeOpts + @('-o', 'BatchMode=yes', '-o', 'ConnectTimeout=15', "$IMATH_USER@$h", $rc) $run += Start-Process -FilePath $Ssh -ArgumentList (Join-Args $a) -NoNewWindow -PassThru ` -RedirectStandardOutput (Join-Path $dir $h) -RedirectStandardError (Join-Path $dir "$h.err") if ($run.Count -ge $Fanout) { $run | ForEach-Object { $_.WaitForExit() }; $run = @() } } $run | ForEach-Object { $_.WaitForExit() } } # The node names come live from the list the institute maintains itself. function Update-Nodes([switch]$Quiet) { if (-not (Test-Jump)) { return $false } $l = & $Ssh -n -o BatchMode=yes -o ConnectTimeout=10 imath "cat $NodeList" 2>$null $n = @($l | ForEach-Object { "$_".Trim() } | Where-Object { $_ -match '^[a-z][a-z0-9]*$' -and $_ -notmatch '^exam' }) if (-not $n.Count) { Warn 'could not fetch the node list (connection?)'; return $false } New-Item -ItemType Directory -Force -Path $Base | Out-Null [IO.File]::WriteAllLines($Cache, [string[]]$n) Remove-Item $GpuCache -ErrorAction SilentlyContinue # GPU classification has to be redone if (-not $Quiet) { Write-Host "$($n.Count) nodes cached" } return $true } function Get-Nodes { if (-not (Test-Path $Cache) -or (Get-Item $Cache).Length -eq 0) { [void](Update-Nodes -Quiet) } if (Test-Path $Cache) { @(Get-Content $Cache | Where-Object { $_ }) } else { @() } } function Test-Node([string]$h) { @(Get-Nodes) -contains $h } function Get-Paint([double]$f) { if ($f -lt 0.5) { 'Green' } elseif ($f -lt 0.85) { 'Yellow' } else { 'Red' } } # --- imath nodes ------------------------------------------------------------- function Show-Nodes { $n = @(Get-Nodes) if (-not $n.Count) { $script:Code = 1; return } Write-Host '' for ($i = 0; $i -lt $n.Count; $i += 6) { Write-Host (' ' + (($n[$i..([Math]::Min($i + 5, $n.Count - 1))] | ForEach-Object { '{0,-12}' -f $_ }) -join '')) } Write-Host '' } # --- imath gpus -------------------------------------------------------------- function Show-Gpus { if (-not (Test-Jump)) { $script:Code = 1; return } if (-not (Test-Path $GpuCache)) { Warn 'finding the GPU nodes (once, takes ~20 s)...' $all = @(Get-Nodes) $d = New-TempDir Invoke-Fanout $all 'nvidia-smi -L 2>/dev/null | grep "^GPU "' $d $g = @($all | Where-Object { $f = Join-Path $d $_; (Test-Path $f) -and (Get-Item $f).Length -gt 0 }) New-Item -ItemType Directory -Force -Path $Base | Out-Null [IO.File]::WriteAllLines($GpuCache, [string[]]$g) Remove-Item $d -Recurse -Force -ErrorAction SilentlyContinue } $gpu = @(Get-Content $GpuCache | Where-Object { $_ }) if (-not $gpu.Count) { Remove-Item $GpuCache -ErrorAction SilentlyContinue; Fail 'no GPU nodes found.'; return } $q = 'nvidia-smi --query-gpu=index,name,memory.used,memory.total,utilization.gpu --format=csv,noheader,nounits 2>/dev/null | sed "s/^/G|/"' + "`n" + 'printf "U|"; who | cut -d" " -f1 | sort -u | tr "\n" " "; echo' $d = New-TempDir Invoke-Fanout $gpu $q $d Write-Host '' Write-Host (' {0,-10} {1,-24} {2,-12} {3,11} {4,5} {5}' -f 'NODE', 'GPU', 'IN-USE', 'VRAM GB', 'LOAD', 'LOGGED IN') -ForegroundColor White Write-Host (' ' + ('-' * 80)) foreach ($h in $gpu) { $f = Join-Path $d $h $lines = if (Test-Path $f) { @(Get-Content $f) } else { @() } $g = @($lines | Where-Object { $_ -like 'G|*' } | ForEach-Object { $_.Substring(2) }) $u = $lines | Where-Object { $_ -like 'U|*' } | Select-Object -First 1 $users = if ($u) { $u.Substring(2).Trim() } else { '' } if (-not $users) { $users = '-' } if (-not $g.Count) { Write-Host (' {0,-10} {1}' -f $h, '(no answer)'); continue } $i = 0 foreach ($row in $g) { $a = $row -split ',\s*' if ($a.Count -lt 5) { continue } $i++ $name = ($a[1] -replace '^NVIDIA ', '') -replace ' Generation$', '' if ($name.Length -gt 22) { $name = $name.Substring(0, 21) + '.' } $used = [double]$a[2]; $tot = [double]$a[3]; $util = [int]$a[4] $fr = if ($tot -gt 0) { $used / $tot } else { 0 } $n = [int][Math]::Floor($fr * 10 + 0.5) $bar = '[' + ('#' * $n) + ('.' * (10 - $n)) + ']' $mem = [string]::Format($Inv, '{0:0.0}/{1:0.0}', $used / 1024, $tot / 1024) $nodeCol = if ($i -eq 1) { $h } else { '' } $userCol = if ($i -eq 1) { $users } else { '' } Write-Host (' {0,-10} {1,-24} ' -f $nodeCol, "$($a[0]) $name") -NoNewline Write-Host ('{0,-12}' -f $bar) -NoNewline -ForegroundColor (Get-Paint $fr) Write-Host (' {0,11} ' -f $mem) -NoNewline Write-Host ('{0,3} %' -f $util) -NoNewline -ForegroundColor (Get-Paint ($util / 100)) Write-Host (' ' + $userCol) } } Remove-Item $d -Recurse -Force -ErrorAction SilentlyContinue Write-Host '' Write-Host ' Idle is not the same as free: the portal decides who may use a node,' Write-Host ' this list only shows what is running right now.' Write-Host " Reserve: $Reserve" Write-Host '' } # --- imath upload / download --------------------------------------------------- # # Without --tmp everything goes to your cluster home, which is the same file # system on every node. With --tmp it goes to /tmp/ on ONE node: local # scratch, fast and big, but node-local and wiped eventually. function Invoke-Upload([string[]]$a) { $a = @($a); $toTmp = $false; $node = '' if ($a.Count -and $a[0] -eq '--tmp') { $toTmp = $true; $a = @($a | Select-Object -Skip 1) } if (-not $a.Count) { Fail 'imath upload [--tmp] [] '; return } if (-not (Test-Jump)) { $script:Code = 1; return } # The first argument is a node if there is nothing here by that name. $first = $a[0] if (-not (Test-Path -LiteralPath $first) -and (Test-Node $first)) { $node = $first; $a = @($a | Select-Object -Skip 1) } elseif ((Test-Path -LiteralPath $first) -and (Test-Node $first)) { Warn "note: '$first' exists here and is also a node - uploading the local file." } if (-not $a.Count) { Fail 'no path given.'; return } if ($toTmp -and -not $node) { Fail '--tmp needs a node: imath upload --tmp aroca data.csv'; return } # A last argument ending in a slash that does not exist here is a target # folder on the far side, not a source path. $sub = ''; $last = $a[-1] if (-not (Test-Path -LiteralPath $last)) { if ($last -match '[/\\]$') { $sub = $last -replace '\\', '/' } } elseif ($last -match '[/\\]$') { Warn "note: '$last' exists here - uploading it as a source, not using it as a target." } if ($sub) { if ($a.Count -lt 2) { Fail 'no path given.'; return } $a = @($a[0..($a.Count - 2)]) } foreach ($p in $a) { if (-not (Test-Path -LiteralPath $p)) { Fail "not found here: $p"; return } } if ($toTmp) { $dest = "/tmp/$IMATH_USER/$sub" Invoke-Node $node ('mkdir -p -- ' + (Quote-Sh $dest)) | Out-Null & $Scp -r @NodeOpts @a "$IMATH_USER@${node}:$dest" if ($LASTEXITCODE -eq 0) { Write-Host "-> ${node}:$dest (node-local scratch)" } else { $script:Code = 1 } return } if ($sub) { # the home is shared, so create the folder via the jump host & $Ssh -n -o BatchMode=yes imath (ConvertTo-Remote ('mkdir -p -- ~/' + (Quote-Sh $sub))) 2>$null | Out-Null } $shown = if ($sub) { "/$sub" } else { '' } if ($node) { & $Scp -r @NodeOpts @a "$IMATH_USER@${node}:~/$sub" if ($LASTEXITCODE -eq 0) { Write-Host "-> home$shown (via $node)" } else { $script:Code = 1 } } else { & $Scp -r @a "imath:~/$sub" if ($LASTEXITCODE -eq 0) { Write-Host "-> home$shown" } else { $script:Code = 1 } } } function Invoke-Download([string[]]$a) { $a = @($a); $fromTmp = $false; $node = '' if ($a.Count -and $a[0] -eq '--tmp') { $fromTmp = $true; $a = @($a | Select-Object -Skip 1) } if (-not $a.Count) { Fail 'imath download [--tmp] [] [target]'; return } if (-not (Test-Jump)) { $script:Code = 1; return } # If a file in your home happens to be named like a node, "./" disambiguates. if ($a[0].StartsWith('./')) { $a[0] = $a[0].Substring(2) } elseif (Test-Node $a[0]) { $node = $a[0]; $a = @($a | Select-Object -Skip 1) } if (-not $a.Count) { Fail 'no path given.'; return } if ($fromTmp -and -not $node) { Fail '--tmp needs a node: imath download --tmp aroca out.csv'; return } $src = $a[0] $target = if ($a.Count -ge 2) { $a[1] } else { '.' } if ($fromTmp) { & $Scp -r @NodeOpts "$IMATH_USER@${node}:/tmp/$IMATH_USER/$src" $target } elseif ($node) { & $Scp -r @NodeOpts "$IMATH_USER@${node}:$src" $target } elseif ($src.StartsWith('/')) { & $Scp -r "imath:$src" $target } else { & $Scp -r "imath:~/$src" $target } if ($LASTEXITCODE -ne 0) { $script:Code = 1 } } # --- imath -------------------------------------------------------------- function Show-Banner([string]$h) { Write-Host '' Write-Host " $h" -ForegroundColor White Write-Host " Reserved a slot? $Reserve (Compute Host / with GPU)" $who = (@(Invoke-Node $h 'who | cut -d" " -f1 | sort -u | tr "\n" " "') -join ' ').Trim() if ($who) { Write-Host " Logged in right now: $who" } else { Write-Host ' Logged in right now: nobody else' } Write-Host '' } # X11 on Windows needs an X server; VcXsrv is the free one. function Test-XServer { if ($env:DISPLAY) { return $true } $x = Get-Process -Name vcxsrv, Xming, X410 -ErrorAction SilentlyContinue | Select-Object -First 1 if (-not $x) { $exe = Join-Path $env:ProgramFiles 'VcXsrv\vcxsrv.exe' if (-not (Test-Path $exe)) { Warn 'No X server found. Install VcXsrv once: winget install marha.VcXsrv' return $false } Warn 'starting VcXsrv...' Start-Process $exe -ArgumentList ':0', '-multiwindow', '-clipboard', '-wgl' Start-Sleep -Seconds 2 } $env:DISPLAY = '127.0.0.1:0.0' return $true } function Connect-Node([string[]]$a) { $a = @($a); $visual = $false if ($a.Count -and $a[0] -eq '--visual') { $visual = $true; $a = @($a | Select-Object -Skip 1) } if (-not $a.Count -or -not $a[0]) { Fail 'no node given.'; return } $h = $a[0] # Catch typos here: otherwise ssh only says "Session open refused". $nodes = @(Get-Nodes) if ($nodes.Count -and $nodes -notcontains $h) { Warn "'$h' is not in the institute's node list." Fail 'see the list: imath nodes - refresh it: imath refresh' return } if ($a.Count -gt 1) { Warn 'imath takes no command to run - log in first, then type it there:' Fail " imath $h" return } if (-not (Test-Jump)) { $script:Code = 1; return } if ($visual) { if (-not (Test-XServer)) { $script:Code = 1; return } Show-Banner $h & $Ssh -Y @NodeOpts "$IMATH_USER@$h" } else { Show-Banner $h & $Ssh @NodeOpts "$IMATH_USER@$h" } $script:Code = $LASTEXITCODE } # --- imath help ---------------------------------------------------------------- function Show-Help { Write-Host @" imath - access to the compute nodes of the I-MATH CONNECT imath Log in to a compute node. Prints the reservation link and who else is on it. imath --visual Log in with X11, for graphical programs (rstudio, for example). Windows: VcXsrv. imath nodes List of all nodes (PowerShell: imath ). imath refresh Fetch the node list from the institute. RESOURCES imath gpus All GPU nodes: card, memory in use, load, who is logged in. Idle is not the same as free - the portal decides that. FILES -- home is shared by every node, /tmp is not imath upload into your cluster home (5 GB, backed up, visible on every node) imath upload / into a subfolder of your home imath upload same thing, routed through that node imath upload --tmp into /tmp/ on THAT node: local scratch, fast and big, node-local and wiped eventually. This is where large data sets and environments belong. imath download [target] out of your cluster home imath download

[target] same thing, via that node imath download --tmp

out of /tmp/ on that node (file named like a node? use ./name) OTHER imath help this overview ssh imath plain login on the jump host More: handbook in the institute wiki, support support@math.uzh.ch "@ } # --- entry point --------------------------------------------------------------- $argv = @($args) $cmd = if ($argv.Count) { [string]$argv[0] } else { '' } $rest = @($argv | Select-Object -Skip 1) switch -Exact ($cmd) { '' { Show-Help } 'help' { Show-Help } '-h' { Show-Help } '--help' { Show-Help } 'refresh' { if (-not (Update-Nodes)) { $script:Code = 1 } } '--refresh' { if (-not (Update-Nodes)) { $script:Code = 1 } } 'nodes' { Show-Nodes } 'gpus' { Show-Gpus } 'upload' { Invoke-Upload $rest } 'download' { Invoke-Download $rest } '--visual' { Connect-Node (@('--visual') + $rest) } '-Y' { Connect-Node (@('--visual') + $rest) } '-X' { Connect-Node (@('--visual') + $rest) } default { if ($cmd.StartsWith('-')) { Fail "unknown option: $cmd (imath help)" } else { Connect-Node $argv } } } exit $script:Code '@ $imathSrc = $imathSrc.Replace('__USER__', $User).Replace('__SSH__', $Ssh) [IO.File]::WriteAllText($ImathPs, ($imathSrc -replace "`r?`n", "`r`n"), $Utf8Bom) # a .cmd shim, so "imath" works in cmd and in PowerShell whatever the execution policy $shim = "@echo off`r`npowershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$ImathPs`" %*`r`nexit /b %errorlevel%`r`n" [IO.File]::WriteAllText((Join-Path $BinDir 'imath.cmd'), $shim, (New-Object Text.ASCIIEncoding)) $up = Get-UserPath if (@($up -split ';') -notcontains $BinDir) { Set-UserPath ((@($up -split ';' | Where-Object { $_ }) + $BinDir) -join ';') } $env:Path = "$env:Path;$BinDir" # Tab completion of node names in PowerShell -- only where profiles may run, # otherwise every new window would greet you with an error. # this window runs with -ExecutionPolicy Bypass, so ask what a normal window gets $policy = 'Restricted' foreach ($sc in 'MachinePolicy', 'UserPolicy', 'CurrentUser', 'LocalMachine') { $v = Get-ExecutionPolicy -Scope $sc if ($v -ne 'Undefined') { $policy = "$v"; break } } if ($policy -in 'RemoteSigned', 'Unrestricted', 'Bypass') { $completion = @" $MarkBegin Register-ArgumentCompleter -Native -CommandName imath, imath.cmd -ScriptBlock { param(`$word, `$ast, `$cursor) `$c = Join-Path `$env:LOCALAPPDATA 'imath\nodes' `$w = @('help', 'refresh', 'nodes', 'gpus', 'upload', 'download', '--visual') if (Test-Path `$c) { `$w += Get-Content `$c } `$w | Where-Object { `$_ -like "`$word*" } | ForEach-Object { [Management.Automation.CompletionResult]::new(`$_, `$_, 'ParameterValue', `$_) } } $MarkEnd "@ foreach ($pf in Get-ProfilePaths) { $old = if (Test-Path $pf) { Remove-ConfigBlock ([IO.File]::ReadAllText($pf)) } else { '' } New-Item -ItemType Directory -Force -Path (Split-Path $pf) | Out-Null $txt = if ($old) { $old + "`r`n`r`n" + $completion } else { $completion } [IO.File]::WriteAllText($pf, ($txt -replace "`r?`n", "`r`n") + "`r`n", $Utf8Bom) } $tabNote = 'imath completes node names in PowerShell' } else { $tabNote = "no tab completion (PowerShell execution policy is $policy) - use: imath nodes" } & $ImathPs refresh | Out-Null Say "ok - open a NEW terminal, then: imath help ($tabNote)" Green # --- 7. Printer list --------------------------------------------------------- Step 'Institute printers' $lines = & $Ssh -o BatchMode=yes imath "LANG=C lpstat -h $PrintHost -a" 2>$null $queues = @($lines | ForEach-Object { if ("$_" -match '^([A-Za-z0-9._-]+)\s+(not\s+)?accepting') { $Matches[1] } } | Sort-Object -Unique) if (-not $queues.Count) { Fail "Could not read the printer list from $PrintHost." } $have = Get-InstalledPrinters for ($i = 0; $i -lt $queues.Count; $i += 4) { $row = '' foreach ($j in $i..([Math]::Min($i + 3, $queues.Count - 1))) { $mark = if ($have -contains $queues[$j]) { '*' } else { ' ' } $row += ('{0,3}) {1}{2,-16}' -f ($j + 1), $mark, $queues[$j]) } Write-Host $row } if ($have.Count) { Say '* = already installed' DarkGray } Write-Host '' Say 'Which printers? Numbers or names, separated by spaces (e.g. "h52 jcolor 23").' $ans = (Read-Host ' Enter = all, 0 = no printers').Trim() $NoPrinters = ($ans -eq '0' -or $ans -eq 'none') if ($NoPrinters) { $pick = @() } elseif (-not $ans) { $pick = $queues } else { $pick = @() foreach ($t in ($ans -split '[\s,;]+' | Where-Object { $_ })) { if ($t -match '^\d+$' -and [int]$t -ge 1 -and [int]$t -le $queues.Count) { $pick += $queues[[int]$t - 1] } elseif ($queues -contains $t) { $pick += $t } else { Say "unknown printer: $t (skipped)" Yellow } } $pick = @($pick | Sort-Object -Unique) } if ($NoPrinters) { Write-Host '' Write-Host ' Done - imath is set up, printers skipped (run this file again to add them).' -ForegroundColor Green Write-Host ' Open a NEW terminal and type: imath help' exit 0 } if (-not $pick.Count) { Fail 'No printer selected.' } # --- 8. Tunnel (background, autostart at login, reconnects by itself) --------- Step 'Background connection to the print server' if (-not (Test-Path $Dir)) { New-Item -ItemType Directory -Path $Dir | Out-Null } $tunnel = @" # I-MATH printer tunnel - installed by imath-windows.cmd # Forwards localhost:$LocalPort to ${PrintHost}:631 through the jump host and # reconnects after sleep / network changes. Remove with: imath-windows.cmd uninstall `$m = New-Object Threading.Mutex(`$false, 'Local\imath-printer-tunnel') if (-not `$m.WaitOne(0)) { exit } `$ErrorActionPreference = 'Continue' while (`$true) { # runs inside this hidden console, so ssh never opens a window of its own `$out = & '$Ssh' -N -o BatchMode=yes -o ExitOnForwardFailure=yes -o ConnectTimeout=15 `` -o ServerAliveInterval=30 -o ServerAliveCountMax=3 `` -L '${LocalPort}:${PrintHost}:631' imath 2>&1 "`$(Get-Date -Format s) ssh ended (`$LASTEXITCODE): `$((`$out | ForEach-Object { "`$_" }) -join ' ')" | Out-File -LiteralPath '$TunnelLog' -Encoding utf8 Start-Sleep -Seconds 15 } "@ [IO.File]::WriteAllText($TunnelPs, $tunnel, $Utf8Bom) $cmd = "powershell.exe -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File `"$TunnelPs`"" New-ItemProperty -Path $RunKey -Name $RunName -Value $cmd -PropertyType String -Force | Out-Null Stop-Tunnel Start-Process powershell.exe -WindowStyle Hidden -ArgumentList '-NoProfile', '-WindowStyle', 'Hidden', '-ExecutionPolicy', 'Bypass', '-File', "`"$TunnelPs`"" $up = $false foreach ($s in 1..30) { Start-Sleep -Seconds 1; if (Test-Port) { $up = $true; break } } if (-not $up) { $why = ''; if (Test-Path $TunnelLog) { $why = (Get-Content $TunnelLog -Raw) } Fail "The tunnel did not come up. $why" } Say 'running - starts automatically when you log in' Green # --- 9. Printers (needs admin once) ----------------------------------------- Step ("Adding {0} printer(s) - Windows asks for administrator rights" -f $pick.Count) $list = ($pick | ForEach-Object { "'$_'" }) -join ',' $r = Invoke-Admin 'I-MATH: adding printers' @" foreach (`$n in @($list)) { `$url = "http://localhost:$LocalPort/printers/`$n" if (Get-Printer -Name `$n -ErrorAction SilentlyContinue) { Out-Log "OK `$n (already there)"; continue } Write-Host "adding `$n ..." `$before = @(Get-Printer | ForEach-Object Name) `$err = `$null try { Add-Printer -IppURL `$url -ErrorAction Stop } catch { try { Add-Printer -Name `$n -PortName `$url -DriverName 'Microsoft IPP Class Driver' -ErrorAction Stop } catch { `$err = `$_.Exception.Message } } if (`$err) { Out-Log "FAIL `$n : `$err"; continue } `$added = @(Get-Printer | ForEach-Object Name | Where-Object { `$before -notcontains `$_ }) if (`$added.Count -eq 1 -and `$added[0] -ne `$n) { Rename-Printer -Name `$added[0] -NewName `$n } Out-Log "OK `$n" } "@ if ($null -eq $r) { Fail 'Administrator rights were refused - no printers added. Run the file again.' } $ok = @($r | Where-Object { $_ -match '^OK (\S+)' } | ForEach-Object { ($_ -split ' ')[1] }) $bad = @($r | Where-Object { $_ -like 'FAIL*' }) $all = @(($have + $ok) | Sort-Object -Unique) [IO.File]::WriteAllLines($State, [string[]]$all, $Utf8) Say ("{0} printer(s) ready: {1}" -f $ok.Count, ($ok -join ', ')) Green foreach ($b in $bad) { Say $b Red } Write-Host '' Write-Host ' Done.' -ForegroundColor Green Write-Host ' - Print from any app as usual: Ctrl+P -> pick h52, jcolor, ...' Write-Host ' - Works in eduroam / UZH network without VPN. Outside UZH it works' Write-Host ' wherever SSH (port 22) is open; otherwise switch on the UZH VPN.' Write-Host ' - imath: open a NEW terminal (cmd or PowerShell), then imath help,' Write-Host ' imath gpus, imath , imath upload ... - same as on the Mac.' Write-Host ' - Run this file again to add printers; "imath-windows.cmd uninstall" removes everything.'