on_Binary__14_custom_install.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. timeout=600, # 10 minute timeout for custom installs
  37. )
  38. if result.returncode != 0:
  39. click.echo(f"Custom install failed (exit={result.returncode})", err=True)
  40. sys.exit(1)
  41. except subprocess.TimeoutExpired:
  42. click.echo("Custom install timed out", err=True)
  43. sys.exit(1)
  44. # Use abx-pkg to load the binary and get its info
  45. provider = EnvProvider()
  46. try:
  47. binary = Binary(name=name, binproviders=[provider]).load()
  48. except Exception:
  49. try:
  50. binary = Binary(
  51. name=name,
  52. binproviders=[provider],
  53. overrides={'env': {'version': '0.0.1'}},
  54. ).load()
  55. except Exception as e:
  56. click.echo(f"{name} not found after custom install: {e}", err=True)
  57. sys.exit(1)
  58. if not binary.abspath:
  59. click.echo(f"{name} not found after custom install", err=True)
  60. sys.exit(1)
  61. machine_id = os.environ.get('MACHINE_ID', '')
  62. # Output Binary JSONL record to stdout
  63. record = {
  64. 'type': 'Binary',
  65. 'name': name,
  66. 'abspath': str(binary.abspath),
  67. 'version': str(binary.version) if binary.version else '',
  68. 'sha256': binary.sha256 or '',
  69. 'binprovider': 'custom',
  70. 'machine_id': machine_id,
  71. 'binary_id': binary_id,
  72. }
  73. print(json.dumps(record))
  74. # Log human-readable info to stderr
  75. click.echo(f"Installed {name} at {binary.abspath}", err=True)
  76. click.echo(f" version: {binary.version}", err=True)
  77. sys.exit(0)
  78. if __name__ == '__main__':
  79. main()