ContainerInfo/Get-NavContainerDebugInfo.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
<#
 .Synopsis
  Extract metadata from a container to be able to give more specific information when reporting an issue on GitHub.
 .Description
  Returns the debugging info for a container when reporting an issue on GitHUb
 .Parameter containerName
  Name of the container for which you want to get the debugging info
 .Parameter ExcludeEnvVars
  Add this switch if you want to exclude environment variables in the debugging info
 .Parameter ExcludeDockerInfo
  Add this switch if you want to exclude docker info in the debugging info
 .Parameter ExcludeDockerLogs
  Add this switch if you want to exclude docker logs in the debugging info
 .Parameter includeSensitiveInformation
  Add this switch to include sensitive information in the debugging info (passwords and urls)
 .Parameter CopyToClipboard
  Add this switch if you want the debugging info to automatically be added to the clipboard
 .Example
  Get-NavContainerDebugInfo -containerName navserver -includeSensitiveInformation
  Get all debug information including sensitive information
  .Example
  Get-NavContainerDebugInfo -containerName navserver -ExcludeDockerInfo
  Get debug information excluding sensitive information and excluding docker info
  .Example
  Get-NavContainerDebugInfo -containerName navserver -ExcludeEnvVars -ExcludeDockerInfo -ExcludeDockerLogs -CopyToClipboard
  Get basic debug information and copy it into the clipboard.
#>

function Get-NavContainerDebugInfo {
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)]
        [string]$containerName,
        [Parameter()]
        [switch]$ExcludeEnvVars,
        [Parameter()]
        [switch]$ExcludeDockerInfo,
        [Parameter()]
        [switch]$ExcludeDockerLogs,
        [Parameter()]
        $ExcludePing,
        [Parameter()]
        [switch]$IncludeSensitiveInformation,
        [Parameter()]
        [switch]$CopyToClipboard
    )

    Process {
        $debugInfo = @{}
        $inspect = docker inspect $containerName | ConvertFrom-Json

        if ($inspect.Config.Labels.psobject.Properties.Match('nav').Count -eq 0) {
            throw "Container $containerName is not a NAV container"
        }
        
        $debugInfo.Add('container.labels', $inspect.Config.Labels)
        
        if (!$ExcludeEnvVars) {
            $envs = $inspect.Config.Env.GetEnumerator() | ForEach-Object {
                if ($includeSensitiveInformation) {
                    $_
                } elseif ($_.ToLowerInvariant().Contains("password=") -or 
                          $_.ToLowerInvariant().Startswith("navdvdurl=") -or 
                          $_.ToLowerInvariant().Startswith("countryurl=") -or 
                          $_.ToLowerInvariant().Startswith("licensefile=") -or 
                          $_.ToLowerInvariant().Startswith("bakfile=") -or 
                          $_.ToLowerInvariant().Startswith("folders=") -or 
                          $_.ToLowerInvariant().Startswith("vsixurl=")) {
                    ($_.Split("=")[0] + "=<specified>")
                } else {
                    $_
                }
            }
            if ($envs) {
                $debugInfo.Add('container.env', $envs)
            }
        }

        if (!$ExcludeDockerInfo) {
            $dockerInfo = docker info
            if ($dockerInfo) {
                $debugInfo.Add('docker.info', $dockerInfo)
            }
        }

        if (!$ExcludeDockerLogs) {
            $logs = docker logs $containerName
            if ($logs) {
                $debugInfo.Add('container.logs', $logs)
            }
        }

        if (!$ExcludePing) {
            $ping = ping $containerName
            if ($ping) {
                $debugInfo.Add('container.ping', $ping)
            }
        }

        $debugInfoJson = ConvertTo-Json $debugInfo

        if ($CopyToClipboard) {
            $debugInfoJson | Set-Clipboard
        }
        
        return $debugInfoJson
    }
}
Export-ModuleMember -function Get-NavContainerDebugInfo