code: def patch_agent(
      enabled: bool,
  ) -> Callable[[argparse.Namespace], None]:
      @authentication.required
      def patch(args: argparse.Namespace) -> None:
          check.check_false(args.all and args.agent_id)
          action = "enable" if enabled else "disable"
  
          if not (args.all or args.agent_id):
              raise errors.CliError(
                  "Please pass agent id or --all option. "
                  f"See `det agent {action} --help` for details."
              )
  
          if args.agent_id:
              agent_ids = [args.agent_id]
          else:
              resp = bindings.get_GetAgents(cli.setup_session(args))
              agent_ids = sorted(
                  local_id(a.id) for a in resp.agents or []
              )
  
          drain_mode = None if enabled else args.drain
  
          for agent_id in agent_ids:
              path = f"api/v1/agents/{agent_id}/{action}"
  
              payload = None
              if not enabled and drain_mode:
                  payload = {
                      "drain": drain_mode,
                  }
  
              api.post(args.master, path, payload)
              status = "Disabled" if not enabled else "Enabled"
              print(f"{status} agent {agent_id}.", file=sys.stderr)
  
          # When draining, check if there're any tasks currently running on
          # these slots, and list them.
          if drain_mode:
              rsp = bindings.get_GetTasks(cli.setup_session(args))
              tasks_data = {
                  k: t
                  for (k, t) in (
                      rsp.allocationIdToSummary.items()
                      if rsp.allocationIdToSummary is not None
                      else {}
                  )
                  if any(
                      a in agent_ids
                      for r in (t.resources or [])
                      for a in (r.agentDevices or {})
                  )
              }
  
              if not (args.json or args.csv):
                  if tasks_data:
                      print(
                          "Tasks still in progress on draining nodes."
                      )
                  else:
                      print("No tasks in progress on draining nodes.")
  
              cli_task.render_tasks(args, tasks_data)
  
      return patch
  
file_path: determined/harness/determined/cli/agent.py
imports: import argparse
  import collections
  import operator
  import os
  import sys
  import typing
  from typing import Any, Callable, Dict, List
  import determined.cli.render
  from determined import cli
  from determined.cli import errors, render
  from determined.cli import task
  from determined.common import api, check
  from determined.common.api import authentication, bindings
  from determined.common.declarative_argparse import Arg, Cmd, Group
label_desc: Use image node_id 4 for calling a global function with example usage: patch_agent(enabled) and returns: patch