1
+ <#
2
+ Copyright 2017 Dominique Broeglin
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ #>
16
+
17
+ function Enable-NSHighAvailability {
18
+ <#
19
+ . SYNOPSIS
20
+ Enable high-availability between two NetScaler instances.
21
+
22
+ . DESCRIPTION
23
+ Enable high-availability between two NetScaler instances.
24
+
25
+ . EXAMPLE
26
+ Enable-NSHighAvailability -PrimarySession $ns1 -SecondarySession Session $ns2
27
+
28
+ Enable high-availability between the netscaler instances corresponding to
29
+ the already opened $ns1 and $ns2.
30
+
31
+ . PARAMETER PrimarySession
32
+ The NetScaler session object for the first NetScaler instance (will end up master).
33
+
34
+ . PARAMETER SecondarySession
35
+ The NetScaler session object for the second NetScaler instance (will end up slave).
36
+
37
+ . PARAMETER PeerNodeId
38
+ The node id used to denote the peer.
39
+
40
+ Default value: 1
41
+
42
+ . PARAMETER Timeout
43
+ Time to wait, in secondes, for the synchronization to complete.
44
+
45
+ Default value: 300
46
+
47
+ . PARAMETER Save
48
+ If true, wait for the synchronization to finish and save configurations.
49
+
50
+ . PARAMETER Force
51
+ Suppress confirmation when activating high-availability.
52
+ #>
53
+ [CmdletBinding (SupportsShouldProcess = $true , ConfirmImpact = ' High' )]
54
+ param (
55
+ [parameter (Mandatory )]
56
+ $PrimarySession ,
57
+
58
+ [parameter (Mandatory )]
59
+ $SecondarySession ,
60
+
61
+ [int ]$PeerNodeId = 1 ,
62
+
63
+ [int ]$Timeout = 300 ,
64
+
65
+ [switch ]$Save ,
66
+
67
+ [switch ]$Force
68
+ )
69
+
70
+ begin {
71
+ _AssertSessionActive - Session $PrimarySession
72
+ _AssertSessionActive - Session $SecondarySession
73
+ }
74
+
75
+ process {
76
+ if ($Force -or $PSCmdlet.ShouldProcess (
77
+ " Enable high-availability of $ ( $PrimarySession.Endpoint ) and $ ( $SecondarySession.Endpoint ) " )) {
78
+ try {
79
+ $primaryIp = $PrimarySession.Endpoint
80
+ $secondaryIp = $SecondarySession.Endpoint
81
+
82
+ Write-Verbose " $primaryIp -> STAYPRIMARY..."
83
+ _InvokeNSRestApi - Session $PrimarySession - Method PUT - Type hanode `
84
+ - Payload @ { id = 0 ; hastatus = " STAYPRIMARY" }
85
+
86
+ Write-Verbose " $secondaryIp -> STAYSECONDARY..."
87
+ _InvokeNSRestApi - Session $SecondarySession - Method PUT - Type hanode `
88
+ - Payload @ { id = 0 ; hastatus = " STAYSECONDARY" }
89
+
90
+ Write-Verbose " $primaryIp -> set secondatory to $secondaryIp ..."
91
+ _InvokeNSRestApi - Session $PrimarySession - Method POST - Type hanode `
92
+ - Payload @ { id = $PeerNodeId ; ipaddress = $secondaryIp } - Action add
93
+
94
+ Write-Verbose " $secondaryIp -> set secondatory to $primaryIp ..."
95
+ _InvokeNSRestApi - Session $SecondarySession - Method POST - Type hanode `
96
+ - Payload @ { id = $PeerNodeId ; ipaddress = $primaryIp } - Action Add
97
+
98
+ Write-Verbose " $primaryIp -> ENABLED..."
99
+ _InvokeNSRestApi - Session $PrimarySession - Method PUT - Type hanode `
100
+ - Payload @ { id = 0 ; hastatus = " ENABLED" }
101
+
102
+ Write-Verbose " $secondaryIp -> ENABLED..."
103
+ _InvokeNSRestApi - Session $SecondarySession - Method PUT - Type hanode `
104
+ - Payload @ { id = 0 ; hastatus = " ENABLED" }
105
+
106
+ if ($Save ) {
107
+ $waitStart = Get-Date
108
+
109
+ while (((Get-Date ) - $waitStart ).TotalSeconds -lt $Timeout ) {
110
+ Write-Verbose " Waiting for synchronization to complete..."
111
+ Start-Sleep - Seconds 5
112
+ $HaNode = Get-NSHaNode - Session $PrimarySession - Id $PeerNodeId
113
+
114
+ if ($HaNode.hasync -match " IN PROGRESS|ENABLED" ) {
115
+ Write-Verbose " Synchronizing..."
116
+ continue
117
+ } elseif ($HaNode.hasync -eq " SUCCESS" ) {
118
+ Write-Verbose " Synchronization succesful. Saving configurations..."
119
+ Save-NSConfig - Session $PrimarySession
120
+ Save-NSConfig - Session $SecondarySession
121
+ break
122
+ } else {
123
+ throw " Unexpected sync status '$ ( $HaNode.hasync ) '"
124
+ }
125
+ }
126
+
127
+ if ($HaNode.hasync -ne " SUCCESS" ) {
128
+ throw " Timeout expired before the synchronization ended. Configurations will not be saved!"
129
+ }
130
+ }
131
+ } catch {
132
+ throw $_
133
+ }
134
+ }
135
+ }
136
+ }
0 commit comments