forked from microsoft/navcontainerhelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelperFunctions.ps1
182 lines (163 loc) · 5.96 KB
/
HelperFunctions.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
function Log([string]$line, [string]$color = "Gray") {
Write-Host -ForegroundColor $color $line
}
function Get-DefaultCredential {
Param(
[string]$Message,
[string]$DefaultUserName,
[switch]$doNotAskForCredential
)
if (Test-Path "$hostHelperFolder\settings.ps1") {
. "$hostHelperFolder\settings.ps1"
if (Test-Path "$hostHelperFolder\aes.key") {
$key = Get-Content -Path "$hostHelperFolder\aes.key"
New-Object System.Management.Automation.PSCredential ($DefaultUserName, (ConvertTo-SecureString -String $adminPassword -Key $key))
} else {
New-Object System.Management.Automation.PSCredential ($DefaultUserName, (ConvertTo-SecureString -String $adminPassword))
}
} else {
if (!$doNotAskForCredential) {
Get-Credential -username $DefaultUserName -Message $Message
}
}
}
function Get-DefaultSqlCredential {
Param(
[Parameter(Mandatory=$true)]
[string]$containerName,
[System.Management.Automation.PSCredential]$sqlCredential = $null,
[switch]$doNotAskForCredential
)
if ($sqlCredential -eq $null) {
$containerAuth = Get-NavContainerAuth -containerName $containerName
if ($containerAuth -ne "Windows") {
$sqlCredential = Get-DefaultCredential -DefaultUserName "" -Message "Please enter the SQL Server System Admin credentials for container $containerName" -doNotAskForCredential:$doNotAskForCredential
}
}
$sqlCredential
}
function DockerDo {
Param(
[Parameter(Mandatory=$true)]
[string]$imageName,
[ValidateSet('run','start','pull','restart','stop')]
[string]$command = "run",
[switch]$accept_eula,
[switch]$accept_outdated,
[switch]$detach,
[switch]$silent,
[string[]]$parameters = @()
)
if ($accept_eula) {
$parameters += "--env accept_eula=Y"
}
if ($accept_outdated) {
$parameters += "--env accept_outdated=Y"
}
if ($detach) {
$parameters += "--detach"
}
$result = $true
$arguments = ("$command "+[string]::Join(" ", $parameters)+" $imageName")
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "docker.exe"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = $arguments
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$outtask = $null
$errtask = $p.StandardError.ReadToEndAsync()
$out = ""
$err = ""
do {
if ($outtask -eq $null) {
$outtask = $p.StandardOutput.ReadLineAsync()
}
$outtask.Wait(100) | Out-Null
if ($outtask.IsCompleted) {
$outStr = $outtask.Result
if ($outStr -eq $null) {
break
}
if (!$silent) {
Write-Host $outStr
}
$out += $outStr
$outtask = $null
if ($outStr.StartsWith("Please login")) {
$registry = $imageName.Split("/")[0]
if ($registry -eq "bcinsider.azurecr.io") {
throw "You need to login to $registry prior to pulling images. Get credentials through the ReadyToGo program on Microsoft Collaborate."
} else {
throw "You need to login to $registry prior to pulling images."
}
}
} elseif ($outtask.IsCanceled) {
break
} elseif ($outtask.IsFaulted) {
break
}
} while(!($p.HasExited))
$err = $errtask.Result
$p.WaitForExit();
if ($p.ExitCode -ne 0) {
$result = $false
if (!$silent) {
$out = $out.Trim()
$err = $err.Trim()
if ($command -eq "run" -and "$out" -ne "") {
Docker rm $out -f
}
$errorMessage = ""
if ("$err" -ne "") {
$errorMessage += "$err`r`n"
if ($err.Contains("authentication required")) {
$registry = $imageName.Split("/")[0]
if ($registry -eq "bcinsider.azurecr.io") {
$errorMessage += "You need to login to $registry prior to pulling images. Get credentials through the ReadyToGo program on Microsoft Collaborate.`r`n"
} else {
$errorMessage += "You need to login to $registry prior to pulling images.`r`n"
}
}
}
$errorMessage += "ExitCode: "+$p.ExitCode + "`r`nCommandline: docker $arguments"
Write-Error -Message $errorMessage
}
}
$result
}
function Get-NavContainerAuth {
Param
(
[string]$containerName
)
Invoke-ScriptInNavContainer -containerName $containerName -ScriptBlock {
$customConfigFile = Join-Path (Get-Item "C:\Program Files\Microsoft Dynamics NAV\*\Service").FullName "CustomSettings.config"
[xml]$customConfig = [System.IO.File]::ReadAllText($customConfigFile)
$customConfig.SelectSingleNode("//appSettings/add[@key='ClientServicesCredentialType']").Value
}
}
function Check-NavContainerName {
Param
(
[string]$containerName = ""
)
if ($containerName -eq "") {
throw "Container name cannot be empty"
}
if ($containerName.Length -gt 15) {
throw "Container name should not exceed 15 characters"
}
$first = $containerName.ToLowerInvariant()[0]
if ($first -lt "a" -or $first -gt "z") {
throw "Container name should start with a letter (a-z)"
}
$containerName.ToLowerInvariant().ToCharArray() | ForEach-Object {
if (($_ -lt "a" -or $_ -gt "z") -and ($_ -lt "0" -or $_ -gt "9") -and ($_ -ne "-")) {
throw "Container name contains invalid characters. Allowed characters are letters (a-z), numbers (0-9) and dashes (-)"
}
}
}