collect-onboarding-template.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """Read-only collection of default-template metadata for isolated onboarding tests."""
  2. import base64
  3. import gzip
  4. import importlib.util
  5. import json
  6. import os
  7. from pathlib import Path
  8. import shlex
  9. import sys
  10. root = Path(__file__).resolve().parents[1]
  11. deps = Path(os.environ.get('TEMP', '')) / 'fmode-baseline-python'
  12. if deps.is_dir():
  13. sys.path.insert(0, str(deps))
  14. spec = importlib.util.spec_from_file_location('server_access', root / 'cloud/scripts/r730_root_v3.py')
  15. server = importlib.util.module_from_spec(spec)
  16. spec.loader.exec_module(server)
  17. server.USER = 'fmode'
  18. client, attempts, error = server._connect(attempts=3)
  19. if client is None:
  20. raise SystemExit('SSH unavailable: ' + type(error).__name__)
  21. out = root / '.local-backup/new-account-20261128'
  22. out.mkdir(parents=True, exist_ok=True)
  23. remote = r'''
  24. import base64,gzip,json,pathlib
  25. base=pathlib.Path('/opt/fmode-templates/default')
  26. manifest=[]
  27. for directory in ['plugins','scripts','.fmode-media-patch','hooks','platforms']:
  28. root=base/directory
  29. if root.is_dir():
  30. for p in sorted(root.rglob('*')):
  31. if p.is_file() and '__pycache__' not in p.parts and '.git' not in p.parts:
  32. manifest.append({'path':str(p.relative_to(base)),'bytes':p.stat().st_size})
  33. selected_plugins=['plugin-active-reporter','plugin-deliverable-inject','plugin-fmode','plugin-gateway-finish-guard','plugin-matrix-cloud-url-fallback','plugin-matrix-outbound-s3','plugin-matrix-process-notice','plugin-media-text-merge']
  34. selected_scripts=['fmode-artifact-drain.sh','fmode-artifact-push.py','fmode-deliverable-hook.py','fmode-process-notice.py','fmode-round-marker.py']
  35. files={}
  36. for item in manifest:
  37. rel=item['path']; parts=pathlib.PurePosixPath(rel).parts
  38. wanted=((parts[0]=='plugins' and len(parts)>1 and parts[1] in selected_plugins) or (parts[0]=='scripts' and parts[-1] in selected_scripts) or (parts[0]=='.fmode-media-patch' and parts[-1] in ['entry.sh','patch_spec.py']) or parts[0]=='platforms')
  39. if wanted and '.bak' not in rel and '/tests/' not in rel and not parts[-1].startswith('.'):
  40. files[rel]=(base/rel).read_text()
  41. result={'source':str(base),'manifest':manifest,'files':files,
  42. 'config':(base/'config.yaml').read_text() if (base/'config.yaml').is_file() else None,
  43. 'envKeys':[line.split('=',1)[0] for line in (base/'.env').read_text().splitlines() if '=' in line and not line.startswith('#')] if (base/'.env').is_file() else []}
  44. print(base64.b64encode(gzip.compress(json.dumps(result).encode())).decode())
  45. '''
  46. try:
  47. stdin, stdout, stderr = client.exec_command('sudo -S -p "" python3 -B -c ' + shlex.quote(remote), timeout=60)
  48. stdin.write(server.PW + '\n')
  49. stdin.flush()
  50. data, error = stdout.read(), stderr.read()
  51. code = stdout.channel.recv_exit_status()
  52. if code:
  53. raise SystemExit('Read-only template query failed: ' + str(code))
  54. result = json.loads(gzip.decompress(base64.b64decode(data.strip())))
  55. (out / 'template-metadata.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
  56. print(json.dumps({'source': result['source'], 'files': list(result['files']),
  57. 'envKeys': result['envKeys'], 'configSavedPrivately': result['config'] is not None}, ensure_ascii=False))
  58. finally:
  59. client.close()