cIBMWebSpherePortal.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Import IBM WebSphere App Server Utils Module
Import-Module $PSScriptRoot\cIBMWebSpherePortalUtils.psm1 -ErrorAction Stop

enum Ensure {
    Absent
    Present
}

enum WASEdition {
    Base
    ND
    Express
    Developer
    Liberty
}

enum PortalEdition {
    MP
    EXPRESS
    WCM
    EXTEND
}

<#
   DSC resource to manage the installation of IBM WebSphere Portal.
   Key features:
    - Install IBM WebSphere Portal Server for the first time
    - Can use media on the local drive as well as from a network share which may require specifying credentials
#>


[DscResource()]
class cIBMWebSpherePortal {
    [DscProperty(Mandatory)]
    [Ensure] $Ensure
    
    [DscProperty(Key)]
    [PortalEdition] $PortalEdition
    
    [DscProperty(Key)]
    [String] $Version
    
    [DscProperty(Mandatory)]
    [String] $HostName
    
    [DscProperty()]
    [String] $InstallationDirectory = "C:\IBM\WebSphere"
    
    [DscProperty(Mandatory)]
    [System.Management.Automation.PSCredential] $WebSphereAdministratorCredential
    
    [DscProperty()]
    [System.Management.Automation.PSCredential] $PortalAdministratorCredential

    [DscProperty()]
    [String] $CellName = "wpCell"
    
    [DscProperty()]
    [String] $NodeName = "wpNode"
    
    [DscProperty()]
    [String[]] $ServerName = "WebSphere_Portal"
    
    [DscProperty()]
    [String] $ProfileName = "wp_profile"
    
    [DscProperty()]
    [String] $IMSharedLocation = "C:\IBM\IMShared"
    
    [DscProperty()]
    [String] $InstallMediaConfig
    
    [DscProperty()]
    [String] $ResponseFileTemplate
    
    [DscProperty()]
    [Bool] $Primary = $true

    [DscProperty()]
    [String] $SourcePath
    
    [DscProperty()]
    [System.Management.Automation.PSCredential] $SourcePathCredential

    <#
        Installs IBM WebSphere Portal
    #>

    [void] Set () {
        try {
            if ($this.Ensure -eq [Ensure]::Present) {
                Write-Verbose -Message "Starting installation of IBM WebSphere Portal"
                $sevenZipExe = Get-SevenZipExecutable
                if (!([string]::IsNullOrEmpty($sevenZipExe)) -and (Test-Path($sevenZipExe))) {
                    $ibmwpEdition = $this.PortalEdition.ToString()
                    $wpVersion = $this.Version
                    $tempServerName = $this.ServerName[0]
                    $mediaConfig = $null
                    $responseFile = $null
                    
                    $WASInsDir = Get-IBMWebSphereAppServerInstallLocation -WASEdition ND
                    $wasndInstalled = ($WASInsDir -and (Test-Path $WASInsDir))
                    if (!($this.InstallMediaConfig)) {
                        if ($wasndInstalled) {
                            $mediaConfig = Join-Path -Path $PSScriptRoot -ChildPath "InstallMediaConfig\$ibmwpEdition-$wpVersion.xml"
                        } else {
                            $mediaConfig = Join-Path -Path $PSScriptRoot -ChildPath "InstallMediaConfig\$ibmwpEdition-$wpVersion-plus-ND.xml"
                        }
                    }
                    if (!($this.ResponseFileTemplate)) {
                        if ($wasndInstalled -and $this.Primary) {
                            $responseFile = Join-Path -Path $PSScriptRoot -ChildPath "ResponseFileTemplates\$ibmwpEdition-$wpVersion-template.xml"
                        } elseif ($wasndInstalled) {
                            $responseFile = Join-Path -Path $PSScriptRoot -ChildPath "ResponseFileTemplates\$ibmwpEdition-$wpVersion-template-binary.xml"
                        } elseif ($this.Primary) {
                            $responseFile = Join-Path -Path $PSScriptRoot -ChildPath "ResponseFileTemplates\$ibmwpEdition-$wpVersion-template-plus-ND-binary.xml"
                        } else {
                            $responseFile = Join-Path -Path $PSScriptRoot -ChildPath "ResponseFileTemplates\$ibmwpEdition-$wpVersion-template-plus-ND.xml"
                        }
                    }
                    
                    # Install Portal Only
                    $installed = Install-IBMWebSpherePortal -InstallMediaConfig $mediaConfig -ResponseFileTemplate $responseFile `
                                -InstallationDirectory $this.InstallationDirectory -IMSharedLocation $this.IMSharedLocation -CellName $this.CellName `
                                -ProfileName $this.ProfileName -NodeName $this.NodeName -ServerName $tempServerName -HostName $this.HostName `
                                -WebSphereAdministratorCredential $this.WebSphereAdministratorCredential -PortalAdministratorCredential $this.PortalAdministratorCredential `
                                -SourcePath $this.SourcePath -SourcePathCredential $this.SourcePathCredential
                    
                    if ($installed) {
                        Write-Verbose "IBM WebSphere Portal Installed Successfully"
                    } else {
                        Write-Error "Unable to install IBM WebSphere Portal, please check installation logs for more information"
                    }
                } else {
                    Write-Error "IBM WebSphere Portal installation depends on 7-Zip, please ensure 7-Zip is installed first"
                }
            } else {
                Write-Verbose "Uninstalling IBM WebSphere Portal (Not Yet Implemented)"
            }
        } catch {
            Write-Error -ErrorRecord $_ -ErrorAction Stop
        }
    }

    <#
        Performs test to check if Portal is in the desired state, includes
        validation of installation directory, version, and products installed
    #>

    [bool] Test () {
        Write-Verbose "Checking the IBM WebSphere Portal installation"
        $wpConfiguredCorrectly = $false
        $wpRsrc = $this.Get()
        
        if (($wpRsrc.Ensure -eq $this.Ensure) -and ($wpRsrc.Ensure -eq [Ensure]::Present)) {
            if ($wpRsrc.Version -eq $this.Version) {
                if (((Get-Item($wpRsrc.InstallationDirectory)).Name -eq 
                    (Get-Item($this.InstallationDirectory)).Name) -and (
                    (Get-Item($wpRsrc.InstallationDirectory)).Parent.FullName -eq 
                    (Get-Item($this.InstallationDirectory)).Parent.FullName)) {
                    if ($wpRsrc.PortalEdition -eq $this.PortalEdition) {
                        if (($wpRsrc.ProfileName -eq $this.ProfileName) -and 
                            ($wpRsrc.CellName -eq $this.CellName) -and
                            ($wpRsrc.NodeName -eq $this.NodeName)) {
                            if ((Compare-Object $wpRsrc.ServerName $this.ServerName | where {$_.SideIndicator -eq "=>"}).InputObject.Count -eq 0) {
                                Write-Verbose "IBM WebSphere Portal is installed and configured correctly"
                                $wpConfiguredCorrectly = $true
                            }
                        }
                    }
                }
            }
        } elseif (($wpRsrc.Ensure -eq $this.Ensure) -and ($wpRsrc.Ensure -eq [Ensure]::Absent)) {
            $wpConfiguredCorrectly = $true
        }

        if (!($wpConfiguredCorrectly)) {
            Write-Verbose "IBM WebSphere Portal not configured correctly"
        }
        
        return $wpConfiguredCorrectly
    }

    <#
        Retrieves the current state of Portal
    #>

    [cIBMWebSpherePortal] Get () {
        $RetEnsure = [Ensure]::Absent
        $RetVersion = $null
        $RetWPEdition = $null
        $RetInsDir = $null
        $RetProfileName = $null
        $RetCellName = $null
        $RetNodeName = $null
        $RetServerName = $null

        # Check if WAS ND is installed / Portal depends on it
        $WASInsDir = Get-IBMWebSphereAppServerInstallLocation -WASEdition ND
        
        if($WASInsDir -and (Test-Path($WASInsDir))) {
            Write-Verbose "IBM WAS ND is Present"
            # Attempt to retrieve the Portal version information
            $portalDir = Join-Path -Path (Split-Path $WASInsDir) -ChildPath "PortalServer"
            if ($portalDir -and (Test-Path $portalDir)) {
                $wpVersionInfo = Get-IBMWebSpherePortalVersionInfo ($this.InstallationDirectory) -ErrorAction Continue
                if ($wpVersionInfo -and $wpVersionInfo["Product Directory"]) {
                    Write-Verbose "IBM WebSphere Portal is Present"
                    $portalHome = $wpVersionInfo["Product Directory"]
                    $RetEnsure = [Ensure]::Present
                    if ($portalHome -and ((Split-Path $portalHome) -eq $this.InstallationDirectory)) {
                        $RetInsDir = $this.InstallationDirectory
                        $wpEdition = $this.PortalEdition.ToString()
                        # Ensure that it is the right Portal Edition (i.e. WCM vs EXPRESS vs MP)
                        if ($wpVersionInfo.Products[$wpEdition]) {
                            $RetWPEdition = $this.PortalEdition
                            $RetVersion = $wpVersionInfo.Products[$wpEdition].Version
                        } elseif ($wpVersionInfo.Products.Keys.Count -gt 0) {
                            ForEach ($wpProduct in $wpVersionInfo.Products.Keys) {
                                if (!((@('MP','CFGFW')).Contains($wpProduct))) {
                                    $RetWPEdition = $wpProduct
                                    $RetVersion = $wpVersionInfo.Products.$wpProduct.Version
                                    break;
                                }
                            }
                            if (!($RetWPEdition)) {
                                $RetWPEdition = [PortalEdition]::MP
                                $RetVersion = $wpVersionInfo.Products.MP.Version
                            }
                        }
                    } elseif ($portalHome) {
                        $RetInsDir = (Split-Path $portalHome)
                    }
                    # Retreive the current topology, if it doesn't match return the current topology
                    $wpProfilePath = Join-Path -Path ($this.InstallationDirectory) -ChildPath ($this.ProfileName)
                    if (Test-WebSphereTopology $wpProfilePath $this.CellName $this.NodeName $this.ServerName) {
                        $RetProfileName = $this.ProfileName
                        $RetCellName = $this.CellName
                        $RetNodeName = $this.NodeName
                        $RetServerName = $this.ServerName
                    } else {
                        $wasTopology = Get-WebSphereTopology $wpProfilePath
                        # Return the first node / set of servers found
                        if ($wasTopology -and ($wasTopology.Count -gt 0)) {
                            $RetProfileName = $this.ProfileName
                            ForEach ($wasCell in $wasTopology.Keys) {
                                if ($wasTopology.$wasCell.Count -gt 0) {
                                    ForEach ($wasNode in $wasTopology.$wasCell.Keys) {
                                        if ($wasTopology.$wasCell.$wasNode) {
                                            $RetCellName = $wasCell
                                            $RetNodeName = $wasNode
                                            $RetServerName = $wasTopology.$wasCell.$wasNode
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                } else {
                    Write-Verbose "Unable to retrieve the Portal version, Portal is NOT present"
                }
            }
        } else {
            Write-Verbose "IBM WAS ND is NOT Present"
        }

        $returnValue = @{
            Ensure = $RetEnsure
            InstallationDirectory = $RetInsDir
            Version = $RetVersion
            ProfileName = $RetProfileName
            CellName = $RetCellName
            NodeName = $RetNodeName
            ServerName = $RetServerName
        }
        if ($RetWPEdition -ne $null) {
            $returnValue.Add('PortalEdition', $RetWPEdition)
        }

        return $returnValue
    }
}