def create(self, conn, hostname='localhost.localdomain'):
        """
        Connects to the server and creates the relevant artifacts,
        including the database, the modules database, and the HTTP
        server.

        :param conn: The server connection
        :return:A map containing the content db, the modules db and the HTTP server.
        """
        self.logger.info("Create simple application")
        data_database = Database(self._db_name, hostname)
        data_database.set_forest_names(self._forests)

        modules_database = Database(self._modules_db_name, hostname)

        server = HttpServer(self._http_server, "Default",
                            self._app_port, self._db_name, self._modules_db_name)
        server.set_modules_database_name(self._modules_db_name)

        data_database.create(conn)
        modules_database.create(conn)
        server.create(conn)

        return {
            u'content': data_database,
            u'modules': modules_database,
            u'server': server
        }
Esempio n. 2
0
    def database(self, database_name, host=None, connection=None):
        """
        Get the named database.
        """
        if host is None:
            if connection is None:
                db = Database(database_name,
                              connection=self.connection,
                              save_connection=self.save_connection)
            else:
                db = Database(database_name,
                              connection=connection,
                              save_connection=False)
        else:
            if connection is None:
                db = Database(database_name,
                              host.host_name(),
                              connection=self.connection,
                              save_connection=self.save_connection)
            else:
                db = Database(database_name,
                              host.host_name(),
                              connection=connection,
                              save_connection=False)

        if connection is None:
            return db.read(self.connection)
        else:
            return db.read(connection)
    def delete(self, args, config, connection):
        database = Database(args['name'], connection=connection)
        if not database.exists():
            return

        print("Delete database {0}...".format(args['name']))
        forest_delete = args['forest_delete']
        database.delete(forest_delete,connection)
    def perform(self, args, config, connection):
        database = Database(args['name'], connection=connection)
        if not database.exists():
            print("Error: Database does not exist: {0}".format(args['name']))
            sys.exit(1)

        jf = open(args['json']).read()
        data = json.loads(jf)

        print("Perform {0} on database {1}..."
              .format(data['operation'],args['name']))
        response = database.operation(data, connection=connection)
        print(json.dumps(response, sort_keys=True, indent=2))
Esempio n. 5
0
    def create(self, args, config, connection):
        forest = Forest(args['name'],
                        args['forest_host'],
                        connection=connection)
        if forest.exists():
            self.logger.error("Forest already exists: {0}".format(
                args['name']))
            sys.exit(1)

        if args['json'] is not None:
            forest = self._read(args['name'], args['json'])
            if forest.host() is None:
                forest.set_host(args['forest_host'])

        self._properties(forest, args)
        dbname = forest.database()

        if dbname is not None:
            database = Database(dbname)
            database.read(connection)
        else:
            database = None

        self.logger.info("Create forest {0}...".format(args['name']))
        forest.create(connection=connection)

        if database is not None:
            database.add_forest_name(forest.forest_name())
            database.update(connection)
Esempio n. 6
0
    def databases(self, connection=None):
        """
        Get a list of the databases in the local cluster.
        """
        if connection is None:
            connection = self.connection

        return Database.list(connection)
Esempio n. 7
0
    def databases(self, connection=None):
        """
        Get a list of the databases in the local cluster.
        """
        if connection is None:
            connection = self.connection

        return Database.list(connection)
Esempio n. 8
0
    def create(self, args, config, connection):
        forest = Forest(args['name'], args['forest_host'], connection=connection)
        if forest.exists():
            self.logger.error("Forest already exists: {0}".format(args['name']))
            sys.exit(1)

        if args['json'] is not None:
            forest = self._read(args['name'], args['json'])
            if forest.host() is None:
                forest.set_host(args['forest_host'])

        self._properties(forest, args)
        dbname = forest.database()

        if dbname is not None:
            database = Database(dbname)
            database.read(connection)
        else:
            database = None

        self.logger.info("Create forest {0}...".format(args['name']))
        forest.create(connection=connection)

        if database is not None:
            database.add_forest_name(forest.forest_name())
            database.update(connection)
Esempio n. 9
0
    def modify(self, args, config, connection):
        forest = Database(args['name'], connection=connection)
        if not forest.exists():
            print("Error: Forest does not exist: {0}".format(args['name']))
            sys.exit(1)

        if args['json'] is not None:
            forest = self._read(args['name'], args['json'])
            if forest.host() is None:
                forest.set_host(args['forest_host'])

        self._properties(forest, args)
        print("Modify forest {0}...".format(args['name']))
        forest.update(connection=connection)
Esempio n. 10
0
    def database(self, database_name, host=None, connection=None):
        """
        Get the named database.
        """
        if host is None:
            if connection is None:
                db = Database(database_name, connection=self.connection,
                              save_connection=self.save_connection)
            else:
                db = Database(database_name, connection=connection,
                              save_connection=False)
        else:
            if connection is None:
                db = Database(database_name, host.host_name(),
                              connection=self.connection,
                              save_connection=self.save_connection)
            else:
                db = Database(database_name, host.host_name(),
                              connection=connection,
                              save_connection=False)

        if connection is None:
            return db.read(self.connection)
        else:
            return db.read(connection)
Esempio n. 11
0
    def test_create_field(self):
        db = Database("testdb")

        assert 'field' not in db._config

        field = PathField("invoice-id", FieldPath("bill:invoice-id", 1))
        field.add_field_path(FieldPath("inv:id", 1))

        result = db.add_field(field)
        assert 'field' in  db._config
        assert result == db

        assert 1 == len(db._config['field'])

        field = db.fields()[0]
        assert "invoice-id" == field.field_name()

        field = db.fields()[0]
        assert 2 == len(field.field_paths())

        assert "bill:invoice-id" == field.field_paths()[0].path()
        assert 1 == field.field_paths()[0].weight()
    def _read(self, name, jsonfile,
              hostname=None,
              connection=None, save_connection=True):
        jf = open(jsonfile).read()
        data = json.loads(jf)

        if name is not None:
            data['database-name'] = name

        database = Database.unmarshal(data, hostname=hostname,
                                      connection=connection,
                                      save_connection=save_connection)
        return database
Esempio n. 13
0
    def test_create_field(self):
        db = Database("testdb")

        assert 'field' not in db._config

        field = PathField("invoice-id", FieldPath("bill:invoice-id", 1))
        field.add_field_path(FieldPath("inv:id", 1))

        result = db.add_field(field)
        assert 'field' in db._config
        assert result == db

        assert 1 == len(db._config['field'])

        field = db.fields()[0]
        assert "invoice-id" == field.field_name()

        field = db.fields()[0]
        assert 2 == len(field.field_paths())

        assert "bill:invoice-id" == field.field_paths()[0].path()
        assert 1 == field.field_paths()[0].weight()
Esempio n. 14
0
    def test_create_field(self):
        db = Database("testdb")

        self.assertNotIn('field', db._config)

        field = PathField("invoice-id", FieldPath("bill:invoice-id", 1))
        field.add_field_path(FieldPath("inv:id", 1))

        result = db.add_field(field)
        self.assertIn('field', db._config)
        self.assertEqual(result, db)

        self.assertEqual(1, len(db._config['field']))

        field = db.fields()[0]
        self.assertEqual("invoice-id", field.field_name())

        field = db.fields()[0]
        self.assertEqual(2, len(field.field_paths()))

        self.assertEqual("bill:invoice-id", field.field_paths()[0].path())
        self.assertEqual(1, field.field_paths()[0].weight())
Esempio n. 15
0
    def test_create_range_field(self):
        db = Database("foo")

        field = RootField("invoice-id", False)
        db.add_field(field)

        range_field = FieldRangeIndex("int", "invoice-id")
        db.add_index(range_field)

        index = db.field_range_indexes()[0]
        assert "invoice-id" == index.field_name()
        assert "int" == index.scalar_type()

        indexes = db.field_range_indexes()
        assert 1 == len(indexes)
Esempio n. 16
0
    def modify(self, args, config, connection):
        forest = Database(args['name'], connection=connection)
        if not forest.exists():
            print("Error: Forest does not exist: {0}".format(args['name']))
            sys.exit(1)

        if args['json'] is not None:
            forest = self._read(args['name'], args['json'])
            if forest.host() is None:
                forest.set_host(args['forest_host'])

        self._properties(forest, args)
        print("Modify forest {0}...".format(args['name']))
        forest.update(connection=connection)
Esempio n. 17
0
    def modify(self, args, config, connection):
        name = args['name']
        database = Database(name, connection=connection)
        if not database.exists():
            print("Error: Database does not exist: {0}".format(args['name']))
            sys.exit(1)

        if args['json'] is not None:
            database = self._read(None, args['json'], connection=connection)
            database.name = name

        self.forests = []
        self._properties(database, args)
        if len(self.forests) > 0:
            database.set_forest_names(self.forests)

        print("Modify database {0}...".format(name))
        database.update(connection=connection)
Esempio n. 18
0
    def test_create_range_field(self):
        db = Database("foo")

        field = RootField("invoice-id", False)
        db.add_field(field)

        range_field = FieldRangeIndex("int", "invoice-id")
        db.add_index(range_field)

        index = db.field_range_indexes()[0]
        assert "invoice-id" == index.field_name()
        assert "int" == index.scalar_type()

        indexes = db.field_range_indexes()
        assert 1 == len(indexes)
Esempio n. 19
0
    def test_create_range_field(self):
        db = Database("foo")

        field = RootField("invoice-id", False)
        db.add_field(field)

        range_field = FieldRangeIndex("int", "invoice-id")
        db.add_index(range_field)

        index = db.field_range_indexes()[0]
        self.assertEqual("invoice-id", index.field_name())
        self.assertEqual("int", index.scalar_type())

        indexes = db.field_range_indexes()
        self.assertEqual(1, len(indexes))
    def get(self, args, config, connection):
        database = Database(args['name'], connection=connection)
        if not database.exists():
            print("Error: Database does not exist: {0}".format(args['name']))
            sys.exit(1)

        if args['view'] == None:
            database.read(connection)
            self.jprint(database)
        else:
            results = database.view(args['view'])
            print(json.dumps(results, sort_keys=True, indent=2))
Esempio n. 21
0
    def create(self, args, config, connection):
        name = args['name']
        host = args['forest_host']

        if args['json'] is not None:
            forest = self._read(name, args['json'], connection=connection)
            name = forest.forest_name()
            host = forest.host()
        else:
            forest = Forest(name, host, connection=connection)

        if forest.exists():
            self.logger.error("Forest already exists: {0}".format(name))
            sys.exit(1)

        self._properties(forest, args)
        dbname = forest.database()

        # Strip out properties that we know the server will reject
        cluster = LocalCluster(connection)
        cluster.read()
        if cluster.security_version() is None:
            for key in ['database-replication', 'failover-enable']:
                if key in forest._config:
                    del (forest._config[key])
                    self.logger.debug(
                        "Ignoring {0}, not supported by server".format(key))

        if dbname is not None:
            database = Database(dbname)
            database.read(connection)
        else:
            database = None

        self.logger.info("Create forest {0}...".format(name))
        forest.create(connection=connection)

        if database is not None:
            database.add_forest_name(forest.forest_name())
            database.update(connection)
    def create(self, args, config, connection):
        database = Database(args['name'],
                            args['forest_host'],
                            connection=connection)
        if database.exists():
            print("Error: Database already exists: {0}".format(args['name']))
            sys.exit(1)

        if args['json'] is not None:
            database = self._read(args['name'], args['json'])

        self.forests = []
        self._properties(database, args)
        if len(self.forests) > 0:
            database.set_forest_names(self.forests)

        print("Create database {0}...".format(args['name']))
        database.create()
Esempio n. 23
0
    def modify(self, args, config, connection):
        name = args['name']
        database = Database(name, connection=connection)
        if not database.exists():
            print("Error: Database does not exist: {0}".format(args['name']))
            sys.exit(1)

        if args['json'] is not None:
            database = self._read(None, args['json'],
                                  connection=connection)
            database.name = name

        self.forests = []
        self._properties(database, args)
        if len(self.forests) > 0:
            database.set_forest_names(self.forests)

        print("Modify database {0}...".format(name))
        database.update(connection=connection)
Esempio n. 24
0
    def create(self, args, config, connection):
        database = Database(args['name'], args['forest_host'],
                            connection=connection)
        if database.exists():
            print("Error: Database already exists: {0}".format(args['name']))
            sys.exit(1)

        if args['json'] is not None:
            database = self._read(args['name'], args['json'])

        self.forests = []
        self._properties(database, args)
        if len(self.forests) > 0:
            database.set_forest_names(self.forests)

        print("Create database {0}...".format(args['name']))
        database.create()
Esempio n. 25
0
    def create(self, args, config, connection):
        name = args['name']
        host = args['forest_host']

        if args['json'] is not None:
            forest = self._read(name, args['json'], connection=connection)
            name = forest.forest_name()
            host = forest.host()
        else:
            forest = Forest(name, host, connection=connection)

        if forest.exists():
            self.logger.error("Forest already exists: {0}".format(name))
            sys.exit(1)

        self._properties(forest, args)
        dbname = forest.database()

        # Strip out properties that we know the server will reject
        cluster = LocalCluster(connection)
        cluster.read()
        if cluster.security_version() is None:
            for key in ['database-replication', 'failover-enable']:
                if key in forest._config:
                    del(forest._config[key])
                    self.logger.debug("Ignoring {0}, not supported by server"
                                      .format(key))

        if dbname is not None:
            database = Database(dbname)
            database.read(connection)
        else:
            database = None

        self.logger.info("Create forest {0}...".format(name))
        forest.create(connection=connection)

        if database is not None:
            database.add_forest_name(forest.forest_name())
            database.update(connection)
Esempio n. 26
0
 def _read(self, name, jsonfile):
     jf = open(jsonfile).read()
     data = json.loads(jf)
     data['database-name'] = name
     database = Database.unmarshal(data)
     return database
 def _read(self, name, jsonfile):
     jf = open(jsonfile).read()
     data = json.loads(jf)
     data['database-name'] = name
     database = Database.unmarshal(data)
     return database
 def list(self, args, config, connection):
     names = Database.list(connection)
     print(json.dumps(names, sort_keys=True, indent=2))
Esempio n. 29
0
    def databases(self, connection=None):
        if connection is None:
            connection = self.connection

        return Database.list(connection)
    def databases(self, connection=None):
        if connection is None:
            connection = self.connection

        return Database.list(connection)
    if f is None:
        print("Need to create forest: {0}".format(name))
        f = Forest(name)
        f.create(conn)

# Update forests
for config in data['forests']:
    name = config['forest-name']
    f = Forest.unmarshal(config)
    print("Updating forest: {0}".format(name))
    f.update(conn)

# Create databases
for config in data['databases']:
    name = config['database-name']
    db = Database.lookup(conn, name)
    if db is None:
        print("Need to create database: {0}".format(name))
        db = Database(name)
        db.create(conn)

# Update databases
for config in data['databases']:
    name = config['database-name']
    db = Database.unmarshal(config)
    print("Updating database: {0}".format(name))
    db.update(conn)

# Update servers
for config in data['servers']:
    name = config['server-name']
    def close(self, conn, group='Default'):
        closed = False
        while not closed:
            closed = True

            newitems = []
            for key in self.servers:
                item = self.servers[key]
                if item is None:
                    closed = False
                    newitems.append(Server.lookup(conn, key, group))

            for server in newitems:
                self._close_over_server(server)

            newitems = []
            for key in self.databases:
                item = self.databases[key]
                if item is None:
                    closed = False
                    newitems.append(Database.lookup(conn, key))

            for database in newitems:
                self._close_over_database(database)

            newitems = []
            for key in self.forests:
                item = self.forests[key]
                if item is None:
                    closed = False
                    newitems.append(Forest.lookup(conn, key))

            for forest in newitems:
                self._close_over_forest(forest)

            newitems = []
            for key in self.users:
                item = self.users[key]
                if item is None:
                    closed = False
                    newitems.append(User.lookup(conn, key))

            for user in newitems:
                self._close_over_user(user)

            newitems = []
            for key in self.roles:
                item = self.roles[key]
                if item is None:
                    closed = False
                    newitems.append(Role.lookup(conn, key))

            for role in newitems:
                self._close_over_role(role)

            delitems = []
            newitems = []
            for key in self.privileges:
                item = self.privileges[key]
                parts = key.split("|")
                kind = parts[0]
                name = parts[1]
                if isinstance(item, str):
                    closed = False
                    if "//" in key:
                        # Assume it's an action
                        priv = Privilege.lookup(conn, action=name, kind=kind)
                        delitems.append(key)
                    else:
                        priv = Privilege.lookup(conn, name, kind)
                    newitems.append(priv)

            for item in delitems:
                del self.privileges[item]

            for priv in newitems:
                self._close_over_privilege(priv)
    logging.basicConfig(level=logging.WARNING)
    logging.getLogger("requests").setLevel(logging.WARNING)
    logging.getLogger("marklogic").setLevel(logging.DEBUG)

closure = Closure()

conn = Connection(args.host, HTTPDigestAuth(args.username, args.password))

if args.server:
    for name in args.server:
        server = Server.lookup(conn, name)
        closure.add_server(server)

if args.database:
    for name in args.database:
        database = Database.lookup(conn, name)
        closure.add_database(database)

if args.user:
    for name in args.user:
        user = User.lookup(conn, name)
        closure.add_user(user)

if args.role:
    for name in args.role:
        role = Role.lookup(conn, name)
        closure.add_role(server)

if args.execute_privilege:
    for name in args.execute_privilege:
        closure.add_privilege(name, "execute")
Esempio n. 34
0
    def close(self, conn, group='Default'):
        closed = False
        while not closed:
            closed = True

            newitems = []
            for key in self.servers:
                item = self.servers[key]
                if item is None:
                    closed = False
                    newitems.append(Server.lookup(conn, key, group))

            for server in newitems:
                self._close_over_server(server)

            newitems = []
            for key in self.databases:
                item = self.databases[key]
                if item is None:
                    closed = False
                    newitems.append(Database.lookup(conn, key))

            for database in newitems:
                self._close_over_database(database)

            newitems = []
            for key in self.users:
                item = self.users[key]
                if item is None:
                    closed = False
                    newitems.append(User.lookup(conn, key))

            for user in newitems:
                self._close_over_user(user)

            newitems = []
            for key in self.roles:
                item = self.roles[key]
                if item is None:
                    closed = False
                    newitems.append(Role.lookup(conn, key))

            for role in newitems:
                self._close_over_role(role)

            delitems = []
            newitems = []
            for key in self.privileges:
                item = self.privileges[key]
                parts = key.split("|")
                kind = parts[0]
                name = parts[1]
                if isinstance(item, str):
                    closed = False

                    if self.PRIVILEGES is None:
                        self.PRIVILEGES = Privilege.list(conn)

                    for priv in self.PRIVILEGES:
                        parts = priv.split("|")
                        if parts[0] == kind and parts[2] == name:
                            delitems.append(key)
                            name = parts[1]

                    newitems.append(Privilege.lookup(conn, name, kind))

            for item in delitems:
                del self.privileges[item]

            for priv in newitems:
                self._close_over_privilege(priv)