From 18330ab042f6c784583c9b673cee1bef69b4ac5c Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Mon, 11 Jun 2018 18:15:19 +0200 Subject: [PATCH] Check available space in /var/lib/postgresql before running postgresql migration --- locales/en.json | 1 + src/yunohost/data_migrations/0005_postgresql_9p4_to_9p6.py | 7 +++---- src/yunohost/utils/filesystem.py | 4 ++++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/locales/en.json b/locales/en.json index 85745e1fb..5eb977713 100644 --- a/locales/en.json +++ b/locales/en.json @@ -242,6 +242,7 @@ "migration_0003_modified_files": "Please note that the following files were found to be manually modified and might be overwritten at the end of the upgrade : {manually_modified_files}", "migration_0005_postgresql_94_not_installed": "Postgresql was not installed on your system. Nothing to do!", "migration_0005_postgresql_96_not_installed": "Postgresql 9.4 has been found to be installed, but not postgresql 9.6 !? Something weird might have happened on your system :( ...", + "migration_0005_not_enough_space": "Not enough space is available in {path} to run the migration right now :(.", "migrations_backward": "Migrating backward.", "migrations_bad_value_for_target": "Invalid number for target argument, available migrations numbers are 0 or {}", "migrations_cant_reach_migration_file": "Can't access migrations files at path %s", diff --git a/src/yunohost/data_migrations/0005_postgresql_9p4_to_9p6.py b/src/yunohost/data_migrations/0005_postgresql_9p4_to_9p6.py index a6bfafcf2..9df51979d 100644 --- a/src/yunohost/data_migrations/0005_postgresql_9p4_to_9p6.py +++ b/src/yunohost/data_migrations/0005_postgresql_9p4_to_9p6.py @@ -5,6 +5,7 @@ from moulinette.core import MoulinetteError from moulinette.utils.log import getActionLogger from yunohost.tools import Migration +from yunohost.utils.filesystem import free_space_in_directory, space_used_by_directory logger = getActionLogger('yunohost.migration') @@ -12,7 +13,6 @@ logger = getActionLogger('yunohost.migration') class MyMigration(Migration): "Migrate DBs from Postgresql 9.4 to 9.6 after migrating to Stretch" - def migrate(self): if not self.package_is_installed("postgresql-9.4"): @@ -22,8 +22,8 @@ class MyMigration(Migration): if not self.package_is_installed("postgresql-9.6"): raise MoulinetteError(m18n.n("migration_0005_postgresql_96_not_installed")) - # FIXME / TODO : maybe add checks about the size of - # /var/lib/postgresql/9.4/main/base/ compared to available space ? + if not space_used_by_directory("/var/lib/postgresql/9.4") < free_space_in_directory("/var/lib/postgresql"): + raise MoulinetteError(m18n.n("migration_0005_not_enough_space", path="/var/lib/postgresql/")) subprocess.check_call("service postgresql stop", shell=True) subprocess.check_call("pg_dropcluster --stop 9.6 main", shell=True) @@ -35,7 +35,6 @@ class MyMigration(Migration): pass - def package_is_installed(self, package_name): p = subprocess.Popen("dpkg --list | grep -q -w {}".format(package_name), shell=True) diff --git a/src/yunohost/utils/filesystem.py b/src/yunohost/utils/filesystem.py index 9b39f5daa..3f026f980 100644 --- a/src/yunohost/utils/filesystem.py +++ b/src/yunohost/utils/filesystem.py @@ -23,3 +23,7 @@ import os def free_space_in_directory(dirpath): stat = os.statvfs(dirpath) return stat.f_frsize * stat.f_bavail + +def space_used_by_directory(dirpath): + stat = os.statvfs(dirpath) + return stat.f_frsize * stat.f_blocks