@@ -34,99 +34,12 @@ resource "coder_script" "windows-rdp" {
34
34
agent_id = var. agent_id
35
35
display_name = " windows-rdp"
36
36
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
+ })
128
41
129
- run_on_start = true
42
+ run_on_start = true
130
43
}
131
44
132
45
resource "coder_app" "windows-rdp" {
0 commit comments