Skip to content

feat(agent): add fly agent run, the base of the monitoring agent - #31

Merged
nabil1440 merged 3 commits into
developfrom
agent/26-run
Sep 28, 2026
Merged

nabil1440 merged 3 commits into
developfrom
agent/26-run

Conversation

@nabil1440

@nabil1440 nabil1440 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Layer 1 of 7 of the monitoring agent (#31 → #37). It follows the FlyWP monitoring agent contract v0.2.1.

Summary

  • This PR adds the command fly agent run. systemd starts it on each server, as the server user.
  • The agent reads three values: the control plane URL, the token and the server id. It keeps its files in the state folder of systemd.
  • Only one agent can operate at a time. The agent works at a fixed second of each minute, which the server id sets. Thus, the servers do not all send at the same time.
  • The agent operates without Docker, and it does not operate as root.
  • In this layer, the agent sends no data. The next layers add the data.

Change

  • cmd/agent.go: fly agent run. It stops on SIGTERM with exit 0.
  • internal/agent: the configuration (FLY_AGENT_URL, FLY_AGENT_TOKEN, FLY_AGENT_SERVER_ID, STATE_DIRECTORY), the lock, the schedule and the client for the control plane.
  • internal/statefile: JSON files that survive a crash (temporary file, sync, rename, sync of the folder).

Adversarial review

The first version was refuted. These items are fixed in this PR:

  • The client did not refuse redirects. A redirect could drop samples (a POST replayed as a GET) or send the token over http. A 3xx now keeps the data.
  • Retry-After is at most one hour.
  • A wall clock that steps back cannot run the same tick two times.
  • The URL must not hold a user, a password, a query or a fragment, and an error never shows a password. The token must be printable ASCII.
  • The agent removes the temporary files of a crash at start.
  • A flaky Docker test (older than this work) has more time under load.

Tests

  • Unit tests for the configuration, the lock, the schedule (with a clock step) and the client (redirects, Retry-After, status errors).
  • The loop runs under testing/synctest.
  • End-to-end: configuration errors exit 1, a second agent stops, the agent runs without Docker, SIGTERM gives exit 0, and the log has no token.

Refs #26 (#32 completes it: the report interval in state.json)

- Read FLY_AGENT_URL, FLY_AGENT_TOKEN, FLY_AGENT_SERVER_ID and
  STATE_DIRECTORY. An error names each key that is not set or not valid.
- Accept only an https URL. Plain http is accepted only for a loopback
  host, for tests and local development.
- Lock a file in the state directory, so that only one agent operates.
- Work at server_id % 60 seconds past each minute, and report after each
  report interval (1 to 10 samples, saved in state.json).
- Stop on SIGTERM. The agent does not need Docker and does not run as root.
- Add a client for the control plane and internal/statefile for crash-safe
  JSON files. The next layers use them.

Refs #26
Fixes from the adversarial review of this layer.

- The control plane client does not follow redirects. A followed redirect
  replays a POST as a GET without the body, so a 200 to it would drop
  samples that were never stored, and a redirect to http would send the
  token in plain text. A 3xx is now a reply that keeps the data.
- A Retry-After value is at most one hour and cannot overflow.
- A wall clock that steps back cannot make the same tick run two times.
- FLY_AGENT_URL must not hold a user, a password, a query or a fragment,
  and an error never shows a password. localhost is accepted in any case.
- FLY_AGENT_TOKEN may hold only printable ASCII characters.
- At start, remove the temporary files that a crash during a write left.

Refs #26
Under go test ./... -race, the fake "docker compose version" could take
more than the 1 s test timeout, so the test reported the Compose plugin
instead of the daemon. Use 3 s.

Refs #26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant