2019-10-08 12:08:15 +02:00
#!/usr/bin/env bash
config( )
{
if [ " $( whoami) " = "root" ]
then
echo "It is not recommended to run this script as root. As a normal user, elevation will be prompted if needed."
2019-10-08 16:17:48 +02:00
read -rp "Continue anyway? (Y/n) " confirm </dev/tty
2019-10-08 12:08:15 +02:00
if [ [ " $confirm " = "n" ] ] ; then exit 1; fi
2019-10-09 11:02:43 +02:00
else
if ! command -v sudo
then
echo "Please install and configure sudo before running this script."
echo "sudo was not found, exiting..."
exit 1
elif ! groups | grep sudo; then
echo "Please add your current user to the sudoers."
2019-10-09 11:17:15 +02:00
echo " You can run the following as root: \"usermod -aG sudo $( whoami) \", then logout and login again "
2019-10-09 11:02:43 +02:00
echo "sudo was not configured, exiting..."
exit 1
fi
2019-10-09 11:17:15 +02:00
if ! groups | grep docker; then
echo "Please add your current user to the docker group."
echo " You can run the following as root: \"usermod -aG docker $( whoami) \", then logout and login again "
echo "current user is not allowed to use docker, exiting..."
exit 1
fi
2019-10-08 12:08:15 +02:00
fi
2019-10-08 15:23:45 +02:00
if ! command -v awk || ! [ [ $( awk -W version) = ~ ^GNU ] ]
then
echo "Please install GNU Awk before running this script."
echo "gawk was not found, exiting..."
exit 1
fi
2019-10-08 12:08:15 +02:00
FM_PATH = $( pwd )
TYPE = "NOT-FOUND"
2019-10-08 16:17:48 +02:00
read -rp " Is fab-manager installed at \" $FM_PATH \"? (y/N) " confirm </dev/tty
2019-10-22 11:04:21 +02:00
if [ " $confirm " = "y" ] ; then
2019-10-08 12:08:15 +02:00
test_docker_compose
if [ [ " $TYPE " = "NOT-FOUND" ] ]
then
echo "PostgreSQL was not found on the current system, exiting..."
exit 2
fi
else
echo "Please run this script from the fab-manager's installation folder"
exit 1
fi
}
2019-10-08 16:57:58 +02:00
test_free_space( )
{
# checking disk space (minimum required = 1.2GB)
required = $( du -d 0 " $PG_PATH " | awk '{ print $1 }' )
2019-10-22 10:44:29 +02:00
space = $( df " $FM_PATH " | awk '/[0-9]%/{print $(NF-2)}' )
2019-10-08 16:57:58 +02:00
if [ " $space " -lt " $required " ]
then
echo " Not enough free disk space to perform upgrade. Please free at least $required bytes of disk space and try again "
2019-10-22 10:44:29 +02:00
df -h " $FM_PATH "
2019-10-08 16:57:58 +02:00
exit 7
fi
}
2019-10-08 12:08:15 +02:00
test_docker_compose( )
{
if [ [ -f " $FM_PATH /docker-compose.yml " ] ]
then
2019-10-22 11:25:34 +02:00
docker-compose ps | grep postgres
if [ [ $? = 0 ] ]
2019-10-08 12:08:15 +02:00
then
TYPE = "DOCKER-COMPOSE"
fi
fi
}
read_path( )
{
2019-10-08 15:27:16 +02:00
PG_PATH = $( awk " BEGIN { FS=\"\n\"; RS=\"\"; } { match(\$0, /image: postgres: $OLD (\n|.)+volumes:(\n|.)+(-.*postgresql\/data)/, lines); FS=\"[ :]+\"; RS=\"\r\n\"; split(lines[3], line); print line[2] } " " $FM_PATH /docker-compose.yml " )
2019-10-22 12:20:00 +02:00
PG_PATH = " ${ PG_PATH / \$ \{ PWD \} / $FM_PATH } "
PG_PATH = " ${ PG_PATH / \. / $FM_PATH } "
2019-10-08 15:41:27 +02:00
PG_PATH = " ${ PG_PATH /[[ : space : ]]/ } "
2019-10-08 12:08:15 +02:00
}
prepare_path( )
{
if ! ls " $PG_PATH /base " 2>/dev/null
then
echo " PostgreSQL does not seems to be installed in $PG_PATH "
read -rep "Please specify the PostgreSQL data folder: " PG_PATH </dev/tty
prepare_path
else
NEW_PATH = " $PG_PATH - $NEW "
mkdir -p " $NEW_PATH "
fi
}
2019-10-09 11:57:55 +02:00
docker_down( )
{
docker-compose down
2019-10-22 10:44:29 +02:00
ensure_pg_down
}
ensure_pg_down( )
{
if [ -f " $PG_PATH /postmaster.pid " ] ; then
echo 'ERROR: lock file "postmaster.pid" exists'
2019-10-22 11:25:34 +02:00
docker-compose ps | grep postgres
if [ [ $? = 1 ] ] ; then
2019-10-22 10:44:29 +02:00
read -rp 'docker-compose container is not running. Confirm delete the lock file? (y/N) ' confirm </dev/tty
if [ " $confirm " = "y" ] ; then
if [ " $( whoami) " = "root" ] ; then COMMAND = "rm"
else COMMAND = "sudo rm" ; fi
" $COMMAND " -f " $PG_PATH /postmaster.pid "
fi
else
echo 'docker-compose container is still running, retrying to stop...'
sleep 2
docker_down
fi
fi
2019-10-09 11:57:55 +02:00
}
2019-10-08 12:08:15 +02:00
pg_upgrade( )
{
2019-10-22 11:54:37 +02:00
echo " docker run --rm -v $PG_PATH :/var/lib/postgresql/ $OLD /data -v $NEW_PATH :/var/lib/postgresql/ $NEW /data tianon/postgres-upgrade: $OLD -to- $NEW "
2019-10-08 12:08:15 +02:00
docker run --rm \
-v " $PG_PATH :/var/lib/postgresql/ $OLD /data " \
2019-10-09 09:52:58 +02:00
-v " $NEW_PATH :/var/lib/postgresql/ $NEW /data " \
2019-10-09 12:07:54 +02:00
" tianon/postgres-upgrade: $OLD -to- $NEW "
2019-10-08 12:08:15 +02:00
}
upgrade_compose( )
{
echo -e " \nUpgrading docker-compose installation from $OLD to $NEW ... "
2019-10-08 16:28:15 +02:00
# update image tag and data directory into docker-compose file
awk " BEGIN { FS=\"\n\"; RS=\"\"; } { print gensub(/(image: postgres: $OLD (\n|.)+volumes:(\n|.)+(-.*postgresql\/data))/, \"image: postgres: $NEW \n volumes:\n - ${ NEW_PATH } :/var/lib/postgresql/data\", \"g\") } " " $FM_PATH /docker-compose.yml " > " $FM_PATH /.awktmpfile " && mv " $FM_PATH /.awktmpfile " " $FM_PATH /docker-compose.yml "
2019-10-08 12:08:15 +02:00
}
2019-10-09 11:02:43 +02:00
trust_pg_hba_conf( )
{
if [ " $( whoami) " = "root" ] ; then COMMAND = "tee"
else COMMAND = "sudo tee" ; fi
{
echo
echo "host all all all trust"
} | " $COMMAND " -a " $NEW_PATH /pg_hba.conf " > /dev/null
}
2019-10-09 11:57:55 +02:00
docker_up( )
{
docker-compose pull
docker-compose up -d
}
2019-10-08 16:17:48 +02:00
clean( )
{
read -rp "Remove the previous PostgreSQL data folder? (y/N) " confirm </dev/tty
2019-10-22 11:47:04 +02:00
if [ [ " $confirm " = "y" ] ] ; then
read -rp "WARNING: This cannot be undone! If something went wrong during the upgrade, you'll lost all your data. Are you really sure? (y/N) " confirm </dev/tty
if [ [ " $confirm " = "y" ] ] ; then
echo " Deleting $PG_PATH ... "
rm -rf " $PG_PATH "
fi
2019-10-08 16:17:48 +02:00
fi
}
upgrade_postgres( )
2019-10-08 12:08:15 +02:00
{
config
2019-10-08 16:17:48 +02:00
read -rp "Continue with upgrading? (y/N) " confirm </dev/tty
2019-10-08 12:08:15 +02:00
if [ [ " $confirm " = "y" ] ]
then
OLD = '9.4'
2019-10-09 09:52:58 +02:00
NEW = '9.6'
2019-10-08 12:08:15 +02:00
read_path
2019-10-08 16:57:58 +02:00
test_free_space
2019-10-08 12:08:15 +02:00
prepare_path
2019-10-09 11:57:55 +02:00
docker_down
2019-10-08 12:08:15 +02:00
pg_upgrade
upgrade_compose
2019-10-09 11:57:55 +02:00
trust_pg_hba_conf
docker_up
2019-10-08 16:17:48 +02:00
clean
2019-10-08 12:08:15 +02:00
fi
}
2019-10-08 16:17:48 +02:00
upgrade_postgres " $@ "