Skip to content

Commit 4182835

Browse files
committed
Add az cli commands to start & stop
Signed-off-by: Spike Curtis <spike@coder.com>
1 parent 101d796 commit 4182835

File tree

3 files changed

+92
-32
lines changed

3 files changed

+92
-32
lines changed

examples/templates/azure-windows/Initialize.ps1.tftpl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,6 @@ if ($volume.FileSystemType -Eq 'Unknown')
2929
"data disk is already formatted"
3030
}
3131

32-
# Above operations are careful not to repartition & overwrite data since the disk
33-
# might come from a previous build, but this should only be run once per VM and is not idempotent
34-
#$h = "C:\Users\coder"
35-
# We can only mount to empty directory, so if there is data in the home dir, move it
36-
#if (Test-Path -Path $home) {
37-
# "Home directory exists"
38-
# Rename-Item -Path $h -NewName "coderold"
39-
#}
40-
# Create the directory and mount the partition
41-
#New-Item -ItemType "directory" -Path $h
42-
#Add-PartitionAccessPath -InputObject $partition -AccessPath $h
43-
4432
# Mount the partition
4533
Add-PartitionAccessPath -InputObject $partition -AccessPath "F:"
4634

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
name: Develop in Windows on Azure
3+
description: Get started with Windows development on Microsoft Azure.
4+
tags: [cloud, azure, windows]
5+
icon: /icon/azure.png
6+
---
7+
8+
# azure-windows
9+
10+
To get started, run `coder templates init`. When prompted, select this template.
11+
Follow the on-screen instructions to proceed.
12+
13+
## Authentication
14+
15+
This template assumes that coderd is run in an environment that is authenticated
16+
with Azure. For example, run `az login` then `az account set --subscription=<id>`
17+
to import credentials on the system and user running coderd. For other ways to
18+
authenticate [consult the Terraform docs](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs#authenticating-to-azure).
19+
20+
## Dependencies
21+
22+
This template depends on the Azure CLI tool (`az`) to start and stop the Windows VM. Ensure this
23+
tool is installed and available in the path on the machine that runs coderd.

examples/templates/azure-windows/main.tf

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ data "coder_parameter" "location" {
4444
}
4545
}
4646

47+
data "coder_parameter" "data_disk_size" {
48+
description = "Size of your data (F:) drive in GB"
49+
display_name = "Data disk size"
50+
name = "data_disk_size"
51+
default = 20
52+
mutable = "false"
53+
type = "number"
54+
validation {
55+
min = 5
56+
max = 5000
57+
}
58+
}
59+
4760
resource "coder_agent" "main" {
4861
arch = "amd64"
4962
auth = "azure-instance-identity"
@@ -52,34 +65,38 @@ resource "coder_agent" "main" {
5265
login_before_ready = false
5366
}
5467

68+
resource "random_password" "admin_password" {
69+
length = 16
70+
special = true
71+
# https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/password-must-meet-complexity-requirements#reference
72+
# we remove characters that require special handling in XML, as this is how we pass it to the VM
73+
# namely: <>&'"
74+
override_special = "~!@#$%^*_-+=`|\\(){}[]:;,.?/"
75+
}
76+
5577
locals {
56-
prefix = "spike"
78+
prefix = "coder-win"
5779
admin_username = "coder"
58-
# Password to log in via RDP
59-
#
60-
# Must meet Windows password complexity requirements:
61-
# https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/password-must-meet-complexity-requirements#reference
62-
admin_password = "coderRDP!"
6380
}
6481

6582
resource "azurerm_resource_group" "main" {
66-
name = "${local.prefix}-${data.coder_workspace.me.name}-resources"
83+
name = "${local.prefix}-${data.coder_workspace.me.id}"
6784
location = data.coder_parameter.location.value
6885
tags = {
6986
Coder_Provisioned = "true"
7087
}
7188
}
7289

7390
// Uncomment here and in the azurerm_network_interface resource to obtain a public IP
74-
resource "azurerm_public_ip" "main" {
75-
name = "publicip"
76-
resource_group_name = azurerm_resource_group.main.name
77-
location = azurerm_resource_group.main.location
78-
allocation_method = "Static"
79-
tags = {
80-
Coder_Provisioned = "true"
81-
}
82-
}
91+
#resource "azurerm_public_ip" "main" {
92+
# name = "publicip"
93+
# resource_group_name = azurerm_resource_group.main.name
94+
# location = azurerm_resource_group.main.location
95+
# allocation_method = "Static"
96+
# tags = {
97+
# Coder_Provisioned = "true"
98+
# }
99+
#}
83100
resource "azurerm_virtual_network" "main" {
84101
name = "network"
85102
address_space = ["10.0.0.0/24"]
@@ -104,7 +121,7 @@ resource "azurerm_network_interface" "main" {
104121
subnet_id = azurerm_subnet.internal.id
105122
private_ip_address_allocation = "Dynamic"
106123
// Uncomment for public IP address as well as azurerm_public_ip resource above
107-
public_ip_address_id = azurerm_public_ip.main.id
124+
# public_ip_address_id = azurerm_public_ip.main.id
108125
}
109126
tags = {
110127
Coder_Provisioned = "true"
@@ -133,14 +150,14 @@ resource "azurerm_managed_disk" "data" {
133150
resource_group_name = azurerm_resource_group.main.name
134151
storage_account_type = "Standard_LRS"
135152
create_option = "Empty"
136-
disk_size_gb = 20
153+
disk_size_gb = data.coder_parameter.data_disk_size.value
137154
}
138155

139156
# Create virtual machine
140157
resource "azurerm_windows_virtual_machine" "main" {
141158
name = "vm"
142159
admin_username = local.admin_username
143-
admin_password = local.admin_password
160+
admin_password = random_password.admin_password.result
144161
location = azurerm_resource_group.main.location
145162
resource_group_name = azurerm_resource_group.main.name
146163
network_interface_ids = [azurerm_network_interface.main.id]
@@ -160,7 +177,7 @@ resource "azurerm_windows_virtual_machine" "main" {
160177
version = "latest"
161178
}
162179
additional_unattend_content {
163-
content = "<AutoLogon><Password><Value>${local.admin_password}</Value></Password><Enabled>true</Enabled><LogonCount>1</LogonCount><Username>${local.admin_username}</Username></AutoLogon>"
180+
content = "<AutoLogon><Password><Value>${random_password.admin_password.result}</Value></Password><Enabled>true</Enabled><LogonCount>1</LogonCount><Username>${local.admin_username}</Username></AutoLogon>"
164181
setting = "AutoLogon"
165182
}
166183
additional_unattend_content {
@@ -175,9 +192,41 @@ resource "azurerm_windows_virtual_machine" "main" {
175192
}
176193
}
177194

195+
resource "coder_metadata" "rdp_login" {
196+
resource_id = azurerm_windows_virtual_machine.main.id
197+
item {
198+
key = "Username"
199+
value = local.admin_username
200+
}
201+
item {
202+
key = "Password"
203+
value = random_password.admin_password.result
204+
sensitive = true
205+
}
206+
}
207+
178208
resource "azurerm_virtual_machine_data_disk_attachment" "main_data" {
179209
managed_disk_id = azurerm_managed_disk.data.id
180210
virtual_machine_id = azurerm_windows_virtual_machine.main.id
181211
lun = "10"
182212
caching = "ReadWrite"
183213
}
214+
215+
# Stop the VM
216+
resource "null_resource" "stop_vm" {
217+
count = data.coder_workspace.me.transition == "stop" ? 1 : 0
218+
depends_on = [azurerm_windows_virtual_machine.main]
219+
provisioner "local-exec" {
220+
# Use deallocate so the VM is not charged
221+
command = "az vm deallocate --ids ${azurerm_windows_virtual_machine.main.id}"
222+
}
223+
}
224+
225+
# Start the VM
226+
resource "null_resource" "start" {
227+
count = data.coder_workspace.me.transition == "start" ? 1 : 0
228+
depends_on = [azurerm_windows_virtual_machine.main]
229+
provisioner "local-exec" {
230+
command = "az vm start --ids ${azurerm_windows_virtual_machine.main.id}"
231+
}
232+
}

0 commit comments

Comments
 (0)