Skip to content

Commit c49584b

Browse files
committed
WIP windows uses data disk
Signed-off-by: Spike Curtis <spike@coder.com>
1 parent 129f036 commit c49584b

File tree

2 files changed

+84
-14
lines changed

2 files changed

+84
-14
lines changed
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
<FirstLogonCommands>
2+
<SynchronousCommand>
3+
<CommandLine>powershell.exe -sta -ExecutionPolicy Unrestricted -Command "Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) | Out-File C:\AzureData\chocolatey.log"</CommandLine>
4+
<Description>Install Chocolatey Package Manager</Description>
5+
<Order>1</Order>
6+
</SynchronousCommand>
7+
<SynchronousCommand>
8+
<CommandLine>powershell.exe -sta -ExecutionPolicy Unrestricted -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12"</CommandLine>
9+
<Description>Set SecurityProtocol to TLS1.2</Description>
10+
<Order>2</Order>
11+
</SynchronousCommand>
212
<SynchronousCommand>
313
<CommandLine>cmd /c "copy C:\AzureData\CustomData.bin C:\AzureData\CoderAgent.ps1"</CommandLine>
414
<Description>Move the CustomData file to the working directory</Description>
5-
<Order>10</Order>
15+
<Order>3</Order>
616
</SynchronousCommand>
717
<SynchronousCommand>
8-
<CommandLine>powershell.exe -sta -ExecutionPolicy Unrestricted -Command "C:\AzureData\CoderAgent.ps1 | Out-File C:\AzureData\CoderAgent.log</CommandLine>
18+
<CommandLine>powershell.exe -sta -ExecutionPolicy Unrestricted -Command "C:\AzureData\CoderAgent.ps1 | Out-File C:\AzureData\CoderAgent.log"</CommandLine>
919
<Description>Execute CustomData as powershell.</Description>
10-
<Order>20</Order>
20+
<Order>4</Order>
1121
</SynchronousCommand>
1222
</FirstLogonCommands>

examples/templates/azure-windows/main.tf

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,66 @@ data "coder_parameter" "location" {
4444
}
4545
}
4646
resource "coder_agent" "main" {
47-
arch = "amd64"
48-
auth = "azure-instance-identity"
49-
os = "windows"
47+
arch = "amd64"
48+
auth = "azure-instance-identity"
49+
os = "windows"
50+
dir = "F:\\Users\\coder"
51+
login_before_ready = false
5052
startup_script = <<EOF
51-
# Set admin password
52-
Get-LocalUser -Name "Administrator" | Set-LocalUser -Password (ConvertTo-SecureString -AsPlainText "${local.admin_password}" -Force)
53-
# To disable password entirely, see https://serverfault.com/a/968240
53+
$disk = Get-Disk -Number 2
54+
if ($disk.PartitionStyle -Eq 'RAW')
55+
{
56+
Write-Host "Initializing data disk"
57+
$disk | Initialize-Disk
58+
} else {
59+
Write-Host "data disk already initialized"
60+
}
61+
62+
$partitions = Get-Partition -DiskNumber $disk.Number | Where-Object Type -Ne 'Reserved'
63+
if ($partitions.Count -Eq 0) {
64+
Write-Host "Creating partition on data disk"
65+
New-Partition -DiskNumber $disk.Number -UseMaximumSize -DriveLetter F
66+
} else {
67+
$s = "data disk already has partition of size {0:n1} GiB" -f ($partitions[0].Size / 1073741824)
68+
Write-Host $s
69+
if ($partitions[0].DriveLetter -Ne "F") {
70+
Set-Partition -InputObject $partitions[0] -NewDriveLetter F
71+
}
72+
}
73+
74+
$volume = Get-Volume -DriveLetter F
75+
if ($volume.FileSystemType -Eq 'Unknown')
76+
{
77+
Write-Host "Formatting data disk"
78+
Format-Volume -InputObject $volume -FileSystem NTFS -Confirm:$false
79+
} else {
80+
Write-Host "data disk is already formatted"
81+
}
82+
83+
$h = "F:\Users\coder"
84+
if (Test-Path -Path $home) {
85+
Write-Host "Home directory exists"
86+
} else {
87+
New-Item -ItemType "directory" -Path $h
88+
}
89+
90+
$u = Get-LocalUser -Name coder
91+
$profile = "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\{0}" -f $u.SID
92+
$curHome = Get-ItemProperty -Path $profile -Name "ProfileImagePath"
93+
if ($curHome -Ne $h) {
94+
Write-Host "Updating user profile location"
95+
Set-ItemProperty -Path $profile -Name "ProfileImagePath" -Value $h
96+
}
97+
5498
# Enable RDP
5599
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -name "fDenyTSConnections" -value 0
56100
# Enable RDP through Windows Firewall
57101
Enable-NetFirewallRule -DisplayGroup "Remote Desktop"
58102
# Disable Network Level Authentication (NLA)
59103
# Clients will connect via Coder's tunnel
60104
(Get-WmiObject -class "Win32_TSGeneralSetting" -Namespace root\cimv2\terminalservices -ComputerName $env:COMPUTERNAME -Filter "TerminalName='RDP-tcp'").SetUserAuthenticationRequired(0)
61-
choco feature enable -n=allowGlobalConfirmation
62-
choco install visualstudio2022community --package-parameters "--add=Microsoft.VisualStudio.Workload.ManagedDesktop;includeRecommended --passive --locale en-US"
105+
# choco feature enable -n=allowGlobalConfirmation
106+
# choco install visualstudio2022community --package-parameters "--add=Microsoft.VisualStudio.Workload.ManagedDesktop;includeRecommended --passive --locale en-US"
63107
EOF
64108
}
65109
locals {
@@ -75,14 +119,13 @@ locals {
75119
user_data_start = <<EOT
76120
# Install Chocolatey package manager before
77121
# the agent starts to use via startup_script
78-
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
79122
# Reload path so sessions include "choco" and "refreshenv"
80123
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
81124
# Install Git and reload path
82125
choco install -y git
83126
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
84127
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
85-
${coder_agent.main.init_script}
128+
86129
EOT
87130
user_data_end = <<EOT
88131
shutdown /s
@@ -152,6 +195,16 @@ resource "random_id" "storage_id" {
152195
}
153196
byte_length = 8
154197
}
198+
199+
resource "azurerm_managed_disk" "data" {
200+
name = "data_disk"
201+
location = azurerm_resource_group.main.location
202+
resource_group_name = azurerm_resource_group.main.name
203+
storage_account_type = "Standard_LRS"
204+
create_option = "Empty"
205+
disk_size_gb = 20
206+
}
207+
155208
# Create virtual machine
156209
resource "azurerm_windows_virtual_machine" "main" {
157210
name = "vm"
@@ -161,7 +214,7 @@ resource "azurerm_windows_virtual_machine" "main" {
161214
resource_group_name = azurerm_resource_group.main.name
162215
network_interface_ids = [azurerm_network_interface.main.id]
163216
size = "Standard_DS1_v2"
164-
custom_data = base64encode(data.coder_workspace.me.transition == "start" ? local.user_data_start : local.user_data_end)
217+
custom_data = base64encode(coder_agent.main.init_script)
165218
os_disk {
166219
name = "myOsDisk"
167220
caching = "ReadWrite"
@@ -188,3 +241,10 @@ resource "azurerm_windows_virtual_machine" "main" {
188241
Coder_Provisioned = "true"
189242
}
190243
}
244+
245+
resource "azurerm_virtual_machine_data_disk_attachment" "main_data" {
246+
managed_disk_id = azurerm_managed_disk.data.id
247+
virtual_machine_id = azurerm_windows_virtual_machine.main.id
248+
lun = "10"
249+
caching = "ReadWrite"
250+
}

0 commit comments

Comments
 (0)