-
Notifications
You must be signed in to change notification settings - Fork 2
/
PostgresHelper
executable file
·237 lines (192 loc) · 6.65 KB
/
PostgresHelper
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
#!/usr/bin/env bash
. "${BASH_SOURCE[0]%/*}/function.sh" script || exit
usage()
{
ScriptUsage "$1" "\
Usage: $(ScriptName) dir|info|IsInstalled|IsRunning|patroni|run|service|vars
PostgreSQL datbase server functionality.
-i, --instance=INSTANCE the instance to use
-v, --version=VERSION the version to use
-H, --host=HOST the database to use, defaults to localhost
-db, --database=DATABASE the database to use, defaults to postgres
-u, --user=USER the user to use, defaults to postgres
-p, --password=VERSION the password to use"
}
init()
{
defaultCommand="run"
confDirBase="/etc/postgresql"
dataDirBase="/var/lib/postgresql"
patroniConf="/etc/patroni/config.yml"
patroniPortDefault="8008"
patroniService="patroni"
postgresPort="5432"
}
argStart()
{
unset -v binDir confDir dataDir instance version password service;
user="postgres" host="localhost" database="postgres"
}
opt()
{
case "$1" in
--instance|--instance=*|-i|-i=*) ScriptOptGet "instance" "$@";;
--version|--version=*|-n|-n=*) ScriptOptGet "version" "$@";;
--user|--user=*|-u|-u=*) ScriptOptGet "user" "$@";;
--password|--password=*|-p|-p=*) ScriptOptGet "password" "$@";;
--host|--host=*|-H|-H=*) ScriptOptGet "host" "$@";;
--database|--database=*|-db|-db=*) ScriptOptGet "database" "$@";;
*) return 1;;
esac
}
argEnd()
{
export PGHOST="$host"
export PGDATABASE="$database"
export PGUSER="$user"
export PGPASSWORD="$password"
}
#
# commands
#
dirCommand() { findPostgres && echo "$confDir"; }
isInstalledCommand() { [[ -d "$confDirBase" ]]; }
isRunningCommand() { IsAvailablePort localhost "$postgresPort"; }
varsCommand() { findPostgres && ScriptReturn version instance binDir confDir confDirBase dataDir dataDirBase service; }
#
# info command
#
infoUsage() { ScriptUsageEcho "Usage: $(ScriptName) info postgres|patroni"; }
infoCommand()
{
infoPostgresCommand || return
patroniIsInstalled && { patroniInfoCommand || return; }
return 0;
}
infoPostgresCommand()
{
findPostgres || return
local desc; isRunningCommand && desc=" (running)"
header "PostgreSQL$desc"
varsCommand || return
serviceExistsCommand && echo "service=$(serviceStatusCommand)"
return 0
}
infoPatroniCommand()
{
patroniValidateInstall || return
local desc; patroniIsRunningCommand && desc=" (running)"
header "Patroni$desc"
echo "patroniConf=$patroniConf"
echo "port=$(getPatroniPort)"
patroniServiceExistsCommand && echo "service=$(patroniServiceStatusCommand)"
[[ $desc ]] && patroniCtrl "topology"
}
#
# run command
#
runArgs() { commandArg="$@"; shift+=$#; }
runCommand()
{
local args=(); [[ $commandArg ]] && args=(--command "$commandArg")
getPassword && psql "${args[@]}"
}
#
# patroni commands
#
patroniUsage() { ScriptUsageEcho "Usage: $(ScriptName) patroni ctl|info|start|stop|IsInstalled|IsRunning|service"; }
patroniCommand() { usage; }
patroniInfoCommand() { infoPatroniCommand; }
patroniIsInstalled() { InPath patroni; }
patroniIsRunningCommand() { patroniIsRunning; }
patroniIsRunning() { IsAvailablePort localhost "$(getPatroniPort)"; }
patroniStartCommand()
{
patroniValidateInstall || return
patroniIsRunning && { echo "Patroni is already running"; return; }
( cd "/tmp"; sudoc -u postgres patroni "$patroniConf" )
}
patroniStopCommand()
{
patroniValidateInstall || return
! patroniIsRunning && { echo "Patroni is not running"; return; }
echo "Stopping Patroni..."
sudoc pkill "patroni"
}
#
# patroni ctl commands
#
patroniCtlArgStart() { patroniCtlArgs=(topology); }
patroniCtlArgEnd() { patroniValidateInstall; }
patroniCtlArgs() { [[ $@ ]] && patroniCtlArgs=( "$@" ); (( shift+=$# )); return 0; }
patroniCtlCommand() { patroniCtrl "${patroniCtlArgs[@]}" "${otherArgs[@]}"; }
patroniCtrl() { CONSUL_HTTP_ADDR= patronictl --config-file "$patroniConf" "$@"; }
#
# patroni service commands
#
patroniServiceUsage() { ScriptUsageEcho "Usage: $(ScriptName) service detail|exists|install|log|restart|running|start|status|stop"; }
patroniServiceCommand() { serviceStatusCommand; }
patroniServiceArgEnd() { patroniValidateInstall; }
patroniServiceDetailCommand() { service detail $patroniService; }
patroniServiceExistsCommand() { service exists $patroniService; }
patroniServiceLogCommand() { service log $patroniService; }
patroniServiceRestartCommand() { service restart "$patroniService"; }
patroniServiceRunningCommand() { service running "$patroniService"; }
patroniServiceStartCommand() { service start "$patroniService"; }
patroniServiceStatusCommand() { service status "$patroniService"; }
patroniServiceStopCommand() { service stop "$patroniService"; }
#
# patroni helper
#
patroniValidateInstall() { isInstalledCommand && return; ScriptErr "Patroni is not installed"; return 1; }
#
# service commands
#
serviceUsage() { ScriptUsageEcho "Usage: $(ScriptName) service detail|exists|log|restart|running|start|status|stop"; }
serviceArgEnd() { findPostgres; }
serviceCommand() { serviceStatusCommand; }
serviceDetailCommand() { service detail $service; }
serviceExistsCommand() { service exists $service; }
serviceLogCommand() { LogShow "/var/log/postgresql/postgresql-$version-$instance.log"; }
serviceRestartCommand() { service restart "$service"; }
serviceRunningCommand() { service running "$service"; }
serviceStartCommand() { service start "$service"; }
serviceStatusCommand() { service status "$service"; }
serviceStopCommand() { service stop "$service"; }
#
# helper
#
validateInstall() { isInstalledCommand && return; ScriptErr "PostgreSQL is not installed"; return 1; }
getPassword()
{
[[ $password ]] && return
password="$(credential get secure system)" || return
export PGPASSWORD="$password"
}
getPatroniPort()
{
[[ $patroniPort ]] && { echo "$patroniPort"; return; }
[[ -f "$patroniConf" ]] && InPath yq && patroniPort="$(yq ".restapi.listen" "$patroniConf" |& GetUriPort)"
echo "${patroniPort:-$patroniPortDefault}"
}
findPostgres()
{
validateInstall || return
[[ ! $force && $service ]] && return
# version
[[ ! $version ]] && version="$(command ls -1 "$confDirBase" | sort --reverse --numeric | head -1)"
! IsNumeric "$version" && { ScriptErr "PostgreSQL version '$version' is not numeric"; return 1; }
[[ ! -d "$confDirBase/$version" ]] && { ScriptErr "PostgreSQL version '$version' is not installed"; return 1; }
# instance
if [[ ! $instance ]]; then
if [[ -d "$confDirBase/$version/main" ]]; then instance="main"
else instance="$(command ls -1 "$confDirBase/$version" | sort | head -1)"
fi
fi
confDir="$confDirBase/$version/$instance"
[[ ! -d "$confDir" ]] && { ScriptErr "PostgreSQL instance '$instance' is not installed"; return; }
binDir="/usr/lib/postgresql/$version/bin"
dataDir="$dataDirBase/$version/$instance"
service="postgresql@$version-$instance.service"
}
ScriptRun "$@"