void-66-services icon indicating copy to clipboard operation
void-66-services copied to clipboard

PostgreSQL service

Open flexibeast opened this issue 4 years ago • 3 comments

i've been playing around with creating a 66 service for PostgreSQL, and at the moment, i can't seem to do any better than merely using the existing runit run script:

[main]
@type = classic
@version = 0.0.1
@description = "PostgreSQL daemon"
@user = ( root )

[start]
@build = custom
@shebang = "/bin/sh"
@execute = (

. /etc/default/postgresql
: ${PGDATA:="$PGROOT/data"}

if [ "$PGROOT" != "/var/lib/postgresql" ]; then
	echo "Creating symlink /var/lib/postgresql -> $PGROOT"

	# Remove /var/lib/postgres if empty dir, but not if symlink
	if [ ! -L /var/lib/postgres ] && [ -d /var/lib/postgres ]; then
		rmdir /var/lib/postgres
	fi

	ln -sf "$PGROOT" /var/lib/postgresql
fi


if [ ! -d "$PGDATA" ]; then
	echo "Initializing database in $PGDATA"

	mkdir -p "$PGDATA"
	chown -R postgres:postgres "$PGDATA"
	chmod 0700 "$PGDATA"
	su - postgres -m -c "/usr/bin/initdb $INITOPTS -D '$PGDATA'" >/dev/null

	if [ -f /etc/postgresql/postgresql.conf ]; then
		ln -sf /etc/postgresql/postgresql.conf "$PGDATA/postgresql.conf"
	fi
fi

exec chpst -u postgres:postgres postgres -D "$PGDATA" $PGOPTS 2>&1

)

i'm assuming that the service should use /etc/default/postgresql, for compatibility with existing installations. But in terms of then creating an execline script, i haven't been able to work out:

  • how to derive PGDATA from PGROOT if the latter is 'sourced' via execl-envfile. Is this possible?

  • whether a variable in an [environment] section can refer to a variable defined previously. E.g. can one do:

pg_root=!/var/lib/postgresql
pg_data=${pg_root}/data

? The frontend docs don't say anything either way, and a search of this repo doesn't seem to show any such usage ....

Finally, assuming all this isn't ported to execline, do we make changes such as converting:

mkdir -p "$PGDATA"
chown -R postgres:postgres "$PGDATA"
chmod 0700 "$PGDATA"

to

execl-toc -X -d "$PGDATA" -u postgres -g postgres -m 0700

?

flexibeast avatar Oct 07 '21 11:10 flexibeast

Thank you for taking this up! My first services were like that - but much simpler ofc :)

i'm assuming that the service should use /etc/default/postgresql, for compatibility with existing installations. But in terms of then creating an execline script, i haven't been able to work out:

  • how to derive PGDATA from PGROOT if the latter is 'sourced' via execl-envfile. Is this possible?

By default, the contents of the [environment] section are indeed passed though execl-envfile. If you do not have an exclamation mark, they should be added to the environment.

As far as the default configuration file, I would love to use the existing ones via execl-envfile but it is not straightforward. It is certainly safer than sourcing them.

In the earlyoom service @linuxergr used the [environment] section instead of the default configuration file. This way we get everything related to the conf file management (versioning, scripting with 66-env -r) for free. With postgres it is probably best to use the default file, indeed.

  • whether a variable in an [environment] section can refer to a variable defined previously. E.g. can one do:
pg_root=!/var/lib/postgresql
pg_data=${pg_root}/data

That is not possible at the moment, I think @Obarun is experimenting with adding it.

Finally, assuming all this isn't ported to execline, do we make changes such as converting:

mkdir -p "$PGDATA"
chown -R postgres:postgres "$PGDATA"
chmod 0700 "$PGDATA"

to

execl-toc -X -d "$PGDATA" -u postgres -g postgres -m 0700

?

Yes you can go ahead and add calls to the 66-tools and s6 utilities (eg replacing chpst) in the shellscript when you think that makes sense. You do not need to use execline in order to use these utilities. I recently replaced mkdir in the repo with execl-toc -d.

mobinmob avatar Oct 07 '21 13:10 mobinmob

  • how to derive PGDATA from PGROOT if the latter is 'sourced' via execl-envfile. Is this possible?

I have never done that with execline... It should be though, I will make some tests.

mobinmob avatar Oct 07 '21 13:10 mobinmob

  • whether a variable in an [environment] section can refer to a variable defined previously. E.g. can one do:
pg_root=!/var/lib/postgresql
pg_data=${pg_root}/data

I stand partially corrected :) That is indeed supported by the parser as @Obarun pointed out in the obarun xmpp channel ([email protected]), but the feature is not yet implemented on a release. The relevant commit is : https://framagit.org/Obarun/66/-/commit/c5390b667d0d8d7d387707f91795970e078b30b0

mobinmob avatar Oct 08 '21 21:10 mobinmob