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

Skip to content

chore(examples/templates/azure-windows): remove dependency on az CLI #16039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/examples.gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"linux",
"azure"
],
"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"
"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"
},
{
"id": "digitalocean-linux",
Expand Down
34 changes: 29 additions & 5 deletions examples/templates/azure-linux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,35 @@ This template provisions the following resources:

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.

> **Note**
> [!NOTE]
> This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.

## code-server

`code-server` is installed via the `startup_script` argument in the `coder_agent`
resource block. The `coder_app` resource is defined to access `code-server` through
the dashboard UI over `localhost:13337`.
### Persistent VM

> [!IMPORTANT]
> 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.
> You will have to do this installation manually as it is not included in our official images.

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:

```hcl
# Stop the VM
resource "null_resource" "stop_vm" {
count = data.coder_workspace.me.transition == "stop" ? 1 : 0
depends_on = [azurerm_linux_virtual_machine.main]
provisioner "local-exec" {
# Use deallocate so the VM is not charged
command = "az vm deallocate --ids ${azurerm_linux_virtual_machine.main.id}"
}
}

# Start the VM
resource "null_resource" "start" {
count = data.coder_workspace.me.transition == "start" ? 1 : 0
depends_on = [azurerm_linux_virtual_machine.main]
provisioner "local-exec" {
command = "az vm start --ids ${azurerm_linux_virtual_machine.main.id}"
}
}
```
2 changes: 1 addition & 1 deletion examples/templates/azure-linux/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ resource "tls_private_key" "dummy" {
}

resource "azurerm_linux_virtual_machine" "main" {
count = data.coder_workspace.me.transition == "start" ? 1 : 0
count = data.coder_workspace.me.start_count
name = "vm"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
Expand Down
31 changes: 30 additions & 1 deletion examples/templates/azure-windows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,34 @@ This template provisions the following resources:

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).

> **Note**
> [!NOTE]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we render these within Coder?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good thinking, will check!

> This template is designed to be a starting point! Edit the Terraform to extend the template to support your use case.

### Persistent VM

> [!IMPORTANT]
> 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.
> You will have to do this installation manually as it is not included in our official images.

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:

```hcl
# Stop the VM
resource "null_resource" "stop_vm" {
count = data.coder_workspace.me.transition == "stop" ? 1 : 0
Copy link
Member

@matifali matifali Jan 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

count = data.coder_workspave.me.start_count

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matifali I'm not sure though, the issue is that if we use it for stopping, then it will look something like count = data.coder_workspave.me.start_count == 1 ? 0 : 1, we should have data.coder_workspave.me.stop_count i think

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"stop" ? 1 : 0 is fine. Adding a stop_count is too niche.

depends_on = [azurerm_windows_virtual_machine.main]
provisioner "local-exec" {
# Use deallocate so the VM is not charged
command = "az vm deallocate --ids ${azurerm_windows_virtual_machine.main.id}"
}
}

# Start the VM
resource "null_resource" "start" {
count = data.coder_workspace.me.transition == "start" ? 1 : 0
depends_on = [azurerm_windows_virtual_machine.main]
provisioner "local-exec" {
command = "az vm start --ids ${azurerm_windows_virtual_machine.main.id}"
}
}
```
26 changes: 5 additions & 21 deletions examples/templates/azure-windows/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ resource "azurerm_managed_disk" "data" {

# Create virtual machine
resource "azurerm_windows_virtual_machine" "main" {
count = data.coder_workspace.me.start_count
name = "vm"
admin_username = local.admin_username
admin_password = random_password.admin_password.result
Expand Down Expand Up @@ -189,7 +190,8 @@ resource "azurerm_windows_virtual_machine" "main" {
}

resource "coder_metadata" "rdp_login" {
resource_id = azurerm_windows_virtual_machine.main.id
count = data.coder_workspace.me.start_count
resource_id = azurerm_windows_virtual_machine.main[0].id
item {
key = "Username"
value = local.admin_username
Expand All @@ -202,27 +204,9 @@ resource "coder_metadata" "rdp_login" {
}

resource "azurerm_virtual_machine_data_disk_attachment" "main_data" {
count = data.coder_workspace.me.start_count
managed_disk_id = azurerm_managed_disk.data.id
virtual_machine_id = azurerm_windows_virtual_machine.main.id
virtual_machine_id = azurerm_windows_virtual_machine.main[0].id
lun = "10"
caching = "ReadWrite"
}

# Stop the VM
resource "null_resource" "stop_vm" {
count = data.coder_workspace.me.transition == "stop" ? 1 : 0
depends_on = [azurerm_windows_virtual_machine.main]
provisioner "local-exec" {
# Use deallocate so the VM is not charged
command = "az vm deallocate --ids ${azurerm_windows_virtual_machine.main.id}"
}
}

# Start the VM
resource "null_resource" "start" {
count = data.coder_workspace.me.transition == "start" ? 1 : 0
depends_on = [azurerm_windows_virtual_machine.main]
provisioner "local-exec" {
command = "az vm start --ids ${azurerm_windows_virtual_machine.main.id}"
}
}
Loading