-
Notifications
You must be signed in to change notification settings - Fork 7
/
postgresql.init
249 lines (175 loc) · 6.24 KB
/
postgresql.init
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
#!/bin/bash
# postgresql This is the init script for starting up the PostgreSQL {{MAJOR_VERSION}}
# server.
#
# chkconfig: - 64 36
# description: PostgreSQL database server.
# processname: postmaster
###############################################################################
source /etc/init.d/kaosv
###############################################################################
kv.require "2.16"
###############################################################################
INIT_DIR="/etc/init.d"
###############################################################################
VERSION="{{VERSION}}"
MAJOR_VERSION="{{MAJOR_VERSION}}"
PKG_VERSION="{{PKG_VERSION}}"
PREV_VERSION="{{PREV_VERSION}}"
USER_NAME="{{USER_NAME}}"
GROUP_NAME="{{GROUP_NAME}}"
###############################################################################
short_name="pgsql"
kv[prog_name]="postgresql-${PKG_VERSION}"
kv.readSysconfig "postgresql-${MAJOR_VERSION}"
kv[lock_file]="${kv[lock_dir]}/postgresql${PKG_VERSION}"
kv[pid_file]="${kv[pid_dir]}/postgresql${PKG_VERSION}.pid"
pg_engine="${PG_ENGINE:-/usr/${short_name}-${MAJOR_VERSION}/bin}"
pg_postmaster="${PG_POSTMASTER:-${pg_engine}/postmaster}"
pg_data="${PG_DATA:-/var/lib/${short_name}/${MAJOR_VERSION}/data}"
pg_log="${PG_LOG:-/var/lib/${short_name}/${MAJOR_VERSION}/pgstartup.log}"
pg_socket_dir="${PG_SOCKET_DIR:-/var/run/postgresql}"
timeout="${TIMEOUT:-300}"
disable_auto_numa="${DISABLE_AUTO_NUMA}"
pg_ctl="$pg_engine/pg_ctl"
pg_initdb="$pg_engine/initdb"
export pg_data
kv[user]="$USER_NAME"
kv[search_pattern]="$pg_engine"
kv[oom_adj]="$OOM_DISABLE"
###############################################################################
kv.addCommand "initdb" "Init new database" "initdb"
kv.addCommand "reload" "Reload configuration files" "reload"
kv.addHandler "start" "startServiceHandler"
kv.addHandler "start" "preStartServiceHandler" "pre"
kv.addHandler "stop" "stopServiceHandler"
kv.addCommandArgs "stop" "force"
kv.addCommandArgs "initdb" "locale"
kv.disableOutputRedirect "start" "pre"
###############################################################################
preStartServiceHandler() {
[[ ! -f "$pg_postmaster" ]] && return $ACTION_ERROR
[[ ! -x "$pg_postmaster" ]] && return $ACTION_ERROR
createLog || return $ACTION_ERROR
checkData || return $ACTION_ERROR
makeSocketDir || return $ACTION_ERROR
return $ACTION_OK
}
startServiceHandler() {
kv[log]="$pg_log"
export PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
export PG_OOM_ADJUST_VALUE=0
if [[ -n "$disable_auto_numa" || $(getNumaNodesCount) == "1" ]] ; then
kv.run "$pg_ctl" start -D "$pg_data" -s -w -t "$timeout"
else
kv.run numactl --interleave=all "$pg_ctl" start -D "$pg_data" -s -w -t "$timeout"
fi
[[ $? -ne 0 ]] && return $ACTION_ERROR
if kv.getStartStatus "$pg_data/postmaster.pid" true ; then
local pid=$(head -1 "$pg_data/postmaster.pid" 2>/dev/null)
kv.createPid "$pid"
return $ACTION_OK
fi
return $ACTION_ERROR
}
stopServiceHandler() {
local pid=$(kv.getPid)
kv.run "$pg_ctl" stop -D "$pg_data" -s -m fast
if kv.getStopStatus ; then
return $ACTION_OK
else
if [[ "$1" == "true" || "$1" == "force" ]] ; then
kv.killProcess
return $ACTION_FORCED
else
return $ACTION_ERROR
fi
fi
}
initdb() {
if [[ -f "$pg_data/PG_VERSION" ]] ; then
kv.error "Error! Data directory is not empty." $ACTION_ERROR
fi
kv.showProcessMessage "Initializing database"
initNewDB $@
local status=$?
kv.showStatusMessage "$status"
return $status
}
reload() {
kv.showProcessMessage "Reloading configuration"
kv.run "$pg_ctl" reload -D "$pg_data" -s
local status=$?
kv.showStatusMessage "$status"
return $status
}
###############################################################################
initNewDB() {
kv[log]="$pg_log"
local loc="$1"
[[ -z "$loc" ]] && loc=$(getLocale)
if [[ ! -e "$pg_data" && ! -h "$pg_data" ]] ; then
mkdir -p "$pg_data" || return $ACTION_ERROR
kv.chown "$USER_NAME:$GROUP_NAME" "$pg_data"
kv.chmod go-rwx "$pg_data"
fi
kv.restoreSEContext "$pg_data"
createLog || return $ACTION_ERROR
kv.run "$pg_initdb" --pgdata="$pg_data" --auth="ident" --locale="$loc"
mkdir "$pg_data/pg_log"
kv.chown "$USER_NAME:$GROUP_NAME" "$pg_data/pg_log"
kv.chmod go-rwx "$pg_data/pg_log"
if [[ -f "$pg_data/PG_VERSION" ]] ; then
return $ACTION_OK
fi
return $ACTION_ERROR
}
getLocale() {
local lang=$(locale | head -1 | cut -f2 -d= | sed 's/"//g')
if [[ -n "$lang" ]] ; then
echo "$lang"
else
echo "en_US.UTF-8"
fi
}
getNumaNodesCount() {
if [[ ! -x /usr/bin/numactl ]] ; then
echo 1
fi
numactl --hardware | head -1 | cut -f2 -d' '
}
###############################################################################
checkData() {
if [[ -f "$pg_data/PG_VERSION" && -d "$pg_data/base" ]] ; then
if [[ $(cat "$pg_data/PG_VERSION") != "$MAJOR_VERSION" ]] ; then
local docdir="(Your System's documentation directory)"
[[ -d "/usr/doc/postgresql-$VERSION" ]] && docdir=/usr/doc
[[ -d "/usr/share/doc/postgresql-$VERSION" ]] && docdir=/usr/share/doc
[[ -d "/usr/doc/packages/postgresql-$VERSION" ]] && docdir=/usr/doc/packages
[[ -d "/usr/share/doc/packages/postgresql-$VERSION" ]] && docdir=/usr/share/doc/packages
kv.show "An old version of the database format was found."
kv.show "You need to upgrade the data format before using PostgreSQL."
kv.show "See $docdir/postgresql-$VERSION/README.rpm-dist for more information."
return $ACTION_ERROR
fi
else
kv.show "Directory <$pg_data> is missing. Use ${CL_BOLD}service ${kv[script]} initdb${CL_NORM} to initialize the cluster first."
return $ACTION_ERROR
fi
}
createLog() {
if [[ ! -e "$pg_log" && ! -h "$pg_log" ]] ; then
touch "$pg_log" || return 1
kv.chown "$USER_NAME:$GROUP_NAME" "$pg_log"
kv.chmod go-rwx "$pg_log"
kv.restoreSEContext "$pg_log"
fi
}
makeSocketDir() {
if [[ ! -e "$pg_socket_dir" ]] ; then
mkdir "$pg_socket_dir" || return $ACTION_ERROR
kv.chown "$USER_NAME:$GROUP_NAME" "$pg_socket_dir" || return $ACTION_ERROR
fi
}
###############################################################################
kv.go $@