@@ -61,15 +61,56 @@ resource "aws_instance" "member_server" {
61
61
62
62
user_data = << EOF
63
63
<powershell>
64
+ # set variables for retry loops
65
+ $timeout = 300
66
+ $interval = 30
67
+
64
68
# Set up SSH so we can remotely manage the instance
65
69
## Install OpenSSH Server and Client
66
- Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
67
- Set-Service -Name sshd -StartupType 'Automatic'
68
- Start-Service sshd
69
-
70
- Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
71
- Set-Service -Name ssh-agent -StartupType Automatic
72
- Start-Service ssh-agent
70
+ # Loop to make sure that SSH installs correctly
71
+ $elapsed = 0
72
+ do {
73
+ try {
74
+ Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
75
+ Set-Service -Name sshd -StartupType 'Automatic'
76
+ Start-Service sshd
77
+ $result = Get-Process -Name "sshd" -ErrorAction SilentlyContinue
78
+ if ($result) {
79
+ Write-Host "Successfully added and started openSSH server"
80
+ break
81
+ }
82
+ } catch {
83
+ Write-Host "SSH server was not installed, retrying"
84
+ Start-Sleep -Seconds $interval
85
+ $elapsed += $interval
86
+ }
87
+ if ($elapsed -ge $timeout) {
88
+ Write-Host "SSH server installation failed after 5 minutes. Exiting."
89
+ exit 1
90
+ }
91
+ } while ($true)
92
+
93
+ $elapsed = 0
94
+ do {
95
+ try {
96
+ Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
97
+ Set-Service -Name ssh-agent -StartupType Automatic
98
+ Start-Service ssh-agent
99
+ $result = Get-Process -Name "ssh-agent" -ErrorAction SilentlyContinue
100
+ if ($result) {
101
+ Write-Host "Successfully added and started openSSH agent"
102
+ break
103
+ }
104
+ } catch {
105
+ Write-Host "SSH server was not installed, retrying"
106
+ Start-Sleep -Seconds $interval
107
+ $elapsed += $interval
108
+ }
109
+ if ($elapsed -ge $timeout) {
110
+ Write-Host "SSH server installation failed after 5 minutes. Exiting."
111
+ exit 1
112
+ }
113
+ } while ($true)
73
114
74
115
## Set PowerShell as the default SSH shell
75
116
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value (Get-Command powershell.exe).Path -PropertyType String -Force
@@ -91,45 +132,68 @@ resource "aws_instance" "member_server" {
91
132
92
133
# Adds member server to the domain
93
134
[int]$intix = Get-NetAdapter | % { Process { If ( $_.Status -eq "up" ) { $_.ifIndex } }}
94
- Set-DNSClientServerAddress -interfaceIndex $intix -ServerAddresses ("${ var . domain_controller_ip } ","127.0.0.1")
95
- $here_string_password = @'
135
+ Set-DNSClientServerAddress -interfaceIndex $intix -ServerAddresses ("${ var . domain_controller_ip } ","127.0.0.1")
136
+ $here_string_password = @'
96
137
${ var . domain_admin_password }
97
138
'@
98
- $password = ConvertTo-SecureString $here_string_password -AsPlainText -Force
99
- $username = "${ local . domain_sld } \Administrator"
100
- $credential = New-Object System.Management.Automation.PSCredential($username,$password)
101
-
102
- # check that domain can be reached
103
- $timeout = 300
104
- $interval = 10
105
- $elapsed = 0
106
-
107
- do {
108
- try {
109
- $result = Resolve-DnsName -Name "${ var . active_directory_domain } " -Server "${ var . domain_controller_ip } " -ErrorAction Stop
110
- if ($result) {
111
- Write-Host "DNS resolved successfully."
112
- break
113
- }
114
- } catch {
115
- Write-Host "DNS not resolved yet. Retrying in $interval seconds..."
116
- Start-Sleep -Seconds $interval
117
- $elapsed += $interval
118
- }
119
- if ($elapsed -ge $timeout) {
120
- Write-Host "DNS resolution failed after 5 minutes. Exiting."
121
- exit 1
122
- }
123
- } while ($true)
124
-
125
- # add computer to domain
126
- Add-Computer -DomainName "${ var . active_directory_domain } " -Credential $credential
127
-
128
- # Enable audio
129
- Set-Service -Name "Audiosrv" -StartupType Automatic
130
- Start-Service -Name "Audiosrv"
131
-
132
- Restart-Computer -Force
139
+ $password = ConvertTo-SecureString $here_string_password -AsPlainText -Force
140
+ $username = "${ local . domain_sld } \Administrator"
141
+ $credential = New-Object System.Management.Automation.PSCredential($username,$password)
142
+
143
+ # check that domain can be reached
144
+ $timeout = 300
145
+ $interval = 10
146
+ $elapsed = 0
147
+
148
+ # check that domain can be reached
149
+ do {
150
+ try {
151
+ Resolve-DnsName -Name "${ var . active_directory_domain } " -Server "${ var . domain_controller_ip } " -ErrorAction Stop
152
+ Write-Host "resolved domain successfully."
153
+ break
154
+ } catch {
155
+ Write-Host "Could not resolve domain. Retrying in $interval seconds..."
156
+ Start-Sleep -Seconds $interval
157
+ $elapsed += $interval
158
+ }
159
+ if ($elapsed -ge $timeout) {
160
+ Write-Host "Resovling domain after 5 minutes. Exiting."
161
+ exit 1
162
+ }
163
+ } while ($true)
164
+
165
+ #logging to troubleshoot domain issues
166
+ Resolve-DnsName -Name "${ var . active_directory_domain } " -Server "${ var . domain_controller_ip } " -ErrorAction SilentlyContinue
167
+ Get-Service -Name LanmanWorkstation, Netlogon, RpcSs | Select-Object Name, DisplayName, Status
168
+
169
+ # Add computer to domain
170
+ $elapsed = 0
171
+ do {
172
+ try {
173
+ Add-Computer -DomainName "${ var . active_directory_domain } " -Credential $credential
174
+ $result = (Get-WmiObject Win32_ComputerSystem).Domain
175
+ if ($result -ne "WORKGROUP") {
176
+ Write-Host "Added to domain successfully."
177
+ break
178
+ }
179
+ } catch {
180
+ Write-Host "Could not add to domain. Retrying in $interval seconds..."
181
+ Start-Sleep -Seconds $interval
182
+ $elapsed += $interval
183
+ }
184
+ if ($elapsed -ge $timeout) {
185
+ Write-Host "Adding to domain after 5 minutes. Exiting."
186
+ exit 1
187
+ }
188
+ } while ($true)
189
+ # Logging to determine domain and ssh state for debugging
190
+ (Get-WmiObject Win32_ComputerSystem).Domain
191
+ Get-Process -Name *ssh* -ErrorAction SilentlyContinue
192
+
193
+ # Enable audio
194
+ Set-Service -Name "Audiosrv" -StartupType Automatic
195
+ Start-Service -Name "Audiosrv"
196
+ Restart-Computer -Force
133
197
</powershell>
134
198
EOF
135
199
0 commit comments