Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1235238

Browse files
stirbymatifali
authored andcommittedAug 12, 2024
partial completion of admin/templates/extending-templates
1 parent 5d07ed9 commit 1235238

File tree

10 files changed

+914
-1
lines changed

10 files changed

+914
-1
lines changed
 

‎docs/admin/templates/concepts.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
# Extending templates
22

3-
There are a variety of Coder-native features to extend the configuration of your development environments.
3+
There are a variety of Coder-native features to extend the configuration of your development environments. Many of the following features are defined in your templates using the [Coder Terraform provider](https://registry.terraform.io/providers/coder/coder/latest/docs). The provider docs will provide code examples for usage; alternatively, you can view our [example templates](https://github.com/coder/coder/tree/main/examples/templates) to get started.
44

55
<!-- TODO: May divide into sub-pages later. -->
66

77
## Workspace agents
88

9+
For users to connect to a workspace, the template must include a [`coder_agent`](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent). The associated agent will facilitate [workspace connections](../../user-guides/workspace-access/README.md) via SSH, port forwarding, and IDEs. The agent may also display [workspace metadata](#agent-metadata) like resource usage.
10+
11+
```hcl
12+
resource "coder_agent" "dev" {
13+
os = "linux"
14+
arch = "amd64"
15+
dir = "/workspace"
16+
display_apps {
17+
vscode = true
18+
}
19+
}
20+
```
21+
22+
Templates must include some computational resource to start the agent. All processes on the workspace are then spawned from the agent. All information in the dashboard's workspace view is pulled from the agent.
23+
24+
![A healthy workspace agent](../../images/templates/healthy-workspace-agent.png)
25+
26+
Multiple agents may be used in a single template or even a single resource. Each agent may have it's own apps, startup script, and metadata. This can be used to associate multiple containers or VMs with a workspace.
27+
928
## Resource persistence
1029

1130
The resources you define in a template may be _ephemeral_ or _persistent_. Persistent resources stay provisioned when workspaces are stopped, where as ephemeral resources are destroyed and recreated on restart. All resources are destroyed when a workspace is deleted.
@@ -21,6 +40,8 @@ A common configuration is a template whose only persistent resource is the home
2140

2241
## Template variables
2342

43+
44+
2445
## Parameters
2546

2647
## Coder apps
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Extending templates
2+
3+
There are a variety of Coder-native features to extend the configuration of your development environments. Many of the following features are defined in your templates using the [Coder Terraform provider](https://registry.terraform.io/providers/coder/coder/latest/docs). The provider docs will provide code examples for usage; alternatively, you can view our [example templates](https://github.com/coder/coder/tree/main/examples/templates) to get started.
4+
5+
<!-- TODO: Review structure
6+
7+
extending-templates/
8+
README.md
9+
- workspace agent overview
10+
- resource persistence
11+
- coder apps
12+
- coder parameters
13+
- template variables
14+
agent-metadata.md (from old docs)
15+
resource-metadata.md (from old docs)
16+
resource-ordering.md (from old docs)
17+
-->
18+
19+
## Workspace agents
20+
21+
For users to connect to a workspace, the template must include a [`coder_agent`](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent). The associated agent will facilitate [workspace connections](../../../user-guides/workspace-access/README.md) via SSH, port forwarding, and IDEs. The agent may also display [workspace metadata](#agent-metadata) like resource usage.
22+
23+
```hcl
24+
resource "coder_agent" "dev" {
25+
os = "linux"
26+
arch = "amd64"
27+
dir = "/workspace"
28+
display_apps {
29+
vscode = true
30+
}
31+
}
32+
```
33+
34+
Templates must include some computational resource to start the agent. All processes on the workspace are then spawned from the agent. All information in the dashboard's workspace view is pulled from the agent.
35+
36+
![A healthy workspace agent](../../../images/templates/healthy-workspace-agent.png)
37+
38+
Multiple agents may be used in a single template or even a single resource. Each agent may have it's own apps, startup script, and metadata. This can be used to associate multiple containers or VMs with a workspace.
39+
40+
## Resource persistence
41+
42+
The resources you define in a template may be _ephemeral_ or _persistent_. Persistent resources stay provisioned when workspaces are stopped, where as ephemeral resources are destroyed and recreated on restart. All resources are destroyed when a workspace is deleted.
43+
44+
> You can read more about how resource behavior and workspace state in the [workspace lifecycle documentation](../../workspaces/lifecycle.md).
45+
46+
Template resources follow the [behavior of Terraform resources](https://developer.hashicorp.com/terraform/language/resources/behavior#how-terraform-applies-a-configuration) and can be further configured  using the [lifecycle argument](https://developer.hashicorp.com/terraform/language/meta-arguments/lifecycle).
47+
48+
### Example usage
49+
50+
A common configuration is a template whose only persistent resource is the home directory. This allows the developer to retain their work while ensuring the rest of their environment is consistently up-to-date on each workspace restart.
51+
52+
53+
## Template variables
54+
55+
You can show live operational metrics to workspace users with agent metadata. It is the dynamic complement of resource metadata.
56+
57+
You specify agent metadata in the coder_agent.
58+
59+
## Parameters
60+
61+
## Coder apps
62+
63+
### App ordering
64+
65+
## Agent metadata
66+
67+
68+
<children>
69+
70+
</children>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Agent metadata
2+
3+
![agent-metadata](../../../images/admin/templates/agent-metadata-ui.png)
4+
5+
You can show live operational metrics to workspace users with agent metadata. It
6+
is the dynamic complement of [resource metadata](./resource-metadata.md).
7+
8+
You specify agent metadata in the
9+
[`coder_agent`](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent).
10+
11+
## Examples
12+
13+
All of these examples use
14+
[heredoc strings](https://developer.hashicorp.com/terraform/language/expressions/strings#heredoc-strings)
15+
for the script declaration. With heredoc strings, you can script without messy
16+
escape codes, just as if you were working in your terminal.
17+
18+
Some of the examples use the [`coder stat`](../../../reference/cli/stat.md) command. This is
19+
useful for determining CPU and memory usage of the VM or container that the
20+
workspace is running in, which is more accurate than resource usage about the
21+
workspace's host.
22+
23+
Here's a standard set of metadata snippets for Linux agents:
24+
25+
```hcl
26+
resource "coder_agent" "main" {
27+
os = "linux"
28+
...
29+
metadata {
30+
display_name = "CPU Usage"
31+
key = "cpu"
32+
# Uses the coder stat command to get container CPU usage.
33+
script = "coder stat cpu"
34+
interval = 1
35+
timeout = 1
36+
}
37+
38+
metadata {
39+
display_name = "Memory Usage"
40+
key = "mem"
41+
# Uses the coder stat command to get container memory usage in GiB.
42+
script = "coder stat mem --prefix Gi"
43+
interval = 1
44+
timeout = 1
45+
}
46+
47+
metadata {
48+
display_name = "CPU Usage (Host)"
49+
key = "cpu_host"
50+
# calculates CPU usage by summing the "us", "sy" and "id" columns of
51+
# top.
52+
script = <<EOT
53+
top -bn1 | awk 'FNR==3 {printf "%2.0f%%", $2+$3+$4}'
54+
EOT
55+
interval = 1
56+
timeout = 1
57+
}
58+
59+
metadata {
60+
display_name = "Memory Usage (Host)"
61+
key = "mem_host"
62+
script = <<EOT
63+
free | awk '/^Mem/ { printf("%.0f%%", $4/$2 * 100.0) }'
64+
EOT
65+
interval = 1
66+
timeout = 1
67+
}
68+
69+
metadata {
70+
display_name = "Disk Usage"
71+
key = "disk"
72+
script = "df -h | awk '$6 ~ /^\\/$/ { print $5 }'"
73+
interval = 1
74+
timeout = 1
75+
}
76+
77+
metadata {
78+
display_name = "Load Average"
79+
key = "load"
80+
script = <<EOT
81+
awk '{print $1,$2,$3}' /proc/loadavg
82+
EOT
83+
interval = 1
84+
timeout = 1
85+
}
86+
}
87+
```
88+
89+
## Useful utilities
90+
91+
You can also show agent metadata for information about the workspace's host.
92+
93+
[top](https://manpages.ubuntu.com/manpages/jammy/en/man1/top.1.html) is
94+
available in most Linux distributions and provides virtual memory, CPU and IO
95+
statistics. Running `top` produces output that looks like:
96+
97+
```text
98+
%Cpu(s): 65.8 us, 4.4 sy, 0.0 ni, 29.3 id, 0.3 wa, 0.0 hi, 0.2 si, 0.0 st
99+
MiB Mem : 16009.0 total, 493.7 free, 4624.8 used, 10890.5 buff/cache
100+
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 11021.3 avail Mem
101+
```
102+
103+
[vmstat](https://manpages.ubuntu.com/manpages/jammy/en/man8/vmstat.8.html) is
104+
available in most Linux distributions and provides virtual memory, CPU and IO
105+
statistics. Running `vmstat` produces output that looks like:
106+
107+
```text
108+
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
109+
r b swpd free buff cache si so bi bo in cs us sy id wa st
110+
0 0 19580 4781680 12133692 217646944 0 2 4 32 1 0 1 1 98 0 0
111+
```
112+
113+
[dstat](https://manpages.ubuntu.com/manpages/jammy/man1/dstat.1.html) is
114+
considerably more parseable than `vmstat` but often not included in base images.
115+
It is easily installed by most package managers under the name `dstat`. The
116+
output of running `dstat 1 1` looks like:
117+
118+
```text
119+
--total-cpu-usage-- -dsk/total- -net/total- ---paging-- ---system--
120+
usr sys idl wai stl| read writ| recv send| in out | int csw
121+
1 1 98 0 0|3422k 25M| 0 0 | 153k 904k| 123k 174k
122+
```
123+
124+
## Managing the database load
125+
126+
Agent metadata can generate a significant write load and overwhelm your Coder
127+
database if you're not careful. The approximate writes per second can be
128+
calculated using the formula:
129+
130+
```text
131+
(metadata_count * num_running_agents * 2) / metadata_avg_interval
132+
```
133+
134+
For example, let's say you have
135+
136+
- 10 running agents
137+
- each with 6 metadata snippets
138+
- with an average interval of 4 seconds
139+
140+
You can expect `(10 * 6 * 2) / 4`, or 30 writes per second.
141+
142+
One of the writes is to the `UNLOGGED` `workspace_agent_metadata` table and the
143+
other to the `NOTIFY` query that enables live stats streaming in the UI.
144+
145+
## Next Steps
146+
147+
- [Resource metadata](./resource-metadata.md)
148+
- [Parameters](./parameters.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Icons
2+
3+
Coder uses icons in several places, including ones that can be configured
4+
throughout the app, or specified in your Terraform. They're specified by a URL,
5+
which can be to an image hosted on a CDN of your own, or one of the icons that
6+
come bundled with your Coder deployment.
7+
8+
- **Template Icons**:
9+
10+
- Make templates and workspaces visually recognizable with a relevant or
11+
memorable icon
12+
13+
- [**Terraform**](https://registry.terraform.io/providers/coder/coder/latest/docs):
14+
15+
- [`coder_app`](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/app#icon)
16+
- [`coder_parameter`](https://registry.terraform.io/providers/coder/coder/latest/docs/data-sources/parameter#icon)
17+
and
18+
[`option`](https://registry.terraform.io/providers/coder/coder/latest/docs/data-sources/parameter#nested-schema-for-option)
19+
blocks
20+
- [`coder_script`](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/script#icon)
21+
- [`coder_metadata`](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/metadata#icon)
22+
23+
These can all be configured to use an icon by setting the `icon` field.
24+
25+
```hcl
26+
data "coder_parameter" "my_parameter" {
27+
icon = "/icon/coder.svg"
28+
29+
option {
30+
icon = "/emojis/1f3f3-fe0f-200d-26a7-fe0f.png"
31+
}
32+
}
33+
```
34+
35+
- [**Authentication Providers**](https://coder.com/docs/v2/latest/admin/external-auth):
36+
37+
- Use icons for external authentication providers to make them recognizable.
38+
You can set an icon for each provider by setting the
39+
`CODER_EXTERNAL_AUTH_X_ICON` environment variable, where `X` is the number
40+
of the provider.
41+
42+
```env
43+
CODER_EXTERNAL_AUTH_0_ICON=/icon/github.svg
44+
CODER_EXTERNAL_AUTH_1_ICON=/icon/google.svg
45+
```
46+
47+
- [**Support Links**](../admin/appearance.md#support-links):
48+
49+
- Use icons for support links to make them recognizable. You can set the
50+
`icon` field for each link in `CODER_SUPPORT_LINKS` array.
51+
52+
## Bundled icons
53+
54+
Coder is distributed with a bundle of icons for popular cloud providers and
55+
programming languages. You can see all of the icons (or suggest new ones) in our
56+
repository on
57+
[GitHub](https://github.com/coder/coder/tree/main/site/static/icon).
58+
59+
You can also view the entire list, with search and previews, by navigating to
60+
/icons on your Coder deployment. E.g. [https://coder.example.com/icons](#). This
61+
can be particularly useful in airgapped deployments.
62+
63+
![The icon gallery](../images/icons-gallery.png)
64+
65+
## External icons
66+
67+
You can use any image served over HTTPS as an icon, by specifying the full URL
68+
of the image. We recommend that you use a CDN that you control, but it can be
69+
served from any source that you trust.
70+
71+
You can also embed an image by using data: URLs.
72+
73+
- Only the https: and data: protocols are supported in icon URLs (not http:)
74+
75+
- Be careful when using images hosted by someone else; they might disappear or
76+
change!
77+
78+
- Be careful when using data: URLs. They can get rather large, and can
79+
negatively impact loading times for pages and queries they appear in. Only use
80+
them for very small icons that compress well.
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.