@@ -44,22 +44,66 @@ data "coder_parameter" "location" {
44
44
}
45
45
}
46
46
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
50
52
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
+
54
98
# Enable RDP
55
99
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -name "fDenyTSConnections" -value 0
56
100
# Enable RDP through Windows Firewall
57
101
Enable-NetFirewallRule -DisplayGroup "Remote Desktop"
58
102
# Disable Network Level Authentication (NLA)
59
103
# Clients will connect via Coder's tunnel
60
104
(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"
63
107
EOF
64
108
}
65
109
locals {
@@ -75,14 +119,13 @@ locals {
75
119
user_data_start = << EOT
76
120
# Install Chocolatey package manager before
77
121
# 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'))
79
122
# Reload path so sessions include "choco" and "refreshenv"
80
123
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
81
124
# Install Git and reload path
82
125
choco install -y git
83
126
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
84
127
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
85
- ${ coder_agent . main . init_script }
128
+
86
129
EOT
87
130
user_data_end = << EOT
88
131
shutdown /s
@@ -152,6 +195,16 @@ resource "random_id" "storage_id" {
152
195
}
153
196
byte_length = 8
154
197
}
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
+
155
208
# Create virtual machine
156
209
resource "azurerm_windows_virtual_machine" "main" {
157
210
name = " vm"
@@ -161,7 +214,7 @@ resource "azurerm_windows_virtual_machine" "main" {
161
214
resource_group_name = azurerm_resource_group. main . name
162
215
network_interface_ids = [azurerm_network_interface . main . id ]
163
216
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 )
165
218
os_disk {
166
219
name = " myOsDisk"
167
220
caching = " ReadWrite"
@@ -188,3 +241,10 @@ resource "azurerm_windows_virtual_machine" "main" {
188
241
Coder_Provisioned = " true"
189
242
}
190
243
}
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