archivebox_machine.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python3
  2. """
  3. archivebox machine <action> [--filters]
  4. Manage Machine records (system-managed, mostly read-only).
  5. Machine records track the host machines where ArchiveBox runs.
  6. They are created automatically by the system and are primarily for debugging.
  7. Actions:
  8. list - List Machines as JSONL (with optional filters)
  9. Examples:
  10. # List all machines
  11. archivebox machine list
  12. # List machines by hostname
  13. archivebox machine list --hostname__icontains=myserver
  14. """
  15. __package__ = 'archivebox.cli'
  16. __command__ = 'archivebox machine'
  17. import sys
  18. from typing import Optional
  19. import rich_click as click
  20. from rich import print as rprint
  21. from archivebox.cli.cli_utils import apply_filters
  22. # =============================================================================
  23. # LIST
  24. # =============================================================================
  25. def list_machines(
  26. hostname__icontains: Optional[str] = None,
  27. os_platform: Optional[str] = None,
  28. limit: Optional[int] = None,
  29. ) -> int:
  30. """
  31. List Machines as JSONL with optional filters.
  32. Exit codes:
  33. 0: Success (even if no results)
  34. """
  35. from archivebox.misc.jsonl import write_record
  36. from archivebox.machine.models import Machine
  37. is_tty = sys.stdout.isatty()
  38. queryset = Machine.objects.all().order_by('-created_at')
  39. # Apply filters
  40. filter_kwargs = {
  41. 'hostname__icontains': hostname__icontains,
  42. 'os_platform': os_platform,
  43. }
  44. queryset = apply_filters(queryset, filter_kwargs, limit=limit)
  45. count = 0
  46. for machine in queryset:
  47. if is_tty:
  48. rprint(f'[cyan]{machine.hostname:30}[/cyan] [dim]{machine.os_platform:10}[/dim] {machine.id}')
  49. else:
  50. write_record(machine.to_json())
  51. count += 1
  52. rprint(f'[dim]Listed {count} machines[/dim]', file=sys.stderr)
  53. return 0
  54. # =============================================================================
  55. # CLI Commands
  56. # =============================================================================
  57. @click.group()
  58. def main():
  59. """Manage Machine records (read-only, system-managed)."""
  60. pass
  61. @main.command('list')
  62. @click.option('--hostname__icontains', help='Filter by hostname contains')
  63. @click.option('--os-platform', help='Filter by OS platform')
  64. @click.option('--limit', '-n', type=int, help='Limit number of results')
  65. def list_cmd(hostname__icontains: Optional[str], os_platform: Optional[str], limit: Optional[int]):
  66. """List Machines as JSONL."""
  67. sys.exit(list_machines(
  68. hostname__icontains=hostname__icontains,
  69. os_platform=os_platform,
  70. limit=limit,
  71. ))
  72. if __name__ == '__main__':
  73. main()