-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathnavstart.ps1
310 lines (265 loc) · 11 KB
/
navstart.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
Write-Host "Initializing..."
$startTime = [DateTime]::UtcNow
$runPath = "c:\Run"
$myPath = Join-Path $runPath "my"
$navDvdPath = "C:\NAVDVD"
$publicDnsNameFile = "$RunPath\PublicDnsName.txt"
$startCountFile = "$RunPath\StartCount.txt"
$restartingInstance = Test-Path -Path $publicDnsNameFile -PathType Leaf
function Get-MyFilePath([string]$FileName)
{
if ((Test-Path $myPath -PathType Container) -and (Test-Path (Join-Path $myPath $FileName) -PathType Leaf)) {
(Join-Path $myPath $FileName)
} else {
(Join-Path $runPath $FileName)
}
}
$hostname = hostname
. (Get-MyFilePath "ServiceSettings.ps1")
. (Get-MyFilePath "HelperFunctions.ps1")
. (Get-MyFilePath "SetupVariables.ps1")
$newPublicDnsName = $true
if ($restartingInstance) {
Write-Host "Restarting Container"
$prevPublicDnsName = Get-Content -Path $publicDnsNameFile
if ($prevPublicDnsName -eq $publicDnsName) {
$newPublicDnsName = $false
Write-Host "PublicDnsName unchanged"
}
else {
Write-Host "PublicDnsName was changed"
}
} else {
Write-Host "Starting Container"
}
$startCount = 0
if (Test-Path $startCountFile -PathType Leaf) {
if (![int]::TryParse((Get-Content -Path $startCountFile), [ref]$startCount)) {
$startCount = 0
}
}
$startCount++
if ($startCount -gt 1) {
Write-Host "Restart count $($startCount-1)"
if ($startCount -gt 3) {
throw "Error starting container"
}
}
Set-Content -Path $startCountFile -Value "$startCount"
$applicationInsightsInstrumentationKeyFile = Get-MyFilePath "applicationInsightsInstrumentationKey.txt"
if (Test-Path $applicationInsightsInstrumentationKeyFile) {
$applicationInsightsInstrumentationKey = Get-Content $applicationInsightsInstrumentationKeyFile
}
elseif ($applicationInsightsInstrumentationKey) {
Set-Content -Path $applicationInsightsInstrumentationKeyFile -Value $applicationInsightsInstrumentationKey
}
Write-Host "Hostname is $hostname"
Write-Host "PublicDnsName is $publicDnsName"
if ($Accept_eula -ne "Y")
{
Write-Error "You must accept the End User License Agreement before this container can start.
Use Docker inspect to locate the Url for the EULA under Labels/legal.
set the environment variable ACCEPT_EULA to 'Y' if you accept the agreement."
exit 1
}
if (!$restartingInstance) {
$containerAge = [System.DateTime]::Now.Subtract((Get-Item "C:\RUN").CreationTime).Days
if ($containerAge -gt 90) {
if ($Accept_outdated -ne "Y") {
Write-Error "You are trying to run a container which is more than 90 days old.
Microsoft recommends that you always run the latest version of our containers.
Set the environment variable ACCEPT_OUTDATED to 'Y' if you want to run this container anyway."
exit 1
}
}
if (!(Resolve-DnsName -Name download.microsoft.com -erroraction Ignore -Type CNAME)) {
Write-Host "WARNING: DNS resolution not working from within the container."
}
$timeZoneId = (Get-TimeZone).Id
$timeZone = Get-TimeZone -ListAvailable | Where-Object { $_.Id -eq $timeZoneId }
if (!($timeZone)) {
Write-Host "WARNING: Container starts with TimeZone = $timeZoneId, which is not recognized in the list of TimeZones."
}
}
Write-Host "Using $auth Authentication"
$usingLocalSQLServer = ($databaseServer -eq "localhost")
if ($usingLocalSQLServer) {
if ((Get-Service -name $SqlServiceName).Status -ne "Running") {
# start the SQL Server
Write-Host "Starting Local SQL Server"
Start-Service -Name $SqlBrowserServiceName
Start-Service -Name $SqlWriterServiceName
Start-Service -Name $SqlServiceName
}
}
if (($webClient -ne "N") -or ($httpSite -ne "N")) {
if ((Get-Service -name $IisServiceName).Status -ne "Running") {
# start IIS services
Write-Host "Starting Internet Information Server"
Start-Service -name $IisServiceName
}
}
$serviceTierFolder = (Get-Item "C:\Program Files\Microsoft Dynamics NAV\*\Service").FullName
$roleTailoredClientItem = Get-Item "C:\Program Files (x86)\Microsoft Dynamics NAV\*\RoleTailored Client" -ErrorAction Ignore
if ($roleTailoredClientItem) {
$roleTailoredClientFolder = $roleTailoredClientItem.FullName
}
else {
$roleTailoredClientFolder = ""
}
$WebClientFolder = (Get-Item "C:\Program Files\Microsoft Dynamics NAV\*\Web Client")[0]
$NAVAdministrationScriptsFolder = "$runPath\NAVAdministration"
if (!(Test-Path $NAVAdministrationScriptsFolder)) { $NAVAdministrationScriptsFolder = '' }
$CustomConfigFile = Join-Path $serviceTierFolder "CustomSettings.config"
$CustomConfig = [xml](Get-Content $CustomConfigFile)
$serverInstance = $CustomConfig.SelectSingleNode("//appSettings/add[@key='ServerInstance']").Value
if (Test-Path "$serviceTierFolder\Microsoft.Dynamics.Nav.Management.psm1") {
Import-Module "$serviceTierFolder\Microsoft.Dynamics.Nav.Management.psm1" -wa SilentlyContinue
} else {
Import-Module "$serviceTierFolder\Microsoft.Dynamics.Nav.Management.dll" -wa SilentlyContinue
}
# Setup Database Connection
. (Get-MyFilePath "SetupDatabase.ps1")
$usingLocalSQLServer = ($databaseServer -eq "localhost")
if (!$usingLocalSQLServer) {
if ((Get-service -name $SqlServiceName).Status -eq 'Running') {
Write-Host "Stopping local SQL Server"
Stop-Service -Name $SqlServiceName -ErrorAction Ignore
Stop-Service -Name $SqlWriterServiceName -ErrorAction Ignore
Stop-Service -Name $SqlBrowserServiceName -ErrorAction Ignore
}
}
if ($newPublicDnsName) {
# Certificate
if ($navUseSSL -or $servicesUseSSL) {
. (Get-MyFilePath "SetupCertificate.ps1")
}
. (Get-MyFilePath "SetupConfiguration.ps1")
}
else {
$CustomConfigFile = Join-Path $ServiceTierFolder "CustomSettings.config"
$CustomConfig = [xml](Get-Content $CustomConfigFile)
$publicWebBaseUrl = $CustomConfig.SelectSingleNode("//appSettings/add[@key='PublicWebBaseUrl']").Value
}
if (!$restartingInstance) {
. (Get-MyFilePath "SetupAddIns.ps1")
}
$service = Get-Service -name $NavServiceName -ErrorAction Ignore
if (!($service)) {
Write-Error "Service Tier doesn't exist / is not installed"
} elseif ($service.Status -ne "Running") {
Write-Host "Starting Service Tier"
Start-Service -Name $NavServiceName -WarningAction Ignore
} else {
Write-Host "Restarting Service Tier"
Restart-Service -Name $NavServiceName -WarningAction Ignore
}
if (!$restartingInstance -and $bakfile -ne "" -and !$multitenant) {
Sync-NavTenant -ServerInstance $serverInstance -Force
}
$wwwRootPath = Get-WWWRootPath
$httpPath = Join-Path $wwwRootPath "http"
if ($newPublicDnsName -and $webClient -ne "N") {
try {
. (Get-MyFilePath "SetupWebClient.ps1")
}
catch {
Write-Host "WARNING: SetupWebClient failed, retrying..."
. (Get-MyFilePath "SetupWebClient.ps1")
}
. (Get-MyFilePath "SetupWebConfiguration.ps1")
}
. (Get-MyFilePath "SetupLicense.ps1")
if ($multitenant) {
. (Get-MyFilePath "SetupTenant.ps1")
}
if (!$restartingInstance) {
if ($httpSite -ne "N") {
Write-Host "Creating http download site"
New-Item -Path $httpPath -ItemType Directory | Out-Null
New-Website -Name http -Port $fileSharePort -PhysicalPath $httpPath | Out-Null
$webConfigFile = Join-Path $httpPath "web.config"
Copy-Item -Path (Join-Path $runPath "web.config") -Destination $webConfigFile
get-item -Path $webConfigFile | % { $_.Attributes = "Hidden" }
. (Get-MyFilePath "SetupFileShare.ps1")
}
. (Get-MyFilePath "SetupWindowsUsers.ps1")
if ($usingLocalSQLServer) {
. (Get-MyFilePath "SetupSqlUsers.ps1")
. (Get-MyFilePath "SetupNavUsers.ps1")
}
}
if ($newPublicDnsName -and $httpSite -ne "N" -and $clickOnce -eq "Y") {
$setupClickOnceScript = Get-MyFilePath "SetupClickOnce.ps1"
if (Test-Path $setupClickOnceScript) {
Write-Host "Creating ClickOnce Manifest"
$clickOnceInstallerToolsFolder = (Get-Item "C:\Program Files (x86)\Microsoft Dynamics NAV\*\ClickOnce Installer Tools").FullName
. $setupClickOnceScript
} else
{
Write-Host "Skipping clickOnce, ClickOnce is not supported by this version"
$ClickOnce = "N"
}
}
Set-Content -Path $publicDnsNameFile -Value $publicDnsName
Set-Content -Path $startCountFile -Value "0"
. (Get-MyFilePath "AdditionalSetup.ps1")
$CustomConfigFile = Join-Path $ServiceTierFolder "CustomSettings.config"
$CustomConfig = [xml](Get-Content $CustomConfigFile)
$ip = "127.0.0.1"
$ips = Get-NetIPAddress | Where-Object { $_.AddressFamily -eq "IPv4" -and $_.IPAddress -ne "127.0.0.1" }
if ($ips) {
$ips | ForEach-Object {
if ($ip -eq "127.0.0.1") {
$ip = $_.IPAddress
}
Write-Host "Container IP Address: $($_.IPAddress)"
}
} else {
Write-Host "Container IP Address: UNKNOWN"
}
Write-Host "Container Hostname : $hostname"
Write-Host "Container Dns Name : $publicDnsName"
if ($webClient -ne "N") {
Write-Host "Web Client : $publicWebBaseUrl$webTenantParam"
}
if ($auth -ne "Windows" -and $usingLocalSQLServer -and !$passwordSpecified -and !$restartingInstance) {
Write-Host "Admin Username : $username"
Write-Host ("Admin Password : "+[System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)))
}
if ($httpSite -ne "N") {
if (Test-Path -Path (Join-Path $httpPath "*.vsix")) {
Write-Host "Dev. Server : $protocol$publicDnsName"
Write-Host "Dev. ServerInstance : $serverInstance"
if ($multitenant) {
Write-Host "Dev. Server Tenant : $tenantId"
}
}
if ($clickOnce -eq "Y") {
Write-Host "ClickOnce Manifest : $clickOnceWebSiteUrl"
}
}
. (Get-MyFilePath "AdditionalOutput.ps1")
Write-Host
if ($httpSite -ne "N") {
Write-Host "Files:"
Get-ChildItem -Path $httpPath -file | % {
Write-Host "http://${publicDnsName}:$publicFileSharePort/$($_.Name)"
}
Write-Host
}
if ($containerAge -gt 60) {
Write-Host "WARNING: You are running a container which is $containerAge days old.
Microsoft recommends that you always run the latest version of our containers."
Write-Host
}
if ("$securepassword") {
Clear-Variable -Name "securePassword"
}
$cimInstance = Get-CIMInstance Win32_OperatingSystem
Write-Host "Container Total Physical Memory is $(($cimInstance.TotalVisibleMemorySize/1024/1024).ToString('F1',[CultureInfo]::InvariantCulture))Gb"
Write-Host "Container Free Physical Memory is $(($cimInstance.FreePhysicalMemory/1024/1024).ToString('F1',[CultureInfo]::InvariantCulture))Gb"
Write-Host
$timespend = [Math]::Round([DateTime]::UtcNow.Subtract($startTime).Totalseconds)
Write-Host "Initialization took $timespend seconds"
Write-Host "Ready for connections!"