vamp-mcp 0.1.9 → 0.2.3
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.
- package/LICENSE +21 -21
- package/README.md +142 -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 +66 -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/setup-user.py +53 -35
- package/runtime/guest_setup/recorder/setup-win.cmd +4 -11
- package/runtime/guest_setup/recorder/setup.ps1 +108 -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 +49 -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
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
"""Shared asynchronous Packer orchestration for VSIX and MCP."""
|
|
2
|
+
import argparse
|
|
3
|
+
import hashlib
|
|
4
|
+
from http.server import BaseHTTPRequestHandler,ThreadingHTTPServer
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
import re
|
|
9
|
+
import secrets
|
|
10
|
+
import shutil
|
|
11
|
+
import subprocess
|
|
12
|
+
import sys
|
|
13
|
+
import threading
|
|
14
|
+
import uuid
|
|
15
|
+
|
|
16
|
+
HERE=Path(__file__).resolve().parent
|
|
17
|
+
ROOT=HERE.parents[1]
|
|
18
|
+
sys.path[:0]=[str(HERE),str(ROOT/'backends/common'),str(ROOT/'scripts')]
|
|
19
|
+
from toolchain import home,init,environment,executable,POLICY
|
|
20
|
+
from guest_install import require_install_host,post_create_notice
|
|
21
|
+
from templates import render
|
|
22
|
+
from images import list_images,delete,directory
|
|
23
|
+
|
|
24
|
+
def write(path,data):
|
|
25
|
+
temp=path.with_suffix('.tmp');temp.write_text(json.dumps(data,indent=2)+'\n');temp.replace(path)
|
|
26
|
+
|
|
27
|
+
def run(exe,args,timeout=120,**kwargs):
|
|
28
|
+
return subprocess.run([str(exe),*map(str,args)],capture_output=True,text=True,timeout=timeout,check=True,**kwargs).stdout
|
|
29
|
+
|
|
30
|
+
def vbox():
|
|
31
|
+
return os.environ.get('VBOXMANAGE_PATH') or (str(Path(os.environ.get('ProgramFiles','C:/Program Files'))/'Oracle/VirtualBox/VBoxManage.exe') if sys.platform=='win32' else 'VBoxManage')
|
|
32
|
+
|
|
33
|
+
def check_request(request):
|
|
34
|
+
request=dict(request);backend=request.get('backend','virtualbox');request['backend']=backend
|
|
35
|
+
require_install_host(backend)
|
|
36
|
+
request.setdefault('guest_os','debian');guest=request['guest_os']
|
|
37
|
+
if guest not in ('debian','windows11'):raise ValueError('Packer profiles currently require a Debian amd64 installer ISO or Windows 11 x64 ISO')
|
|
38
|
+
if request.get('installation_mode','auto') not in ('auto','unattended'):raise ValueError('Packer owns unattended installation; manual mode is no longer a Packer profile')
|
|
39
|
+
if request.get('product_key') and not re.fullmatch(r'[A-Za-z0-9]{5}(?:-[A-Za-z0-9]{5}){4}',request['product_key']):raise ValueError('Invalid Windows product key format')
|
|
40
|
+
name=request.get('name') or 'VAMP-'+uuid.uuid4().hex[:8];request['name']=name
|
|
41
|
+
if not re.fullmatch(r'[A-Za-z][A-Za-z0-9_-]{0,62}',name):raise ValueError('Invalid VM name')
|
|
42
|
+
iso=Path(request['iso_path']).expanduser().resolve(strict=True)
|
|
43
|
+
if not iso.is_file():raise ValueError('Choose a readable local ISO')
|
|
44
|
+
request['iso_path']=str(iso)
|
|
45
|
+
for key,default,low,high in [('memory_mb',4096 if guest=='windows11' else 2048,4096 if guest=='windows11' else 2048,131072),('cpus',2,1,64),('disk_mb',65536,32768,2097152),('image_index',1,1,99)]:
|
|
46
|
+
if request.get(key) is None:request[key]=default
|
|
47
|
+
if type(request[key]) is not int or not low<=request[key]<=high:raise ValueError('Invalid '+key)
|
|
48
|
+
return request
|
|
49
|
+
|
|
50
|
+
def conflicts(backend):
|
|
51
|
+
if not sys.platform.startswith('linux'):return
|
|
52
|
+
if backend=='kvm':
|
|
53
|
+
try:
|
|
54
|
+
if run(vbox(),['list','runningvms']).strip():raise ValueError('A VirtualBox VM is active; shut it down before starting KVM')
|
|
55
|
+
except FileNotFoundError:pass
|
|
56
|
+
if backend=='virtualbox':
|
|
57
|
+
for uri in ['qemu:///session','qemu:///system']:
|
|
58
|
+
try:
|
|
59
|
+
if run('virsh',['-c',uri,'list','--name']).strip():raise ValueError('A KVM VM is active; shut it down before starting VirtualBox')
|
|
60
|
+
except (FileNotFoundError,subprocess.CalledProcessError):pass
|
|
61
|
+
|
|
62
|
+
def names(backend,request):
|
|
63
|
+
if backend=='virtualbox':return [m[1] for m in re.finditer(r'^"(.*)" \{',run(vbox(),['list','vms']),re.M)]
|
|
64
|
+
if backend=='kvm':return run('virsh',['-c',request.get('libvirt_uri','qemu:///session'),'list','--all','--name']).splitlines()
|
|
65
|
+
return json.loads(run('powershell.exe',['-NoProfile','-NonInteractive','-Command','ConvertTo-Json -InputObject @(Get-VM | Select-Object -ExpandProperty Name)']))
|
|
66
|
+
|
|
67
|
+
def create(request):
|
|
68
|
+
request=check_request(request)
|
|
69
|
+
if request.get('preflight_only'):return {'status':'preflight_ready','engine':'packer','vm_created':False,'request':request,'post_reboot_wait_seconds':60}
|
|
70
|
+
if request['guest_os']=='windows11' and not request.get('windows_confirmed'):
|
|
71
|
+
return {'status':'needs_windows_confirmation','vm_created':False,'action_required':'Windows requires explicit confirmation and your own valid license.'}
|
|
72
|
+
conflicts(request['backend'])
|
|
73
|
+
if request['name'] in names(request['backend'],request):raise ValueError('VM name already exists; refusing to replace it')
|
|
74
|
+
live=home()/'vms'/request['backend']/request['name']
|
|
75
|
+
if live.exists():raise ValueError('VM storage path already exists; refusing to replace it')
|
|
76
|
+
image_id=uuid.uuid4().hex;folder=directory(image_id)
|
|
77
|
+
locks=home()/'locks';locks.mkdir(parents=True,exist_ok=True)
|
|
78
|
+
lock=locks/(request['backend']+'-'+request['name']+'.lock')
|
|
79
|
+
try:
|
|
80
|
+
with lock.open('x') as file:file.write(image_id)
|
|
81
|
+
except FileExistsError:raise ValueError('A build already reserves this VM name. Finish it or delete its failed golden image first.')
|
|
82
|
+
folder.mkdir(parents=True)
|
|
83
|
+
record={'id':image_id,'name':request['name'],'backend':request['backend'],'guest_os':request['guest_os'],'status':'queued','engine':'HashiCorp Packer','installation_verified':False,'post_reboot_wait_seconds':60}
|
|
84
|
+
write(folder/'image.json',record);write(folder/'request.json',request)
|
|
85
|
+
flags={'creationflags':subprocess.CREATE_NO_WINDOW | subprocess.DETACHED_PROCESS} if sys.platform=='win32' else {'start_new_session':True}
|
|
86
|
+
try:
|
|
87
|
+
with (folder/'worker.log').open('ab') as output:
|
|
88
|
+
child=subprocess.Popen([sys.executable,str(HERE/'runner.py'),'worker','--id',image_id],stdin=subprocess.DEVNULL,stdout=output,stderr=output,**flags)
|
|
89
|
+
except Exception as exc:
|
|
90
|
+
record.update(status='failed',error=str(exc));write(folder/'image.json',record);lock.unlink(missing_ok=True);raise
|
|
91
|
+
notice=post_create_notice(request['guest_os'],'http://10.0.2.2:8421')
|
|
92
|
+
notice['title']='Packer build queued: OS installation and post-install setup'
|
|
93
|
+
notice['message']='HashiCorp Packer downloads its CLI and official plugin, installs the guest, waits 60 seconds after the first reboot and completes post-install setup. Windows also installs the hypervisor guest drivers and reboots before completion. Debian uses automatic desktop login. Check Golden images for status, paths and logs. The live VM is deployed only after the build succeeds.'
|
|
94
|
+
return {'status':'installation_started','engine':'packer','installation_mode':'unattended','installation_verified':False,'image_id':image_id,'worker_pid':child.pid,
|
|
95
|
+
'vm':request['name'],'golden_image_path':str(folder),'log_path':str(folder/'packer.log'),
|
|
96
|
+
'post_reboot_wait_seconds':60,'credentials':{'username':'vboxuser','password':'password123!','public_sandbox_credentials':True},
|
|
97
|
+
'post_create_notice':notice,
|
|
98
|
+
'next_action':'Read golden image status. Packer builds and verifies guest setup, then exports a golden image and deploys a separate live VM disk.'}
|
|
99
|
+
|
|
100
|
+
def deploy(request,folder):
|
|
101
|
+
backend=request['backend'];name=request['name'];live=home()/'vms'/backend/name
|
|
102
|
+
live.mkdir(parents=True,exist_ok=False);artifact=folder/'artifact'
|
|
103
|
+
if backend=='virtualbox':
|
|
104
|
+
ova=next(artifact.glob('*.ova'))
|
|
105
|
+
run(vbox(),['import',ova,'--vsys','0','--vmname',name,'--basefolder',live],timeout=1800)
|
|
106
|
+
info=run(vbox(),['showvminfo',name,'--machinereadable'])
|
|
107
|
+
vm_id=re.search(r'^UUID="([^"]+)"',info,re.M)[1]
|
|
108
|
+
cfg=Path(re.search(r'^CfgFile="([^"]+)"',info,re.M)[1]);journal={'status':'completed','installation_verified':True,'installation_mode':'packer','golden_image_id':folder.name}
|
|
109
|
+
write(cfg.parent/'vamp-install.json',journal)
|
|
110
|
+
run(vbox(),['startvm',name,'--type','headless'])
|
|
111
|
+
state=run(vbox(),['showvminfo',name,'--machinereadable'])
|
|
112
|
+
if not re.search(r'^VMState="running"',state,re.M):raise RuntimeError('Deployed VM did not reach Running state')
|
|
113
|
+
elif backend=='kvm':
|
|
114
|
+
uri=request.get('libvirt_uri','qemu:///session')
|
|
115
|
+
profile=request['osinfo'];volume=None
|
|
116
|
+
if uri.endswith('/system'):
|
|
117
|
+
volume='vamp-'+name+'.qcow2'
|
|
118
|
+
run('virsh',['-c',uri,'pool-info','default'])
|
|
119
|
+
run('virsh',['-c',uri,'vol-create-as','default',volume,str(request['disk_mb'])+'MiB','--format','qcow2'])
|
|
120
|
+
disk=Path(run('virsh',['-c',uri,'vol-path','--pool','default',volume]).strip())
|
|
121
|
+
write(live/'deployment.json',{'volume':volume,'pool':'default','disk':str(disk),'status':'uploading'})
|
|
122
|
+
run('virsh',['-c',uri,'vol-upload','--pool','default',volume,artifact/'golden.qcow2','--sparse'],timeout=1800)
|
|
123
|
+
disk_spec='vol=default/'+volume+',format=qcow2,bus='+profile['disk_bus']
|
|
124
|
+
network='network=default,model='+profile['nic']
|
|
125
|
+
else:
|
|
126
|
+
disk=live/'disk.qcow2'
|
|
127
|
+
run('qemu-img',['convert','-O','qcow2',artifact/'golden.qcow2',disk],timeout=1800)
|
|
128
|
+
disk_spec=f'path={disk},format=qcow2,bus='+profile['disk_bus'];network='user,model='+profile['nic']
|
|
129
|
+
args=['--connect',uri,'--name',name,'--memory',request['memory_mb'],'--vcpus',request['cpus'],'--import','--disk',disk_spec,
|
|
130
|
+
'--boot','uefi','--graphics','vnc,listen=127.0.0.1','--video',profile['video'],'--input','tablet,bus=usb','--network',network,'--osinfo','name='+profile['short_id'],'--noautoconsole']
|
|
131
|
+
if request['guest_os']=='windows11':args+=['--tpm','backend.type=emulator,backend.version=2.0,model=tpm-crb']
|
|
132
|
+
run('virt-install',args);vm_id=run('virsh',['-c',uri,'domuuid',name]).strip()
|
|
133
|
+
if run('virsh',['-c',uri,'domstate',vm_id]).strip()!='running':raise RuntimeError('Deployed VM did not reach Running state')
|
|
134
|
+
else:
|
|
135
|
+
vm_id=run('powershell.exe',['-NoProfile','-NonInteractive','-File',HERE/'deploy-hyperv.ps1','-Artifact',artifact,'-Destination',live,'-Name',name],timeout=1800).strip()
|
|
136
|
+
ownership={'id':vm_id,'name':name,'folder':str(live),'guest_os':request['guest_os'],'golden_image_id':folder.name}
|
|
137
|
+
if backend=='kvm':ownership.update(disk=str(disk),volume=volume,pool='default' if volume else None)
|
|
138
|
+
write(live/'vamp-ui.json',ownership)
|
|
139
|
+
return {'vm_id':vm_id,'vm_path':str(live),'vm_disk_path':str(disk) if backend=='kvm' else str(live),'vm':name,'startup_verified':True}
|
|
140
|
+
|
|
141
|
+
def worker(image_id):
|
|
142
|
+
folder=directory(image_id);record=json.loads((folder/'image.json').read_text());request=json.loads((folder/'request.json').read_text());server=None
|
|
143
|
+
def status(value,**extra):
|
|
144
|
+
record.update(status=value,**extra);write(folder/'image.json',record)
|
|
145
|
+
try:
|
|
146
|
+
status('building',worker_pid=os.getpid())
|
|
147
|
+
conflicts(request['backend'])
|
|
148
|
+
token=secrets.token_hex(32);completed=threading.Event()
|
|
149
|
+
class Handler(BaseHTTPRequestHandler):
|
|
150
|
+
def do_POST(self):
|
|
151
|
+
if self.path!='/complete/'+token:self.send_error(404);return
|
|
152
|
+
record.update(guest_setup_completed=True,stage='golden_export');write(folder/'image.json',record);completed.set();self.send_response(200);self.end_headers();self.wfile.write(b'OK')
|
|
153
|
+
def log_message(self,*args):pass
|
|
154
|
+
server=ThreadingHTTPServer(('0.0.0.0',0),Handler)
|
|
155
|
+
threading.Thread(target=server.serve_forever,daemon=True).start()
|
|
156
|
+
host='10.0.2.2'
|
|
157
|
+
if request['backend']=='hyperv':
|
|
158
|
+
network=json.loads(run('powershell.exe',['-NoProfile','-NonInteractive','-File',HERE/'hyperv-network.ps1','-SwitchName',request.get('switch_name','Default Switch')]))
|
|
159
|
+
host=network['host_ipv4'];status('building',network=network)
|
|
160
|
+
if request['backend']=='kvm':
|
|
161
|
+
from osinfo import resolve
|
|
162
|
+
profile=resolve(request,folder)
|
|
163
|
+
if profile['libvirt_uri'].endswith('/system'):
|
|
164
|
+
uri=profile['libvirt_uri']
|
|
165
|
+
run('virsh',['-c',uri,'net-info','default']);run('virsh',['-c',uri,'pool-info','default'])
|
|
166
|
+
if 'default' not in run('virsh',['-c',uri,'net-list','--name']).splitlines():run('virsh',['-c',uri,'net-start','default'])
|
|
167
|
+
if 'default' not in run('virsh',['-c',uri,'pool-list','--name']).splitlines():run('virsh',['-c',uri,'pool-start','default'])
|
|
168
|
+
status('building',osinfo_profile=profile,resource_profile={key:request[key] for key in ['memory_mb','cpus','disk_mb']})
|
|
169
|
+
from script_server import start
|
|
170
|
+
started=start(port=8421,ttl=10800)
|
|
171
|
+
if started.get('status')!='running':raise RuntimeError('Guest setup script server failed to start')
|
|
172
|
+
callback=f'http://{host}:{server.server_port}/complete/{token}'
|
|
173
|
+
with Path(request['iso_path']).open('rb') as file:iso_hash=hashlib.file_digest(file,'sha256').hexdigest()
|
|
174
|
+
from drivers import prepare as prepare_drivers
|
|
175
|
+
driver_iso=prepare_drivers(request,vbox(),run)
|
|
176
|
+
if driver_iso:status('building',driver_media=str(driver_iso))
|
|
177
|
+
template=render(request,folder,iso_hash,f'http://{host}:8421',callback)
|
|
178
|
+
with (folder/'packer.log').open('w',encoding='utf8') as log:
|
|
179
|
+
# Init a separate directory so duplicate required_plugins blocks never coexist.
|
|
180
|
+
init(request['backend'],home()/'tools/packer/init'/request['backend'],log)
|
|
181
|
+
for args in [['validate',str(template)],['build','-color=false','-on-error=abort',str(template)]]:
|
|
182
|
+
subprocess.run([str(executable()),*args],env=environment(),stdout=log,stderr=subprocess.STDOUT,timeout=10800,check=True)
|
|
183
|
+
if not completed.is_set():raise RuntimeError('Packer exited without the authenticated guest post-install completion signal')
|
|
184
|
+
status('deploying',installation_verified=True,artifact_path=str(folder/'artifact'))
|
|
185
|
+
result=deploy(request,folder)
|
|
186
|
+
status('completed',**result)
|
|
187
|
+
except Exception as exc:
|
|
188
|
+
status('failed',error=str(exc),partial_files_preserved=True)
|
|
189
|
+
finally:
|
|
190
|
+
if server:server.shutdown();server.server_close()
|
|
191
|
+
lock=home()/'locks'/(request['backend']+'-'+request['name']+'.lock')
|
|
192
|
+
if lock.exists() and lock.read_text()==image_id:lock.unlink()
|
|
193
|
+
|
|
194
|
+
def delete_image(image_id,confirmed=False):
|
|
195
|
+
folder=directory(image_id);record=json.loads((folder/'image.json').read_text());backend=record.get('backend')
|
|
196
|
+
build_name='VAMP-build-'+image_id[:12]
|
|
197
|
+
if backend in ('virtualbox','hyperv') and build_name in names(backend,{}):
|
|
198
|
+
raise ValueError('A partial build VM is still registered: '+build_name+'. Shut it down and remove it in VM Manager before deleting the golden files.')
|
|
199
|
+
if backend=='kvm' and sys.platform.startswith('linux'):
|
|
200
|
+
for process in Path('/proc').iterdir():
|
|
201
|
+
if not process.name.isdigit():continue
|
|
202
|
+
try:
|
|
203
|
+
command=(process/'cmdline').read_bytes()
|
|
204
|
+
if b'qemu-system' in command and str(folder).encode() in command:raise ValueError('A QEMU build still uses this image; stop that build before deleting it')
|
|
205
|
+
except (PermissionError,FileNotFoundError,ProcessLookupError):pass
|
|
206
|
+
result=delete(image_id,confirmed)
|
|
207
|
+
if result['status']=='deleted' and backend and record.get('name'):
|
|
208
|
+
lock=home()/'locks'/(backend+'-'+record['name']+'.lock')
|
|
209
|
+
if lock.exists() and lock.read_text()==image_id:lock.unlink()
|
|
210
|
+
return result
|
|
211
|
+
|
|
212
|
+
def main():
|
|
213
|
+
parser=argparse.ArgumentParser();parser.add_argument('action',choices=['prepare','check','create','worker','images','delete-image']);parser.add_argument('--backend',default='virtualbox');parser.add_argument('--id');parser.add_argument('--confirmed',action='store_true');args=parser.parse_args()
|
|
214
|
+
if args.action=='worker':worker(args.id);return
|
|
215
|
+
if args.action=='prepare':result=init(args.backend,home()/'tools/packer/init'/args.backend)
|
|
216
|
+
elif args.action=='check':result={'status':'ready' if executable().is_file() else 'missing','executable':str(executable()),'engine':'packer'}
|
|
217
|
+
elif args.action=='create':result=create(json.load(sys.stdin))
|
|
218
|
+
elif args.action=='images':result={'images':list_images(),'storage':str(home())}
|
|
219
|
+
else:result=delete_image(args.id,args.confirmed)
|
|
220
|
+
print(json.dumps(result))
|
|
221
|
+
|
|
222
|
+
if __name__=='__main__':
|
|
223
|
+
try:main()
|
|
224
|
+
except Exception as exc:print(json.dumps({'status':'failed','error':str(exc)}));sys.exit(1)
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"""Packer owns ISO boot, hardware creation, installation wait and golden export."""
|
|
2
|
+
import json
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from xml.sax.saxutils import escape
|
|
5
|
+
from toolchain import POLICY,answer_iso
|
|
6
|
+
|
|
7
|
+
ROOT=Path(__file__).resolve().parents[2]
|
|
8
|
+
|
|
9
|
+
def windows_postinstall(base,callback,backend='virtualbox'):
|
|
10
|
+
script=r'''$ErrorActionPreference='Stop'
|
|
11
|
+
$principal=New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
|
|
12
|
+
if(-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)){
|
|
13
|
+
$child=Start-Process powershell.exe -Verb RunAs -ArgumentList ('-NoProfile -ExecutionPolicy Unrestricted -File "'+$PSCommandPath+'"') -Wait -PassThru
|
|
14
|
+
exit $child.ExitCode
|
|
15
|
+
}
|
|
16
|
+
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted -Force
|
|
17
|
+
$state=Join-Path $env:ProgramData 'VAMP-Packer'
|
|
18
|
+
New-Item -ItemType Directory -Force -Path $state | Out-Null
|
|
19
|
+
Start-Transcript -Path (Join-Path $state 'postinstall.log') -Append
|
|
20
|
+
try {
|
|
21
|
+
Start-Sleep -Seconds 60
|
|
22
|
+
$env:VAMP_SCRIPT_BASE_URL='__BASE__'
|
|
23
|
+
$backend='__BACKEND__'
|
|
24
|
+
$driverMarker=Join-Path $state 'drivers-installed.json'
|
|
25
|
+
if(-not(Test-Path -LiteralPath $driverMarker)){
|
|
26
|
+
$entry=Join-Path $env:TEMP 'vamp-postwin.ps1'
|
|
27
|
+
Invoke-WebRequest -UseBasicParsing '__BASE__/postwin.ps1' -OutFile $entry
|
|
28
|
+
& $entry
|
|
29
|
+
if($backend -in @('virtualbox','kvm')){
|
|
30
|
+
$install=Join-Path $env:ProgramData 'RPA-Recorder-Setup\recorder\install-hypervisor-drivers.ps1'
|
|
31
|
+
$drivers=& $install -Backend $backend
|
|
32
|
+
$drivers | ConvertTo-Json | Set-Content -Encoding UTF8 -LiteralPath $driverMarker
|
|
33
|
+
$resume=Join-Path $state 'resume-postinstall.ps1'
|
|
34
|
+
Copy-Item -LiteralPath $PSCommandPath -Destination $resume -Force
|
|
35
|
+
$action=New-ScheduledTaskAction -Execute 'powershell.exe' -Argument ('-NoProfile -ExecutionPolicy Unrestricted -File "'+$resume+'"')
|
|
36
|
+
$trigger=New-ScheduledTaskTrigger -AtLogOn -User $env:USERNAME
|
|
37
|
+
$principal=New-ScheduledTaskPrincipal -UserId $env:USERNAME -LogonType Interactive -RunLevel Highest
|
|
38
|
+
Register-ScheduledTask -TaskName 'VAMP-Packer-Finish' -Action $action -Trigger $trigger -Principal $principal -Force | Out-Null
|
|
39
|
+
$login='HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
|
|
40
|
+
foreach($item in (@{AutoAdminLogon='1';DefaultUserName='vboxuser';DefaultPassword='password123!';DefaultDomainName=$env:COMPUTERNAME}).GetEnumerator()){
|
|
41
|
+
New-ItemProperty -Path $login -Name $item.Key -Value $item.Value -PropertyType String -Force | Out-Null
|
|
42
|
+
}
|
|
43
|
+
Remove-ItemProperty -Path $login -Name 'AutoLogonCount' -ErrorAction SilentlyContinue
|
|
44
|
+
New-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name LogPixels -Value 96 -PropertyType DWord -Force | Out-Null
|
|
45
|
+
shutdown.exe /r /t 10
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if($backend -eq 'virtualbox' -and -not(Get-Service VBoxService -ErrorAction SilentlyContinue)){throw 'Guest Additions service is missing after reboot'}
|
|
50
|
+
$reset=Join-Path ([Environment]::GetFolderPath('Desktop')) 'Set-Resolution-1920x1080.ps1'
|
|
51
|
+
if(Test-Path -LiteralPath $reset){& $reset;if(-not $?){throw 'Full HD display setup failed'}}
|
|
52
|
+
@{status='completed';drivers_rebooted=(Test-Path -LiteralPath $driverMarker)} | ConvertTo-Json | Set-Content -Encoding UTF8 -LiteralPath (Join-Path $state 'completed.json')
|
|
53
|
+
Invoke-WebRequest -UseBasicParsing -Method POST '__CALLBACK__' | Out-Null
|
|
54
|
+
Unregister-ScheduledTask -TaskName 'VAMP-Packer-Finish' -Confirm:$false -ErrorAction SilentlyContinue
|
|
55
|
+
} finally { Stop-Transcript }
|
|
56
|
+
shutdown.exe /s /t 10
|
|
57
|
+
'''
|
|
58
|
+
return script.replace('__BASE__',base).replace('__CALLBACK__',callback).replace('__BACKEND__',backend)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def windows_answer(base,callback,image_index,language="en-US",product_key=""):
|
|
62
|
+
# FirstLogonCommands.CommandLine is limited to 1024 characters. Keep the
|
|
63
|
+
# actual setup code on the answer ISO and execute it through a short launcher.
|
|
64
|
+
command='cmd.exe /c for %d in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do @if exist %d:\\vamp-postinstall.ps1 powershell.exe -NoProfile -ExecutionPolicy Unrestricted -File %d:\\vamp-postinstall.ps1'
|
|
65
|
+
component=lambda name,content: '<component name="'+name+'" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">'+content+'</component>'
|
|
66
|
+
locale='<InputLocale>en-US</InputLocale><SystemLocale>en-US</SystemLocale><UILanguage>en-US</UILanguage><UserLocale>en-US</UserLocale>'
|
|
67
|
+
partitions=''.join(f'<CreatePartition wcm:action="add"><Order>{i}</Order><Type>{kind}</Type>{"<Extend>true</Extend>" if not size else "<Size>"+str(size)+"</Size>"}</CreatePartition>' for i,kind,size in [(1,'EFI',100),(2,'MSR',16),(3,'Primary',0)])
|
|
68
|
+
modify='<ModifyPartition wcm:action="add"><Order>1</Order><PartitionID>1</PartitionID><Format>FAT32</Format><Label>System</Label></ModifyPartition><ModifyPartition wcm:action="add"><Order>2</Order><PartitionID>3</PartitionID><Format>NTFS</Format><Label>Windows</Label><Letter>C</Letter></ModifyPartition>'
|
|
69
|
+
setup=f'<DiskConfiguration><Disk wcm:action="add"><DiskID>0</DiskID><WillWipeDisk>true</WillWipeDisk><CreatePartitions>{partitions}</CreatePartitions><ModifyPartitions>{modify}</ModifyPartitions></Disk></DiskConfiguration><ImageInstall><OSImage><InstallFrom><MetaData wcm:action="add"><Key>/IMAGE/INDEX</Key><Value>{image_index}</Value></MetaData></InstallFrom><InstallTo><DiskID>0</DiskID><PartitionID>3</PartitionID></InstallTo><WillShowUI>OnError</WillShowUI></OSImage></ImageInstall><UserData><ProductKey><Key>{escape(product_key)}</Key><WillShowUI>OnError</WillShowUI></ProductKey><AcceptEula>true</AcceptEula><FullName>VAMP</FullName><Organization>VAMP</Organization></UserData>'
|
|
70
|
+
shell='<OOBE><HideEULAPage>true</HideEULAPage><HideOnlineAccountScreens>true</HideOnlineAccountScreens><HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE><ProtectYourPC>3</ProtectYourPC></OOBE><UserAccounts><LocalAccounts><LocalAccount wcm:action="add"><Name>vboxuser</Name><Group>Administrators</Group><Password><Value>password123!</Value><PlainText>true</PlainText></Password></LocalAccount></LocalAccounts></UserAccounts><AutoLogon><Username>vboxuser</Username><Enabled>true</Enabled><LogonCount>1</LogonCount><Password><Value>password123!</Value><PlainText>true</PlainText></Password></AutoLogon><FirstLogonCommands><SynchronousCommand wcm:action="add"><Order>1</Order><Description>VAMP Packer guest setup</Description><CommandLine>'+escape(command)+'</CommandLine></SynchronousCommand></FirstLogonCommands>'
|
|
71
|
+
return ('<?xml version="1.0" encoding="utf-8"?><unattend xmlns="urn:schemas-microsoft-com:unattend" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"><settings pass="windowsPE">'+component('Microsoft-Windows-International-Core-WinPE',locale+'<SetupUILanguage><UILanguage>en-US</UILanguage></SetupUILanguage>')+component('Microsoft-Windows-Setup',setup)+'</settings><settings pass="specialize">'+component('Microsoft-Windows-Shell-Setup','<ComputerName>*</ComputerName>')+'</settings><settings pass="oobeSystem">'+component('Microsoft-Windows-International-Core',locale)+component('Microsoft-Windows-Shell-Setup',shell)+'</settings></unattend>').replace('en-US',language)
|
|
72
|
+
|
|
73
|
+
def render(request,folder,iso_hash,base,callback):
|
|
74
|
+
backend=request['backend'];guest=request['guest_os'];plugin=POLICY['plugins'][backend]
|
|
75
|
+
http=folder/'http';http.mkdir()
|
|
76
|
+
source={'iso_url':Path(request['iso_path']).as_uri(),'iso_checksum':'sha256:'+iso_hash,
|
|
77
|
+
'communicator':'none','headless':True,'boot_wait':'10s','shutdown_timeout':'2h',
|
|
78
|
+
'memory':request['memory_mb'],'cpus':request['cpus'],'disk_size':request['disk_mb'],
|
|
79
|
+
'output_directory':str(folder/'artifact')}
|
|
80
|
+
if guest=='debian':
|
|
81
|
+
text=(ROOT/'guest_setup/http/debian-preseed.cfg').read_text()
|
|
82
|
+
text='\n'.join(line for line in text.splitlines() if not line.startswith('d-i preseed/late_command'))
|
|
83
|
+
text+='\nd-i preseed/late_command string wget -qO /tmp/vamp-postdeb.sh '+base+'/postdeb.sh && VAMP_SCRIPT_BASE_URL='+base+' VAMP_PACKER_CALLBACK='+callback+' sh /tmp/vamp-postdeb.sh\n'
|
|
84
|
+
if backend=='kvm':text+='d-i grub-installer/force-efi-extra-removable boolean true\n'
|
|
85
|
+
(http/'preseed.cfg').write_text(text)
|
|
86
|
+
source.update(http_directory=str(http),boot_command=['c<wait>','linux /install.amd/vmlinuz auto=true priority=critical preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg vamp_base='+base+' --- quiet<enter>','initrd /install.amd/initrd.gz<enter>','boot<enter>'])
|
|
87
|
+
else:
|
|
88
|
+
answer=folder/'Autounattend.xml';answer.write_text(windows_answer(base,callback,request.get('image_index',1),'ko-KR' if 'ko-kr' in request['iso_path'].lower() else 'en-US',request.get('product_key','')),encoding='utf8')
|
|
89
|
+
script=folder/'vamp-postinstall.ps1';script.write_text(windows_postinstall(base,callback,backend),encoding='utf8')
|
|
90
|
+
media=folder/'answer.iso';answer_iso(answer,media,script,request.get('driver_iso'))
|
|
91
|
+
# Windows media accepts a key only briefly. Send a short burst on the
|
|
92
|
+
# initial Packer boot; Packer never replays boot_command after reboots.
|
|
93
|
+
source.update(boot_wait='1s',boot_command=['<enter><wait1>']*10)
|
|
94
|
+
if backend=='virtualbox':
|
|
95
|
+
source.update(vm_name='VAMP-build-'+folder.name[:12],guest_os_type='Windows11_64' if guest=='windows11' else 'Debian_64',
|
|
96
|
+
firmware='efi',hard_drive_interface='sata',iso_interface='sata',sata_port_count=3,format='ova',guest_additions_mode='disable',virtualbox_version_file='',disable_shutdown=True,
|
|
97
|
+
vboxmanage=[['modifyvm','{{.Name}}','--mouse','usbtablet','--vram','128','--nat-localhostreachable1','on','--graphicscontroller','vboxsvga' if guest=='windows11' else 'vmsvga']]+([['modifyvm','{{.Name}}','--tpm-type','2.0']] if guest=='windows11' else []))
|
|
98
|
+
if guest=='windows11':source['vboxmanage'].append(['storageattach','{{.Name}}','--storagectl','SATA','--port','2','--device','0','--type','dvddrive','--medium',str(media)])
|
|
99
|
+
elif backend=='hyperv':
|
|
100
|
+
source.update(vm_name='VAMP-build-'+folder.name[:12],generation=2,switch_name=request.get('switch_name','Default Switch'),
|
|
101
|
+
enable_secure_boot=guest=='windows11',enable_tpm=guest=='windows11',disable_shutdown=True,
|
|
102
|
+
temp_path=str(folder/'temporary'),skip_compaction=True)
|
|
103
|
+
if guest=='windows11':source['secondary_iso_images']=[str(media)]
|
|
104
|
+
else:
|
|
105
|
+
profile=request.get('osinfo',{})
|
|
106
|
+
source.update(vm_name='golden.qcow2',format='qcow2',accelerator='kvm',cpu_model='host',disk_interface='sata' if guest=='windows11' else 'virtio',
|
|
107
|
+
net_device='e1000',efi_boot=True)
|
|
108
|
+
if profile:
|
|
109
|
+
source.update(machine_type=profile['machine'],disk_interface=profile['disk_bus'],
|
|
110
|
+
net_device='virtio-net' if profile['nic']=='virtio' else profile['nic'],
|
|
111
|
+
vga={'vga':'std','bochs':'bochs-display'}.get(profile['video'],profile['video']),
|
|
112
|
+
efi_firmware_code=profile['firmware_code'],efi_firmware_vars=profile['firmware_vars'])
|
|
113
|
+
if guest=='windows11':source.update(vtpm=True,tpm_device_type='tpm-crb',qemuargs=[['-drive','file='+str(media)+',media=cdrom,index=3,readonly=on']])
|
|
114
|
+
def value(item):
|
|
115
|
+
return json.dumps(item,ensure_ascii=True).replace('${','$${').replace('%{','%%{')
|
|
116
|
+
header='packer {\n required_version = ">= 1.16.2"\n required_plugins {\n '+backend+' = {\n source = '+value(plugin['source'])+'\n version = '+value('= '+plugin['version'])+'\n }\n }\n}\n'
|
|
117
|
+
hcl=header+'source '+value(plugin['builder'])+' "vamp" {\n'+''.join(' '+key+' = '+value(val)+'\n' for key,val in source.items())+'}\nbuild {\n sources = ["source.'+plugin['builder']+'.vamp"]\n}\n'
|
|
118
|
+
template=folder/'guest.pkr.hcl';template.write_text(hcl,encoding='utf8');return template
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""Managed, checksum-pinned Packer CLI and isolated official plugin installation."""
|
|
2
|
+
import hashlib
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
import platform
|
|
7
|
+
import subprocess
|
|
8
|
+
import sys
|
|
9
|
+
import tempfile
|
|
10
|
+
import urllib.request
|
|
11
|
+
import zipfile
|
|
12
|
+
import venv
|
|
13
|
+
|
|
14
|
+
POLICY=json.loads(Path(__file__).with_name('policy.json').read_text())
|
|
15
|
+
|
|
16
|
+
def home():
|
|
17
|
+
return Path(os.environ.get('VAMP_HOME',str(Path.home()/'.vamp'))).expanduser().resolve()
|
|
18
|
+
|
|
19
|
+
def distribution(system=None,machine=None):
|
|
20
|
+
system=system or sys.platform
|
|
21
|
+
system={'win32':'windows','darwin':'darwin','linux':'linux'}.get(system,system)
|
|
22
|
+
machine=machine or platform.machine()
|
|
23
|
+
arch={'AMD64':'amd64','x86_64':'amd64','x64':'amd64','aarch64':'arm64','ARM64':'arm64'}.get(machine,machine)
|
|
24
|
+
key=system+'_'+arch
|
|
25
|
+
if key not in POLICY['checksums']:raise ValueError('No pinned Packer binary for '+key)
|
|
26
|
+
return key
|
|
27
|
+
|
|
28
|
+
def executable():
|
|
29
|
+
return home()/'tools/packer'/POLICY['version']/distribution()/('packer.exe' if sys.platform=='win32' else 'packer')
|
|
30
|
+
|
|
31
|
+
def environment():
|
|
32
|
+
env={**os.environ,'PACKER_PLUGIN_PATH':str(home()/'tools/packer/plugins'),
|
|
33
|
+
'PACKER_CACHE_DIR':str(home()/'tools/packer/cache'),'CHECKPOINT_DISABLE':'1'}
|
|
34
|
+
if os.environ.get('VBOXMANAGE_PATH'):
|
|
35
|
+
env['PATH']=str(Path(os.environ['VBOXMANAGE_PATH']).parent)+os.pathsep+env.get('PATH','')
|
|
36
|
+
return env
|
|
37
|
+
|
|
38
|
+
def install():
|
|
39
|
+
target=executable()
|
|
40
|
+
if target.is_file():
|
|
41
|
+
result=subprocess.run([str(target),'version'],capture_output=True,text=True,timeout=20,check=True)
|
|
42
|
+
if POLICY['version'] not in result.stdout:raise RuntimeError('Managed Packer version mismatch')
|
|
43
|
+
return str(target)
|
|
44
|
+
key=distribution();version=POLICY['version']
|
|
45
|
+
url=f'https://releases.hashicorp.com/packer/{version}/packer_{version}_{key}.zip'
|
|
46
|
+
target.parent.mkdir(parents=True,exist_ok=True)
|
|
47
|
+
with tempfile.TemporaryDirectory(dir=target.parent) as directory:
|
|
48
|
+
archive=Path(directory)/'packer.zip'
|
|
49
|
+
with urllib.request.urlopen(url,timeout=120) as response,archive.open('wb') as output:
|
|
50
|
+
import shutil
|
|
51
|
+
shutil.copyfileobj(response,output)
|
|
52
|
+
if hashlib.file_digest(archive.open('rb'),'sha256').hexdigest()!=POLICY['checksums'][key]:
|
|
53
|
+
raise RuntimeError('Packer archive SHA-256 mismatch; refusing to execute it')
|
|
54
|
+
with zipfile.ZipFile(archive) as bundle:
|
|
55
|
+
data=bundle.read(target.name)
|
|
56
|
+
candidate=Path(directory)/target.name;candidate.write_bytes(data);candidate.chmod(0o755)
|
|
57
|
+
candidate.replace(target)
|
|
58
|
+
return str(target)
|
|
59
|
+
|
|
60
|
+
def init(backend,directory,log=None):
|
|
61
|
+
plugin=POLICY['plugins'][backend]
|
|
62
|
+
template=Path(directory)/'plugins.pkr.hcl'
|
|
63
|
+
template.parent.mkdir(parents=True,exist_ok=True)
|
|
64
|
+
template.write_text('packer {\n required_plugins {\n '+backend+' = {\n source = '+json.dumps(plugin['source'])+'\n version = '+json.dumps('= '+plugin['version'])+'\n }\n }\n}\n')
|
|
65
|
+
subprocess.run([install(),'init',str(template)],env=environment(),stdout=log or sys.stderr,stderr=subprocess.STDOUT,timeout=300,check=True)
|
|
66
|
+
return {'status':'ready','engine':'HashiCorp Packer','version':POLICY['version'],'executable':str(executable()),
|
|
67
|
+
'plugin':plugin,'storage':str(home()),'notice':'VAMP uses HashiCorp Packer and downloads its CLI and the selected official hypervisor plugin. Hypervisor software is installed separately.'}
|
|
68
|
+
|
|
69
|
+
def answer_iso(answer,target,script,driver_iso=None):
|
|
70
|
+
"""The media helper is isolated from the user's system Python packages."""
|
|
71
|
+
folder=home()/'tools/packer/answer-media'
|
|
72
|
+
python=folder/('Scripts/python.exe' if sys.platform=='win32' else 'bin/python')
|
|
73
|
+
marker=folder/'pycdlib-1.14.0.ready'
|
|
74
|
+
if not marker.is_file():
|
|
75
|
+
if not python.is_file():venv.EnvBuilder(with_pip=True).create(folder)
|
|
76
|
+
subprocess.run([str(python),'-m','pip','install','--disable-pip-version-check','pycdlib==1.14.0'],stdout=sys.stderr,stderr=subprocess.STDOUT,timeout=300,check=True)
|
|
77
|
+
marker.touch()
|
|
78
|
+
args=[str(python),str(Path(__file__).with_name('answer_media.py')),str(answer),str(target),str(script)]
|
|
79
|
+
if driver_iso:args.append(str(driver_iso))
|
|
80
|
+
subprocess.run(args,timeout=300,check=True)
|