3 # autocreate_ldap_accounts_in_postgresql Helper service for auto-populating ldap accounts in postgresql.
6 # description: Provide a per-database/per-role way to auto-create ldap accounts and auto assign a single role.
9 # Provides: autocreate_ldap_accounts_in_postgresql
10 # Required-Start: $local_fs $network
11 # Required-Stop: $local_fs $network
12 # Default-Start: 3 4 5
13 # Default-Stop: 0 1 2 6
14 # Short-Description: Auto-create ldap-based accounts in postgresql.
15 # Description: Provide a per-database/per-role way to auto-create ldap accounts and auto assign a single role.
18 # Source function library.
19 . /etc/rc.d/init.d/functions
22 local process_owner="alap"
23 local process_group="alap"
24 local path_programs="/programs/"
25 local path_service="${path_programs}bin/autocreate_ldap_accounts_in_postgresql"
26 local path_settings="${path_programs}settings/autocreate_ldap_accounts_in_postgresql/"
27 local path_systems="${path_settings}systems.settings"
28 local path_pids="/var/run/autocreate_ldap_accounts_in_postgresql/"
29 local parameter_system=$2
34 # when process_owner is defined, make sure that the binary has the following set:
35 # setcap cap_net_bind_service=ep /programs/bin/autocreate_ldap_accounts_in_postgresql
37 if [[ ! -f $path_systems ]] ; then
38 echo "No valid path_systems file defined at: $path_systems"
42 if [[ ! -d $path_pids ]] ; then
46 if [[ $process_owner != "" ]] ; then
47 chown $process_owner $path_pids
50 alap_systems=$(grep -o '^alap_systems[[:space:]][[:space:]]*.*$' $path_systems | sed -e 's|^alap_systems[[:space:]][[:space:]]*||')
52 if [[ $alap_systems == "" ]] ; then
53 echo "No valid systems defined by setting 'alap_systems' in file: $path_systems"
57 if [[ $parameter_system != "" ]] ; then
62 if [[ $i == $parameter_system ]] ; then
71 if [[ $alap_systems == "" ]] ; then
72 echo "System '$parameter_system' is not a valid system defined by setting 'alap_systems' in file: $path_systems"
80 if [[ -f $path_settings${i}.settings ]] ; then
81 alap_systems="$alap_systems$i "
83 echo "Skipping system '$i' because it does not have a settings file defined here: '$path_settings${i}.settings'"
104 echo "Usage: autocreate_ldap_accounts_in_postgresql {start|stop|restart|status}"
112 local alap_name_system=
113 local alap_name_group=
114 local alap_name_database=
115 local alap_connect_user=
116 local alap_connect_password=
123 for alap_system in $alap_systems ; do
127 if [[ $result -eq -1 ]] ; then
129 elif [[ $result -gt 0 ]] ; then
130 echo "Not starting process for $alap_system, it is already running with pid=$pid."
136 if [[ $result -eq 0 ]] ; then
139 if [[ $result -eq 0 ]] ; then
143 if [[ $pid == "" ]] ; then
144 echo "Started process for $alap_system but was unable to determine pid, command: $path_service $alap_name_system $alap_name_group $alap_name_database $alap_port."
146 echo "Successfully started process for $alap_system, pid=$pid, command: $path_service $alap_name_system $alap_name_group $alap_name_database $alap_port."
151 if [[ $any_success -ne 0 || $any_failure -eq 1 ]] ; then
159 local alap_name_system=
160 local alap_name_group=
161 local alap_name_database=
169 for alap_system in $alap_systems ; do
173 if [[ $pid == "" ]] ; then
179 if [[ $result -eq 0 ]] ; then
183 if [[ $result -eq -2 ]] ; then
184 echo "Successfully stopped process for $alap_system, pid=$original_pid."
186 echo "Sent stop command for $alap_system, pid=$pid, but pid file ($pid_file) still exists."
191 if [[ $any_success -ne 0 || $any_failure -eq 1 ]] ; then
199 local alap_name_system=
200 local alap_name_group=
201 local alap_name_database=
209 for alap_system in $alap_systems ; do
213 if [[ $result -lt 0 ]] ; then
215 elif [[ $result -gt 0 ]] ; then
218 if [[ $result -ne 0 ]] ; then
225 if [[ $result -eq -2 ]] ; then
226 echo "Successfully stopped process for $alap_system, pid=$original_pid."
228 echo "Sent stop command for $alap_system, pid=$original_pid, but pid file ($pid_file) still exists (cannot start process, skipping)."
235 if [[ $result -eq 0 ]] ; then
238 if [[ $result -eq 0 ]] ; then
242 if [[ $pid == "" ]] ; then
243 echo "Started process for $alap_system but was unable to determine pid, command: $path_service $alap_name_system $alap_name_group $alap_name_database $alap_port."
245 echo "Successfully started process for $alap_system, pid=$pid, command: $path_service $alap_name_system $alap_name_group $alap_name_database $alap_port."
250 if [[ $any_success -ne 0 ]] ; then
258 local alap_name_system=
259 local alap_name_group=
260 local alap_name_database=
267 for alap_system in $alap_systems ; do
271 if [[ $pid == "" ]] ; then
275 echo "The system '$alap_system' appears to be running as process $pid."
281 load_system_settings() {
282 local path_system=$path_settings${alap_system}.settings
287 alap_connect_password=
290 if [[ $alap_system == "" || ! -f $path_system ]] ; then
291 echo "No valid path_systems file defined at: $path_system"
295 alap_name_system=$(grep -o '^alap_name_system[[:space:]][[:space:]]*.*$' $path_system | sed -e 's|^alap_name_system[[:space:]][[:space:]]*||')
296 alap_name_group=$(grep -o '^alap_name_group[[:space:]][[:space:]]*.*$' $path_system | sed -e 's|^alap_name_group[[:space:]][[:space:]]*||')
297 alap_name_database=$(grep -o '^alap_name_database[[:space:]][[:space:]]*.*$' $path_system | sed -e 's|^alap_name_database[[:space:]][[:space:]]*||')
298 alap_connect_user=$(grep -o '^alap_connect_user[[:space:]][[:space:]]*.*$' $path_system | sed -e 's|^alap_connect_user[[:space:]][[:space:]]*||')
299 alap_connect_password=$(grep -o '^alap_connect_password[[:space:]][[:space:]]*.*$' $path_system | sed -e 's|^alap_connect_password[[:space:]][[:space:]]*||')
300 alap_port=$(grep -o '^alap_port[[:space:]][[:space:]]*.*$' $path_system | sed -e 's|^alap_port[[:space:]][[:space:]]*||')
302 if [[ $alap_name_system == "" ]] ; then
303 echo "No valid alap_name_system setting defined in file: $path_system"
307 if [[ $alap_name_group == "" ]] ; then
308 echo "No valid alap_name_group setting defined in file: $path_system"
312 if [[ $alap_name_database == "" ]] ; then
313 echo "No valid alap_name_database setting defined in file: $path_system"
317 if [[ $alap_connect_user == "" ]] ; then
318 echo "No valid alap_connect_user setting defined in file: $path_system"
322 if [[ $alap_port == "" ]] ; then
323 echo "No valid alap_port setting defined in file: $path_system"
329 export alap_connect_user="$alap_connect_user"
330 export alap_connect_password="$alap_connect_password"
332 if [[ $process_owner == "" ]] ; then
333 $path_service $alap_name_system $alap_name_group $alap_name_database $alap_port
336 su $process_owner -m -c "$path_service $alap_name_system $alap_name_group $alap_name_database $alap_port"
340 if [[ $result -ne 0 ]] ; then
341 echo "Failed to start process, command: $path_service $alap_name_system $alap_name_group $alap_name_database $alap_port."
349 # -3 = SIGQUIT, -15 = SIGTERM, -9 = SIGKILL
353 if [[ $result -ne 0 ]] ; then
354 echo "Signal to quit failed, command: kill -3 $pid."
359 # pause and give the process time to close down.
369 pid_file=$path_pids$alap_system.pid
372 # the started process will go into the background, so wait until the pid file is created, but only wait for so long.
374 while [[ $k -lt $max ]] ; do
375 if [[ -f $pid_file ]] ; then
390 pid_file=$path_pids$alap_system.pid
392 if [[ ! -f $pid_file ]] ; then
393 echo "No pid file ($pid_file) found for system '$alap_system', it must not be running."
400 if [[ $result -ne 0 ]] ; then
401 echo "Failed to read the pid file ($pid_file) for system '$alap_system', command: cat $pid_file."
406 if [[ $pid == "" ]] ; then
407 echo "The pid file ($pid_file) for system '$alap_system' is empty."
412 result=$(ps --no-headers -o pid -p $pid)
413 if [[ $? -lt 0 ]] ; then
414 echo "An error occured while searching for the process for system '$alap_system', command: ps --no-headers -o pid -p $pid."
419 if [[ $result == "" ]] ; then
420 echo "No process $pid was found for the system '$alap_system', the pid file might be stale or inaccurate."
428 pid_file=$path_pids$alap_system.pid
430 if [[ ! -f $pid_file ]] ; then
438 if [[ $result -ne 0 ]] ; then
439 echo "Failed to read the pid file ($pid_file) for system '$alap_system', command: cat $pid_file."
444 if [[ $pid == "" ]] ; then
449 result=$(ps --no-headers -o pid -p $pid)
450 if [[ $? -lt 0 ]] ; then
451 echo "An error occured while searching for the process for system '$alap_system', command: ps --no-headers -o pid -p $pid."
456 if [[ $result == "" ]] ; then
459 # the pid file is invalid, so remove the pid file.
462 # return 0 to allow for starting a new process.