|
1 | 1 | # Reference
|
2 | 2 |
|
3 |
| -Autogenerated documentation around Coder. |
| 3 | +# Automation |
4 | 4 |
|
5 |
| -- [REST API](./api) |
6 |
| -- [Command Line](./cli) |
7 |
| -- [Agent API](./agent-api) |
| 5 | +All actions possible through the Coder dashboard can also be automated. There are several ways to extend/automate |
| 6 | +Coder: |
| 7 | + |
| 8 | +- [coderd Terraform Provider](https://registry.terraform.io/providers/coder/coderd/latest) |
| 9 | +- [CLI](../reference/cli/README.md) |
| 10 | +- [REST API](../reference/api/README.md) |
| 11 | +- [Coder SDK](https://pkg.go.dev/github.com/coder/coder/v2/codersdk) |
| 12 | +- [Agent API](../reference/agent-api/README.md) |
| 13 | + |
| 14 | +## Quickstart |
| 15 | + |
| 16 | +Generate a token on your Coder deployment by visiting: |
| 17 | + |
| 18 | +```shell |
| 19 | +https://coder.example.com/settings/tokens |
| 20 | +``` |
| 21 | + |
| 22 | +List your workspaces |
| 23 | + |
| 24 | +```shell |
| 25 | +# CLI |
| 26 | +coder ls \ |
| 27 | + --url https://coder.example.com \ |
| 28 | + --token <your-token> \ |
| 29 | + --output json |
| 30 | + |
| 31 | +# REST API (with curl) |
| 32 | +curl https://coder.example.com/api/v2/workspaces?q=owner:me \ |
| 33 | + -H "Coder-Session-Token: <your-token>" |
| 34 | +``` |
| 35 | + |
| 36 | +## Documentation |
| 37 | + |
| 38 | +We publish an [API reference](../reference/api/README.md) in our documentation. |
| 39 | +You can also enable a |
| 40 | +[Swagger endpoint](../reference/cli/server.md#--swagger-enable) on your Coder |
| 41 | +deployment. |
| 42 | + |
| 43 | +## Use cases |
| 44 | + |
| 45 | +We strive to keep the following use cases up to date, but please note that |
| 46 | +changes to API queries and routes can occur. For the most recent queries and |
| 47 | +payloads, we recommend checking the relevant documentation. |
| 48 | + |
| 49 | +### Users & Groups |
| 50 | + |
| 51 | +- [Manage Users via Terraform](https://registry.terraform.io/providers/coder/coderd/latest/docs/resources/user) |
| 52 | +- [Manage Groups via Terraform](https://registry.terraform.io/providers/coder/coderd/latest/docs/resources/group) |
| 53 | + |
| 54 | +### Templates |
| 55 | + |
| 56 | +- [Manage templates via Terraform or CLI](../admin/templates/managing-templates/change-management.md): |
| 57 | + Store all templates in git and update them in CI/CD pipelines. |
| 58 | + |
| 59 | +### Workspace agents |
| 60 | + |
| 61 | +Workspace agents have a special token that can send logs, metrics, and workspace |
| 62 | +activity. |
| 63 | + |
| 64 | +- [Custom workspace logs](../reference/api/agents.md#patch-workspace-agent-logs): |
| 65 | + Expose messages prior to the Coder init script running (e.g. pulling image, VM |
| 66 | + starting, restoring snapshot). |
| 67 | + [coder-logstream-kube](https://github.com/coder/coder-logstream-kube) uses |
| 68 | + this to show Kubernetes events, such as image pulls or ResourceQuota |
| 69 | + restrictions. |
| 70 | + |
| 71 | + ```shell |
| 72 | + curl -X PATCH https://coder.example.com/api/v2/workspaceagents/me/logs \ |
| 73 | + -H "Coder-Session-Token: $CODER_AGENT_TOKEN" \ |
| 74 | + -d "{ |
| 75 | + \"logs\": [ |
| 76 | + { |
| 77 | + \"created_at\": \"$(date -u +'%Y-%m-%dT%H:%M:%SZ')\", |
| 78 | + \"level\": \"info\", |
| 79 | + \"output\": \"Restoring workspace from snapshot: 05%...\" |
| 80 | + } |
| 81 | + ] |
| 82 | + }" |
| 83 | + ``` |
| 84 | + |
| 85 | +- [Manually send workspace activity](../reference/api/agents.md#submit-workspace-agent-stats): |
| 86 | + Keep a workspace "active," even if there is not an open connection (e.g. for a |
| 87 | + long-running machine learning job). |
| 88 | + |
| 89 | + ```shell |
| 90 | + #!/bin/bash |
| 91 | + # Send workspace activity as long as the job is still running |
| 92 | + |
| 93 | + while true |
| 94 | + do |
| 95 | + if pgrep -f "my_training_script.py" > /dev/null |
| 96 | + then |
| 97 | + curl -X POST "https://coder.example.com/api/v2/workspaceagents/me/report-stats" \ |
| 98 | + -H "Coder-Session-Token: $CODER_AGENT_TOKEN" \ |
| 99 | + -d '{ |
| 100 | + "connection_count": 1 |
| 101 | + }' |
| 102 | + |
| 103 | + # Sleep for 30 minutes (1800 seconds) if the job is running |
| 104 | + sleep 1800 |
| 105 | + else |
| 106 | + # Sleep for 1 minute (60 seconds) if the job is not running |
| 107 | + sleep 60 |
| 108 | + fi |
| 109 | + done |
| 110 | + ``` |
0 commit comments