|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import sys |
| 4 | + |
| 5 | +import click |
| 6 | + |
| 7 | +from ai.backend.client.session import Session |
| 8 | +from ai.backend.client.output.fields import agent_fields |
| 9 | +from ..types import CLIContext |
| 10 | +from . import admin |
| 11 | + |
| 12 | + |
| 13 | +@admin.group() |
| 14 | +def agent(): |
| 15 | + """ |
| 16 | + Agent administration commands. |
| 17 | + """ |
| 18 | + |
| 19 | + |
| 20 | +@agent.command() |
| 21 | +@click.pass_obj |
| 22 | +@click.argument('agent_id') |
| 23 | +def info(ctx: CLIContext, agent_id: str) -> None: |
| 24 | + """ |
| 25 | + Show the information about the given agent. |
| 26 | + """ |
| 27 | + fields = [ |
| 28 | + agent_fields['id'], |
| 29 | + agent_fields['status'], |
| 30 | + agent_fields['region'], |
| 31 | + agent_fields['first_contact'], |
| 32 | + agent_fields['cpu_cur_pct'], |
| 33 | + agent_fields['available_slots'], |
| 34 | + agent_fields['occupied_slots'], |
| 35 | + agent_fields['hardware_metadata'], |
| 36 | + agent_fields['live_stat'], |
| 37 | + ] |
| 38 | + with Session() as session: |
| 39 | + try: |
| 40 | + item = session.Agent.detail(agent_id=agent_id, fields=fields) |
| 41 | + ctx.output.print_item(item, fields) |
| 42 | + except Exception as e: |
| 43 | + ctx.output.print_error(e) |
| 44 | + sys.exit(1) |
| 45 | + |
| 46 | + |
| 47 | +@agent.command() |
| 48 | +@click.pass_obj |
| 49 | +@click.option('-s', '--status', type=str, default='ALIVE', |
| 50 | + help='Filter agents by the given status.') |
| 51 | +@click.option('--scaling-group', '--sgroup', type=str, default=None, |
| 52 | + help='Filter agents by the scaling group.') |
| 53 | +@click.option('--filter', 'filter_', default=None, |
| 54 | + help='Set the query filter expression.') |
| 55 | +@click.option('--order', default=None, |
| 56 | + help='Set the query ordering expression.') |
| 57 | +@click.option('--offset', default=0, |
| 58 | + help='The index of the current page start for pagination.') |
| 59 | +@click.option('--limit', default=None, |
| 60 | + help='The page size for pagination.') |
| 61 | +def list( |
| 62 | + ctx: CLIContext, |
| 63 | + status: str, |
| 64 | + scaling_group: str | None, |
| 65 | + filter_: str | None, |
| 66 | + order: str | None, |
| 67 | + offset: int, |
| 68 | + limit: int | None, |
| 69 | +) -> None: |
| 70 | + """ |
| 71 | + List agents. |
| 72 | + (super-admin privilege required) |
| 73 | + """ |
| 74 | + fields = [ |
| 75 | + agent_fields['id'], |
| 76 | + agent_fields['status'], |
| 77 | + agent_fields['scaling_group'], |
| 78 | + agent_fields['region'], |
| 79 | + agent_fields['first_contact'], |
| 80 | + agent_fields['cpu_cur_pct'], |
| 81 | + agent_fields['mem_cur_bytes'], |
| 82 | + agent_fields['available_slots'], |
| 83 | + agent_fields['occupied_slots'], |
| 84 | + ] |
| 85 | + try: |
| 86 | + with Session() as session: |
| 87 | + fetch_func = lambda pg_offset, pg_size: session.Agent.paginated_list( |
| 88 | + status, |
| 89 | + scaling_group, |
| 90 | + fields=fields, |
| 91 | + page_offset=pg_offset, |
| 92 | + page_size=pg_size, |
| 93 | + filter=filter_, |
| 94 | + order=order, |
| 95 | + ) |
| 96 | + ctx.output.print_paginated_list( |
| 97 | + fetch_func, |
| 98 | + initial_page_offset=offset, |
| 99 | + page_size=limit, |
| 100 | + ) |
| 101 | + except Exception as e: |
| 102 | + ctx.output.print_error(e) |
| 103 | + sys.exit(1) |
| 104 | + |
| 105 | + |
| 106 | +@admin.group() |
| 107 | +def watcher(): |
| 108 | + """ |
| 109 | + Agent watcher commands. |
| 110 | +
|
| 111 | + Available only for Linux-based agents. |
| 112 | + """ |
| 113 | + |
| 114 | + |
| 115 | +@watcher.command() |
| 116 | +@click.pass_obj |
| 117 | +@click.argument('agent', type=str) |
| 118 | +def status(ctx: CLIContext, agent: str) -> None: |
| 119 | + """ |
| 120 | + Get agent and watcher status. |
| 121 | + (superadmin privilege required) |
| 122 | +
|
| 123 | + \b |
| 124 | + AGENT: Agent id. |
| 125 | + """ |
| 126 | + with Session() as session: |
| 127 | + try: |
| 128 | + status = session.AgentWatcher.get_status(agent) |
| 129 | + print(status) |
| 130 | + except Exception as e: |
| 131 | + ctx.output.print_error(e) |
| 132 | + sys.exit(1) |
| 133 | + |
| 134 | + |
| 135 | +@watcher.command() |
| 136 | +@click.pass_obj |
| 137 | +@click.argument('agent', type=str) |
| 138 | +def agent_start(ctx: CLIContext, agent: str) -> None: |
| 139 | + """ |
| 140 | + Start agent service. |
| 141 | + (superadmin privilege required) |
| 142 | +
|
| 143 | + \b |
| 144 | + AGENT: Agent id. |
| 145 | + """ |
| 146 | + with Session() as session: |
| 147 | + try: |
| 148 | + status = session.AgentWatcher.agent_start(agent) |
| 149 | + print(status) |
| 150 | + except Exception as e: |
| 151 | + ctx.output.print_error(e) |
| 152 | + sys.exit(1) |
| 153 | + |
| 154 | + |
| 155 | +@watcher.command() |
| 156 | +@click.pass_obj |
| 157 | +@click.argument('agent', type=str) |
| 158 | +def agent_stop(ctx: CLIContext, agent: str) -> None: |
| 159 | + """ |
| 160 | + Stop agent service. |
| 161 | + (superadmin privilege required) |
| 162 | +
|
| 163 | + \b |
| 164 | + AGENT: Agent id. |
| 165 | + """ |
| 166 | + with Session() as session: |
| 167 | + try: |
| 168 | + status = session.AgentWatcher.agent_stop(agent) |
| 169 | + print(status) |
| 170 | + except Exception as e: |
| 171 | + ctx.output.print_error(e) |
| 172 | + sys.exit(1) |
| 173 | + |
| 174 | + |
| 175 | +@watcher.command() |
| 176 | +@click.pass_obj |
| 177 | +@click.argument('agent', type=str) |
| 178 | +def agent_restart(ctx: CLIContext, agent: str) -> None: |
| 179 | + """ |
| 180 | + Restart agent service. |
| 181 | + (superadmin privilege required) |
| 182 | +
|
| 183 | + \b |
| 184 | + AGENT: Agent id. |
| 185 | + """ |
| 186 | + with Session() as session: |
| 187 | + try: |
| 188 | + status = session.AgentWatcher.agent_restart(agent) |
| 189 | + print(status) |
| 190 | + except Exception as e: |
| 191 | + ctx.output.print_error(e) |
| 192 | + sys.exit(1) |
0 commit comments