Example #1
0
File: sql.py Project: brtsz/zato
    def _on_config_CHANGE_PASSWORD_SQL_CONNECTION_POOL(self, params):
        """ Changes the pool's password, which in fact means creating a new pool
        with all the parameters copied from the old one except for the new
        password will be used.
        """
        # Do not allow for any reads or updates while the password's being changed.
        self.is_get_allowed.clear()
        try:
            pool_name = params["pool_name"]
            password = params["password"]
            password_encrypted = params["password_encrypted"]

            new_pool_list = copy.deepcopy(self.pool_list)
            new_params = copy.deepcopy(new_pool_list[pool_name])
            new_params["password"] = password

            new_pool_list[pool_name]["password"] = password_encrypted

            # First save the changes on-disk..
            if not self.create_sa_engines:
                self.config_repo_manager.update_sql_pool_list(new_pool_list)

            new_engine_url = engine_def.substitute(new_params)

            if self.create_sa_engines:
                # .. dispose of the old engine and create a new one..
                self.engines[pool_name].dispose()
                new_engine = create_engine(new_engine_url, pool_size=new_params["pool_size"], **new_params["extra"])
            else:
                new_engine = _EngineInfo(new_engine_url, new_params)

            # .. and assign it to self
            self.engines[pool_name] = new_engine

            # .. update the list of available pools.
            self.pool_list = new_pool_list

        except Exception, e:
            msg = "Could not change the SQL connection pool's password, pool_name=[%s], e=[%s]" % (params["pool_name"], format_exc())
            self.logger.error(msg)
            raise ZatoException(msg)
Example #2
0
File: sql.py Project: brtsz/zato
    def _on_config_EDIT_SQL_CONNECTION_POOL(self, params):
        """ Changes the parameters of an SQL connection pool. Unless only a change
        of the connection name is requested, an old connection pool is disposed
        of and a new one is created.
        """

        # Do not allow for any reads or updates during the pools are being updated.
        self.is_get_allowed.clear()
        try:
            original_pool_name = params["original_pool_name"]
            pool_name = params["pool_name"]
            engine = params["engine"]
            user = params["user"]
            host = params["host"]
            db_name = params["db_name"]
            pool_size = int(params["pool_size"])
            extra = params.get("extra", {})

            extra = dict((str(key), extra[key]) for key in extra)

            old_pool = self.engines[original_pool_name]
            old_params = self.pool_list[original_pool_name]
            old_extra = old_params.get("extra", "")

            new_pool_list = copy.deepcopy(self.pool_list)
            pool_renamed = original_pool_name != pool_name

            # Are we changing the name of a pool only?
            if pool_renamed and (
                engine == old_params["engine"] and user == old_params["user"] and
                host == old_params["host"] and db_name == old_params["db_name"] and
                int(pool_size) == old_params["pool_size"] and extra == old_extra):

                self.logger.debug("Renaming SQL connection pool from [%s] to [%s]" % (original_pool_name, pool_name))

                new_pool_list[pool_name] = new_pool_list.pop(original_pool_name)

                # First save the changes on-disk.
                if not self.create_sa_engines:
                    self.config_repo_manager.update_sql_pool_list(new_pool_list)

                self.engines[pool_name] = self.engines.pop(original_pool_name)
                self.pool_list = new_pool_list

                self.logger.info("SQL connection pool renamed from [%s] to [%s]" % (original_pool_name, pool_name))

            # .. nope, we need to create a new one with updated parameters.
            else:
                self.logger.debug("About to create a new pool with updated parameters.")

                password = old_params["password"]

                if password:
                    password_decrypted = str(self.crypto_manager.decrypt(password))
                else:
                    password_decrypted = ""

                new_params = copy.deepcopy(params)
                new_params["password"] = password_decrypted

                if pool_renamed:
                    new_pool_name = pool_name
                    new_pool_list.pop(original_pool_name)
                    new_pool_list[new_pool_name] = {}
                else:
                    new_pool_name = original_pool_name

                new_pool_list[new_pool_name]["engine"] = engine
                new_pool_list[new_pool_name]["user"] = user
                new_pool_list[new_pool_name]["password"] = password
                new_pool_list[new_pool_name]["host"] = host
                new_pool_list[new_pool_name]["db_name"] = db_name
                new_pool_list[new_pool_name]["pool_size"] = pool_size
                new_pool_list[new_pool_name]["extra"] = extra

                new_engine_url = engine_def.substitute(new_params)

                # First save the changes on-disk.
                if not self.create_sa_engines:
                    # It's needed here to catch any incorrect extra arguments
                    # passed in the URL. It will raise an exception if SingletonServer
                    # is trying to create such an incorrect engine definition
                    # and the request to create it will never reach the ParallelServers.
                    create_engine(new_engine_url, pool_size=pool_size, **extra)

                    self.config_repo_manager.update_sql_pool_list(new_pool_list)

                # .. dispose of the old engine.
                if self.create_sa_engines:
                    self.engines[original_pool_name].dispose()
                    new_engine = create_engine(new_engine_url, pool_size=pool_size, **extra)
                else:
                    new_engine = _EngineInfo(new_engine_url, new_params)

                # .. are the new parameters to be saved under the same pool name?
                if pool_renamed:
                    self.engines.pop(original_pool_name)
                    self.engines[pool_name] = new_engine
                else:
                    self.engines[original_pool_name] = new_engine

                # .. update the list of available pools.
                self.pool_list = new_pool_list

        except Exception, e:
            msg = "Could not update SQL connection pool, params=[%s], e=[%s]" % (pprint(params), format_exc())
            self.logger.error(msg)
            raise ZatoException(msg)
Example #3
0
File: sql.py Project: brtsz/zato
    def _on_config_CREATE_SQL_CONNECTION_POOL(self, params):
        """ Creates a new SQL connection factory.
        """

        # Do not allow for any reads or updates during the pools are being updated.
        self.is_get_allowed.clear()
        try:
            pool_name = params["pool_name"]
            engine = params["engine"]
            user = params["user"]
            host = params["host"]
            db_name = params["db_name"]
            pool_size = int(params["pool_size"])
            extra = params.get("extra", {})

            extra = dict((str(key), extra[key]) for key in extra)

            # The only place where we can check it, while no other updates
            # are allowed.
            pool_exists = self.pool_list.get(pool_name)
            if pool_exists:
                msg = "SQL connection pool [%s] already exists, list_id=[%s]." % (pool_name, id(self.pool_list))
                self.logger.error(msg)
                raise ZatoException(msg)
            else:
                msg = "SQL connection pool [%s] doesn't exist yet, list_id=[%s]." % (pool_name, id(self.pool_list))
                self.logger.log(TRACE1, msg)

            new_pool_list = copy.deepcopy(self.pool_list)
            new_pool_list[pool_name] = {}
            new_pool_list[pool_name]["engine"] = engine
            new_pool_list[pool_name]["user"] = user
            new_pool_list[pool_name]["host"] = host
            new_pool_list[pool_name]["db_name"] = db_name
            new_pool_list[pool_name]["pool_size"] = pool_size
            new_pool_list[pool_name]["extra"] = extra
            new_pool_list[pool_name]["password"] = "" # No password yet.

            engine_url = engine_def.substitute(new_pool_list[pool_name])

            # First save the changes on-disk..
            if not self.create_sa_engines:
                # It's needed here to catch any incorrect extra arguments
                # passed in the URL. It will raise an exception if SingletonServer
                # is trying to create such an incorrect engine definition
                # and the request to create it will never reach the ParallelServers.
                create_engine(engine_url, pool_size=pool_size, **extra)

                self.config_repo_manager.update_sql_pool_list(new_pool_list)

            # .. create the engine ..
            if self.create_sa_engines:
                engine = create_engine(engine_url, pool_size=pool_size, **extra)
            else:
                engine = _EngineInfo(engine_url, params)

            # .. and update the list of pools and engines available.
            self.engines[pool_name] = engine
            self.pool_list = new_pool_list

        except Exception, e:
            msg = "Could not create the SQL connection pool, params=[%s], e=[%s]" % (
                pprint(params), format_exc())
            self.logger.error(msg)
            raise ZatoException(msg)