@classmethod
  def TerminateFlow(cls, flow_id, reason=None, status=None, token=None,
                    force=False):
    """Terminate a flow.

    Args:
      flow_id: The flow session_id to terminate.
      reason: A reason to log.
      status: Status code used in the generated status message.
      token: The access token to be used for this request.
      force: If True then terminate locked flows hard.

    Raises:
      FlowError: If the flow can not be found.
    """
    if not force:
      flow_obj = aff4.FACTORY.OpenWithLock(flow_id, aff4_type="GRRFlow",
                                           blocking=True, token=token)
    else:
      flow_obj = aff4.FACTORY.Open(flow_id, aff4_type="GRRFlow", mode="rw",
                                   token=token)

    if not flow_obj:
      raise FlowError("Could not terminate flow %s" % flow_id)

    with flow_obj:
      runner = flow_obj.GetRunner()
      if not runner.IsRunning():
        return

      if token is None:
        token = access_control.ACLToken()

      if reason is None:
        reason = "Manual termination by console."

      # Make sure we are only allowed to terminate this flow, if we are
      # allowed to start it. The fact that we could open the flow object
      # means that we have access to the client (if it's not a global
      # flow).
      data_store.DB.security_manager.CheckIfCanStartFlow(
          token, flow_obj.Name(), with_client_id=runner.args.client_id)

      # This calls runner.Terminate to kill the flow
      runner.Error(reason, status=status)

      flow_obj.Log("Terminated by user {0}. Reason: {1}".format(
          token.username, reason))

      # From now on we run with supervisor access
      super_token = token.SetUID()

      # Also terminate its children
      children_to_kill = aff4.FACTORY.MultiOpen(
          flow_obj.ListChildren(), token=super_token, aff4_type="GRRFlow")

      for child_obj in children_to_kill:
        cls.TerminateFlow(child_obj.urn, reason="Parent flow terminated.",
                          token=super_token, force=force)
dataset/ETHPy150Open google/grr/grr/lib/flow.py/GRRFlow.TerminateFlow/original