#!/bin/bash # run an arbitrary psql command as cfpostgres user # expectation is that a line will be output with exit_code= # this exit code will then be used to exit # This design enables cfengine policy to run a commands promise # that can be kept, repaired or failed depending on an exit # code from psql since psql can't get different codes from scripts. if [ $# -ne 2 ]; then echo "Usage: $0 [DBNAME] [SQL STRING]" exit 2 fi TMP=$(mktemp) cd /tmp OUT=$(su - cfpostgres --command "{{{vars.sys.bindir}}}/psql --quiet --tuples-only --no-align --no-psqlrc \"$1\" --command=\"$2\"" 2> $TMP) RETURN_CODE=$? ERR=$(<$TMP) EXIT_CODE=$(echo $OUT | awk -F= '{ if ( /exit_code/ ) print $2}') rm $TMP if [ $RETURN_CODE -ne 0 ]; then echo "stdout: $OUT" echo "stderr: $ERR" exit 2; # failed fi exit $EXIT_CODE