Private/Tools.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
function Start-MonocleSleepWhileBusy
{
    [CmdletBinding()]
    param ()

    $count = 0
    $timeout = Get-MonocleTimeout

    while ((Invoke-MonocleJavaScript -Script 'return document.readyState') -ine 'complete')
    {
        if ($count -ge $timeout) {
            throw "Loading URL has timed-out after $timeout second(s)"
        }

        Start-Sleep -Seconds 1
        $count++
    }

    if ($count -gt 0) {
        Write-MonocleHost -Message "Browser busy for $count seconds(s)"
    }
}

function Invoke-MonocleDownloadImage
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string]
        $Source,

        [Parameter(Mandatory=$true)]
        [string]
        $Path
    )

    Write-Verbose -Message "Downloading '$Source' to '$Path'"

    if ($PSVersionTable.PSVersion.Major -le 5) {
        Invoke-WebRequest -Uri $Source -OutFile $Path -UseBasicParsing | Out-Null
    }
    else {
        Invoke-WebRequest -Uri $Source -OutFile $Path | Out-Null
    }

    if (!$?) {
        throw 'Failed to download image'
    }
}

function Write-MonocleHost
{
    [CmdletBinding()]
    param (
        [Parameter()]
        $Message,

        [switch]
        $Backdent,

        [switch]
        $NoIndent
    )

    if ($NoIndent) {
        Write-Host -Object $Message
    }
    else {
        $Depth = [int]$env:MONOCLE_OUTPUT_DEPTH
        if ($Backdent) {
            $Depth--
        }

        Write-Host -Object "$('-' * $Depth)> $Message"
    }
}

function Test-MonocleUrl
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [ValidateNotNull()]
        [string]
        $Url
    )

    # truncate the URL of any query parameters
    $Url = ([System.Uri]$Url).GetLeftPart([System.UriPartial]::Path)

    # initial code setting as success
    $code = 200
    $timeout = Get-MonocleTimeout
    $message = [string]::Empty

    $count = 1
    while ($count -le $timeout) {
        try {
            Write-MonocleHost -Message "Testing: $url [attempt: $($count)]"

            if ($PSVersionTable.PSVersion.Major -le 5) {
                $result = Invoke-WebRequest -Uri $Url -TimeoutSec $timeout -UseBasicParsing -ErrorAction Stop
            }
            else {
                $result = Invoke-WebRequest -Uri $Url -TimeoutSec $timeout -ErrorAction Stop
            }

            $code = [int]$result.StatusCode
            $message = $result.StatusDescription
        }
        catch [System.Net.WebException] {
            $ex = $_.Exception

            # if the exception doesn't contain a Response, then either the
            # host doesn't exist, there were SSL issues, or something else went wrong
            if ($null -eq $ex.Response) {
                $code = -1
                $message = $ex.Message
            }
            else {
                $code = [int]$ex.Response.StatusCode.Value__
                $message = $ex.Response.StatusDescription
            }
        }
        catch {
            $code = -1
            $message = $_.Exception.Message
        }

        if (($code -eq -1) -or ($code -ge 400)) {
            $count++
            if ($count -gt $timeout) {
                break
            }

            Start-Sleep -Seconds 1
        }
        else {
            break
        }
    }

    # anything that is 1xx-2xx is normally successful, anything that's
    # 400+ is normally always a failure to load
    # -1 is a fatal error (SSL, invalid host, etc)
    if (($code -eq -1) -or ($code -ge 400)) {
        throw "Failed to load URL: '$Url'`nStatus: $code`nMessage: $message"
    }

    return $code
}

function Get-MonocleCustomDriverPath
{
    return './custom_drivers'
}

function Add-MonocleOutputDepth
{
    $env:MONOCLE_OUTPUT_DEPTH = [string](([int]$env:MONOCLE_OUTPUT_DEPTH) + 1)
}

function Remove-MonocleOutputDepth
{
    $env:MONOCLE_OUTPUT_DEPTH = [string](([int]$env:MONOCLE_OUTPUT_DEPTH) - 1)
}