Metadata-Version: 2.4
Name: mrg-dew
Version: 0.1.3
Summary: Distributed Experiment Workflow: event-driven coordination across SPHERE experiment nodes
Author: Sphere DEW contributors
License-Expression: BSD-3-Clause
Keywords: sphere,mergetb,testbed,experiment,workflow,rabbitmq
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pika
Dynamic: license-file

# DEW — Distributed Experiment Workflow

DEW is a lightweight **coordination layer** for multi-node testbed experiments. A small
per-node agent plus a shared RabbitMQ event bus let you run commands **in order across
machines** — "node A does X, and only once A announces it is done does node B do Y" —
with no central scheduler. It fills the gap plain install/automation tools (Ansible,
per-node config) leave open: **cross-node runtime ordering**.

It runs on the SPHERE testbed. An **XDC** drives a run; the **agent** runs on each
experiment node; one node hosts the **broker**.

**New here?** [`USER_GUIDE.md`](USER_GUIDE.md) is a step-by-step walk-through of every feature;
[`docs/dew-on-sphere.md`](docs/dew-on-sphere.md) is the self-contained reference for SPHERE users;
[`sphere/FILES.md`](sphere/FILES.md) explains what each file does.

`docs/dew-on-sphere.md` is written for the SPHERE documentation site
(<https://gitlab.com/mergetb/testbeds/sphere/documentation>, Docusaurus), where it lives as
`docs/Experimentation/dew.md`; merging it to that repo's `main` also updates the SPHERE chatbot.
Edit it here first, then copy it over in a merge request.

Licensed under the BSD 3-Clause License; see [`LICENSE`](LICENSE).

## Layout

All functional code lives in [`sphere/`](sphere/):

| File | Runs on | Purpose |
|---|---|---|
| `schedulejob.py` | each node | the agent — parses a scenario, runs its tasks, coordinates via events |
| `primitives.py` | node + XDC | resolves `$nid`, `$IP(A)`, `$expeth(A)`, … in bindings |
| `run.py` | XDC | publishes a run; `--follow` watches it live |
| `stop.py` | XDC | stops a run |
| `dewctl` | XDC | front end: `deploy` / `run` / `stop` / `status` / `collect` / `expand` |
| `expand-scenario.py` | XDC | expands role-based scenarios (`num N`, `{role}`) into concrete ones |
| `deploy-dew.sh` | XDC | installs DEW across an experiment in one command (`dewctl deploy`) |
| `install-node.sh`, `install.sh` | node / broker | install helpers, copied to the nodes by `dewctl deploy` |
| `install-xdc.sh` | XDC | sets up a venv for running from a git clone (not needed with pip) |
| `collect-dew.sh` | XDC | pulls result files from many nodes |
| `createtable.sql` | node | the agent's process-table schema |
| `cli.py`, `__init__.py` | XDC | the pip-installed `dewctl` command, and the version |
| `tests/` | anywhere | offline unit tests |

Packaging is in [`pyproject.toml`](pyproject.toml). Small examples are in [`examples/`](examples/);
a fuller set of worked scenarios is at <https://github.com/TuetrungNguyen/dewscenarios>.

## Install

On the XDC, once per user. DEW goes in its own venv (the XDC's Python rejects `pip --user`); home
directories carry over between XDCs, so this survives making a new one.

DEW is packaged for PyPI as `mrg-dew` but **not published there yet**, so for now install it with
pip from a copy of this repository (which needs access to the repository):

```bash
git clone https://gitlab.com/glawler/dew.git ~/dew
python3 -m venv ~/dewenv
~/dewenv/bin/pip install ~/dew
echo 'export PATH="$HOME/dewenv/bin:$PATH"' | tee -a ~/.bashrc ~/.profile > /dev/null
export PATH="$HOME/dewenv/bin:$PATH"
dewctl --version
```

The PATH line goes in both files because the XDC's terminal is a login shell, which reads
`~/.profile` and not `~/.bashrc`; other shells read `~/.bashrc`.

The command it installs is `dewctl`. Once `mrg-dew` is on PyPI, the clone step goes away and the
install line becomes `~/dewenv/bin/pip install mrg-dew` (pin a release with `mrg-dew==<version>`).

**Updating:** reinstall, then redeploy so the nodes get the new agent too:

```bash
git -C ~/dew pull && ~/dewenv/bin/pip install --force-reinstall --no-deps ~/dew
dewctl deploy all --broker server
```

**Build releases on Linux** (e.g. the XDC), never on Windows: a build packs files with their
on-disk line endings, and Windows ones break the node install. PyPI never accepts a version
number twice, so bump `__version__` in `sphere/__init__.py` for every upload.

## Quick start

```bash
# once per materialization (re-run after an update)
dewctl deploy all --broker server          # or: dewctl deploy <nodes> --broker <node>

# per run
dewctl run scenario.json                   # expands if needed, starts, watches live; Ctrl-C stops the run
dewctl stop
dewctl status
dewctl collect /tmp/result.json
```

### Running straight from the clone (development)

For working on DEW itself, without reinstalling after every edit:

```bash
cd ~/dew/sphere
./install-xdc.sh                           # creates ~/dewenv with pika
./dewctl deploy all --broker server
./dewctl run ../examples/scenario-iperf.json
```

## Scenario format

A scenario is JSON with three sections, plus an optional fourth:

```json
{
  "Scenario":   { "t1": "server start_web emit up",
                  "t2": "when up client fetch" },
  "Bindings":   { "start_web": "bash -c \"...\"",
                  "up":        "started(start_web)",
                  "fetch":     "bash -c \"curl http://server\"" },
  "Constraints": {},
  "Labels":     { "setup": ["t1"], "experiment": ["t2"] }
}
```

- **Scenario** — one task per line: `[when <events>] [wait <n>] <node> <action> [emit <event> ...]`.
  Ordering comes only from events: `emit` announces one, `when` waits for one, `wait <n>`
  delays n seconds after being unblocked.
- **Bindings** — the shell command for each action, and the definition of each event.
- **Constraints** — role expansion (e.g. `"c1": "client num 5"` → `client1…client5`).
- **Labels** (optional) — named groups of tasks; `dewctl run <file> --only <label>` runs just
  that group, e.g. to re-run the experiment without repeating setup.

### Events: `emit` + a definition

`emit` is the only event keyword; *when* an event fires is declared in `Bindings` as a
function call on the action:

| Definition in `Bindings` | The event fires when the action… |
|---|---|
| `success(action)` | exits 0 — the default for a bare `emit` with no definition |
| `started(action)` | launches — use for daemons that never exit |
| `finished(action)` | exits, pass or fail |
| `failed(action)` | exits non-zero, or cannot launch — route a failure to a recovery task |

A teardown (`stop` / Ctrl-C) fires none of success/failed/finished.

### Primitives

Bindings may use `$nid`, `$eid`, `$project`, `$epoch`, `$expIP`, `$expeth`, `$IP(A)`,
`$expIP(A)`, `$expeth(A)` — resolved on the node when the scenario is parsed (`$epoch` at
launch). A bare `$` not matching a primitive is left for the shell.

## Tests

```bash
python3 -m unittest discover sphere/tests
```

## CI and releasing

[`.gitlab-ci.yml`](.gitlab-ci.yml) follows GitLab's tutorial
[Automatically build and publish packages with CI/CD](https://docs.gitlab.com/user/packages/pypi_repository/auto_publish_tutorial/)
(build → test → publish, pip cache, artifacts, pytest, tokens in CI/CD variables, publish only on
a tag), plus DEW-specific checks. On every push:

- **build**: builds the wheel and sdist, `twine check --strict`, and checks the built files have
  no CRLF line endings;
- **test**: the unit tests with pytest (results appear in the pipeline's Tests tab), `bash -n` on
  every script, a source line-ending check, every example in
  [`docs/dew-on-sphere.md`](docs/dew-on-sphere.md) through DEW's pre-run checks, and the built
  wheel installed into a clean venv with its `dewctl` run.

The helper scripts are in [`ci/`](ci/) and are not part of the package.

**To release:** bump `__version__` in `sphere/__init__.py`, commit and push, then push a matching
tag:

```bash
git tag v0.1.3 && git push origin v0.1.3
```

The tag's pipeline uploads to **TestPyPI automatically** (`publish-testpypi`), then adds a
**manual** `publish-pypi` job: check the TestPyPI release, then start it from the pipeline page.
Both refuse a tag that does not match `__version__`.

**One-time setup** (repository and PyPI owners):

- Settings → CI/CD → Variables: `PYPI_TOKEN` (a pypi.org API token) and `TEST_PYPI_TOKEN` (a
  test.pypi.org API token), both **masked** and **protected**.
- Settings → Repository → Protected tags: protect `v*`, so only maintainers can start a release
  and tag pipelines can read the protected variables.

## Requirements

- **XDC:** Python 3 with `venv`; pip installs `pika` along with DEW.
- **Each node:** Python 3 and `pika`; one node runs a RabbitMQ broker. `dewctl deploy` sets
  all of this up and writes `~/.dewrc` (broker host, node list) that `dewctl` reads.

## Security notes

- **Broker login.** The broker has one account, `dew`/`dew` by default, used by every agent and
  by `dewctl`. It is a shared default, not a secret: the broker runs on one of your experiment
  nodes, not on a public host. To use a different login, set `DEW_RABBITMQ_USER` /
  `DEW_RABBITMQ_PASS` the same way for the install and for the agents.
- **Passwordless sudo.** `dewctl deploy` adds a `NOPASSWD` sudo rule on each node
  (`/etc/sudoers.d/90-dew`), which is how it installs without a prompt per step. It lasts until
  the node is reimaged; remove that file to undo it.
