vamp-mcp 0.1.9 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of vamp-mcp might be problematic. Click here for more details.
- package/LICENSE +21 -21
- package/README.md +144 -107
- package/THIRD_PARTY_NOTICES.md +32 -27
- package/bin/vamp-mcp.mjs +37 -37
- package/notices/mcp-virtualbox-LICENSE +21 -21
- package/notices/mcp-virtualbox-PROVENANCE.json +37 -37
- package/package.json +51 -17
- package/runtime/backends/common/Test-Environment.ps1 +7 -6
- package/runtime/backends/common/guest-install.json +58 -0
- package/runtime/backends/common/guest_install.py +57 -0
- package/runtime/backends/common/host-support.cjs +16 -0
- package/runtime/backends/common/packer.cjs +16 -0
- package/runtime/backends/hyperv/scripts/Manage-Desktop.ps1 +40 -67
- package/runtime/backends/hyperv/scripts/Send-InstallerBoot.ps1 +25 -0
- package/runtime/backends/kvm/debian-preseed.cfg +35 -35
- package/runtime/backends/library/registry.py +77 -77
- package/runtime/backends/library/replay.py +154 -154
- package/runtime/backends/packer/answer_media.py +16 -0
- package/runtime/backends/packer/deploy-hyperv.ps1 +10 -0
- package/runtime/backends/packer/drivers.py +49 -0
- package/runtime/backends/packer/hyperv-network.ps1 +10 -0
- package/runtime/backends/packer/images.py +75 -0
- package/runtime/backends/packer/osinfo.py +113 -0
- package/runtime/backends/packer/policy.json +15 -0
- package/runtime/backends/packer/runner.py +224 -0
- package/runtime/backends/packer/templates.py +118 -0
- package/runtime/backends/packer/toolchain.py +80 -0
- package/runtime/backends/remote_ssh/server.mjs +321 -94
- package/runtime/backends/virtualbox/desktop.cjs +37 -0
- package/runtime/backends/virtualbox/display.py +101 -101
- package/runtime/backends/virtualbox/history.py +139 -139
- package/runtime/backends/virtualbox/mouse_click.py +15 -15
- package/runtime/backends/virtualbox/mouse_move.py +18 -18
- package/runtime/backends/virtualbox/profiles.json +46 -46
- package/runtime/backends/virtualbox/provision.py +238 -184
- package/runtime/backends/virtualbox/recording_listener.py +29 -29
- package/runtime/backends/virtualbox/recordings.py +39 -39
- package/runtime/backends/virtualbox/requirements.txt +7 -7
- package/runtime/backends/virtualbox/rpas/debian_first_boot/manifest.json +1 -1
- package/runtime/backends/virtualbox/rpas/debian_first_boot/run.robot +13 -13
- package/runtime/backends/virtualbox/rpas/debian_first_boot/workflow.py +75 -75
- package/runtime/backends/virtualbox/rpas/observe/manifest.json +7 -7
- package/runtime/backends/virtualbox/rpas/observe/run.robot +13 -13
- package/runtime/backends/virtualbox/rpas/observe/workflow.py +14 -14
- package/runtime/backends/virtualbox/server.mjs +440 -63
- package/runtime/backends/virtualbox/setup_windows_sdk.py +58 -58
- package/runtime/backends/virtualbox/shares.py +68 -68
- package/runtime/backends/virtualbox/snapshots.py +53 -53
- package/runtime/backends/virtualbox/transfers.py +89 -89
- package/runtime/backends/virtualbox/vnc.py +61 -61
- package/runtime/guest_keys.py +13 -13
- package/runtime/guest_setup/http/debian-preseed.cfg +39 -0
- package/runtime/guest_setup/http/postdeb.sh +94 -0
- package/runtime/guest_setup/http/postwin.ps1 +53 -0
- package/runtime/guest_setup/http/setup-all.ps1 +25 -0
- package/runtime/guest_setup/http/setup-win.cmd +7 -0
- package/runtime/guest_setup/http/setup.ps1 +44 -0
- package/runtime/guest_setup/http/setup.sh +50 -0
- package/runtime/guest_setup/kvm/setup-debian.sh +73 -0
- package/runtime/guest_setup/kvm/setup-windows.ps1 +52 -0
- package/runtime/guest_setup/recorder/README.txt +12 -8
- package/runtime/guest_setup/recorder/RECORDER-LICENSE +674 -674
- package/runtime/guest_setup/recorder/RECORDING-GUIDE.txt +31 -31
- package/runtime/guest_setup/recorder/apply-display.py +41 -41
- package/runtime/guest_setup/recorder/configure-debian-mirrors.py +62 -62
- package/runtime/guest_setup/recorder/configure-gdm.py +23 -0
- package/runtime/guest_setup/recorder/configure-session.ps1 +14 -0
- package/runtime/guest_setup/recorder/gnome-display.py +60 -60
- package/runtime/guest_setup/recorder/install-display-startup.py +67 -39
- package/runtime/guest_setup/recorder/install-hypervisor-drivers.ps1 +34 -0
- package/runtime/guest_setup/recorder/install-resolution-shortcut.ps1 +36 -0
- package/runtime/guest_setup/recorder/postwin.cmd +10 -10
- package/runtime/guest_setup/recorder/prepare-recording.py +30 -30
- package/runtime/guest_setup/recorder/recorder.zip +0 -0
- package/runtime/guest_setup/recorder/requirements-recorder.txt +2 -2
- package/runtime/guest_setup/recorder/session-policy.py +22 -0
- package/runtime/guest_setup/recorder/set-resolution-registry.ps1 +40 -0
- package/runtime/guest_setup/recorder/setup-user.py +53 -35
- package/runtime/guest_setup/recorder/setup-win.cmd +4 -11
- package/runtime/guest_setup/recorder/setup.ps1 +112 -30
- package/runtime/guest_setup/recorder/setup.sh +19 -1
- package/runtime/guest_setup/recorder/vamp-recorder.py +148 -148
- package/runtime/guest_setup/recorder/verify-recorder.py +4 -4
- package/runtime/guest_setup/recorder/windows-display.py +76 -76
- package/runtime/guest_setup/requirements.txt +2 -0
- package/runtime/guest_setup/setup-all.ps1 +35 -0
- package/runtime/guest_setup/setup-all.sh +22 -0
- package/runtime/guest_setup/vault/Install-VCRuntime.ps1 +26 -0
- package/runtime/guest_setup/vault/setup-guest-vault.ps1 +148 -0
- package/runtime/guest_setup/vault/setup-guest-vault.sh +75 -0
- package/runtime/recording_converter.py +230 -230
- package/runtime/requirements-mcp.txt +11 -11
- package/runtime/rpas/README.md +161 -161
- package/runtime/rpas/captcha_handoff/manifest.json +27 -27
- package/runtime/rpas/captcha_handoff/run.robot +16 -16
- package/runtime/rpas/captcha_handoff/workflow.py +11 -11
- package/runtime/rpas/common.resource +26 -26
- package/runtime/rpas/debian_update/manifest.json +27 -27
- package/runtime/rpas/debian_update/run.robot +17 -17
- package/runtime/rpas/debian_update/workflow.py +13 -13
- package/runtime/rpas/desktop_control/manifest.json +26 -26
- package/runtime/rpas/desktop_control/run.robot +27 -27
- package/runtime/rpas/desktop_control/workflow.py +51 -51
- package/runtime/rpas/firefox_launch/manifest.json +27 -27
- package/runtime/rpas/firefox_launch/run.robot +15 -15
- package/runtime/rpas/firefox_launch/workflow.py +25 -25
- package/runtime/rpas/firefox_naver/manifest.json +27 -27
- package/runtime/rpas/firefox_naver/run.robot +21 -21
- package/runtime/rpas/firefox_naver/workflow.py +28 -28
- package/runtime/rpas/kvm_validation/README.md +11 -11
- package/runtime/rpas/kvm_validation/manifest.json +28 -28
- package/runtime/rpas/kvm_validation/run.robot +30 -30
- package/runtime/rpas/kvm_validation/workflow.py +91 -91
- package/runtime/rpas/manifest.schema.json +105 -105
- package/runtime/rpas/naver_login/login.robot +25 -25
- package/runtime/rpas/naver_login/manifest.json +33 -33
- package/runtime/rpas/naver_login/run.robot +20 -20
- package/runtime/rpas/naver_login/workflow.py +32 -32
- package/runtime/rpas/windows_naver_recorded/README.md +24 -24
- package/runtime/rpas/windows_naver_recorded/conversion.json +25 -25
- package/runtime/rpas/windows_naver_recorded/manifest.json +28 -28
- package/runtime/rpas/windows_naver_recorded/run.robot +37 -37
- package/runtime/rpas/windows_naver_recorded/workflow.py +32 -32
- package/runtime/scripts/build_recorder_media.py +39 -39
- package/runtime/scripts/macos/setup-host.sh +126 -0
- package/runtime/scripts/remote_host_setup.sh +7 -4
- package/runtime/scripts/script_server.py +571 -0
- package/runtime/skills/vamp-rpa-ui-checks/SKILL.md +26 -0
- package/runtime/skills/vamp-rpa-ui-checks/agents/openai.yaml +4 -0
- package/runtime/skills/vamp-rpa-ui-checks/references/display.md +12 -0
- package/runtime/skills/vamp-rpa-ui-checks/references/user-guide.md +51 -0
- package/runtime/skills/vamp-ui-cleanup/SKILL.md +15 -0
- package/runtime/skills/vamp-ui-cleanup/agents/openai.yaml +4 -0
- package/runtime/skills/vamp-ui-validation/SKILL.md +27 -0
- package/runtime/skills/vamp-ui-validation/agents/openai.yaml +4 -0
- package/runtime/third_party/mcp-virtualbox/scripts/mouse_click.py +65 -65
- package/runtime/third_party/pyautogui-recorder/LICENSE +674 -674
- package/runtime/third_party/pyautogui-recorder/PROVENANCE.json +16 -16
- package/runtime/third_party/pyautogui-recorder/PyAutoGuiRecorder.py +169 -169
- package/runtime/third_party/pyautogui-recorder/README.md +53 -53
- package/runtime/scripts/build_debian_installer.py +0 -48
|
@@ -1,154 +1,154 @@
|
|
|
1
|
-
"""Replay reviewed, AST-parsed actions on guest devices. Never exec recorder code."""
|
|
2
|
-
import asyncio
|
|
3
|
-
import hashlib
|
|
4
|
-
import json
|
|
5
|
-
import os
|
|
6
|
-
from pathlib import Path
|
|
7
|
-
import re
|
|
8
|
-
import subprocess
|
|
9
|
-
import sys
|
|
10
|
-
import uuid
|
|
11
|
-
import xml.etree.ElementTree as ET
|
|
12
|
-
ROOT=Path(__file__).resolve().parents[2];sys.path.insert(0,str(ROOT))
|
|
13
|
-
from guest_keys import SCANCODES,SHIFTED
|
|
14
|
-
|
|
15
|
-
VNC_KEYS={'ctrl':'Ctrl','ctrlright':'Ctrl_R','alt':'Alt','altright':'Alt_R','shift':'Shift','shiftright':'Shift_R','super':'Super_L','superright':'Super_R','enter':'Return','esc':'Escape','space':'space','backspace':'BackSpace','delete':'Delete','tab':'Tab','left':'Left','right':'Right','up':'Up','down':'Down','home':'Home','end':'End','pageup':'Page_Up','pagedown':'Page_Down','insert':'Insert','capslock':'Caps_Lock'}
|
|
16
|
-
EXTRA={'shiftright':54,'ctrlright':0x11d,'altright':0x138,'superright':0x15c,'home':0x147,'end':0x14f,'pageup':0x149,'pagedown':0x151,'insert':0x152,'capslock':58}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
def load(request):
|
|
20
|
-
identifier=str(uuid.UUID(request['id']));folder=Path(request['registry']).resolve()/identifier
|
|
21
|
-
metadata=json.loads((folder/'rpa.json').read_text(encoding='utf8'))
|
|
22
|
-
if metadata.get('id')!=identifier or metadata.get('format')!='vamp-saved-rpa':raise ValueError('Unknown saved RPA')
|
|
23
|
-
if not(folder/'.git').is_dir():raise ValueError('Version history is missing. Edit and save this RPA in the library first')
|
|
24
|
-
committed=subprocess.run(['git','-C',str(folder),'diff','--quiet','HEAD','--','source.py','actions.json','rpa.json'],capture_output=True,timeout=15)
|
|
25
|
-
if committed.returncode:raise ValueError('RPA has uncommitted changes. Save and review a new revision before replay')
|
|
26
|
-
if metadata.get('reviewed') is not True or request.get('confirmed') is not True:raise ValueError('Review this RPA in the library and explicitly confirm execution first')
|
|
27
|
-
if hashlib.sha256((folder/'source.py').read_bytes()).hexdigest()!=metadata['source_sha256']:raise ValueError('RPA source changed; recreate and review it before running')
|
|
28
|
-
raw=(folder/'actions.json').read_bytes()
|
|
29
|
-
if hashlib.sha256(raw).hexdigest()!=metadata['actions_sha256']:raise ValueError('RPA actions changed; recreate and review it before running')
|
|
30
|
-
values=request.get('inputs',{})
|
|
31
|
-
if not isinstance(values,dict) or set(values)!=set(metadata['inputs']) or any(not isinstance(v,str) or len(v)>10000 for v in values.values()):raise ValueError('Supply exactly the declared input names as strings')
|
|
32
|
-
actions=json.loads(raw)
|
|
33
|
-
for action in actions:
|
|
34
|
-
if action.get('op')=='write':
|
|
35
|
-
action['text']=re.sub(r'\$\{([A-Za-z_][A-Za-z0-9_]*)\}',lambda m:values[m[1]] if m[1] in values else m[0],action['text'])
|
|
36
|
-
uuid.UUID(request['vm_id'])
|
|
37
|
-
return metadata,actions
|
|
38
|
-
|
|
39
|
-
class VBox:
|
|
40
|
-
def __init__(self,vm):
|
|
41
|
-
import contextlib
|
|
42
|
-
self.vm=vm;self.held=set();self.implicit=set();self.owns_shift=False;self.mask=0;self.x=1;self.y=1
|
|
43
|
-
try:from vboxapi import VirtualBoxManager
|
|
44
|
-
except ImportError:raise ValueError('VirtualBox mouse SDK is missing. Run VAMP Python setup on Windows, or configure VAMP Mouse Python Executable with vboxapi on Linux/macOS') from None
|
|
45
|
-
self.manager=VirtualBoxManager(None,None);self.box=self.manager.getVirtualBox();machine=self.box.findMachine(vm)
|
|
46
|
-
if str(machine.state) not in ('5','Running'):raise ValueError('Start the target VM before replay')
|
|
47
|
-
self.session=self.manager.getSessionObject();machine.lockMachine(self.session,1);self.console=self.session.console
|
|
48
|
-
def geometry(self):return list(self.console.display.getScreenResolution(0)[:2])
|
|
49
|
-
def _scans(self,key,release=False):
|
|
50
|
-
base=SHIFTED.get(key,key.lower());scan={**SCANCODES,**EXTRA}.get(base)
|
|
51
|
-
if scan is None:raise ValueError('Unsupported VirtualBox key: '+key)
|
|
52
|
-
code=scan&0xff;return ([0xe0] if scan>0xff else [])+[code | (0x80 if release else 0)]
|
|
53
|
-
def down(self,key):
|
|
54
|
-
if key in self.held:return
|
|
55
|
-
if key in SHIFTED or (len(key)==1 and key.isupper()):
|
|
56
|
-
if 'shift' not in self.held:self.down('shift');self.owns_shift=True
|
|
57
|
-
self.implicit.add(key)
|
|
58
|
-
self.console.keyboard.putScancodes(self._scans(key));self.held.add(key)
|
|
59
|
-
def up(self,key):
|
|
60
|
-
if key not in self.held:return
|
|
61
|
-
self.console.keyboard.putScancodes(self._scans(key,True));self.held.discard(key)
|
|
62
|
-
self.implicit.discard(key)
|
|
63
|
-
if not self.implicit and self.owns_shift:
|
|
64
|
-
self.owns_shift=False;self.up('shift')
|
|
65
|
-
def move(self,x,y):
|
|
66
|
-
if not self.console.mouse.absoluteSupported:raise ValueError('Enable USB tablet input for this VM')
|
|
67
|
-
self.x=int(x)+1;self.y=int(y)+1;self.console.mouse.putMouseEventAbsolute(self.x,self.y,0,0,self.mask)
|
|
68
|
-
def click(self,button):
|
|
69
|
-
mask={'left':1,'right':2,'middle':4}[button];self.console.mouse.putMouseEventAbsolute(self.x,self.y,0,0,mask);self.console.mouse.putMouseEventAbsolute(self.x,self.y,0,0,0)
|
|
70
|
-
def scroll(self,n):self.console.mouse.putMouseEventAbsolute(self.x,self.y,-int(n),0,0)
|
|
71
|
-
async def drain(self):pass
|
|
72
|
-
def close(self):
|
|
73
|
-
try:
|
|
74
|
-
for key in list(self.held):self.up(key)
|
|
75
|
-
finally:self.session.unlockMachine()
|
|
76
|
-
|
|
77
|
-
class VNC:
|
|
78
|
-
def __init__(self,client):self.client=client;self.held={}
|
|
79
|
-
def geometry(self):return [self.client.video.width,self.client.video.height]
|
|
80
|
-
def key(self,key):return VNC_KEYS.get(key,key.upper() if key.startswith('f') and key[1:].isdigit() else key)
|
|
81
|
-
def down(self,key):
|
|
82
|
-
if key in self.held:return
|
|
83
|
-
keys=[self.key(key)]
|
|
84
|
-
if key in SHIFTED or (len(key)==1 and key.isupper()):keys=['Shift',SHIFTED.get(key,key)]
|
|
85
|
-
hold=self.client.keyboard.hold(*keys);hold.__enter__();self.held[key]=hold
|
|
86
|
-
def up(self,key):
|
|
87
|
-
hold=self.held.pop(key,None)
|
|
88
|
-
if hold:hold.__exit__(None,None,None)
|
|
89
|
-
def move(self,x,y):self.client.mouse.move(int(x),int(y))
|
|
90
|
-
def click(self,button):self.client.mouse.click({'left':0,'middle':1,'right':2}[button])
|
|
91
|
-
def scroll(self,n):
|
|
92
|
-
for _ in range(abs(n)):self.client.mouse.click(3 if n>0 else 4)
|
|
93
|
-
async def drain(self):await self.client.drain()
|
|
94
|
-
def close(self):
|
|
95
|
-
for key in list(self.held):self.up(key)
|
|
96
|
-
|
|
97
|
-
async def replay(driver,actions,resolution):
|
|
98
|
-
completed=0
|
|
99
|
-
try:
|
|
100
|
-
if driver.geometry()!=resolution:raise ValueError(f'Recorded guest resolution {resolution} does not match {driver.geometry()}; recalibrate rather than guessing')
|
|
101
|
-
for a in actions:
|
|
102
|
-
op=a['op']
|
|
103
|
-
if op=='wait':await asyncio.sleep(a['seconds'])
|
|
104
|
-
elif op=='key_down':driver.down(a['key'])
|
|
105
|
-
elif op=='key_up':driver.up(a['key'])
|
|
106
|
-
elif op=='write':
|
|
107
|
-
for character in a['text']:
|
|
108
|
-
key={'\n':'enter','\t':'tab'}.get(character,character);driver.down(key);driver.up(key);await driver.drain();await asyncio.sleep(a.get('interval',.03))
|
|
109
|
-
elif op=='hotkey':
|
|
110
|
-
try:
|
|
111
|
-
for key in a['keys']:driver.down(key)
|
|
112
|
-
finally:
|
|
113
|
-
for key in reversed(a['keys']):driver.up(key)
|
|
114
|
-
elif op=='press':
|
|
115
|
-
for _ in range(a['presses']):
|
|
116
|
-
for key in a['keys']:driver.down(key);driver.up(key)
|
|
117
|
-
await asyncio.sleep(a['interval'])
|
|
118
|
-
elif op in ('move','click'):
|
|
119
|
-
driver.move(a['x'],a['y'])
|
|
120
|
-
if op=='click':
|
|
121
|
-
for _ in range(a['clicks']):driver.click(a['button']);await driver.drain();await asyncio.sleep(a['interval'])
|
|
122
|
-
elif op=='scroll':
|
|
123
|
-
if 'x' in a:driver.move(a['x'],a['y'])
|
|
124
|
-
driver.scroll(a['clicks'])
|
|
125
|
-
else:raise ValueError('Unsupported replay operation: '+op)
|
|
126
|
-
await driver.drain();completed+=1;await asyncio.sleep(.01)
|
|
127
|
-
finally:driver.close()
|
|
128
|
-
return completed
|
|
129
|
-
|
|
130
|
-
async def run(request):
|
|
131
|
-
metadata,actions=load(request)
|
|
132
|
-
if metadata['backend']=='virtualbox':
|
|
133
|
-
completed=await replay(VBox(request['vm_id']),actions,metadata['resolution'])
|
|
134
|
-
elif metadata['backend']=='kvm':
|
|
135
|
-
import asyncvnc
|
|
136
|
-
uri=request.get('libvirt_uri','qemu:///session')
|
|
137
|
-
if uri not in ('qemu:///session','qemu:///system'):raise ValueError('Only local libvirt connections are supported')
|
|
138
|
-
def virsh(*args):
|
|
139
|
-
p=subprocess.run(['virsh','-c',uri,*args],capture_output=True,text=True,timeout=30)
|
|
140
|
-
if p.returncode:raise RuntimeError(p.stderr[:300])
|
|
141
|
-
return p.stdout
|
|
142
|
-
if virsh('domstate',request['vm_id']).strip()!='running':raise ValueError('Start the target VM before replay')
|
|
143
|
-
xml=ET.fromstring(virsh('dumpxml',request['vm_id']));graphics=xml.find("devices/graphics[@type='vnc']")
|
|
144
|
-
if graphics is None:raise ValueError('The target needs a localhost VNC display')
|
|
145
|
-
listen=graphics.find('listen');host=graphics.get('listen') or (listen.get('address') if listen is not None else None) or '127.0.0.1';port=int(graphics.get('port','-1'))
|
|
146
|
-
if host not in ('127.0.0.1','localhost','::1') or not 1<=port<=65535:raise ValueError('Only active localhost VNC is supported')
|
|
147
|
-
async with asyncvnc.connect(host,port,password=os.environ.get('VAMP_VNC_PASSWORD')) as client:
|
|
148
|
-
completed=await replay(VNC(client),actions,metadata['resolution'])
|
|
149
|
-
else:raise ValueError('This backend has no recorded desktop replay adapter')
|
|
150
|
-
return {'status':'completed_unverified','rpa_id':metadata['id'],'actions_completed':completed,'expected_result':metadata['expected_result'],'message':'Replay completed. Verify the expected application result; no screenshot or model call was made.'}
|
|
151
|
-
|
|
152
|
-
if __name__=='__main__':
|
|
153
|
-
try:print(json.dumps(asyncio.run(asyncio.wait_for(run(json.load(sys.stdin)),timeout=600)),ensure_ascii=False))
|
|
154
|
-
except Exception as error:print(json.dumps({'status':'error','message':str(error)}));sys.exit(1)
|
|
1
|
+
"""Replay reviewed, AST-parsed actions on guest devices. Never exec recorder code."""
|
|
2
|
+
import asyncio
|
|
3
|
+
import hashlib
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import re
|
|
8
|
+
import subprocess
|
|
9
|
+
import sys
|
|
10
|
+
import uuid
|
|
11
|
+
import xml.etree.ElementTree as ET
|
|
12
|
+
ROOT=Path(__file__).resolve().parents[2];sys.path.insert(0,str(ROOT))
|
|
13
|
+
from guest_keys import SCANCODES,SHIFTED
|
|
14
|
+
|
|
15
|
+
VNC_KEYS={'ctrl':'Ctrl','ctrlright':'Ctrl_R','alt':'Alt','altright':'Alt_R','shift':'Shift','shiftright':'Shift_R','super':'Super_L','superright':'Super_R','enter':'Return','esc':'Escape','space':'space','backspace':'BackSpace','delete':'Delete','tab':'Tab','left':'Left','right':'Right','up':'Up','down':'Down','home':'Home','end':'End','pageup':'Page_Up','pagedown':'Page_Down','insert':'Insert','capslock':'Caps_Lock'}
|
|
16
|
+
EXTRA={'shiftright':54,'ctrlright':0x11d,'altright':0x138,'superright':0x15c,'home':0x147,'end':0x14f,'pageup':0x149,'pagedown':0x151,'insert':0x152,'capslock':58}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def load(request):
|
|
20
|
+
identifier=str(uuid.UUID(request['id']));folder=Path(request['registry']).resolve()/identifier
|
|
21
|
+
metadata=json.loads((folder/'rpa.json').read_text(encoding='utf8'))
|
|
22
|
+
if metadata.get('id')!=identifier or metadata.get('format')!='vamp-saved-rpa':raise ValueError('Unknown saved RPA')
|
|
23
|
+
if not(folder/'.git').is_dir():raise ValueError('Version history is missing. Edit and save this RPA in the library first')
|
|
24
|
+
committed=subprocess.run(['git','-C',str(folder),'diff','--quiet','HEAD','--','source.py','actions.json','rpa.json'],capture_output=True,timeout=15)
|
|
25
|
+
if committed.returncode:raise ValueError('RPA has uncommitted changes. Save and review a new revision before replay')
|
|
26
|
+
if metadata.get('reviewed') is not True or request.get('confirmed') is not True:raise ValueError('Review this RPA in the library and explicitly confirm execution first')
|
|
27
|
+
if hashlib.sha256((folder/'source.py').read_bytes()).hexdigest()!=metadata['source_sha256']:raise ValueError('RPA source changed; recreate and review it before running')
|
|
28
|
+
raw=(folder/'actions.json').read_bytes()
|
|
29
|
+
if hashlib.sha256(raw).hexdigest()!=metadata['actions_sha256']:raise ValueError('RPA actions changed; recreate and review it before running')
|
|
30
|
+
values=request.get('inputs',{})
|
|
31
|
+
if not isinstance(values,dict) or set(values)!=set(metadata['inputs']) or any(not isinstance(v,str) or len(v)>10000 for v in values.values()):raise ValueError('Supply exactly the declared input names as strings')
|
|
32
|
+
actions=json.loads(raw)
|
|
33
|
+
for action in actions:
|
|
34
|
+
if action.get('op')=='write':
|
|
35
|
+
action['text']=re.sub(r'\$\{([A-Za-z_][A-Za-z0-9_]*)\}',lambda m:values[m[1]] if m[1] in values else m[0],action['text'])
|
|
36
|
+
uuid.UUID(request['vm_id'])
|
|
37
|
+
return metadata,actions
|
|
38
|
+
|
|
39
|
+
class VBox:
|
|
40
|
+
def __init__(self,vm):
|
|
41
|
+
import contextlib
|
|
42
|
+
self.vm=vm;self.held=set();self.implicit=set();self.owns_shift=False;self.mask=0;self.x=1;self.y=1
|
|
43
|
+
try:from vboxapi import VirtualBoxManager
|
|
44
|
+
except ImportError:raise ValueError('VirtualBox mouse SDK is missing. Run VAMP Python setup on Windows, or configure VAMP Mouse Python Executable with vboxapi on Linux/macOS') from None
|
|
45
|
+
self.manager=VirtualBoxManager(None,None);self.box=self.manager.getVirtualBox();machine=self.box.findMachine(vm)
|
|
46
|
+
if str(machine.state) not in ('5','Running'):raise ValueError('Start the target VM before replay')
|
|
47
|
+
self.session=self.manager.getSessionObject();machine.lockMachine(self.session,1);self.console=self.session.console
|
|
48
|
+
def geometry(self):return list(self.console.display.getScreenResolution(0)[:2])
|
|
49
|
+
def _scans(self,key,release=False):
|
|
50
|
+
base=SHIFTED.get(key,key.lower());scan={**SCANCODES,**EXTRA}.get(base)
|
|
51
|
+
if scan is None:raise ValueError('Unsupported VirtualBox key: '+key)
|
|
52
|
+
code=scan&0xff;return ([0xe0] if scan>0xff else [])+[code | (0x80 if release else 0)]
|
|
53
|
+
def down(self,key):
|
|
54
|
+
if key in self.held:return
|
|
55
|
+
if key in SHIFTED or (len(key)==1 and key.isupper()):
|
|
56
|
+
if 'shift' not in self.held:self.down('shift');self.owns_shift=True
|
|
57
|
+
self.implicit.add(key)
|
|
58
|
+
self.console.keyboard.putScancodes(self._scans(key));self.held.add(key)
|
|
59
|
+
def up(self,key):
|
|
60
|
+
if key not in self.held:return
|
|
61
|
+
self.console.keyboard.putScancodes(self._scans(key,True));self.held.discard(key)
|
|
62
|
+
self.implicit.discard(key)
|
|
63
|
+
if not self.implicit and self.owns_shift:
|
|
64
|
+
self.owns_shift=False;self.up('shift')
|
|
65
|
+
def move(self,x,y):
|
|
66
|
+
if not self.console.mouse.absoluteSupported:raise ValueError('Enable USB tablet input for this VM')
|
|
67
|
+
self.x=int(x)+1;self.y=int(y)+1;self.console.mouse.putMouseEventAbsolute(self.x,self.y,0,0,self.mask)
|
|
68
|
+
def click(self,button):
|
|
69
|
+
mask={'left':1,'right':2,'middle':4}[button];self.console.mouse.putMouseEventAbsolute(self.x,self.y,0,0,mask);self.console.mouse.putMouseEventAbsolute(self.x,self.y,0,0,0)
|
|
70
|
+
def scroll(self,n):self.console.mouse.putMouseEventAbsolute(self.x,self.y,-int(n),0,0)
|
|
71
|
+
async def drain(self):pass
|
|
72
|
+
def close(self):
|
|
73
|
+
try:
|
|
74
|
+
for key in list(self.held):self.up(key)
|
|
75
|
+
finally:self.session.unlockMachine()
|
|
76
|
+
|
|
77
|
+
class VNC:
|
|
78
|
+
def __init__(self,client):self.client=client;self.held={}
|
|
79
|
+
def geometry(self):return [self.client.video.width,self.client.video.height]
|
|
80
|
+
def key(self,key):return VNC_KEYS.get(key,key.upper() if key.startswith('f') and key[1:].isdigit() else key)
|
|
81
|
+
def down(self,key):
|
|
82
|
+
if key in self.held:return
|
|
83
|
+
keys=[self.key(key)]
|
|
84
|
+
if key in SHIFTED or (len(key)==1 and key.isupper()):keys=['Shift',SHIFTED.get(key,key)]
|
|
85
|
+
hold=self.client.keyboard.hold(*keys);hold.__enter__();self.held[key]=hold
|
|
86
|
+
def up(self,key):
|
|
87
|
+
hold=self.held.pop(key,None)
|
|
88
|
+
if hold:hold.__exit__(None,None,None)
|
|
89
|
+
def move(self,x,y):self.client.mouse.move(int(x),int(y))
|
|
90
|
+
def click(self,button):self.client.mouse.click({'left':0,'middle':1,'right':2}[button])
|
|
91
|
+
def scroll(self,n):
|
|
92
|
+
for _ in range(abs(n)):self.client.mouse.click(3 if n>0 else 4)
|
|
93
|
+
async def drain(self):await self.client.drain()
|
|
94
|
+
def close(self):
|
|
95
|
+
for key in list(self.held):self.up(key)
|
|
96
|
+
|
|
97
|
+
async def replay(driver,actions,resolution):
|
|
98
|
+
completed=0
|
|
99
|
+
try:
|
|
100
|
+
if driver.geometry()!=resolution:raise ValueError(f'Recorded guest resolution {resolution} does not match {driver.geometry()}; recalibrate rather than guessing')
|
|
101
|
+
for a in actions:
|
|
102
|
+
op=a['op']
|
|
103
|
+
if op=='wait':await asyncio.sleep(a['seconds'])
|
|
104
|
+
elif op=='key_down':driver.down(a['key'])
|
|
105
|
+
elif op=='key_up':driver.up(a['key'])
|
|
106
|
+
elif op=='write':
|
|
107
|
+
for character in a['text']:
|
|
108
|
+
key={'\n':'enter','\t':'tab'}.get(character,character);driver.down(key);driver.up(key);await driver.drain();await asyncio.sleep(a.get('interval',.03))
|
|
109
|
+
elif op=='hotkey':
|
|
110
|
+
try:
|
|
111
|
+
for key in a['keys']:driver.down(key)
|
|
112
|
+
finally:
|
|
113
|
+
for key in reversed(a['keys']):driver.up(key)
|
|
114
|
+
elif op=='press':
|
|
115
|
+
for _ in range(a['presses']):
|
|
116
|
+
for key in a['keys']:driver.down(key);driver.up(key)
|
|
117
|
+
await asyncio.sleep(a['interval'])
|
|
118
|
+
elif op in ('move','click'):
|
|
119
|
+
driver.move(a['x'],a['y'])
|
|
120
|
+
if op=='click':
|
|
121
|
+
for _ in range(a['clicks']):driver.click(a['button']);await driver.drain();await asyncio.sleep(a['interval'])
|
|
122
|
+
elif op=='scroll':
|
|
123
|
+
if 'x' in a:driver.move(a['x'],a['y'])
|
|
124
|
+
driver.scroll(a['clicks'])
|
|
125
|
+
else:raise ValueError('Unsupported replay operation: '+op)
|
|
126
|
+
await driver.drain();completed+=1;await asyncio.sleep(.01)
|
|
127
|
+
finally:driver.close()
|
|
128
|
+
return completed
|
|
129
|
+
|
|
130
|
+
async def run(request):
|
|
131
|
+
metadata,actions=load(request)
|
|
132
|
+
if metadata['backend']=='virtualbox':
|
|
133
|
+
completed=await replay(VBox(request['vm_id']),actions,metadata['resolution'])
|
|
134
|
+
elif metadata['backend']=='kvm':
|
|
135
|
+
import asyncvnc
|
|
136
|
+
uri=request.get('libvirt_uri','qemu:///session')
|
|
137
|
+
if uri not in ('qemu:///session','qemu:///system'):raise ValueError('Only local libvirt connections are supported')
|
|
138
|
+
def virsh(*args):
|
|
139
|
+
p=subprocess.run(['virsh','-c',uri,*args],capture_output=True,text=True,timeout=30)
|
|
140
|
+
if p.returncode:raise RuntimeError(p.stderr[:300])
|
|
141
|
+
return p.stdout
|
|
142
|
+
if virsh('domstate',request['vm_id']).strip()!='running':raise ValueError('Start the target VM before replay')
|
|
143
|
+
xml=ET.fromstring(virsh('dumpxml',request['vm_id']));graphics=xml.find("devices/graphics[@type='vnc']")
|
|
144
|
+
if graphics is None:raise ValueError('The target needs a localhost VNC display')
|
|
145
|
+
listen=graphics.find('listen');host=graphics.get('listen') or (listen.get('address') if listen is not None else None) or '127.0.0.1';port=int(graphics.get('port','-1'))
|
|
146
|
+
if host not in ('127.0.0.1','localhost','::1') or not 1<=port<=65535:raise ValueError('Only active localhost VNC is supported')
|
|
147
|
+
async with asyncvnc.connect(host,port,password=os.environ.get('VAMP_VNC_PASSWORD')) as client:
|
|
148
|
+
completed=await replay(VNC(client),actions,metadata['resolution'])
|
|
149
|
+
else:raise ValueError('This backend has no recorded desktop replay adapter')
|
|
150
|
+
return {'status':'completed_unverified','rpa_id':metadata['id'],'actions_completed':completed,'expected_result':metadata['expected_result'],'message':'Replay completed. Verify the expected application result; no screenshot or model call was made.'}
|
|
151
|
+
|
|
152
|
+
if __name__=='__main__':
|
|
153
|
+
try:print(json.dumps(asyncio.run(asyncio.wait_for(run(json.load(sys.stdin)),timeout=600)),ensure_ascii=False))
|
|
154
|
+
except Exception as error:print(json.dumps({'status':'error','message':str(error)}));sys.exit(1)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Create a small answer ISO without external ADK/xorriso dependencies."""
|
|
2
|
+
import sys
|
|
3
|
+
import pycdlib
|
|
4
|
+
|
|
5
|
+
if __name__=='__main__':
|
|
6
|
+
iso=pycdlib.PyCdlib()
|
|
7
|
+
iso.new(interchange_level=3,joliet=3,vol_ident='VAMP_SETUP')
|
|
8
|
+
iso.add_file(sys.argv[1],iso_path='/AUTOUNATTEND.XML;1',joliet_path='/Autounattend.xml')
|
|
9
|
+
iso.add_file(sys.argv[3],iso_path='/VAMPPOST.PS1;1',joliet_path='/vamp-postinstall.ps1')
|
|
10
|
+
if len(sys.argv)>4:
|
|
11
|
+
import os
|
|
12
|
+
name=os.path.basename(sys.argv[4])
|
|
13
|
+
# Vendor ISO is nested inside answer media; no additional controller or
|
|
14
|
+
# unsupported guest storage driver is needed to reach it during setup.
|
|
15
|
+
iso.add_file(sys.argv[4],iso_path='/DRIVERS.ISO;1',joliet_path='/'+name)
|
|
16
|
+
iso.write(sys.argv[2]);iso.close()
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
param([string]$Artifact,[string]$Destination,[string]$Name)
|
|
2
|
+
$ErrorActionPreference='Stop'
|
|
3
|
+
Import-Module Hyper-V
|
|
4
|
+
$config=Get-ChildItem -LiteralPath $Artifact -Recurse -Filter *.vmcx | Select-Object -First 1
|
|
5
|
+
if(-not $config){throw 'Packer did not export a Hyper-V configuration'}
|
|
6
|
+
$vm=Import-VM -Path $config.FullName -Copy -GenerateNewId -VirtualMachinePath $Destination -VhdDestinationPath $Destination
|
|
7
|
+
Rename-VM -VM $vm -NewName $Name
|
|
8
|
+
Start-VM -VM $vm
|
|
9
|
+
if([string](Get-VM -Id $vm.Id).State -ne 'Running'){throw 'Deployed VM did not reach Running state'}
|
|
10
|
+
$vm.Id.ToString()
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Stage vendor driver media before Packer starts a new Windows guest."""
|
|
2
|
+
import hashlib
|
|
3
|
+
import os
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
import re
|
|
6
|
+
import shutil
|
|
7
|
+
import tempfile
|
|
8
|
+
import urllib.request
|
|
9
|
+
from toolchain import home
|
|
10
|
+
|
|
11
|
+
VIRTIO_URL='https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.302-1/virtio-win.iso'
|
|
12
|
+
|
|
13
|
+
def download(url,target,checksum=None):
|
|
14
|
+
target=Path(target);target.parent.mkdir(parents=True,exist_ok=True)
|
|
15
|
+
if target.is_file():
|
|
16
|
+
with target.open('rb') as file:digest=hashlib.file_digest(file,'sha256').hexdigest()
|
|
17
|
+
if checksum is None or digest==checksum:return target
|
|
18
|
+
raise ValueError('Cached driver ISO checksum does not match: '+str(target))
|
|
19
|
+
pending=None
|
|
20
|
+
try:
|
|
21
|
+
with tempfile.NamedTemporaryFile(dir=target.parent,delete=False) as file:
|
|
22
|
+
pending=Path(file.name)
|
|
23
|
+
with urllib.request.urlopen(url,timeout=120) as response:shutil.copyfileobj(response,file)
|
|
24
|
+
except Exception:
|
|
25
|
+
if pending:pending.unlink(missing_ok=True)
|
|
26
|
+
raise
|
|
27
|
+
with pending.open('rb') as file:digest=hashlib.file_digest(file,'sha256').hexdigest()
|
|
28
|
+
if checksum and digest!=checksum:pending.unlink();raise ValueError('Driver ISO SHA-256 mismatch')
|
|
29
|
+
pending.replace(target);target.with_suffix('.sha256').write_text(digest+' '+target.name+'\n')
|
|
30
|
+
return target
|
|
31
|
+
|
|
32
|
+
def prepare(request,vbox_executable=None,execute=None):
|
|
33
|
+
if request['guest_os']!='windows11':return None
|
|
34
|
+
backend=request['backend']
|
|
35
|
+
if backend=='virtualbox':
|
|
36
|
+
candidates=[Path(vbox_executable).parent/'VBoxGuestAdditions.iso',Path('/usr/share/virtualbox/VBoxGuestAdditions.iso'),Path('/Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso')]
|
|
37
|
+
iso=next((file for file in candidates if file.is_file()),None)
|
|
38
|
+
if iso is None:
|
|
39
|
+
version=re.search(r'\d+\.\d+\.\d+',execute(vbox_executable,['--version']))[0]
|
|
40
|
+
filename='VBoxGuestAdditions_'+version+'.iso';base='https://download.virtualbox.org/virtualbox/'+version+'/'
|
|
41
|
+
sums=urllib.request.urlopen(base+'SHA256SUMS',timeout=30).read().decode()
|
|
42
|
+
digest=next(line.split()[0] for line in sums.splitlines() if line.split()[-1].lstrip('*')==filename)
|
|
43
|
+
iso=download(base+filename,home()/'tools/drivers/virtualbox'/version/'VBoxGuestAdditions.iso',digest)
|
|
44
|
+
elif backend=='kvm':
|
|
45
|
+
# Fixed upstream release over HTTPS. The guest also requires a valid
|
|
46
|
+
# Authenticode signature before running the driver/agent MSI packages.
|
|
47
|
+
iso=download(VIRTIO_URL,home()/'tools/drivers/virtio-win/0.1.302/virtio-win.iso',os.environ.get('VAMP_VIRTIO_ISO_SHA256'))
|
|
48
|
+
else:return None
|
|
49
|
+
request['driver_iso']=str(iso);return iso
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
param([string]$SwitchName='Default Switch')
|
|
2
|
+
$ErrorActionPreference='Stop'
|
|
3
|
+
Import-Module Hyper-V
|
|
4
|
+
Get-VMHost | Out-Null
|
|
5
|
+
$switch=Get-VMSwitch -Name $SwitchName
|
|
6
|
+
$address=Get-NetIPAddress -AddressFamily IPv4 | Where-Object {
|
|
7
|
+
$_.InterfaceAlias -eq ('vEthernet ('+$switch.Name+')') -and $_.IPAddress -notlike '169.254.*'
|
|
8
|
+
} | Select-Object -First 1
|
|
9
|
+
if(-not $address){throw 'The selected Hyper-V switch has no guest-reachable host IPv4 address'}
|
|
10
|
+
@{switch=$switch.Name;host_ipv4=$address.IPAddress;switch_type=[string]$switch.SwitchType} | ConvertTo-Json -Compress
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""Inventory and deletion of VAMP-owned golden artifacts, separate from live disks."""
|
|
2
|
+
import json
|
|
3
|
+
import os
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
import re
|
|
6
|
+
import shutil
|
|
7
|
+
import time
|
|
8
|
+
from toolchain import home
|
|
9
|
+
|
|
10
|
+
def directory(image_id):
|
|
11
|
+
if not re.fullmatch(r'[a-f0-9]{32}',image_id):raise ValueError('Invalid golden image ID')
|
|
12
|
+
root=home()/'golden';folder=root/image_id
|
|
13
|
+
if root.is_symlink() or folder.is_symlink() or (hasattr(folder,'is_junction') and folder.is_junction()) or folder.resolve()!=root.resolve()/image_id:raise ValueError('Unsafe golden image path')
|
|
14
|
+
return folder
|
|
15
|
+
|
|
16
|
+
def size(folder):
|
|
17
|
+
total=0
|
|
18
|
+
for root,dirs,files in os.walk(folder,followlinks=False):
|
|
19
|
+
dirs[:]=[d for d in dirs if not (Path(root)/d).is_symlink()]
|
|
20
|
+
for name in files:
|
|
21
|
+
path=Path(root)/name
|
|
22
|
+
if not path.is_symlink():total+=path.stat().st_size
|
|
23
|
+
return total
|
|
24
|
+
|
|
25
|
+
def process_alive(pid):
|
|
26
|
+
if os.name=='nt':
|
|
27
|
+
import ctypes
|
|
28
|
+
kernel=ctypes.windll.kernel32
|
|
29
|
+
kernel.OpenProcess.restype=ctypes.c_void_p
|
|
30
|
+
handle=kernel.OpenProcess(0x1000,False,int(pid))
|
|
31
|
+
if not handle:return False
|
|
32
|
+
code=ctypes.c_ulong()
|
|
33
|
+
try:return bool(kernel.GetExitCodeProcess(ctypes.c_void_p(handle),ctypes.byref(code))) and code.value==259
|
|
34
|
+
finally:kernel.CloseHandle(ctypes.c_void_p(handle))
|
|
35
|
+
try:os.kill(int(pid),0);return True
|
|
36
|
+
except ProcessLookupError:return False
|
|
37
|
+
except PermissionError:return True
|
|
38
|
+
|
|
39
|
+
def current_record(folder):
|
|
40
|
+
record=json.loads((folder/'image.json').read_text())
|
|
41
|
+
if record['status'] in ('queued','building','deploying'):
|
|
42
|
+
pid=record.get('worker_pid')
|
|
43
|
+
if (pid and not process_alive(pid)) or (not pid and time.time()-(folder/'image.json').stat().st_mtime>60):
|
|
44
|
+
record={**record,'status':'interrupted','error':'Build worker is no longer running; partial files were preserved.'}
|
|
45
|
+
return record
|
|
46
|
+
|
|
47
|
+
def list_images():
|
|
48
|
+
root=home()/'golden';rows=[]
|
|
49
|
+
if not root.exists():return rows
|
|
50
|
+
for folder in sorted(root.iterdir()):
|
|
51
|
+
try:
|
|
52
|
+
if folder.is_symlink():continue
|
|
53
|
+
folder=directory(folder.name)
|
|
54
|
+
record=current_record(folder)
|
|
55
|
+
artifacts=[]
|
|
56
|
+
for root,dirs,files in ([] if (folder/'artifact').is_symlink() else os.walk(folder/'artifact',followlinks=False)):
|
|
57
|
+
dirs[:]=[d for d in dirs if not (Path(root)/d).is_symlink()]
|
|
58
|
+
for name in files:
|
|
59
|
+
file=Path(root)/name
|
|
60
|
+
if file.suffix.lower() in ('.ova','.ovf','.vdi','.qcow2','.vhd','.vhdx','.vmcx') and not file.is_symlink():
|
|
61
|
+
artifacts.append({'path':str(file),'bytes':file.stat().st_size})
|
|
62
|
+
rows.append({**record,'id':folder.name,'path':str(folder),'bytes':size(folder),'files':artifacts,
|
|
63
|
+
'log_path':str(folder/'packer.log')})
|
|
64
|
+
except (OSError,ValueError):continue
|
|
65
|
+
return rows
|
|
66
|
+
|
|
67
|
+
def delete(image_id,confirmed=False):
|
|
68
|
+
folder=directory(image_id)
|
|
69
|
+
record=current_record(folder)
|
|
70
|
+
if record['status'] in ('queued','building','deploying'):raise ValueError('Wait for the Packer build to finish before deleting its files')
|
|
71
|
+
amount=size(folder)
|
|
72
|
+
if not confirmed:return {'status':'needs_confirmation','id':image_id,'path':str(folder),'bytes':amount,'live_vm_disks_deleted':False}
|
|
73
|
+
# Only this known artifact root is removed; deployed VM disks live under vms/.
|
|
74
|
+
shutil.rmtree(folder)
|
|
75
|
+
return {'status':'deleted','id':image_id,'path':str(folder),'bytes_reclaimed':amount,'live_vm_disks_deleted':False}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""Read Ubuntu's osinfo-db and resolve virt-manager/virt-install hardware XML.
|
|
2
|
+
|
|
3
|
+
No vendor XML is bundled. Native libosinfo profiles and virt-install resolve
|
|
4
|
+
inheritance/device defaults; --dry-run --print-xml never creates a probe VM.
|
|
5
|
+
"""
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
import re
|
|
10
|
+
import subprocess
|
|
11
|
+
import xml.etree.ElementTree as ET
|
|
12
|
+
|
|
13
|
+
def database(roots=None):
|
|
14
|
+
roots=roots or [Path('/usr/share/osinfo'),Path('/var/lib/osinfo'),Path('/etc/osinfo'),Path.home()/'.config/osinfo']
|
|
15
|
+
if os.environ.get('VAMP_OSINFO_DB'):roots=[Path(os.environ['VAMP_OSINFO_DB'])]
|
|
16
|
+
entries={}
|
|
17
|
+
for root in roots:
|
|
18
|
+
for file in sorted((Path(root)/'os').rglob('*.xml')):
|
|
19
|
+
try:
|
|
20
|
+
for node in ET.parse(file).getroot().findall('os'):
|
|
21
|
+
entries[node.attrib['id']]={'node':node,'file':str(file)}
|
|
22
|
+
except (OSError,ET.ParseError,KeyError):continue
|
|
23
|
+
if not entries:raise ValueError('Ubuntu KVM setup needs osinfo-db XML. Install osinfo-db, libosinfo-bin and virtinst, or set VAMP_OSINFO_DB.')
|
|
24
|
+
return entries
|
|
25
|
+
|
|
26
|
+
def profile(guest,iso_path,entries=None,detect=None):
|
|
27
|
+
entries=entries if entries is not None else database()
|
|
28
|
+
detected={}
|
|
29
|
+
if detect:
|
|
30
|
+
detected=detect(iso_path)
|
|
31
|
+
else:
|
|
32
|
+
try:
|
|
33
|
+
result=subprocess.run(['osinfo-detect','--type=media','--format=plain',str(iso_path)],env={**os.environ,'LC_ALL':'C'},capture_output=True,text=True,timeout=30,check=True)
|
|
34
|
+
match=re.search(r"OS '(.+) \(([^()]+)\)'",result.stdout)
|
|
35
|
+
if match:
|
|
36
|
+
if match[2]!='x86_64':raise ValueError('The current Packer profile requires x86_64 installation media')
|
|
37
|
+
for key,entry in entries.items():
|
|
38
|
+
names=[entry['node'].findtext('name'),*[variant.findtext('name') for variant in entry['node'].findall('variant')]]
|
|
39
|
+
if match[1] in names:detected={'OSINFO_OS_ID':key};break
|
|
40
|
+
except (FileNotFoundError,subprocess.CalledProcessError):pass
|
|
41
|
+
selected=entries.get(detected.get('OSINFO_OS_ID',''))
|
|
42
|
+
distro='win' if guest=='windows11' else 'debian'
|
|
43
|
+
if selected and selected['node'].findtext('distro')!=distro:raise ValueError('ISO detected by osinfo-db does not match the selected guest OS')
|
|
44
|
+
fallback=False
|
|
45
|
+
if not selected:
|
|
46
|
+
candidates=[entry for entry in entries.values() if entry['node'].findtext('distro')==distro]
|
|
47
|
+
if guest=='windows11':candidates=[entry for entry in candidates if entry['node'].findtext('short-id')=='win11']
|
|
48
|
+
else:
|
|
49
|
+
match=re.search(r'debian[-_ ]?(\d+)',Path(iso_path).name,re.I)
|
|
50
|
+
if match:
|
|
51
|
+
exact=[entry for entry in candidates if entry['node'].findtext('version')==match[1]]
|
|
52
|
+
if exact:candidates=exact
|
|
53
|
+
else:candidates=[entry for entry in candidates if float(entry['node'].findtext('version','0'))<=int(match[1])];fallback=True
|
|
54
|
+
if not candidates:raise ValueError('No compatible '+guest+' profile in Ubuntu osinfo-db; update the host database')
|
|
55
|
+
selected=max(candidates,key=lambda entry:tuple(int(value) for value in re.findall(r'\d+',entry['node'].findtext('version','0'))))
|
|
56
|
+
node=selected['node'];short=node.findtext('short-id')
|
|
57
|
+
if not short or not re.fullmatch(r'[A-Za-z0-9_.-]+',short):raise ValueError('Invalid osinfo short-id')
|
|
58
|
+
minimum={};recommended={};visited=set()
|
|
59
|
+
def resources(current):
|
|
60
|
+
key=current.attrib['id']
|
|
61
|
+
if key in visited:return
|
|
62
|
+
visited.add(key)
|
|
63
|
+
for parent in current.findall('derives-from'):
|
|
64
|
+
if parent.attrib.get('id') in entries:resources(entries[parent.attrib['id']]['node'])
|
|
65
|
+
for arch in ('all','x86_64'):
|
|
66
|
+
for resource in current.findall('resources'):
|
|
67
|
+
if resource.attrib.get('arch')!=arch:continue
|
|
68
|
+
for target,kind in [(minimum,'minimum'),(recommended,'recommended')]:
|
|
69
|
+
for field in ['ram','storage','n-cpus']:
|
|
70
|
+
text=resource.findtext(kind+'/'+field)
|
|
71
|
+
if text and text.isdigit():target[field]=int(text)
|
|
72
|
+
resources(node)
|
|
73
|
+
return {'id':node.attrib['id'],'short_id':short,'name':node.findtext('name',short),'xml_path':selected['file'],
|
|
74
|
+
'profile_fallback':fallback,'minimum':minimum,'recommended':recommended}
|
|
75
|
+
|
|
76
|
+
def resolve(request,folder,execute=None):
|
|
77
|
+
execute=execute or (lambda args:subprocess.check_output(args,text=True,timeout=60))
|
|
78
|
+
selected=profile(request['guest_os'],request['iso_path'])
|
|
79
|
+
minimum=selected['minimum']
|
|
80
|
+
request['memory_mb']=max(request['memory_mb'],(minimum.get('ram',0)+1048575)//1048576)
|
|
81
|
+
request['disk_mb']=max(request['disk_mb'],(minimum.get('storage',0)+1048575)//1048576)
|
|
82
|
+
request['cpus']=max(request['cpus'],minimum.get('n-cpus',1))
|
|
83
|
+
uri=request.get('libvirt_uri','qemu:///session')
|
|
84
|
+
if uri not in ('qemu:///session','qemu:///system'):raise ValueError('Packer KVM creation requires a local qemu:///session or qemu:///system URI')
|
|
85
|
+
args=['virt-install','--connect',uri,'--name','VAMP-Profile-'+folder.name[:12],'--import','--memory',str(request['memory_mb']),'--vcpus',str(request['cpus']),
|
|
86
|
+
'--disk','path='+str(folder/'profile-probe.qcow2')+',format=qcow2,size='+str(request['disk_mb']/1024),
|
|
87
|
+
'--network','user','--graphics','vnc,listen=127.0.0.1','--boot','uefi','--osinfo','name='+selected['short_id'],'--dry-run','--print-xml']
|
|
88
|
+
xml=execute(args);domain=ET.fromstring(xml)
|
|
89
|
+
(folder/'virt-manager-profile.xml').write_text(xml)
|
|
90
|
+
network=domain.find('devices/interface/model');disk=domain.find('devices/disk/target');kind=domain.find('os/type');video=domain.find('devices/video/model')
|
|
91
|
+
loader=domain.find('os/loader');nvram=domain.find('os/nvram')
|
|
92
|
+
# Modern libvirt can choose firmware using feature selectors rather than a
|
|
93
|
+
# loader path. Packer's QEMU builder needs an explicit OVMF image instead.
|
|
94
|
+
code=(loader.text or '').strip() if loader is not None else ''
|
|
95
|
+
variables=nvram.attrib.get('template','') if nvram is not None else ''
|
|
96
|
+
if not code:
|
|
97
|
+
candidates=['/usr/share/OVMF/OVMF_CODE_4M.fd','/usr/share/OVMF/OVMF_CODE.fd']
|
|
98
|
+
code=next((file for file in candidates if Path(file).is_file()),'')
|
|
99
|
+
if not code:raise ValueError('Ubuntu OVMF firmware is missing; install ovmf')
|
|
100
|
+
if not variables:variables=code.replace('CODE','VARS')
|
|
101
|
+
if not Path(variables).is_file():raise ValueError('OVMF variables template is missing: '+variables)
|
|
102
|
+
# Windows installation must use inbox SATA/e1000e drivers unless a separate
|
|
103
|
+
# VirtIO driver payload has explicitly been provisioned.
|
|
104
|
+
selected.update(machine=kind.attrib.get('machine','q35') if kind is not None else 'q35',
|
|
105
|
+
disk_bus='sata' if request['guest_os']=='windows11' else (disk.attrib.get('bus','virtio') if disk is not None else 'virtio'),
|
|
106
|
+
nic='e1000e' if request['guest_os']=='windows11' else (network.attrib.get('type','virtio') if network is not None else 'virtio'),
|
|
107
|
+
video=video.attrib.get('type','virtio') if video is not None else 'virtio',
|
|
108
|
+
firmware_code=code,firmware_vars=variables,libvirt_uri=uri,
|
|
109
|
+
build_network='qemu user-mode NAT (host 10.0.2.2)',
|
|
110
|
+
deploy_network='network=default' if uri.endswith('/system') else 'user')
|
|
111
|
+
request['osinfo']=selected
|
|
112
|
+
(folder/'osinfo-profile.json').write_text(json.dumps(selected,indent=2)+'\n')
|
|
113
|
+
return selected
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "1.16.2",
|
|
3
|
+
"checksums": {
|
|
4
|
+
"windows_amd64": "1b3984ae7f5954cc3ae416b527ae5c5a747ea7bb7977366038609199be4a2710",
|
|
5
|
+
"linux_amd64": "abbfe0cf74985084112659b53e3565f264f70a20914c3b7c4f789f54f61c2414",
|
|
6
|
+
"linux_arm64": "5d7d6de86a49b2d64f3c0eef5766855b580e10a7c45bd6605ba8f1dead7815ea",
|
|
7
|
+
"darwin_amd64": "cca17e6889754c8f6e9d9a2646a9f9cc650a70dfdafc8e7da7e5279977b7d812",
|
|
8
|
+
"darwin_arm64": "3d7e4df8e7e8c21bdf495c4f63ad9189e22540dc8126dee6db791aa8519e8b3a"
|
|
9
|
+
},
|
|
10
|
+
"plugins": {
|
|
11
|
+
"virtualbox": {"source": "github.com/hashicorp/virtualbox", "version": "1.1.6", "builder": "virtualbox-iso"},
|
|
12
|
+
"hyperv": {"source": "github.com/hashicorp/hyperv", "version": "1.1.5", "builder": "hyperv-iso"},
|
|
13
|
+
"kvm": {"source": "github.com/hashicorp/qemu", "version": "1.1.8", "builder": "qemu"}
|
|
14
|
+
}
|
|
15
|
+
}
|