Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0870ff2

Browse files
authored
chore: unify azure-linux and azure-windows templates (#16039)
1 parent 3043e79 commit 0870ff2

File tree

5 files changed

+66
-29
lines changed

5 files changed

+66
-29
lines changed

examples/examples.gen.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"linux",
5454
"azure"
5555
],
56-
"markdown": "\n# Remote Development on Azure VMs (Linux)\n\nProvision Azure Linux VMs as [Coder workspaces](https://coder.com/docs/workspaces) with this example template.\n\n\u003c!-- TODO: Add screenshot --\u003e\n\n## Prerequisites\n\n### Authentication\n\nThis template assumes that coderd is run in an environment that is authenticated\nwith Azure. For example, run `az login` then `az account set --subscription=\u003cid\u003e`\nto import credentials on the system and user running coderd. For other ways to\nauthenticate, [consult the Terraform docs](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs#authenticating-to-azure).\n\n## Architecture\n\nThis template provisions the following resources:\n\n- Azure VM (ephemeral, deleted on stop)\n- Managed disk (persistent, mounted to `/home/coder`)\n\nThis means, when the workspace restarts, any tools or files outside of the home directory are not persisted. To pre-bake tools into the workspace (e.g. `python3`), modify the VM image, or use a [startup script](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/script). Alternatively, individual developers can [personalize](https://coder.com/docs/dotfiles) their workspaces with dotfiles.\n\n\u003e **Note**\n\u003e This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.\n\n## code-server\n\n`code-server` is installed via the `startup_script` argument in the `coder_agent`\nresource block. The `coder_app` resource is defined to access `code-server` through\nthe dashboard UI over `localhost:13337`.\n"
56+
"markdown": "\n# Remote Development on Azure VMs (Linux)\n\nProvision Azure Linux VMs as [Coder workspaces](https://coder.com/docs/workspaces) with this example template.\n\n\u003c!-- TODO: Add screenshot --\u003e\n\n## Prerequisites\n\n### Authentication\n\nThis template assumes that coderd is run in an environment that is authenticated\nwith Azure. For example, run `az login` then `az account set --subscription=\u003cid\u003e`\nto import credentials on the system and user running coderd. For other ways to\nauthenticate, [consult the Terraform docs](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs#authenticating-to-azure).\n\n## Architecture\n\nThis template provisions the following resources:\n\n- Azure VM (ephemeral, deleted on stop)\n- Managed disk (persistent, mounted to `/home/coder`)\n\nThis means, when the workspace restarts, any tools or files outside of the home directory are not persisted. To pre-bake tools into the workspace (e.g. `python3`), modify the VM image, or use a [startup script](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/script). Alternatively, individual developers can [personalize](https://coder.com/docs/dotfiles) their workspaces with dotfiles.\n\n\u003e [!NOTE]\n\u003e This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.\n\n\n### Persistent VM\n\n\u003e [!IMPORTANT] \n\u003e This approach requires the [`az` CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli#install) to be present in the PATH of your Coder Provisioner.\n\u003e You will have to do this installation manually as it is not included in our official images.\n\nIt is possible to make the VM persistent (instead of ephemeral) by removing the `count` attribute in the `azurerm_linux_virtual_machine` resource block as well as adding the following snippet:\n\n```hcl\n# Stop the VM\nresource \"null_resource\" \"stop_vm\" {\n count = data.coder_workspace.me.transition == \"stop\" ? 1 : 0\n depends_on = [azurerm_linux_virtual_machine.main]\n provisioner \"local-exec\" {\n # Use deallocate so the VM is not charged\n command = \"az vm deallocate --ids ${azurerm_linux_virtual_machine.main.id}\"\n }\n}\n\n# Start the VM\nresource \"null_resource\" \"start\" {\n count = data.coder_workspace.me.transition == \"start\" ? 1 : 0\n depends_on = [azurerm_linux_virtual_machine.main]\n provisioner \"local-exec\" {\n command = \"az vm start --ids ${azurerm_linux_virtual_machine.main.id}\"\n }\n}\n```\n"
5757
},
5858
{
5959
"id": "digitalocean-linux",

examples/templates/azure-linux/README.md

+29-5
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,35 @@ This template provisions the following resources:
3131

3232
This means, when the workspace restarts, any tools or files outside of the home directory are not persisted. To pre-bake tools into the workspace (e.g. `python3`), modify the VM image, or use a [startup script](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/script). Alternatively, individual developers can [personalize](https://coder.com/docs/dotfiles) their workspaces with dotfiles.
3333

34-
> **Note**
34+
> [!NOTE]
3535
> This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.
3636
37-
## code-server
3837

39-
`code-server` is installed via the `startup_script` argument in the `coder_agent`
40-
resource block. The `coder_app` resource is defined to access `code-server` through
41-
the dashboard UI over `localhost:13337`.
38+
### Persistent VM
39+
40+
> [!IMPORTANT]
41+
> This approach requires the [`az` CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli#install) to be present in the PATH of your Coder Provisioner.
42+
> You will have to do this installation manually as it is not included in our official images.
43+
44+
It is possible to make the VM persistent (instead of ephemeral) by removing the `count` attribute in the `azurerm_linux_virtual_machine` resource block as well as adding the following snippet:
45+
46+
```hcl
47+
# Stop the VM
48+
resource "null_resource" "stop_vm" {
49+
count = data.coder_workspace.me.transition == "stop" ? 1 : 0
50+
depends_on = [azurerm_linux_virtual_machine.main]
51+
provisioner "local-exec" {
52+
# Use deallocate so the VM is not charged
53+
command = "az vm deallocate --ids ${azurerm_linux_virtual_machine.main.id}"
54+
}
55+
}
56+
57+
# Start the VM
58+
resource "null_resource" "start" {
59+
count = data.coder_workspace.me.transition == "start" ? 1 : 0
60+
depends_on = [azurerm_linux_virtual_machine.main]
61+
provisioner "local-exec" {
62+
command = "az vm start --ids ${azurerm_linux_virtual_machine.main.id}"
63+
}
64+
}
65+
```

examples/templates/azure-linux/main.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ resource "tls_private_key" "dummy" {
264264
}
265265

266266
resource "azurerm_linux_virtual_machine" "main" {
267-
count = data.coder_workspace.me.transition == "start" ? 1 : 0
267+
count = data.coder_workspace.me.start_count
268268
name = "vm"
269269
resource_group_name = azurerm_resource_group.main.name
270270
location = azurerm_resource_group.main.location

examples/templates/azure-windows/README.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,34 @@ This template provisions the following resources:
3131

3232
This means, when the workspace restarts, any tools or files outside of the data directory are not persisted. To pre-bake tools into the workspace (e.g. `python3`), modify the VM image, or use a [startup script](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/script).
3333

34-
> **Note**
34+
> [!NOTE]
3535
> This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.
36+
37+
### Persistent VM
38+
39+
> [!IMPORTANT]
40+
> This approach requires the [`az` CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli#install) to be present in the PATH of your Coder Provisioner.
41+
> You will have to do this installation manually as it is not included in our official images.
42+
43+
It is possible to make the VM persistent (instead of ephemeral) by removing the `count` attribute in the `azurerm_windows_virtual_machine` resource block as well as adding the following snippet:
44+
45+
```hcl
46+
# Stop the VM
47+
resource "null_resource" "stop_vm" {
48+
count = data.coder_workspace.me.transition == "stop" ? 1 : 0
49+
depends_on = [azurerm_windows_virtual_machine.main]
50+
provisioner "local-exec" {
51+
# Use deallocate so the VM is not charged
52+
command = "az vm deallocate --ids ${azurerm_windows_virtual_machine.main.id}"
53+
}
54+
}
55+
56+
# Start the VM
57+
resource "null_resource" "start" {
58+
count = data.coder_workspace.me.transition == "start" ? 1 : 0
59+
depends_on = [azurerm_windows_virtual_machine.main]
60+
provisioner "local-exec" {
61+
command = "az vm start --ids ${azurerm_windows_virtual_machine.main.id}"
62+
}
63+
}
64+
```

examples/templates/azure-windows/main.tf

+5-21
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ resource "azurerm_managed_disk" "data" {
151151

152152
# Create virtual machine
153153
resource "azurerm_windows_virtual_machine" "main" {
154+
count = data.coder_workspace.me.start_count
154155
name = "vm"
155156
admin_username = local.admin_username
156157
admin_password = random_password.admin_password.result
@@ -189,7 +190,8 @@ resource "azurerm_windows_virtual_machine" "main" {
189190
}
190191

191192
resource "coder_metadata" "rdp_login" {
192-
resource_id = azurerm_windows_virtual_machine.main.id
193+
count = data.coder_workspace.me.start_count
194+
resource_id = azurerm_windows_virtual_machine.main[0].id
193195
item {
194196
key = "Username"
195197
value = local.admin_username
@@ -202,27 +204,9 @@ resource "coder_metadata" "rdp_login" {
202204
}
203205

204206
resource "azurerm_virtual_machine_data_disk_attachment" "main_data" {
207+
count = data.coder_workspace.me.start_count
205208
managed_disk_id = azurerm_managed_disk.data.id
206-
virtual_machine_id = azurerm_windows_virtual_machine.main.id
209+
virtual_machine_id = azurerm_windows_virtual_machine.main[0].id
207210
lun = "10"
208211
caching = "ReadWrite"
209212
}
210-
211-
# Stop the VM
212-
resource "null_resource" "stop_vm" {
213-
count = data.coder_workspace.me.transition == "stop" ? 1 : 0
214-
depends_on = [azurerm_windows_virtual_machine.main]
215-
provisioner "local-exec" {
216-
# Use deallocate so the VM is not charged
217-
command = "az vm deallocate --ids ${azurerm_windows_virtual_machine.main.id}"
218-
}
219-
}
220-
221-
# Start the VM
222-
resource "null_resource" "start" {
223-
count = data.coder_workspace.me.transition == "start" ? 1 : 0
224-
depends_on = [azurerm_windows_virtual_machine.main]
225-
provisioner "local-exec" {
226-
command = "az vm start --ids ${azurerm_windows_virtual_machine.main.id}"
227-
}
228-
}

0 commit comments

Comments
 (0)