| author | |
| committer | |
| log | bd135e7d420ebca93413f25f49ed0b0256791a79 |
| tree | 2f796cc0df4d8c7f286a6690c9676da571818ad3 |
| parent | 38232d1d522b2c555b7ccf5fd3d84499829fb64e |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
Ship one compiled worker and install it atomically in guests, replacing repeated PowerShell compilation. Keep existing worker paths pinned for cleanup of active operations.
Assisted-by: gpt-6.1-sol5 files changed, 85 insertions(+), 36 deletions(-)
guest/computer-windows.cs+9-1| ... | @@ -123,9 +123,17 @@ public static class ComputerWorker { | ... | @@ -123,9 +123,17 @@ public static class ComputerWorker { |
| 123 | [DllImport("user32.dll")] static extern bool SetProcessDPIAware(); | 123 | [DllImport("user32.dll")] static extern bool SetProcessDPIAware(); |
| 124 | [DllImport("advapi32.dll",SetLastError=true)] static extern bool GetTokenInformation(IntPtr token,int informationClass,out int elevation,int length,out int returned); | 124 | [DllImport("advapi32.dll",SetLastError=true)] static extern bool GetTokenInformation(IntPtr token,int informationClass,out int elevation,int length,out int returned); |
| 125 | [STAThread] public static int Main(string[] args) { | 125 | [STAThread] public static int Main(string[] args) { |
| 126 | if(args.Length==2 && (args[0]=="--prepare" || args[0]=="--cleanup" || args[0]=="--release")) { | 126 | if(args.Length==2 && (args[0]=="--prepare" || args[0]=="--cleanup" || args[0]=="--release" || args[0]=="--install")) { |
| 127 | try { | 127 | try { |
| 128 | if(args[0]=="--prepare")System.IO.Directory.CreateDirectory(args[1]); | 128 | if(args[0]=="--prepare")System.IO.Directory.CreateDirectory(args[1]); |
| 129 | else if(args[0]=="--install") { | ||
| 130 | System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(args[1])); | ||
| 131 | string temporary=args[1]+"."+Guid.NewGuid().ToString("N")+".new"; | ||
| 132 | try { | ||
| 133 | System.IO.File.Copy(System.Reflection.Assembly.GetExecutingAssembly().Location,temporary,true); | ||
| 134 | if(!System.IO.File.Exists(args[1]))System.IO.File.Move(temporary,args[1]); | ||
| 135 | } finally {System.IO.File.Delete(temporary);} | ||
| 136 | } | ||
| 129 | else Cleanup(args[1],args[0]=="--release"); | 137 | else Cleanup(args[1],args[0]=="--release"); |
| 130 | return 0; | 138 | return 0; |
| 131 | } | 139 | } |
nixos/computer-windows.nix created+19| ... | @@ -0,0 +1,19 @@ | ||
| 1 | { runCommand, fetchurl, mono, unzip }: | ||
| 2 | let | ||
| 3 | references = fetchurl { | ||
| 4 | url = "https://api.nuget.org/v3-flatcontainer/microsoft.netframework.referenceassemblies.net48/1.0.3/microsoft.netframework.referenceassemblies.net48.1.0.3.nupkg"; | ||
| 5 | hash = "sha256-in40hTjn65E1FpaRFon0nj1PY/i6tRdDK74Vm4sRBKI="; | ||
| 6 | }; | ||
| 7 | in runCommand "computer-windows" { | ||
| 8 | nativeBuildInputs = [ mono unzip ]; | ||
| 9 | } '' | ||
| 10 | unzip -q ${references} -d references | ||
| 11 | ref="$PWD/references/build/.NETFramework/v4.8" | ||
| 12 | mkdir -p "$out/bin" | ||
| 13 | mcs -noconfig -nostdlib -deterministic -optimize+ -target:winexe -platform:anycpu \ | ||
| 14 | -r:"$ref/mscorlib.dll" -r:"$ref/System.dll" -r:"$ref/System.Core.dll" \ | ||
| 15 | -r:"$ref/System.Management.dll" -r:"$ref/Microsoft.CSharp.dll" \ | ||
| 16 | -r:"$ref/System.Web.Extensions.dll" -r:"$ref/WindowsBase.dll" \ | ||
| 17 | -r:"$ref/UIAutomationClient.dll" -r:"$ref/UIAutomationTypes.dll" \ | ||
| 18 | -out:"$out/bin/worker.exe" ${../guest/computer-windows.cs} | ||
| 19 | '' | ||
nixos/configuration.nix+3-1| ... | @@ -6,9 +6,10 @@ let | ... | @@ -6,9 +6,10 @@ let |
| 6 | proxyToken = "/var/lib/snowglobe/dashboard-proxy.token"; | 6 | proxyToken = "/var/lib/snowglobe/dashboard-proxy.token"; |
| 7 | hostTools = lib.fileset.toSource { | 7 | hostTools = lib.fileset.toSource { |
| 8 | root = ../.; | 8 | root = ../.; |
| 9 | fileset = lib.fileset.unions [ ../tools/dashboard-host.py ../service/copyparty/manage.py ../service/copyparty/copyparty.conf ../tools/shale-labels.py ../tools/shale-access.py ../config/shale.json ../config/site.pkl ../dashboard/src/host-protocol.json ../tools/vms.py ../tools/vm-computer.py ../tools/vm-ai.py ../tools/vm-ai-limits.json ../tools/vm-rfb.py ../tools/mac-computer.py ../guest/computer-windows.cs ../guest/computer-legacy-mac.py ../guest/computer-linux.py ../guest/computer-linux-launch ../guest/computer-linux.nix ../tools/mac-vms.py ../tools/vm-ssh.py ../tools/dashboard-run.py ../tools/release.py ../guest/protocol.json ]; | 9 | fileset = lib.fileset.unions [ ../tools/dashboard-host.py ../service/copyparty/manage.py ../service/copyparty/copyparty.conf ../tools/shale-labels.py ../tools/shale-access.py ../config/shale.json ../config/site.pkl ../dashboard/src/host-protocol.json ../tools/vms.py ../tools/vm-computer.py ../tools/vm-ai.py ../tools/vm-ai-limits.json ../tools/vm-rfb.py ../tools/mac-computer.py ../guest/computer-legacy-mac.py ../guest/computer-linux.py ../guest/computer-linux-launch ../guest/computer-linux.nix ../tools/mac-vms.py ../tools/vm-ssh.py ../tools/dashboard-run.py ../tools/release.py ../guest/protocol.json ]; |
| 10 | }; | 10 | }; |
| 11 | computerAhk = pkgs.callPackage ./computer-ahk.nix { }; | 11 | computerAhk = pkgs.callPackage ./computer-ahk.nix { }; |
| 12 | computerWindows = pkgs.callPackage ./computer-windows.nix { }; | ||
| 12 | nativePkl = pkgs.callPackage ./pkl.nix { }; | 13 | nativePkl = pkgs.callPackage ./pkl.nix { }; |
| 13 | hostPython = pkgs.callPackage ./host-python.nix { }; | 14 | hostPython = pkgs.callPackage ./host-python.nix { }; |
| 14 | screen = pkgs.callPackage ./vm-screen.nix { }; | 15 | screen = pkgs.callPackage ./vm-screen.nix { }; |
| ... | @@ -259,6 +260,7 @@ in | ... | @@ -259,6 +260,7 @@ in |
| 259 | SNOWGLOBE_POOL = lib.attrByPath [ "SNOWGLOBE_POOL" ] "snowglobe-demo" config.environment.variables; | 260 | SNOWGLOBE_POOL = lib.attrByPath [ "SNOWGLOBE_POOL" ] "snowglobe-demo" config.environment.variables; |
| 260 | SNOWGLOBE_DASHBOARD_USER = "snowglobe-dashboard"; | 261 | SNOWGLOBE_DASHBOARD_USER = "snowglobe-dashboard"; |
| 261 | SNOWGLOBE_COMPUTER_AHK = toString computerAhk; | 262 | SNOWGLOBE_COMPUTER_AHK = toString computerAhk; |
| 263 | SNOWGLOBE_COMPUTER_WINDOWS_WORKER = "${computerWindows}/bin/worker.exe"; | ||
| 262 | SNOWGLOBE_VM_ACCEL = lib.attrByPath [ "SNOWGLOBE_VM_ACCEL" ] "kvm" config.environment.variables; | 264 | SNOWGLOBE_VM_ACCEL = lib.attrByPath [ "SNOWGLOBE_VM_ACCEL" ] "kvm" config.environment.variables; |
| 263 | }; | 265 | }; |
| 264 | serviceConfig = { | 266 | serviceConfig = { |
tools/computer-test.py+32-2| ... | @@ -75,6 +75,11 @@ class GuestCancellation(unittest.TestCase): | ... | @@ -75,6 +75,11 @@ class GuestCancellation(unittest.TestCase): |
| 75 | self.addCleanup(directory.cleanup) | 75 | self.addCleanup(directory.cleanup) |
| 76 | root = Path(directory.name) | 76 | root = Path(directory.name) |
| 77 | (root / 'fixture').mkdir() | 77 | (root / 'fixture').mkdir() |
| 78 | artifact = root / 'worker.exe' | ||
| 79 | artifact.write_bytes(b'compiled worker fixture') | ||
| 80 | environment = patch.dict(computer.os.environ, {'SNOWGLOBE_COMPUTER_WINDOWS_WORKER': str(artifact)}) | ||
| 81 | environment.start() | ||
| 82 | self.addCleanup(environment.stop) | ||
| 78 | patcher = patch.object(computer.vms, 'DISKS', root) | 83 | patcher = patch.object(computer.vms, 'DISKS', root) |
| 79 | patcher.start() | 84 | patcher.start() |
| 80 | self.addCleanup(patcher.stop) | 85 | self.addCleanup(patcher.stop) |
| ... | @@ -129,13 +134,38 @@ class GuestCancellation(unittest.TestCase): | ... | @@ -129,13 +134,38 @@ class GuestCancellation(unittest.TestCase): |
| 129 | return b'' | 134 | return b'' |
| 130 | token = computer.DEADLINE.set(time.monotonic() + 30) | 135 | token = computer.DEADLINE.set(time.monotonic() + 30) |
| 131 | try: | 136 | try: |
| 132 | with patch.object(computer, 'powershell', return_value=b'[true,true]'), patch.object(computer, 'guest_exec', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=b'M'): | 137 | with patch.object(computer, 'guest_exec', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=b'M'): |
| 133 | with self.assertRaises(TimeoutError): | 138 | with self.assertRaises(TimeoutError): |
| 134 | computer.worker('fixture', 'windows', {'action': 'input', 'actions': [{'type': 'text', 'text': 'hello'}]}) | 139 | computer.worker('fixture', 'windows', {'action': 'input', 'actions': [{'type': 'text', 'text': 'hello'}]}) |
| 135 | self.assertEqual(running, []) | 140 | self.assertEqual(running, []) |
| 136 | finally: | 141 | finally: |
| 137 | computer.DEADLINE.reset(token) | 142 | computer.DEADLINE.reset(token) |
| 138 | 143 | ||
| 144 | def test_cancelled_cold_install_cleans_staging_without_launching_a_request(self): | ||
| 145 | calls = [] | ||
| 146 | with computer.vms.computer_lock('fixture', 'status') as active: | ||
| 147 | operation = computer.OPERATION.set(('fixture', active['id'])) | ||
| 148 | deadline = computer.DEADLINE.set(time.monotonic() + 30) | ||
| 149 | def execute(name, executable, arguments, **kwargs): | ||
| 150 | computer.remaining(1) | ||
| 151 | calls.append(arguments) | ||
| 152 | if arguments[0] == '--install': | ||
| 153 | with computer.vms.computer_control(name) as control: | ||
| 154 | computer.vms.computer_cancel(control, active['id']) | ||
| 155 | computer.DEADLINE.set(time.monotonic() - 1) | ||
| 156 | return b'' | ||
| 157 | try: | ||
| 158 | with patch.object(computer, 'read_file', side_effect=ValueError('Missing cache')), \ | ||
| 159 | patch.object(computer, 'write_file'), patch.object(computer, 'guest_exec', execute), \ | ||
| 160 | self.assertRaises(InterruptedError): | ||
| 161 | computer.worker('fixture', 'windows', {'action': 'status'}) | ||
| 162 | self.assertTrue(any('rmdir' in arguments for arguments in calls)) | ||
| 163 | self.assertFalse(any(arguments[0] == '--launch' for arguments in calls)) | ||
| 164 | self.assertFalse((computer.vms.DISKS / 'fixture/computer-operation.json').exists()) | ||
| 165 | finally: | ||
| 166 | computer.OPERATION.reset(operation) | ||
| 167 | computer.DEADLINE.reset(deadline) | ||
| 168 | |||
| 139 | def test_console_remains_available_without_guest_agent(self): | 169 | def test_console_remains_available_without_guest_agent(self): |
| 140 | for platform in ['windows', 'linux']: | 170 | for platform in ['windows', 'linux']: |
| 141 | def virsh(action, *args, **kwargs): | 171 | def virsh(action, *args, **kwargs): |
| ... | @@ -371,7 +401,7 @@ class GuestCancellation(unittest.TestCase): | ... | @@ -371,7 +401,7 @@ class GuestCancellation(unittest.TestCase): |
| 371 | if arguments[0] == '--release': | 401 | if arguments[0] == '--release': |
| 372 | raise TimeoutError() | 402 | raise TimeoutError() |
| 373 | return b'' | 403 | return b'' |
| 374 | with patch.object(computer, 'powershell', return_value=b'[true,true]'), patch.object(computer, 'guest_exec', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=json.dumps(result).encode()): | 404 | with patch.object(computer, 'guest_exec', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=json.dumps(result).encode()): |
| 375 | self.assertEqual(computer.worker('fixture', 'windows', {'action': 'spawn'}), result) | 405 | self.assertEqual(computer.worker('fixture', 'windows', {'action': 'spawn'}), result) |
| 376 | pending = computer.vms.DISKS / 'fixture' / 'computer-operation.json' | 406 | pending = computer.vms.DISKS / 'fixture' / 'computer-operation.json' |
| 377 | operation = json.loads(pending.read_text()) | 407 | operation = json.loads(pending.read_text()) |
tools/vm-computer.py+22-32| ... | @@ -445,49 +445,39 @@ def worker(name, platform, request): | ... | @@ -445,49 +445,39 @@ def worker(name, platform, request): |
| 445 | identity = OPERATION.get() | 445 | identity = OPERATION.get() |
| 446 | run = identity[1].removeprefix('operation:') if identity else uuid.uuid4().hex | 446 | run = identity[1].removeprefix('operation:') if identity else uuid.uuid4().hex |
| 447 | directory = 'C:\\ProgramData\\SnowglobeComputer\\' + run | 447 | directory = 'C:\\ProgramData\\SnowglobeComputer\\' + run |
| 448 | source = Path(__file__).resolve().parent.parent / 'guest/computer-windows.cs' | 448 | artifact = os.environ.get('SNOWGLOBE_COMPUTER_WINDOWS_WORKER') |
| 449 | content = source.read_bytes() | 449 | if not artifact: |
| 450 | raise ValueError('The Windows computer worker is unavailable on the host.') | ||
| 451 | content = Path(artifact).read_bytes() | ||
| 450 | digest = hashlib.sha256(content).hexdigest()[:16] | 452 | digest = hashlib.sha256(content).hexdigest()[:16] |
| 451 | installed_worker = 'C:\\ProgramData\\SnowglobeComputer\\worker-' + digest + '.exe' | 453 | installed_worker = 'C:\\ProgramData\\SnowglobeComputer\\worker-' + digest + '.exe' |
| 452 | try: | 454 | try: |
| 453 | read_file(name, installed_worker, length=1) | 455 | read_file(name, installed_worker, length=1) |
| 454 | except ValueError: | 456 | except ValueError: |
| 455 | setup = "$d='" + directory + "'; New-Item -ItemType Directory -Force -Path $d | Out-Null; " | 457 | staging = 'C:\\Windows\\Temp\\snowglobe-worker-' + uuid.uuid4().hex |
| 456 | setup += "@((Test-Path C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe),(Test-Path -LiteralPath '" + installed_worker + "')) | ConvertTo-Json -Compress" | ||
| 457 | installed, exists = json.loads(powershell(name,setup)) | ||
| 458 | else: | ||
| 459 | exists = True | ||
| 460 | guest_exec(name, installed_worker, ['--prepare', directory]) | ||
| 461 | try: | 458 | try: |
| 462 | read_file(name, 'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe', length=1) | 459 | guest_exec(name, 'cmd.exe', ['/d', '/c', 'mkdir', staging]) |
| 463 | installed = True | 460 | write_file(name, staging + '\\worker.exe', content) |
| 464 | except ValueError: | 461 | guest_exec(name, staging + '\\worker.exe', ['--install', installed_worker]) |
| 465 | installed = False | 462 | finally: |
| 463 | cleanup_deadline = DEADLINE.set(time.monotonic() + 15) | ||
| 464 | cleanup_operation = OPERATION.set(None) | ||
| 465 | try: | ||
| 466 | guest_exec(name, 'cmd.exe', ['/d', '/c', 'rmdir', '/s', '/q', staging, '&', 'exit', '/b', '0']) | ||
| 467 | finally: | ||
| 468 | OPERATION.reset(cleanup_operation) | ||
| 469 | DEADLINE.reset(cleanup_deadline) | ||
| 470 | guest_exec(name, installed_worker, ['--prepare', directory]) | ||
| 471 | try: | ||
| 472 | read_file(name, 'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe', length=1) | ||
| 473 | installed = True | ||
| 474 | except ValueError: | ||
| 475 | installed = False | ||
| 466 | ahk=os.environ.get('SNOWGLOBE_COMPUTER_AHK') | 476 | ahk=os.environ.get('SNOWGLOBE_COMPUTER_AHK') |
| 467 | if not installed and ahk: | 477 | if not installed and ahk: |
| 468 | write_file(name,'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe',(Path(ahk)/'AutoHotkey32.exe').read_bytes()) | 478 | write_file(name,'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe',(Path(ahk)/'AutoHotkey32.exe').read_bytes()) |
| 469 | write_file(name,'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey-license.txt',(Path(ahk)/'license.txt').read_bytes()) | 479 | write_file(name,'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey-license.txt',(Path(ahk)/'license.txt').read_bytes()) |
| 470 | write_file(name,directory+'\\request.json',json.dumps(request).encode()) | 480 | write_file(name,directory+'\\request.json',json.dumps(request).encode()) |
| 471 | if not exists: | ||
| 472 | source_path = directory + '\\worker.cs' | ||
| 473 | write_file(name, source_path, content) | ||
| 474 | compile_script = r""" | ||
| 475 | $ErrorActionPreference='Stop' | ||
| 476 | $lock=New-Object Threading.Mutex($false,'Global\SnowglobeComputerCompile-HASH') | ||
| 477 | if(!$lock.WaitOne(0)){$lock.Dispose();Write-Output 'pending';return} | ||
| 478 | try { | ||
| 479 | if(Test-Path -LiteralPath 'WORKER'){return} | ||
| 480 | $references=@('System.dll','System.Core.dll') | ||
| 481 | foreach($assembly in @('System.Web.Extensions','System.Management','Microsoft.CSharp','UIAutomationClient','UIAutomationTypes','WindowsBase')) { | ||
| 482 | $references += [Reflection.Assembly]::LoadWithPartialName($assembly).Location | ||
| 483 | } | ||
| 484 | Remove-Item -LiteralPath 'WORKER.new.exe' -Force -ErrorAction SilentlyContinue | ||
| 485 | Add-Type -TypeDefinition ([IO.File]::ReadAllText('SOURCE')) -ReferencedAssemblies $references -OutputAssembly 'WORKER.new.exe' -OutputType WindowsApplication | ||
| 486 | Move-Item -LiteralPath 'WORKER.new.exe' -Destination 'WORKER' | ||
| 487 | } finally {$lock.ReleaseMutex();$lock.Dispose()} | ||
| 488 | """.replace('SOURCE', source_path).replace('WORKER', installed_worker).replace('HASH',digest) | ||
| 489 | if powershell(name, compile_script, timeout=60).strip() == b'pending': | ||
| 490 | raise ValueError('Computer tools are still installing. Retry in a moment.') | ||
| 491 | operation = {'platform': platform, 'id': run, 'worker': installed_worker} | 481 | operation = {'platform': platform, 'id': run, 'worker': installed_worker} |
| 492 | record_operation(name, operation) | 482 | record_operation(name, operation) |
| 493 | try: | 483 | try: |