Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit b4153a6

Browse files
committed
refactor: split off Windows script logic into separate file
1 parent 13a8877 commit b4153a6

File tree

2 files changed

+93
-92
lines changed

2 files changed

+93
-92
lines changed

windows-rdp/main.tf

Lines changed: 5 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -34,99 +34,12 @@ resource "coder_script" "windows-rdp" {
3434
agent_id = var.agent_id
3535
display_name = "windows-rdp"
3636
icon = "https://svgur.com/i/158F.svg" # TODO: add to Coder icons
37-
script = <<EOF
38-
function Set-AdminPassword {
39-
param (
40-
[string]$adminPassword
41-
)
42-
# Set admin password
43-
Get-LocalUser -Name "${var.admin_username}" | Set-LocalUser -Password (ConvertTo-SecureString -AsPlainText $adminPassword -Force)
44-
# Enable admin user
45-
Get-LocalUser -Name "${var.admin_username}" | Enable-LocalUser
46-
}
47-
48-
function Configure-RDP {
49-
# Enable RDP
50-
New-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name "fDenyTSConnections" -Value 0 -PropertyType DWORD -Force
51-
# Disable NLA
52-
New-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name "UserAuthentication" -Value 0 -PropertyType DWORD -Force
53-
New-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name "SecurityLayer" -Value 1 -PropertyType DWORD -Force
54-
# Enable RDP through Windows Firewall
55-
Enable-NetFirewallRule -DisplayGroup "Remote Desktop"
56-
}
57-
58-
function Install-DevolutionsGateway {
59-
# Define the module name and version
60-
$moduleName = "DevolutionsGateway"
61-
$moduleVersion = "2024.1.5"
62-
63-
# Install the module with the specified version for all users
64-
# This requires administrator privileges
65-
try {
66-
# Install-PackageProvider is required for AWS. Need to set command to
67-
# terminate on failure so that try/catch actually triggers
68-
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -ErrorAction Stop
69-
Install-Module -Name $moduleName -RequiredVersion $moduleVersion -Force
70-
}
71-
catch {
72-
# If the first command failed, assume that we're on GCP and run
73-
# Install-Module only
74-
Install-Module -Name $moduleName -RequiredVersion $moduleVersion -Force
75-
}
76-
77-
# Construct the module path for system-wide installation
78-
$moduleBasePath = "C:\Windows\system32\config\systemprofile\Documents\PowerShell\Modules\$moduleName\$moduleVersion"
79-
$modulePath = Join-Path -Path $moduleBasePath -ChildPath "$moduleName.psd1"
80-
81-
# Import the module using the full path
82-
Import-Module $modulePath
83-
Install-DGatewayPackage
84-
85-
# Configure Devolutions Gateway
86-
$Hostname = "localhost"
87-
$HttpListener = New-DGatewayListener 'http://*:7171' 'http://*:7171'
88-
$WebApp = New-DGatewayWebAppConfig -Enabled $true -Authentication None
89-
$ConfigParams = @{
90-
Hostname = $Hostname
91-
Listeners = @($HttpListener)
92-
WebApp = $WebApp
93-
}
94-
Set-DGatewayConfig @ConfigParams
95-
New-DGatewayProvisionerKeyPair -Force
96-
97-
# Configure and start the Windows service
98-
Set-Service 'DevolutionsGateway' -StartupType 'Automatic'
99-
Start-Service 'DevolutionsGateway'
100-
}
101-
102-
function Patch-Devolutions-HTML {
103-
$root = "C:\Program Files\Devolutions\Gateway\webapp\client"
104-
$devolutionsHtml = "$root\index.html"
105-
$patch = '<script defer id="coder-patch" src="coder.js"></script>'
106-
107-
# Always copy the file in case we change it.
108-
@'
109-
${templatefile("${path.module}/devolutions-patch.js", {
110-
CODER_USERNAME : var.admin_username,
111-
CODER_PASSWORD : var.admin_password,
112-
})}
113-
'@ | Set-Content "$root\coder.js"
114-
115-
# Only inject the src if we have not before.
116-
$isPatched = Select-String -Path "$devolutionsHtml" -Pattern "$patch" -SimpleMatch
117-
if ($isPatched -eq $null) {
118-
(Get-Content $devolutionsHtml).Replace('</app-root>', "</app-root>$patch") | Set-Content $devolutionsHtml
119-
}
120-
}
121-
122-
Set-AdminPassword -adminPassword "${var.admin_password}"
123-
Configure-RDP
124-
Install-DevolutionsGateway
125-
Patch-Devolutions-HTML
126-
127-
EOF
37+
script = templatefile("./windows-installation.tftpl", {
38+
CODER_USERNAME : var.admin_username,
39+
CODER_PASSWORD : var.admin_password,
40+
})
12841

129-
run_on_start = true
42+
run_on_start = true
13043
}
13144

13245
resource "coder_app" "windows-rdp" {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
function Set-AdminPassword {
2+
param (
3+
[string]$adminPassword
4+
)
5+
# Set admin password
6+
Get-LocalUser -Name "${var.admin_username}" | Set-LocalUser -Password (ConvertTo-SecureString -AsPlainText $adminPassword -Force)
7+
# Enable admin user
8+
Get-LocalUser -Name "${var.admin_username}" | Enable-LocalUser
9+
}
10+
11+
function Configure-RDP {
12+
# Enable RDP
13+
New-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name "fDenyTSConnections" -Value 0 -PropertyType DWORD -Force
14+
# Disable NLA
15+
New-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name "UserAuthentication" -Value 0 -PropertyType DWORD -Force
16+
New-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name "SecurityLayer" -Value 1 -PropertyType DWORD -Force
17+
# Enable RDP through Windows Firewall
18+
Enable-NetFirewallRule -DisplayGroup "Remote Desktop"
19+
}
20+
21+
function Install-DevolutionsGateway {
22+
# Define the module name and version
23+
$moduleName = "DevolutionsGateway"
24+
$moduleVersion = "2024.1.5"
25+
26+
# Install the module with the specified version for all users
27+
# This requires administrator privileges
28+
try {
29+
# Install-PackageProvider is required for AWS. Need to set command to
30+
# terminate on failure so that try/catch actually triggers
31+
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -ErrorAction Stop
32+
Install-Module -Name $moduleName -RequiredVersion $moduleVersion -Force
33+
}
34+
catch {
35+
# If the first command failed, assume that we're on GCP and run
36+
# Install-Module only
37+
Install-Module -Name $moduleName -RequiredVersion $moduleVersion -Force
38+
}
39+
40+
# Construct the module path for system-wide installation
41+
$moduleBasePath = "C:\Windows\system32\config\systemprofile\Documents\PowerShell\Modules\$moduleName\$moduleVersion"
42+
$modulePath = Join-Path -Path $moduleBasePath -ChildPath "$moduleName.psd1"
43+
44+
# Import the module using the full path
45+
Import-Module $modulePath
46+
Install-DGatewayPackage
47+
48+
# Configure Devolutions Gateway
49+
$Hostname = "localhost"
50+
$HttpListener = New-DGatewayListener 'http://*:7171' 'http://*:7171'
51+
$WebApp = New-DGatewayWebAppConfig -Enabled $true -Authentication None
52+
$ConfigParams = @{
53+
Hostname = $Hostname
54+
Listeners = @($HttpListener)
55+
WebApp = $WebApp
56+
}
57+
Set-DGatewayConfig @ConfigParams
58+
New-DGatewayProvisionerKeyPair -Force
59+
60+
# Configure and start the Windows service
61+
Set-Service 'DevolutionsGateway' -StartupType 'Automatic'
62+
Start-Service 'DevolutionsGateway'
63+
}
64+
65+
function Patch-Devolutions-HTML {
66+
$root = "C:\Program Files\Devolutions\Gateway\webapp\client"
67+
$devolutionsHtml = "$root\index.html"
68+
$patch = '<script defer id="coder-patch" src="coder.js"></script>'
69+
70+
# Always copy the file in case we change it.
71+
@'
72+
${templatefile("${path.module}/devolutions-patch.js", {
73+
CODER_USERNAME : var.admin_username,
74+
CODER_PASSWORD : var.admin_password,
75+
})}
76+
'@ | Set-Content "$root\coder.js"
77+
78+
# Only inject the src if we have not before.
79+
$isPatched = Select-String -Path "$devolutionsHtml" -Pattern "$patch" -SimpleMatch
80+
if ($isPatched -eq $null) {
81+
(Get-Content $devolutionsHtml).Replace('</app-root>', "</app-root>$patch") | Set-Content $devolutionsHtml
82+
}
83+
}
84+
85+
Set-AdminPassword -adminPassword "${var.admin_password}"
86+
Configure-RDP
87+
Install-DevolutionsGateway
88+
Patch-Devolutions-HTML

0 commit comments

Comments
 (0)