on_Binary__install_using_custom_bash.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python3
  2. """
  3. Install a binary using a custom bash command.
  4. This provider runs arbitrary shell commands to install binaries
  5. that don't fit into standard package managers.
  6. Usage: on_Binary__install_using_custom_bash.py --binary-id=<uuid> --machine-id=<uuid> --name=<name> --custom-cmd=<cmd>
  7. Output: Binary JSONL record to stdout after installation
  8. Environment variables:
  9. MACHINE_ID: Machine UUID (set by orchestrator)
  10. """
  11. import json
  12. import os
  13. import subprocess
  14. import sys
  15. import rich_click as click
  16. from abx_pkg import Binary, EnvProvider
  17. @click.command()
  18. @click.option('--binary-id', required=True, help="Binary UUID")
  19. @click.option('--machine-id', required=True, help="Machine UUID")
  20. @click.option('--name', required=True, help="Binary name to install")
  21. @click.option('--binproviders', default='*', help="Allowed providers (comma-separated)")
  22. @click.option('--custom-cmd', required=True, help="Custom bash command to run")
  23. def main(binary_id: str, machine_id: str, name: str, binproviders: str, custom_cmd: str):
  24. """Install binary using custom bash command."""
  25. if binproviders != '*' and 'custom' not in binproviders.split(','):
  26. click.echo(f"custom provider not allowed for {name}", err=True)
  27. sys.exit(0)
  28. if not custom_cmd:
  29. click.echo("custom provider requires --custom-cmd", err=True)
  30. sys.exit(1)
  31. click.echo(f"Installing {name} via custom command: {custom_cmd}", err=True)
  32. try:
  33. result = subprocess.run(
  34. custom_cmd,
  35. shell=True,
  36. capture_output=True,
  37. text=True,
  38. timeout=600, # 10 minute timeout for custom installs
  39. )
  40. if result.returncode != 0:
  41. click.echo(f"Custom install failed: {result.stderr}", err=True)
  42. sys.exit(1)
  43. except subprocess.TimeoutExpired:
  44. click.echo("Custom install timed out", err=True)
  45. sys.exit(1)
  46. # Use abx-pkg to load the binary and get its info
  47. provider = EnvProvider()
  48. try:
  49. binary = Binary(name=name, binproviders=[provider]).load()
  50. except Exception as e:
  51. click.echo(f"{name} not found after custom install: {e}", err=True)
  52. sys.exit(1)
  53. if not binary.abspath:
  54. click.echo(f"{name} not found after custom install", err=True)
  55. sys.exit(1)
  56. machine_id = os.environ.get('MACHINE_ID', '')
  57. # Output Binary JSONL record to stdout
  58. record = {
  59. 'type': 'Binary',
  60. 'name': name,
  61. 'abspath': str(binary.abspath),
  62. 'version': str(binary.version) if binary.version else '',
  63. 'sha256': binary.sha256 or '',
  64. 'binprovider': 'custom',
  65. 'machine_id': machine_id,
  66. 'binary_id': binary_id,
  67. }
  68. print(json.dumps(record))
  69. # Log human-readable info to stderr
  70. click.echo(f"Installed {name} at {binary.abspath}", err=True)
  71. click.echo(f" version: {binary.version}", err=True)
  72. sys.exit(0)
  73. if __name__ == '__main__':
  74. main()