Skip to content

Commit

Permalink
Merge pull request #107 from manics/db-check
Browse files Browse the repository at this point in the history
Simplify install/upgrade commands and db flags
  • Loading branch information
joshmoore authored Jun 20, 2017
2 parents 134d4a2 + 62acb73 commit abdd852
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 14 deletions.
99 changes: 87 additions & 12 deletions omego/upgrade.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import argparse
import copy
import os
import shutil
Expand All @@ -21,22 +22,22 @@
class Install(object):

def __init__(self, cmd, args):

self.args = args
self.args, newinstall = self._handle_args(cmd, args)
log.info("%s: %s", self.__class__.__name__, cmd)
log.debug("Current directory: %s", os.getcwd())
self.symlink_check_and_set()

if cmd == 'upgrade':
newinstall = False
if newinstall is None:
# Automatically install or upgrade
newinstall = not os.path.exists(args.sym)
elif newinstall is False:
if not os.path.exists(args.sym):
raise Stop(30, 'Symlink is missing: %s' % args.sym)
elif cmd == 'install':
newinstall = True
elif newinstall is True:
if os.path.exists(args.sym):
raise Stop(30, 'Symlink already exists: %s' % args.sym)
else:
raise Exception('Unexpected command: %s' % cmd)
assert False

server_dir = self.get_server_dir()

Expand Down Expand Up @@ -71,6 +72,65 @@ def __init__(self, cmd, args):
self.external.save_env_vars(args.savevarsfile, args.savevars.split())
self.start()

def _handle_args(self, cmd, args):
"""
We need to support deprecated behaviour for now which makes this
quite complicated
Current behaviour:
- install: Installs a new server, existing server causes an error
- install --upgrade: Installs or upgrades a server
- install --managedb: Automatically initialise or upgrade the db
Deprecated:
- install --upgradedb --initdb: Replaced by install --managedb
- install --upgradedb: upgrade the db, must exist
- install --initdb: initialise the db
- upgrade: Upgrades a server, must already exist
- upgrade --upgradedb: Automatically upgrade the db
returns:
- Modified args object, flag to indicate new/existing/auto install
"""
if cmd == 'install':
if args.upgrade:
# Current behaviour: install or upgrade
if args.initdb or args.upgradedb:
raise Stop(10, (
'Deprecated --initdb --upgradedb flags '
'are incompatible with --upgrade'))
newinstall = None
else:
# Current behaviour: Server must not exist
newinstall = True

if args.managedb:
# Current behaviour
if args.initdb or args.upgradedb:
raise Stop(10, (
'Deprecated --initdb --upgradedb flags '
'are incompatible with --managedb'))
args.initdb = True
args.upgradedb = True
else:
if args.initdb or args.upgradedb:
log.warn('--initdb and --upgradedb are deprecated, '
'use --managedb')

elif cmd == 'upgrade':
# Deprecated behaviour
log.warn(
'"omero upgrade" is deprecated, use "omego install --upgrade"')
cmd = 'install'
args.upgrade = True
# Deprecated behaviour: Server must exist
newinstall = False

else:
raise Exception('Unexpected command: %s' % cmd)

return args, newinstall

def get_server_dir(self):
"""
Either downloads and/or unzips the server if necessary
Expand Down Expand Up @@ -209,6 +269,9 @@ def handle_database(self):
status = db.check()
log.debug('OMERO database upgrade status: %s', status)

# TODO: When initdb and upgradedb are dropped we can just test
# managedb, but for backwards compatibility we need to support
# initdb without upgradedb and vice-versa
if status == DB_INIT_NEEDED:
if self.args.initdb:
log.debug('Initialising OMERO database')
Expand All @@ -226,7 +289,7 @@ def handle_database(self):
else:
raise Stop(
DB_UPGRADE_NEEDED,
'Pass --upgradedb or upgrade your OMERO database manually')
'Pass --managedb or upgrade your OMERO database manually')

else:
assert status == DB_UPTODATE
Expand Down Expand Up @@ -460,23 +523,35 @@ def __call__(self, args):

class InstallCommand(InstallBaseCommand):
"""
Setup a new OMERO installation.
Setup or upgrade an OMERO installation.
"""
# TODO: When UpgradeCommand is removed InstallBaseCommand and
# InstallCommand can be combined

NAME = "install"

def __init__(self, sub_parsers):
super(InstallCommand, self).__init__(sub_parsers)
group = self.parser.add_argument_group('Database management')
group.add_argument("--initdb", action="store_true",
help="Initialise the database if necessary")
help=argparse.SUPPRESS)
group.add_argument("--upgradedb", action="store_true",
help="Upgrade the database if necessary")
help=argparse.SUPPRESS)
self.parser.add_argument(
"--upgrade", action="store_true",
help="Upgrade the server if already installed")
self.parser.add_argument(
"--managedb", action="store_true",
help="Initialise or upgrade the database if necessary")
self.parser.add_argument(
"--archivelogs", default=None, help=(
"If a logs directory exists archive to this zip file, "
"overwriting if it exists"))


class UpgradeCommand(InstallBaseCommand):
"""
Upgrade an existing OMERO installation.
DEPRECATED: Use `omego install --upgrade` instead
"""

NAME = "upgrade"
Expand Down
15 changes: 15 additions & 0 deletions test/unit/test_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ def setup_method(self, method):
def teardown_method(self, method):
self.mox.UnsetStubs()

@pytest.mark.parametrize('cmd,expected', [
('install', True),
('upgrade', False),
])
def test_handle_args_deprecated(self, cmd, expected):
args = self.Args({
'initdb': False,
'upgradedb': False,
'managedb': False,
'upgrade': False,
})
upgrade = self.PartialMockUnixInstall(args, None)
args, newinstall = upgrade._handle_args(cmd, args)
assert newinstall is expected

@pytest.mark.parametrize('server', [None, 'local', 'remote'])
def test_get_server_dir(self, server):
ext = self.mox.CreateMock(External)
Expand Down
27 changes: 25 additions & 2 deletions travis-build
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ omego -h
if [ $TEST = install ]; then
omego install --initdb --dbhost localhost --dbname omero --prestartfile $HOME/config.omero -v --release 5.0.8 --ice 3.5

# Should return 0 DB_UPTODATE
omego db upgrade -n --dbhost localhost --dbname omero --serverdir OMERO.server-5.0.8-ice35-b60

# Check the expected server version was downloaded
test $(readlink OMERO.server) = './OMERO.server-5.0.8-ice35-b60'

Expand All @@ -38,13 +41,33 @@ if [ $TEST = install ]; then
pg_restore -e travis-omero.pgdump | grep 'CREATE TABLE dbpatch'
fi

#Test a multistage DB upgrade (4.4 -> 5.2) as part of the server upgrade
#Test a multistage DB upgrade (4.4 -> 5.3) as part of the server upgrade
if [ $TEST = upgrade ]; then
omego download --release 4 --ice 3.5 server
ln -s OMERO.server-4.4.12-ice35-b116 OMERO.server;

# Should return 3 DB_INIT_NEEDED
RC=0;
omego db upgrade -n --dbname omero --serverdir OMERO.server || RC=$?
test $RC -eq 3

OMERO.server/bin/omero db script "" "" ome -f OMERO.sql;
psql -q -h localhost -U omero omero < OMERO.sql;
OMERO.server/bin/omero load $HOME/config.omero;
OMERO.server/bin/omero admin start;
omego upgrade --release=5.2 --ice 3.5 --upgradedb --no-web -v;

# Should return 0 DB_UPTODATE
omego db upgrade -n --serverdir OMERO.server

omego download --release 5.3 --ice 3.5 server --sym download-server-50
# Should return 2 DB_UPGRADE_NEEDED
RC=0;
omego db upgrade -n --dbname omero --serverdir download-server-50 || RC=$?
test $RC -eq 2

# Note this should use the already downloaded zip from the previous step
omego install --upgrade --managedb --release=5.3 --ice 3.5 --no-web;

# Should return 0 DB_UPTODATE
omego db upgrade -n --serverdir OMERO.server
fi

0 comments on commit abdd852

Please sign in to comment.