-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathocUnityDbConnect.ps1
More file actions
128 lines (111 loc) · 3.88 KB
/
ocUnityDbConnect.ps1
File metadata and controls
128 lines (111 loc) · 3.88 KB
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
# Prompt the user to optionally login to OpenShift
Write-Host "Do you want to log in to OpenShift now? (y/n)" -ForegroundColor Green
$loginResponse = Read-Host
if ($loginResponse -match '^(y|yes)$') {
try {
oc login --web --server=https://api.silver.devops.gov.bc.ca:6443
}
catch {
Write-Host "Login failed. Please check your connection and credentials." -ForegroundColor Red
exit 1
}
}
# Prompt user for environment selection
$validEnvironments = @("dev", "test", "prod")
do {
Write-Host "Enter environment (dev, test, prod)" -ForegroundColor Green
$environment = Read-Host
} while (-not ($validEnvironments -contains $environment))
# Define cluster mappings
$clusterMappings = @{
'dev' = @('dev', 'dev2')
'test' = @('test', 'uat')
'prod' = @('prod')
}
if ($environment -eq 'prod') {
$cluster = 'prod'
} else {
$validClusters = $clusterMappings[$environment]
do {
Write-Host "Please select between $($validClusters -join ' and ')" -ForegroundColor Green
$cluster = Read-Host "Enter cluster ($($validClusters -join ', '))"
} while (-not ($validClusters -contains $cluster))
}
# Configuration parameters (dynamically updated based on environment)
$NameSpace = "d18498-$environment" # OpenShift project namespace
$ClusterName = "$cluster-crunchy-postgres"
$LocalPort = 5436
$RemotePort = 5432
$ListenInterface = "localhost"
$RetrySeconds = 3
# Check if oc is installed
if (-not (Get-Command oc -ErrorAction SilentlyContinue)) {
Write-Host "The OpenShift CLI (oc) is not installed or not available in PATH." -ForegroundColor Red
exit 1
}
# Set the OpenShift project namespace
Write-Host "Setting OpenShift project to $NameSpace..." -ForegroundColor Cyan
try {
oc project $NameSpace
}
catch {
Write-Host "Error setting project: $_" -ForegroundColor Red
Write-Host "Please ensure you're logged in to OpenShift before running this script." -ForegroundColor Yellow
exit 1
}
# Function to get the current primary pod using selectors
function Get-PrimaryPod {
$selector = "postgres-operator.crunchydata.com/cluster=$ClusterName,postgres-operator.crunchydata.com/role=master"
try {
$primaryPod = oc get pod -o name --selector=$selector
if ([string]::IsNullOrEmpty($primaryPod)) {
Write-Host "No primary pod found with selector: $selector" -ForegroundColor Yellow
return $null
}
return ($primaryPod -replace "^pod/", "").Trim()
}
catch {
Write-Host "Error getting primary pod: $_" -ForegroundColor Red
return $null
}
}
# Function to check if still logged in
function Test-OCLogin {
try {
$null = oc project $NameSpace 2>&1
return $true
}
catch {
Write-Host "OpenShift login has expired or cannot access namespace '$NameSpace'. Please run the script again to re-authenticate." -ForegroundColor Red
return $false
}
}
# Main connection loop
while ($true) {
Write-Host ""
$datetime = Get-Date -Format "yyyy-MM-dd HH:mm:ss K"
Write-Host $datetime -ForegroundColor Cyan
# Get the current primary pod
$primaryPod = Get-PrimaryPod
if ($primaryPod) {
Write-Host "Connecting to primary pod: $primaryPod" -ForegroundColor Green
# Forward the port
try {
oc port-forward --address $ListenInterface $primaryPod "${LocalPort}:${RemotePort}"
}
catch {
Write-Host "Error occurred during port forwarding: $_" -ForegroundColor Red
}
}
else {
Write-Host "Unable to find primary PostgreSQL pod. Retrying in $RetrySeconds seconds..." -ForegroundColor Yellow
}
# Pause before retry
Start-Sleep -Seconds $RetrySeconds
# Check login status
if (-not (Test-OCLogin)) {
break
}
}
Write-Host "Press any key to exit..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")