Private/Bootstrap.psm1

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
function Get-ServicePath {
    [CmdletBinding()]
    [OutputType([System.IO.FileInfo])]
    param (
        [Parameter(
            Position = 0,
            ValueFromPipelineByPropertyName = $true
        )]
        [string[]]
        $Name,

        [Parameter()]
        [string]
        $Filter = 'Name like "{0}"'
    )

    begin {
        $Query = $null
        $Names = @()
    }

    process {
        if ($input) {
            $Names += $Name
        }
        elseif ($Name) {
            $Query = $Filter -f ($Name -replace '\*', '%')
            Write-Verbose "WQL = $Query"
        }
    }

    end {
        $Services = @(Get-CimInstance -ClassName Win32_Service -Property PathName -Filter $Query)
        Write-Verbose "Found $($Services.Count) services"
        if ($Names.Count) {
            $Services = @($Services | Where-Object Name -in $Names)
            Write-Verbose "Filtering services, input=$($Names.Count), output=$($Services.Count)"
        }
        foreach ($PathName in $Services.PathName) {
            $PathMatch = ($PathName | Select-String '"([^\"]*)"|[^\s]*').Matches[0]
            if ($PathMatch.Groups[1].Success) {
                $Path = $PathMatch.Groups[1].Value
            }
            else {
                $Path = $PathMatch.Groups[0].Value
            }
            Write-Verbose "Service path resolved to $Path"
            [System.IO.FileInfo]$Path
        }
    }
}

function Start-SenseBootstrap {
    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory = $true,
            Position = 0,
            ValueFromPipelineByPropertyName = $true
        )]
        [Alias('Name')]
        [ValidateSet('Repository', 'Proxy', 'Scheduler')]
        [string]
        $Service,

        [Parameter()]
        [switch]
        $IsCentral,

        [Parameter()]
        [switch]
        $RestoreHostname
    )

    process {
        Write-Verbose "Starting bootstrap of $Service service"
        $Arguments = '-bootstrap -standalone'
        if ($RestoreHostname.IsPresent) {
            $Arguments += '-restorehostname'
        }
        if ($IsCentral.IsPresent) {
            $Arguments += '-iscentral'
        }

        $ServiceName = "QlikSense${Service}Service"
        $ServicePath = Get-ServicePath -Name $ServiceName

        if ((Get-Service $ServiceName).Status -eq 'Running') {
            Write-Verbose "Stopping service $ServiceName"
            Stop-Service -Name $ServiceName -Force
        }
        if ($ServiceName -ne 'QlikSenseRepositoryService') {
            Start-Service -Name QlikSenseRepositoryService
        }
        Write-Verbose 'Starting service QlikSenseServiceDispatcher'
        Start-Service -Name QlikSenseServiceDispatcher

        $process = RunShellCommand -FileName $ServicePath -Arguments $Arguments
        $lineCount = 0

        while ($null -ne ($line = $process.StandardOutput.ReadLine())) {
            if (! $line) {
                continue
            }

            Write-Verbose $line
            if ($line -match 'Waiting for certificates to be installed') {
                return $process
            }

            $lineCount++
        }

        if ($process.ExitCode -ne 0) {
            Write-Error "Bootstrap failed with Exitcode: $($process.ExitCode)"
        }
        Write-Verbose "Bootstrap job completed"
    }
}

function RunShellCommand {
    param (
        $FileName,
        $Arguments
    )

    process {
        $startInfo = New-Object System.Diagnostics.ProcessStartInfo
        $startInfo.UseShellExecute = $false
        $startInfo.FileName = $FileName
        $startInfo.WorkingDirectory = ([System.IO.FileInfo]$FileName).DirectoryName
        $startInfo.CreateNoWindow = $true
        $startInfo.Arguments = $Arguments
        $startInfo.RedirectStandardOutput = $true
        $startInfo.RedirectStandardError = $true
        $startInfo.RedirectStandardInput = $true

        $process = New-Object System.Diagnostics.Process
        $process.StartInfo = $startInfo

        Write-Verbose "Starting `"$($startInfo.FileName)`" with arguments ($($startInfo.Arguments))"
        $process.Start() | Out-Null
        $process
    }
}