vdi-cloud/tools/platform-terminal-simulator.py

54 lines
2.4 KiB
Python

"""Exercise device registration/heartbeat/command receipts without controlling real hardware."""
import argparse
import getpass
import json
import os
import time
from urllib.request import Request, urlopen
from urllib.error import HTTPError
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--server', default='http://127.0.0.1:8088')
parser.add_argument('--sn', required=True)
args = parser.parse_args()
server = args.server.rstrip('/')
token = ''
def request(path, body=None):
headers = {'Content-Type': 'application/json'}
if token:
headers['Authorization'] = 'Bearer ' + token
req = Request(server + path, data=json.dumps(body).encode() if body is not None else None, headers=headers)
try:
with urlopen(req, timeout=15) as response:
return json.load(response)
except HTTPError as error:
detail = json.load(error)
raise RuntimeError(detail.get('message', 'API request failed')) from None
if request('/healthz').get('mode') != 'demo':
raise SystemExit('This simulator only runs against VDI_MODE=demo.')
secret = os.environ.get('VDI_DEVICE_ACTIVATION_SECRET') or getpass.getpass('One-time activation secret: ')
result = request('/api/v1/terminal/register', {'sn': args.sn, 'activationSecret': secret, 'agentVersion': 'simulator-0.1'})
token = result['accessToken']
print('Registered terminal:', result['terminalId'])
print('Simulator sends heartbeat only; all requested device operations return failed.')
print('Press Ctrl+C to stop. No reboot, session change, or log collection is performed.')
while True:
request('/api/v1/terminal/heartbeat', {'agentVersion': 'simulator-0.1', 'fps': None, 'rtt': None})
desktops = request('/api/v1/terminal/desktops')['items']
print('Authorized desktops:', ', '.join(d['name'] for d in desktops) or '(none)')
for command in request('/api/v1/terminal/commands')['items']:
request('/api/v1/terminal/commands/' + command['id'] + '/ack', {'status': 'failed', 'message': 'Device simulator does not execute operations'})
print('Returned failed receipt for:', command['action'], command['id'])
time.sleep(10)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('\nSimulator stopped.')