From 1dc2de50268c36e52dff1a022952c6b4b03fd06b Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Fri, 29 May 2020 16:12:20 -0700 Subject: [PATCH] puppet: Update setup-disks to be idempotent. The end state it produces is _either_: - `/srv/postgresql` already existed, which was symlinked into `/var/lib/postgresql`; postgres is left untouched. This is the situation if `setup_disks` is run on the database primary, or a replica which was correctly configured. - An empty `/srv/postgresql` now exists, symlinked into `/var/lib/postgresql`, and postgres is stopped. This is the situation if `puppet` was just run on a new host, or a previously-configured host was rebooted (clearing the temporary disk in `/dev/nvme0`) In the latter case, where `/srv/postgresql` is now empty, any previous contents of `/var/lib/postgresql` are placed under `/root`, timestamped for uniqueness. In either case, the tool should now be idempotent. --- .../zulip_ops/files/postgresql/setup_disks.sh | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/puppet/zulip_ops/files/postgresql/setup_disks.sh b/puppet/zulip_ops/files/postgresql/setup_disks.sh index 7415fbf54a..c7739ab949 100755 --- a/puppet/zulip_ops/files/postgresql/setup_disks.sh +++ b/puppet/zulip_ops/files/postgresql/setup_disks.sh @@ -1,14 +1,28 @@ #!/bin/sh +set -x +set -e LOCALDISK=/dev/nvme0n1 -mkfs.xfs $LOCALDISK +if ! grep -q $LOCALDISK /etc/fstab; then + echo "$LOCALDISK /srv xfs nofail,noatime 1 1" >> /etc/fstab +fi -echo "$LOCALDISK /srv xfs nofail,noatime 1 1" >> /etc/fstab -mount /srv +if ! mountpoint -q /srv; then + mkfs.xfs $LOCALDISK + mount /srv +fi -service postgresql stop -mv /var/lib/postgresql /srv -ln -s /srv/postgresql/ /var/lib +if [ ! -L /var/lib/postgresql ]; then + service postgresql stop + if [ -e /var/lib/postgresql ]; then + mv /var/lib/postgresql "/root/postgres-data-$(date +'%m-%d-%Y-%T')" + fi + ln -s /srv/postgresql/ /var/lib +fi -service postgresql start +if [ ! -e "/srv/postgresql" ]; then + service postgresql stop + mkdir "/srv/postgresql" + chown postgres:postgres /srv/postgresql +fi