|
| 1 | +if ($isWindows) { |
| 2 | + $programDataFolder = 'C:\ProgramData\BcContainerHelper' |
| 3 | + $artifactsCacheFolder = "c:\bcartifacts.cache" |
| 4 | +} |
| 5 | +else { |
| 6 | + $programDataFolder = "/home/$myUsername/.bccontainerhelper" |
| 7 | + $artifactsCacheFolder = "/home/$myUsername/.bcartifacts.cache" |
| 8 | +} |
| 9 | + |
| 10 | +function Get-ContainerHelperConfig { |
| 11 | + if (!((Get-Variable -scope Script bcContainerHelperConfig -ErrorAction SilentlyContinue) -and $bcContainerHelperConfig)) { |
| 12 | + Set-Variable -scope Script -Name bcContainerHelperConfig -Value @{ |
| 13 | + "bcartifactsCacheFolder" = "" |
| 14 | + "genericImageName" = 'mcr.microsoft.com/businesscentral:{0}' |
| 15 | + "genericImageNameFilesOnly" = 'mcr.microsoft.com/businesscentral:{0}-filesonly' |
| 16 | + "usePsSession" = $isAdministrator # -and ("$ENV:GITHUB_ACTIONS" -ne "true") -and ("$ENV:TF_BUILD" -ne "true") |
| 17 | + "addTryCatchToScriptBlock" = $true |
| 18 | + "killPsSessionProcess" = $false |
| 19 | + "useVolumes" = $false |
| 20 | + "useVolumeForMyFolder" = $false |
| 21 | + "use7zipIfAvailable" = $true |
| 22 | + "defaultNewContainerParameters" = @{ } |
| 23 | + "hostHelperFolder" = "" |
| 24 | + "containerHelperFolder" = $programDataFolder |
| 25 | + "defaultContainerName" = "bcserver" |
| 26 | + "digestAlgorithm" = "SHA256" |
| 27 | + "timeStampServer" = "http://timestamp.digicert.com" |
| 28 | + "sandboxContainersAreMultitenantByDefault" = $true |
| 29 | + "useSharedEncryptionKeys" = $true |
| 30 | + "DOCKER_SCAN_SUGGEST" = $false |
| 31 | + "psSessionTimeout" = 0 |
| 32 | + "baseUrl" = "https://businesscentral.dynamics.com" |
| 33 | + "apiBaseUrl" = "https://api.businesscentral.dynamics.com" |
| 34 | + "mapCountryCode" = [PSCustomObject]@{ |
| 35 | + "ae" = "w1" |
| 36 | + "ar" = "w1" |
| 37 | + "bd" = "w1" |
| 38 | + "dz" = "w1" |
| 39 | + "cl" = "w1" |
| 40 | + "pr" = "w1" |
| 41 | + "eg" = "w1" |
| 42 | + "fo" = "dk" |
| 43 | + "gl" = "dk" |
| 44 | + "id" = "w1" |
| 45 | + "ke" = "w1" |
| 46 | + "lb" = "w1" |
| 47 | + "lk" = "w1" |
| 48 | + "lu" = "w1" |
| 49 | + "ma" = "w1" |
| 50 | + "mm" = "w1" |
| 51 | + "mt" = "w1" |
| 52 | + "my" = "w1" |
| 53 | + "ng" = "w1" |
| 54 | + "qa" = "w1" |
| 55 | + "sa" = "w1" |
| 56 | + "sg" = "w1" |
| 57 | + "tn" = "w1" |
| 58 | + "ua" = "w1" |
| 59 | + "za" = "w1" |
| 60 | + "ao" = "w1" |
| 61 | + "bh" = "w1" |
| 62 | + "ba" = "w1" |
| 63 | + "bw" = "w1" |
| 64 | + "cr" = "br" |
| 65 | + "cy" = "w1" |
| 66 | + "do" = "br" |
| 67 | + "ec" = "br" |
| 68 | + "sv" = "br" |
| 69 | + "gt" = "br" |
| 70 | + "hn" = "br" |
| 71 | + "jm" = "w1" |
| 72 | + "mv" = "w1" |
| 73 | + "mu" = "w1" |
| 74 | + "ni" = "br" |
| 75 | + "pa" = "br" |
| 76 | + "py" = "br" |
| 77 | + "tt" = "br" |
| 78 | + "uy" = "br" |
| 79 | + "zw" = "w1" |
| 80 | + } |
| 81 | + "mapNetworkSettings" = [PSCustomObject]@{ |
| 82 | + } |
| 83 | + "AddHostDnsServersToNatContainers" = $false |
| 84 | + "TraefikUseDnsNameAsHostName" = $false |
| 85 | + "TreatWarningsAsErrors" = @() |
| 86 | + "PartnerTelemetryConnectionString" = "" |
| 87 | + "MicrosoftTelemetryConnectionString" = "InstrumentationKey=5b44407e-9750-4a07-abe9-30c3b853821b;IngestionEndpoint=https://southcentralus-0.in.applicationinsights.azure.com/" |
| 88 | + "SendExtendedTelemetryToMicrosoft" = $false |
| 89 | + "TraefikImage" = "tobiasfenster/traefik-for-windows:v1.7.34" |
| 90 | + "ObjectIdForInternalUse" = 88123 |
| 91 | + "WinRmCredentials" = $null |
| 92 | + "WarningPreference" = "SilentlyContinue" |
| 93 | + "UseNewFormatForGetBcContainerAppInfo" = $false |
| 94 | + "NoOfSecondsToSleepAfterPublishBcContainerApp" = 1 |
| 95 | + "RenewClientContextBetweenTests" = $false |
| 96 | + } |
| 97 | + |
| 98 | + if ($isInsider) { |
| 99 | + $bcContainerHelperConfig.genericImageName = 'mcr.microsoft.com/businesscentral:{0}-dev' |
| 100 | + $bcContainerHelperConfig.genericImageNameFilesOnly = 'mcr.microsoft.com/businesscentral:{0}-filesonly-dev' |
| 101 | + } |
| 102 | + |
| 103 | + if ($bcContainerHelperConfigFile -notcontains (Join-Path $programDataFolder "BcContainerHelper.config.json")) { |
| 104 | + $bcContainerHelperConfigFile = @((Join-Path $programDataFolder "BcContainerHelper.config.json"))+$bcContainerHelperConfigFile |
| 105 | + } |
| 106 | + $bcContainerHelperConfigFile | ForEach-Object { |
| 107 | + $configFile = $_ |
| 108 | + if (Test-Path $configFile) { |
| 109 | + try { |
| 110 | + $savedConfig = Get-Content $configFile | ConvertFrom-Json |
| 111 | + if ("$savedConfig") { |
| 112 | + $keys = $bcContainerHelperConfig.Keys | % { $_ } |
| 113 | + $keys | ForEach-Object { |
| 114 | + if ($savedConfig.PSObject.Properties.Name -eq "$_") { |
| 115 | + if (!$silent) { |
| 116 | + Write-Host "Setting $_ = $($savedConfig."$_")" |
| 117 | + } |
| 118 | + $bcContainerHelperConfig."$_" = $savedConfig."$_" |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + catch { |
| 124 | + throw "Error reading configuration file $configFile, cannot import module." |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if ($isInsideContainer) { |
| 130 | + $bcContainerHelperConfig.usePsSession = $true |
| 131 | + try { |
| 132 | + $myinspect = docker inspect $(hostname) | ConvertFrom-Json |
| 133 | + $bcContainerHelperConfig.WinRmCredentials = New-Object PSCredential -ArgumentList 'WinRmUser', (ConvertTo-SecureString -string "P@ss$($myinspect.Id.SubString(48))" -AsPlainText -Force) |
| 134 | + } |
| 135 | + catch {} |
| 136 | + } |
| 137 | + |
| 138 | + if ($bcContainerHelperConfig.UseVolumes) { |
| 139 | + if ($bcContainerHelperConfig.bcartifactsCacheFolder -eq "") { |
| 140 | + $bcContainerHelperConfig.bcartifactsCacheFolder = "bcartifacts.cache" |
| 141 | + } |
| 142 | + if ($bcContainerHelperConfig.hostHelperFolder -eq "") { |
| 143 | + $bcContainerHelperConfig.hostHelperFolder = "hostHelperFolder" |
| 144 | + } |
| 145 | + $bcContainerHelperConfig.useVolumeForMyFolder = $false |
| 146 | + } |
| 147 | + else { |
| 148 | + if ($bcContainerHelperConfig.bcartifactsCacheFolder -eq "") { |
| 149 | + $bcContainerHelperConfig.bcartifactsCacheFolder = $artifactsCacheFolder |
| 150 | + } |
| 151 | + if ($bcContainerHelperConfig.hostHelperFolder -eq "") { |
| 152 | + $bcContainerHelperConfig.hostHelperFolder = $programDataFolder |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + Export-ModuleMember -Variable bcContainerHelperConfig |
| 157 | + } |
| 158 | + return $bcContainerHelperConfig |
| 159 | +} |
| 160 | + |
| 161 | +$configHelperFolder = $programDataFolder |
| 162 | +if (!(Test-Path $configHelperFolder)) { |
| 163 | + New-Item -Path $configHelperFolder -ItemType Container -Force | Out-Null |
| 164 | + if ($isWindows -and !$isAdministrator) { |
| 165 | + $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($myUsername,'FullControl', 3, 'InheritOnly', 'Allow') |
| 166 | + $acl = [System.IO.Directory]::GetAccessControl($configHelperFolder) |
| 167 | + $acl.AddAccessRule($rule) |
| 168 | + [System.IO.Directory]::SetAccessControl($configHelperFolder,$acl) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +Get-ContainerHelperConfig | Out-Null |
| 173 | + |
| 174 | +$telemetry = @{ |
| 175 | + "Assembly" = $null |
| 176 | + "PartnerClient" = $null |
| 177 | + "MicrosoftClient" = $null |
| 178 | + "CorrelationId" = "" |
| 179 | + "TopId" = "" |
| 180 | + "Debug" = $false |
| 181 | +} |
| 182 | +try { |
| 183 | + if (($bcContainerHelperConfig.MicrosoftTelemetryConnectionString) -and !$Silent) { |
| 184 | + Write-Host -ForegroundColor Green "BC.HelperFunctions emits usage statistics telemetry to Microsoft" |
| 185 | + } |
| 186 | + $dllPath = Join-Path $configHelperFolder 'Microsoft.ApplicationInsights.2.32.0.429.dll' |
| 187 | + if (-not (Test-Path $dllPath)) { |
| 188 | + Copy-Item (Join-Path $PSScriptRoot "Microsoft.ApplicationInsights.dll") -Destination $dllPath |
| 189 | + } |
| 190 | + $telemetry.Assembly = [System.Reflection.Assembly]::LoadFrom($dllPath) |
| 191 | +} catch { |
| 192 | + if (!$Silent) { |
| 193 | + Write-Host -ForegroundColor Yellow "Unable to load ApplicationInsights.dll" |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +. (Join-Path $PSScriptRoot "TelemetryHelper.ps1") |
| 198 | + |
| 199 | +# Telemetry functions |
| 200 | +Export-ModuleMember -Function RegisterTelemetryScope |
| 201 | +Export-ModuleMember -Function InitTelemetryScope |
| 202 | +Export-ModuleMember -Function AddTelemetryProperty |
| 203 | +Export-ModuleMember -Function TrackTrace |
| 204 | +Export-ModuleMember -Function TrackException |
| 205 | + |
| 206 | +# Common functions |
| 207 | +. (Join-Path $PSScriptRoot "Common\Download-File.ps1") |
| 208 | +. (Join-Path $PSScriptRoot "Common\New-DesktopShortcut.ps1") |
| 209 | +. (Join-Path $PSScriptRoot "Common\Remove-DesktopShortcut.ps1") |
| 210 | +. (Join-Path $PSScriptRoot "Common\ConvertTo-HashTable.ps1") |
| 211 | +. (Join-Path $PSScriptRoot "Common\Get-PlainText.ps1") |
| 212 | +. (Join-Path $PSScriptRoot "Common\Invoke-gh.ps1") |
| 213 | +. (Join-Path $PSScriptRoot "Common\Invoke-git.ps1") |
| 214 | +. (Join-Path $PSScriptRoot "Common\ConvertTo-OrderedDictionary.ps1") |
| 215 | + |
| 216 | +# BC Authentication helper functions |
| 217 | +. (Join-Path $PSScriptRoot "Auth\New-BcAuthContext.ps1") |
| 218 | +. (Join-Path $PSScriptRoot "Auth\Renew-BcAuthContext.ps1") |
0 commit comments