diff --git a/setup/migrate.py b/setup/migrate.py index 1ddbd441..86788ffb 100755 --- a/setup/migrate.py +++ b/setup/migrate.py @@ -36,7 +36,16 @@ def migration_1(env): except: pass -if __name__ == "__main__": +def get_current_migration(): + ver = 0 + while True: + next_ver = (ver + 1) + migration_func = globals().get("migration_%d" % next_ver) + if not migration_func: + return ver + ver = next_ver + +def run_migrations(): if not os.access("/etc/mailinabox.conf", os.W_OK, effective_ids=True): print("This script must be run as root.", file=sys.stderr) sys.exit(1) @@ -75,3 +84,11 @@ if __name__ == "__main__": # iterate and try next version... +if __name__ == "__main__": + if sys.argv[-1] == "--current": + # Return the number of the highest migration. + print(str(get_current_migration())) + elif sys.argv[-1] == "--migrate": + # Perform migrations. + run_migrations() + diff --git a/setup/start.sh b/setup/start.sh index b66377a9..fd8a3ddb 100755 --- a/setup/start.sh +++ b/setup/start.sh @@ -30,7 +30,7 @@ fi if [ -f /etc/mailinabox.conf ]; then # Run any system migrations before proceeding. Since this is a second run, # we assume we have Python already installed. - setup/migrate.py + setup/migrate.py --migrate # Okay now load the old .conf file to get existing configuration options. cat /etc/mailinabox.conf | sed s/^/DEFAULT_/ > /tmp/mailinabox.prev.conf @@ -38,7 +38,7 @@ if [ -f /etc/mailinabox.conf ]; then MIGRATIONID=$DEFAULT_MIGRATIONID else # What migration are we at for new installs? - MIGRATIONID=1 + MIGRATIONID=$(setup/migrate.py --current) fi # The box needs a name.