# openloops.runbook_run

Run an approved run plan: exactly the approved commands, no model, close on the check.

ADR-018. A plan ([`openloops.runbook.Plan`](openloops.runbook.html.md#openloops.runbook.Plan)) is written by an agent and approved by
the owner once, by its hash. This module is the executor, and it holds no judgement:

- it refuses a plan with no approval for its **current** hash, an approval older than
  `DFLT_APPROVAL_HOURS`, or a plan [`openloops.runbook.validate()`](openloops.runbook.html.md#openloops.runbook.validate) rejects (the
  > risk floor is recomputed from the text, so a plan cannot talk its way past it);
- it runs each step’s command exactly as written, in order, with a time limit, its
  output captured to a local log (mode 0600) and never posted anywhere;
- before a step it runs the step’s check, and skips the command when the check already
  passes, so running a plan twice is harmless;
- a step whose dependencies did not succeed is skipped; a step that needs a person at a
  terminal (`Step.interactive`) runs only when the owner started the run from one
  (`tty=True`);
- after an issue’s last step it hands the outcome to [`openloops.closer`](openloops.closer.html.md#module-openloops.closer), which
  closes the issue only on a passing check (ADR-015).

`ol runbook run PLAN --only-checks` needs no approval: it runs no command, only the
checks, and closes what already passes (the closer’s four conditions still apply).

Nothing in the core imports this module. [`run_step()`](#openloops.runbook_run.run_step) is the one place a step’s
command is run; `ol do` (ADR-015 §2) is meant to call it too.

### Functions

| [`runbooks_store`](#openloops.runbook_run.runbooks_store)([root])                            | `~/.local/share/openloops/runbooks/`: `plans/`, `approvals/`, `reports/`.                   |
|----------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------|
| [`save_plan`](#openloops.runbook_run.save_plan)(plan, \*, store)                        | Write *plan* under `plans/<id>.json`; returns its hash.                                     |
| [`load_plan`](#openloops.runbook_run.load_plan)(ref, \*, store)                         | A plan by id, or by the path of a plan file.                                                |
| [`approve`](#openloops.runbook_run.approve)(plan, \*, via[, discussed, now])          | Record the owner's approval of *plan*'s current hash.                                       |
| [`approval_of`](#openloops.runbook_run.approval_of)(plan, \*, store[, now, hours])        | The live approval of *plan*'s current hash, or `None`.                                      |
| [`plans`](#openloops.runbook_run.plans)(\*, store[, now, include_ran])              | The stored plans, newest first, each `{plan, title, hash12, state, counts, page, created}`. |
| [`run_step`](#openloops.runbook_run.run_step)(step, \*[, runner, tty, log_path])       | Run one step's command; `ok` when it exits 0 and its output matches `expect`.               |
| [`run_task`](#openloops.runbook_run.run_task)(step, \*[, runner, run_check, tty, ...]) | One task step end to end, short of reporting: check first, then run.                        |
| [`run_plan`](#openloops.runbook_run.run_plan)(plan, \*, store[, runner, ...])          | Run *plan*'s approved steps, in order; return the run report (also stored).                 |
| [`shell_runner`](#openloops.runbook_run.shell_runner)(command, \*, cwd, timeout, tty, ...) | Run *command* with `bash`; return `(exit status, output)`.                                  |

### openloops.runbook_run.approval_of(plan, , store, now=None, hours=48)

The live approval of *plan*’s current hash, or `None`.

`None` too for an approval of another plan with the same steps, one dated in the
future or without a time zone, and one older than `hours`.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)] | [`None`](https://docs.python.org/3/builtins/constants.html#None)

### openloops.runbook_run.approve(plan, , via, discussed=(), store, now=None)

Record the owner’s approval of *plan*’s current hash.

`via` says how the owner approved (`"terminal"`, `"board:<event id>"`,
`"session: <the owner's words>"`); `discussed` names the `discuss`-level steps
the owner said yes to by name. A plan that does not validate is not approvable.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)]

### openloops.runbook_run.load_plan(ref, , store)

A plan by id, or by the path of a plan file.

* **Return type:**
  [`Plan`](openloops.runbook.html.md#openloops.runbook.Plan)

### openloops.runbook_run.plans(, store, now=None, include_ran=False)

The stored plans, newest first, each `{plan, title, hash12, state, counts, page, created}`.

`state` is `waiting` (no live approval for its current hash: it needs the owner),
`approved` (approved, no run since), or `ran` (a report exists since the
approval). `ran` plans are left out unless `include_ran`.

* **Return type:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)]]

### openloops.runbook_run.run_plan(plan, \*, store, runner=<function shell_runner>, run_check=<function default_run_check>, close=None, log_dir=None, tty=False, only_checks=False, only=None, dry_run=False, now=None)

Run *plan*’s approved steps, in order; return the run report (also stored).

`only` limits the run to some step ids (a pilot): the others are `held`, and an
issue with a held step is not reported (it closes when its last step has run).
`only_checks` runs no command, needs no approval, and closes only on the issue’s
**own** Verify line (a check the planner wrote needs the owner’s approval of the plan).
`dry_run` runs nothing and writes nothing. `close` defaults to
[`openloops.closer.report()`](openloops.closer.html.md#openloops.closer.report) with the owners and login resolved once per run;
`log_dir` to `~/.local/share/openloops/runbooks/logs`.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)]

### openloops.runbook_run.run_step(step, \*, runner=<function shell_runner>, tty=False, log_path=None)

Run one step’s command; `ok` when it exits 0 and its output matches `expect`.

Returns `{status, ok, why, tail}`; `tail` is the end of the output, for the local
report only.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)]

### openloops.runbook_run.run_task(step, \*, runner=<function shell_runner>, run_check=<function default_run_check>, tty=False, log_path=None, checks_only=False)

One task step end to end, short of reporting: check first, then run.

`result` is `already-done` (its check passes: the command did not run),
`not-run` (`checks_only`), `needs-terminal` (an interactive step without
`tty`), `ok` or `failed`. ADR-015 §2’s `ol do` is this plus
[`openloops.closer.report()`](openloops.closer.html.md#openloops.closer.report).

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)]

### openloops.runbook_run.runbooks_store(root=None)

`~/.local/share/openloops/runbooks/`: `plans/`, `approvals/`, `reports/`.

* **Return type:**
  [`MutableMapping`](https://docs.python.org/3/library/collections.abc.html#collections.abc.MutableMapping)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]

### openloops.runbook_run.save_plan(plan, , store)

Write *plan* under `plans/<id>.json`; returns its hash.

* **Return type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)

### openloops.runbook_run.shell_runner(command, , cwd, timeout, tty, log_path)

Run *command* with `bash`; return `(exit status, output)`.

Output goes to *log_path* (created 0600). With `tty` the step gets a real terminal
(through `script`, so a login prompt, a one-time code or an `isatty` check behaves
as at a prompt) and its output is read back from the log. Without `tty` input is
closed, so a step that waits for input fails instead of hanging. `None` is a step
that could not run or ran out of time.

* **Return type:**
  [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[`int`](https://docs.python.org/3/builtins/functions.html#int) | [`None`](https://docs.python.org/3/builtins/constants.html#None), [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]
