forked from ungleich-public/cdist
		
	[type/__postgres_conf] Extract PostgreSQL service user detection to separate explorer
This commit is contained in:
		
					parent
					
						
							
								6b18cace75
							
						
					
				
			
			
				commit
				
					
						0835f414a5
					
				
			
		
					 3 changed files with 67 additions and 30 deletions
				
			
		
							
								
								
									
										64
									
								
								cdist/conf/type/__postgres_conf/explorer/postgres_user
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								cdist/conf/type/__postgres_conf/explorer/postgres_user
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,64 @@
 | 
			
		|||
#!/bin/sh -e
 | 
			
		||||
# -*- mode: sh; indent-tabs-mode: t -*-
 | 
			
		||||
#
 | 
			
		||||
# 2021 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
 | 
			
		||||
#
 | 
			
		||||
# This file is part of cdist.
 | 
			
		||||
#
 | 
			
		||||
# cdist is free software: you can redistribute it and/or modify
 | 
			
		||||
# it under the terms of the GNU General Public License as published by
 | 
			
		||||
# the Free Software Foundation, either version 3 of the License, or
 | 
			
		||||
# (at your option) any later version.
 | 
			
		||||
#
 | 
			
		||||
# cdist is distributed in the hope that it will be useful,
 | 
			
		||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
# GNU General Public License for more details.
 | 
			
		||||
#
 | 
			
		||||
# You should have received a copy of the GNU General Public License
 | 
			
		||||
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
os=$("${__explorer:?}/os")
 | 
			
		||||
 | 
			
		||||
case ${os}
 | 
			
		||||
in
 | 
			
		||||
	(alpine)
 | 
			
		||||
		echo 'postgres'
 | 
			
		||||
		;;
 | 
			
		||||
	(centos|rhel|scientific)
 | 
			
		||||
		echo 'postgres'
 | 
			
		||||
		;;
 | 
			
		||||
	(debian|devuan|ubuntu)
 | 
			
		||||
		echo 'postgres'
 | 
			
		||||
		;;
 | 
			
		||||
	(freebsd)
 | 
			
		||||
		test -x /usr/local/etc/rc.d/postgresql || {
 | 
			
		||||
			printf 'could not find postgresql rc script./n' >&2
 | 
			
		||||
			exit 1
 | 
			
		||||
		}
 | 
			
		||||
		pg_status=$(/usr/local/etc/rc.d/postgresql onestatus) || {
 | 
			
		||||
			printf 'postgresql daemon is not running.\n' >&2
 | 
			
		||||
			exit 1
 | 
			
		||||
		}
 | 
			
		||||
		pg_pid=$(printf '%s\n' "${pg_status}" \
 | 
			
		||||
			| sed -n 's/^pg_ctl:.*(PID: *\([0-9]*\))$/\1/p')
 | 
			
		||||
 | 
			
		||||
		# PostgreSQL < 9.6: pgsql
 | 
			
		||||
		# PostgreSQL >= 9.6: postgres
 | 
			
		||||
		ps -o user -p "${pg_pid}" | sed -n '2p'
 | 
			
		||||
		;;
 | 
			
		||||
	(netbsd)
 | 
			
		||||
		echo 'pgsql'
 | 
			
		||||
		;;
 | 
			
		||||
	(openbsd)
 | 
			
		||||
		echo '_postgresql'
 | 
			
		||||
		;;
 | 
			
		||||
	(suse)
 | 
			
		||||
		echo 'postgres'
 | 
			
		||||
		;;
 | 
			
		||||
	(*)
 | 
			
		||||
		echo "Unsupported OS: ${os}" >&2
 | 
			
		||||
		exit 1
 | 
			
		||||
		;;
 | 
			
		||||
esac
 | 
			
		||||
| 
						 | 
				
			
			@ -19,21 +19,7 @@
 | 
			
		|||
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
os=$("${__explorer:?}/os")
 | 
			
		||||
 | 
			
		||||
case ${os}
 | 
			
		||||
in
 | 
			
		||||
	(openbsd)
 | 
			
		||||
		postgres_user='_postgresql'
 | 
			
		||||
		;;
 | 
			
		||||
	(devuan)
 | 
			
		||||
		postgres_user='postgres'
 | 
			
		||||
		;;
 | 
			
		||||
	(*)
 | 
			
		||||
		echo "Unsupported OS: ${os}" >&2
 | 
			
		||||
		exit 1
 | 
			
		||||
		;;
 | 
			
		||||
esac
 | 
			
		||||
postgres_user=$("${__type_explorer:?}/postgres_user")
 | 
			
		||||
 | 
			
		||||
conf_name=${__object_id:?}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,9 +20,9 @@
 | 
			
		|||
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
os=$(cat "${__global:?}/explorer/os")
 | 
			
		||||
state_is=$(cat "${__object:?}/explorer/state")
 | 
			
		||||
state_should=$(cat "${__object:?}/parameter/state")
 | 
			
		||||
postgres_user=$(cat "${__object:?}/explorer/postgres_user")
 | 
			
		||||
 | 
			
		||||
conf_name=${__object_id:?}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -49,19 +49,6 @@ quote() {
 | 
			
		|||
	printf '%s' "$*"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
case ${os}
 | 
			
		||||
in
 | 
			
		||||
	(netbsd)
 | 
			
		||||
		postgres_user='pgsql'
 | 
			
		||||
		;;
 | 
			
		||||
	(openbsd)
 | 
			
		||||
		postgres_user='_postgresql'
 | 
			
		||||
		;;
 | 
			
		||||
	(*)
 | 
			
		||||
		postgres_user='postgres'
 | 
			
		||||
		;;
 | 
			
		||||
esac
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
psql_cmd() {
 | 
			
		||||
	printf 'su - %s -c %s\n' "$(quote "${postgres_user}")" "$(quote "$(quote psql "$@")")"
 | 
			
		||||
| 
						 | 
				
			
			@ -117,7 +104,7 @@ then
 | 
			
		|||
				case $(cat "${__global:?}/explorer/kernel_name")
 | 
			
		||||
				in
 | 
			
		||||
					(FreeBSD)
 | 
			
		||||
						echo 'service postgresql restart'
 | 
			
		||||
						echo '/usr/local/etc/rc.d/postgresql restart'
 | 
			
		||||
						;;
 | 
			
		||||
					(OpenBSD|NetBSD)
 | 
			
		||||
						echo '/etc/rc.d/postgresql restart'
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue