# OpsControl for agents

OpsControl runs servers, apps and full stacks on any cloud provider, in cloud accounts the user owns. Everything it does is a command in the `opsctl` CLI: the same commands the web app and the OpsControl Agent Terminal run, with the same permissions, confirmations and history.

This file is everything you need to operate OpsControl for a user.

## Install

macOS and Linux:

```sh
curl -fsSL https://opsctl.com/install.sh | sh
```

Windows (PowerShell):

```powershell
irm https://opsctl.com/install.ps1 | iex
```

Releases and checksums: https://github.com/opsctl-pub/cli/releases

Check it works with `opsctl version`.

## Sign in

Never ask the user for their password. Ask them to create a token for you, limited to what you need:

```sh
opsctl auth token create --name my-agent --preset project_deploy_agent --expires 90
```

Then pass it through the environment or standard input:

```sh
export OPSCTL_TOKEN='<token>'
opsctl auth whoami --output json
```

```sh
printf '%s\n' "$OPSCTL_TOKEN" | opsctl auth whoami --token-stdin --output json
```

Never print, log or commit the token.

## Choose the organization and project

Set them for every command with `OPSCTL_ORG` and `OPSCTL_PROJECT`, or check the saved context with `opsctl context current`. A project-scoped token only works in its own project.

## Find commands

The command list comes from the server and grows over time. Look commands up instead of guessing:

```sh
opsctl commands --output json
opsctl help <command>
opsctl <command> --help
```

Help shows every parameter, which ones are required, and whether the command needs confirmation.

## Run commands

- Add `--output json` to get one structured result on stdout. Errors and prompts go to stderr.
- Add `--wait` to a change to return only when it has finished. Use `--output ndjson` with `--wait` to stream progress.
- Add `--idempotency-key <key>` so a retried change runs only once.
- Changes that need confirmation fail without `--yes`. Pass `--yes` only when the user approved that exact change.

```sh
opsctl deploy --image=ghcr.io/acme/api:1.4 --server_id=web-1 \
  --domain=api.example.com --wait --output json
```

## Follow operations

Every change runs as an operation with an ID, stages and logs:

```sh
opsctl operations list --output json
opsctl operations get <operation-id> --output json
opsctl operations watch <operation-id> --output ndjson
opsctl operations logs <operation-id>
```

## Exit codes

| Code | Meaning | What to do |
|---:|---|---|
| `0` | Success | |
| `2` | Usage or local configuration error | Check `opsctl help <command>` |
| `3` | Not signed in, or the token lacks permission | Tell the user which permission is missing |
| `4` | Rejected, conflicting, or needs confirmation | Read the error; ask the user before retrying with `--yes` |
| `5` | Network, server or version problem | Check the operation before repeating a change |
| `6` | The watched operation failed or was canceled | Read `opsctl operations logs <operation-id>` |
| `130` | Interrupted | |

## Hand a task to the OpsControl agent

The OpsControl agent knows the platform and the user's infrastructure. Give it a task in plain words:

```sh
opsctl exec "Give the team a wiki on wiki.example.com with nightly backups"
opsctl exec resume <session-id> "Add Uptime Kuma to the same server"
```

If the task needs the user's approval, `exec` stops with `interaction_required` (exit 4). Ask the user to approve it in the web app or by running `opsctl` in their terminal.

## Rules

- Look before you change: read the current state with `get` and `list` first.
- Ask the user before anything that deletes, destroys or replaces, even if your token allows it.
- After exit 5, check the operation's status before repeating a change.
- Saved secrets can't be read back. Ask the user for a value only when a command needs it, and never echo it.
- If your token can't do something, say so. Don't look for another way around it.

## More

- Web app: https://app.opsctl.com
- Status: https://opsctl.com/status
- Pricing: https://opsctl.com/pricing
