class TestMmaDatabase(MLConfig):
    def test_create_database(self):
        self.marklogic = MarkLogic(self.connection)
        dbname = 'test-mma-database'

        mma = MMA(self.connection)
        mma.run(['create', 'database', dbname])

        assert dbname in self.marklogic.databases()

        mma.run(['delete', 'database', dbname])

        assert dbname not in self.marklogic.databases()
Beispiel #2
0
class TestMmaDatabase(MLConfig):
    def test_create_database(self):
        self.marklogic = MarkLogic(self.connection)
        dbname = 'test-mma-database'

        mma = MMA(self.connection)
        mma.run(['create', 'database', dbname])

        assert dbname in self.marklogic.databases()

        mma.run(['delete', 'database', dbname])

        assert dbname not in self.marklogic.databases()
class TestMmaDatabase(unittest.TestCase):
    """
    Basic creation test function.
    """
    def __init__(self, argv):
        super().__init__(argv)
        self.connection = Connection(tc.hostname,
                                     HTTPDigestAuth(tc.admin, tc.password))
        self.marklogic = MarkLogic(self.connection)

    def test_create_database(self):
        dbname = 'test-mma-database'

        mma = MMA(self.connection)
        mma.run(['create','database',dbname])

        self.assertTrue(dbname in self.marklogic.databases())

        mma.run(['delete','database',dbname])

        self.assertFalse(dbname in self.marklogic.databases())
class RestoreDatabases:
    def __init__(self):
        self.marklogic       = None
        self.host            = "localhost"
        self.adminuser       = "******"
        self.adminpass       = "******"
        self.databases       = None
        self.restore_root     = None
        self.journal_arch    = False
        self.lag_limit       = 30
        self.incremental     = False
        self.max_parallel    = 5
        self.dry_run         = False

    # TODO: better checking of argument types

    def set_host(self, host):
        self.host = host
        return self

    def set_user(self, adminuser):
        self.adminuser = adminuser
        return self

    def set_pass(self, adminpass):
        self.adminpass = adminpass
        return self

    def set_databases(self, databases):
        self.databases = databases

    def set_restore_root(self, directory):
        if directory is not None:
            self.restore_root = directory
            if not self.restore_root.endswith("/"):
                self.restore_root += "/"
        return self

    def set_journal_archiving(self, archiving):
        self.journal_arch = archiving
        return self

    def set_lag_limit(self, limit):
        self.lag_limit = limit
        return self

    def set_incremental(self, incremental):
        self.incremental = incremental
        return self

    def set_max_parallel(self, max_parallel):
        self.max_parallel = max_parallel
        return self

    def set_dry_run(self, dry_run):
        self.dry_run = dry_run
        return self

    def restore_databases(self):
        conn = Connection(self.host,
                          HTTPDigestAuth(self.adminuser, self.adminpass))
        self.marklogic = MarkLogic(conn)

        actual_databases = self.marklogic.databases()

        if self.databases is None:
            self.databases = actual_databases

        if self.restore_root is None:
            raise UnsupportedOperation("You must specify the restore root.")

        if self.max_parallel <= 0:
            self.max_parallel = 1

        for dbname in self.databases:
            if not dbname in actual_databases:
                raise UnsupportedOperation("Database does not exist: {0}"
                                           .format(dbname))

        maxp = self.max_parallel
        running = 0
        done = False
        queue = self.databases
        status_list = {}
        min_wait = 5
        max_wait = 30
        wait_incr = 5
        wait = min_wait

        while not done:
            done = True

            while len(queue) > 0 and running < maxp:
                dbname = queue.pop(0)
                running += 1
                done = False

                print("Restoring {0}".format(dbname))
                if not self.dry_run:
                    db = self.marklogic.database(dbname)
                    rst = db.restore(self.restore_root + dbname, connection=conn)
                    status_list[dbname] = rst
                    response = rst.status()
                    for forest in response['forest']:
                        if forest['status'] != 'in-progress':
                            print("Forest {0}: {1}"
                                  .format(forest['forest-name'],
                                          forest['status']))

            if self.dry_run:
                if running > 0 or len(queue) > 0:
                    print("{0} restores in dry-run; {1} in queue..."
                          .format(running, len(queue)))
                    running = 0
            else:
                if len(status_list) > 0:
                    new_list = {}
                    for dbname in status_list:
                        rst = status_list[dbname]
                        response = rst.status()
                        fdone = True
                        for forest in response['forest']:
                            print("Forest {0}: {1}"
                                  .format(forest['forest-name'],
                                          forest['status']))
                            if forest['status'] == 'in-progress':
                                fdone = False
                        if not fdone:
                            done = False
                            new_list[dbname] = rst
                        else:
                            running -= 1
                            wait = min_wait

                    done = done and len(queue) == 0

                    if not done:
                        status_list = new_list
                        if running < maxp and len(queue) != 0:
                            print("Running: {0} restores running; {1} in queue..."
                                  .format(running, len(queue)))
                            wait = min_wait
                            print("")
                        else:
                            print("Waiting {0}s: {1} restores running; {2} in queue..."
                                  .format(wait, running, len(queue)))
                            time.sleep(wait)
                            if wait < max_wait:
                                wait += wait_incr
                            print("")
class BackupDatabases:
    def __init__(self):
        self.marklogic       = None
        self.host            = "localhost"
        self.adminuser       = "******"
        self.adminpass       = "******"
        self.databases       = None
        self.backup_root     = None
        self.journal_arch    = False
        self.lag_limit       = 30
        self.incremental     = False
        self.max_parallel    = 5
        self.dry_run         = False

    # TODO: better checking of argument types

    def set_host(self, host):
        self.host = host
        return self

    def set_user(self, adminuser):
        self.adminuser = adminuser
        return self

    def set_pass(self, adminpass):
        self.adminpass = adminpass
        return self

    def set_databases(self, databases):
        self.databases = databases

    def set_backup_root(self, directory):
        if directory is not None:
            self.backup_root = directory
            if not self.backup_root.endswith("/"):
                self.backup_root += "/"
        return self

    def set_journal_archiving(self, archiving):
        self.journal_arch = archiving
        return self

    def set_lag_limit(self, limit):
        self.lag_limit = limit
        return self

    def set_incremental(self, incremental):
        self.incremental = incremental
        return self

    def set_max_parallel(self, max_parallel):
        self.max_parallel = max_parallel
        return self

    def set_dry_run(self, dry_run):
        self.dry_run = dry_run
        return self

    def backup_databases(self):
        conn = Connection(self.host,
                          HTTPDigestAuth(self.adminuser, self.adminpass))
        self.marklogic = MarkLogic(conn)

        actual_databases = self.marklogic.databases()

        if self.databases is None:
            self.databases = actual_databases

        if self.backup_root is None:
            raise UnsupportedOperation("You must specify the backup root.")

        if self.max_parallel <= 0:
            self.max_parallel = 1

        for dbname in self.databases:
            if not dbname in actual_databases:
                raise UnsupportedOperation("Database does not exist: {0}"
                                           .format(dbname))

        maxp = self.max_parallel
        running = 0
        done = False
        queue = self.databases
        status_list = {}
        min_wait = 5
        max_wait = 30
        wait_incr = 5
        wait = min_wait

        while not done:
            done = True

            while len(queue) > 0 and running < maxp:
                dbname = queue.pop(0)
                running += 1
                done = False

                print("Backing up {0}".format(dbname))
                if not self.dry_run:
                    db = self.marklogic.database(dbname)
                    bkp = db.backup(self.backup_root + dbname, connection=conn)
                    status_list[dbname] = bkp
                    response = bkp.status()
                    if response['status'] != 'in-progress':
                        print("{0}: {1}".format(dbname, response['status']))

            if self.dry_run:
                if running > 0 or len(queue) > 0:
                    print("{0} backups in dry-run; {1} in queue..."
                          .format(running, len(queue)))
                    running = 0
            else:
                if len(status_list) > 0:
                    new_list = {}
                    for dbname in status_list:
                        bkp = status_list[dbname]
                        response = bkp.status()
                        print("{0}: {1}".format(dbname, response['status']))
                        if response['status'] == 'in-progress':
                            done = False
                            new_list[dbname] = bkp
                        else:
                            running -= 1
                            wait = min_wait

                    done = done and len(queue) == 0

                    if not done:
                        status_list = new_list
                        if running < maxp and len(queue) != 0:
                            print("Running: {0} backups running; {1} in queue..."
                                  .format(running, len(queue)))
                            wait = min_wait
                            print("")
                        else:
                            print("Waiting {0}s: {1} backups running; {2} in queue..."
                                  .format(wait, running, len(queue)))
                            time.sleep(wait)
                            if wait < max_wait:
                                wait += wait_incr
                            print("")