Esempio n. 1
0
def db_connect(db_uri=None):
    """DataBase CONNECT

    This function is a simple wrapper to connect to the database with error
    handling.  If successful, it returns the connection object.  Otherwise
    execution is aborted.

    This module keeps a reference to the connection created, so the users
    are permitted to call this function like :

    cursor = xivo_helpers.db_connect().cursor()

    If a previous connection was open when this function is called, its
    changes are committed and the connection is closed before creating a
    new one.
    """
    global db_conn
    db_close()

    if not db_uri:
        db_uri = ConfigDict.ReadSingleKey(AGI_CONFFILE, 'db', 'db_uri')

    try:
        db_conn = anysql.connect_by_uri(db_uri)
    except:
        abort("Unable to connect to %s" % db_uri, show_tb=True)

    return db_conn
Esempio n. 2
0
    def acquire(self):
        with self.lock:
            try:
                conn = self.conns.pop()
                log.debug("acquiring connection: got connection from pool")
            except IndexError:
                conn = anysql.connect_by_uri(self.db_uri)
                log.debug("acquiring connection: pool empty, created new connection")

        return conn
Esempio n. 3
0
    def reload(self, size, db_uri):
        with self.lock:
            for conn in self.conns:
                conn.close()

            self.conns = [anysql.connect_by_uri(db_uri) for _ in xrange(size)]

            self.size = size
            self.db_uri = db_uri
        log.debug("reloaded db conn pool")
Esempio n. 4
0
    def reload(self, size, db_uri):
        with self.lock:
            for conn in self.conns:
                conn.close()

            self.conns = [anysql.connect_by_uri(db_uri) for _ in xrange(size)]

            self.size = size
            self.db_uri = db_uri
        logger.debug("reloaded db conn pool")
Esempio n. 5
0
    def __init__(
        self,
        astid,
        remoteaddr = '127.0.0.1',
        ipaddress_webi = '127.0.0.1',
        ami_port = 5038,
        ami_login = '******',
        ami_pass = '******',
        userfeatures_db_uri = None,
        capafeatures = [],
        cdr_db_uri = None,
        parkingnumber = '700',
        faxcallerid = 'faxcallerid',
        aoriginate = 'AOriginate'
        ):

            self.astid = astid
            self.remoteaddr = remoteaddr
            self.ipaddress_webi = ipaddress_webi
            self.ami_port = ami_port
            self.ami_login = ami_login
            self.ami_pass = ami_pass
            self.capafeatures = capafeatures
            self.parkingnumber = parkingnumber
            self.faxcallerid = faxcallerid
            self.aoriginate = aoriginate

            self.userfeatures_db_conn = None
            try:
                if userfeatures_db_uri is not None:
                    self.userfeatures_db_conn = anysql.connect_by_uri(str(userfeatures_db_uri.replace('\/', '/')))
            except Exception:
                log.exception('(init userfeatures_db_conn for %s)' % astid)

            if cdr_db_uri == userfeatures_db_uri:
                self.cdr_db_conn = self.userfeatures_db_conn
            else:
                self.cdr_db_conn = None
                try:
                    self.cdr_db_conn = anysql.connect_by_uri(str(cdr_db_uri.replace('\/', '/')))
                except Exception:
                    log.exception('(init cdr_db_conn for %s)' % astid)
Esempio n. 6
0
    def acquire(self):
        with self.lock:
            try:
                conn = self.conns.pop()
                logger.debug("acquiring connection: got connection from pool")
            except IndexError:
                conn = anysql.connect_by_uri(self.db_uri)
                logger.debug(
                    "acquiring connection: pool empty, created new connection")

        return conn
Esempio n. 7
0
def send_qlog_from_asternic_db(asternic_db_uri, *args, **kwargs):
    connection = anysql.connect_by_uri(asternic_db_uri)
    try:
        cursor = connection.cursor()
        try:
            reader_factory = _AsternicDBQlogReader.new_factory(cursor)
            _send_qlog(reader_factory, *args, **kwargs)
        finally:
            cursor.close()
    finally:
        connection.close()
Esempio n. 8
0
    def acquire(self):
        self.lock.acquire()
        try:
            try:
                conn = self.conns.pop()
                log.debug("acquiring connection: got connection from pool")
            except IndexError:
                conn = anysql.connect_by_uri(self.db_uri)
                log.debug("acquiring connection: pool empty, created new connection")
        finally:
            log.debug("%s", self)
            self.lock.release()

        return conn
Esempio n. 9
0
    def reload(self, size, db_uri):
        self.lock.acquire()
        try:
            for conn in self.conns:
                conn.close()

            del self.conns[:]

            while len(self.conns) < size:
                self.conns.append(anysql.connect_by_uri(db_uri))

            self.size = size
            self.db_uri = db_uri
            log.debug("reloaded db conn pool")
            log.debug("%s", self)
        finally:
            self.lock.release()
Esempio n. 10
0
def send_agent_infos(server_uri, username, password, ast_db_uri,
                     https_proxy=None, dry_run=False):
    logger.info('Sending agent infos...')
    connection = anysql.connect_by_uri(ast_db_uri)
    try:
        cursor = connection.cursor()
        try:
            agent_infos = _get_agent_infos(cursor)
        finally:
            cursor.close()
    finally:
        connection.close()
    
    content = _serialize_agent_infos_to_json(agent_infos)
    if dry_run:
        logger.info('Not transmitting agent infos to server (dry-run).')
    else:
        logger.info('Transmitting agent infos to server...')
        headers = {'Content-Type': 'application/json'}
        _send_content_to_server(server_uri, username, password, content,
                                headers, False, https_proxy)
Esempio n. 11
0
def findpattern(xivocti, dirname, searchpattern, z, reversedir):
        fullstatlist = []

        if searchpattern == '':
            return []

        if z.dbkind in ['ldap', 'ldaps']:
            ldap_filter = []
            ldap_attributes = []
            for fname in z.match_direct:
                if searchpattern == '*':
                    ldap_filter.append("(%s=*)" % fname)
                else:
                    ldap_filter.append("(%s=*%s*)" %(fname, searchpattern))

            for listvalue in z.fkeys.itervalues():
                for attrib in listvalue:
                    if isinstance(attrib, unicode):
                        ldap_attributes.append(attrib.encode('utf8'))
                    else:
                        ldap_attributes.append(attrib)

            try:
                results = None
                if z.uri not in xivocti.ldapids:
                    # first connection to ldap, or after failure
                    ldapid = xivo_ldap.xivo_ldap(z.uri)
                    if ldapid.ldapobj is not None:
                        xivocti.ldapids[z.uri] = ldapid
                else:
                    # retrieve the connection already setup, if not yet deleted
                    ldapid = xivocti.ldapids[z.uri]
                    if ldapid.ldapobj is None:
                        del xivocti.ldapids[z.uri]

                # at this point, either we have a successful ldapid.ldapobj value, with xivocti.ldapids[z.uri] = ldapid
                #                either ldapid.ldapobj is None, and z.uri not in xivocti.ldapids

                if ldapid.ldapobj is not None:
                    # if the ldapid had already been defined and setup, the failure would occur here
                    try:
                        results = ldapid.getldap('(|%s)' % ''.join(ldap_filter),
                                                 ldap_attributes,
                                                 searchpattern)
                    except Exception:
                        ldapid.ldapobj = None
                        del xivocti.ldapids[z.uri]

                if results is not None:
                    for result in results:
                        futureline = {'xivo-directory' : z.name}
                        for keyw, dbkeys in z.fkeys.iteritems():
                            for dbkey in dbkeys:
                                if futureline.get(keyw, '') != '':
                                    break
                                elif dbkey in result[1]:
                                    futureline[keyw] = result[1][dbkey][0]
                                elif keyw not in futureline:
                                    futureline[keyw] = ''
                        fullstatlist.append(futureline)
            except Exception:
                log.exception('ldaprequest (directory)')

        elif z.dbkind == 'phonebook':
            if reversedir:
                matchkeywords = z.match_reverse
            else:
                matchkeywords = z.match_direct
            for iastid in xivocti.weblist['phonebook'].keys():
                for k, v in xivocti.weblist['phonebook'][iastid].keeplist.iteritems():
                    matchme = False
                    for tmatch in matchkeywords:
                        if v.has_key(tmatch):
                            if reversedir:
                                if v[tmatch].lstrip('0') == searchpattern.lstrip('0'):
                                    matchme = True
                            else:
                                if searchpattern == '*' or v[tmatch].lower().find(searchpattern.lower()) >= 0:
                                    matchme = True
                    if matchme:
                        futureline = {'xivo-directory' : z.name}
                        for keyw, dbkeys in z.fkeys.iteritems():
                            for dbkey in dbkeys:
                                if dbkey in v.keys():
                                    futureline[keyw] = v[dbkey]
                        fullstatlist.append(futureline)

        elif z.dbkind == 'file':
            if reversedir:
                matchkeywords = z.match_reverse
            else:
                matchkeywords = z.match_direct
            fullstatlist = cti_directories_csv.lookup(searchpattern.encode('utf8'),
                                                      z.uri,
                                                      matchkeywords,
                                                      z.fkeys,
                                                      z.delimiter,
                                                      z.name)

        elif z.dbkind == 'http':
            if not reversedir:
                fulluri = z.uri
                # add an ending slash if needed
                if fulluri[8:].find('/') == -1:
                    fulluri += '/'
                fulluri += '?' + '&'.join([key + '=' + urllib.quote(searchpattern.encode('utf-8')) for key in z.match_direct])
                n = 0
                try:
                    f = urllib.urlopen(fulluri)
                    # use f.info() to detect charset
                    charset = 'utf-8'
                    s = f.info().getheader('Content-Type')
                    k = s.lower().find('charset=')
                    if k >= 0:
                        charset = s[k:].split(' ')[0].split('=')[1]                                    
                    for line in f:
                        if n == 0:
                            header = line
                            headerfields = header.strip().split(z.delimiter)
                        else:
                            ll = line.strip()
                            if isinstance(ll, str): # dont try to decode unicode string.
                                ll = ll.decode(charset)
                            t = ll.split(z.delimiter)
                            futureline = {'xivo-directory' : z.name}
                            # XXX problem when badly set delimiter + index()
                            for keyw, dbkeys in z.fkeys.iteritems():
                                for dbkey in dbkeys:
                                    idx = headerfields.index(dbkey)
                                    futureline[keyw] = t[idx]
                            fullstatlist.append(futureline)
                        n += 1
                    f.close()
                except Exception:
                    log.exception('__build_customers_bydirdef__ (http) %s' % fulluri)
                if n == 0:
                    log.warning('WARNING : %s is empty' % z.uri)
                # we don't warn about "only one line" here since the filter has probably already been applied
            else:
                fulluri = z.uri
                # add an ending slash if needed
                if fulluri[8:].find('/') == -1:
                    fulluri += '/'
                fulluri += '?' + '&'.join([key + '=' + urllib.quote(searchpattern) for key in z.match_reverse])
                f = urllib.urlopen(fulluri)
                # TODO : use f.info() to detect charset
                fsl = f.read().strip()
                if fsl:
                    fullstatlist = [ {'xivo-directory' : z.name,
                                      'db-fullname' : fsl}]
                else:
                    fullstatlist = []

        elif z.dbkind in ['sqlite', 'mysql']:
            if searchpattern == '*':
                whereline = ''
            else:
                # prevent SQL injection and make use of '*' wildcard possible
                esc_searchpattern = searchpattern.replace("'", "\\'").replace('%', '\\%').replace('*', '%')
                wl = ["%s LIKE '%%%s%%'" % (fname, esc_searchpattern) for fname in z.match_direct]
                whereline = 'WHERE ' + ' OR '.join(wl)

            results = []
            try:
                conn = anysql.connect_by_uri(str(z.uri))
                cursor = conn.cursor()
                sqlrequest = 'SELECT ${columns} FROM %s %s' % (z.sqltable, whereline)
                cursor.query(sqlrequest,
                             tuple(z.match_direct),
                             None)
                results = cursor.fetchall()
                conn.close()
            except Exception:
                log.exception('sqlrequest for %s' % z.uri)

            for result in results:
                futureline = {'xivo-directory' : z.name}
                for keyw, dbkeys in z.fkeys.iteritems():
                    for dbkey in dbkeys:
                        if dbkey in z.match_direct:
                            n = z.match_direct.index(dbkey)
                            futureline[keyw] = result[n]
                fullstatlist.append(futureline)

        elif z.dbkind in ['internal']:
            pass

        elif z.dbkind in ['mssql']:
            pass

        else:
            log.warning('wrong or no database method defined (%s) - please fill the uri field of the directory <%s> definition'
                        % (z.dbkind, dirname))



        if reversedir:
            display_reverse = z.display_reverse
            if fullstatlist:
                for k, v in fullstatlist[0].iteritems():
                    if isinstance(v, unicode):
                        display_reverse = display_reverse.replace('{%s}' % k, v)
                    elif isinstance(v, str):
                        # decoding utf8 data as we know the DB is storing utf8 so some bug may lead this data to come here still utf8 encoded
                        # in the future, this code could be removed, once we are sure encoding is properly handled "up there" (in sqlite client)
                        display_reverse = display_reverse.replace('{%s}' % k, v.decode('utf8'))
                    else:
                        log.warning('__build_customers_bydirdef__ %s is neither unicode nor str' % k)
                e = fullstatlist[0]
                e.update({'dbr-display' : display_reverse})
        return fullstatlist
Esempio n. 12
0
 def __init__(self, stat_db_uri):
     self.conn = anysql.connect_by_uri(stat_db_uri)
     self.cur = self.conn.cursor()
     self.cache = {}
     self.collect = 1000 
Esempio n. 13
0
 def init(options):
 # {
     ami_logger.conn = anysql.connect_by_uri(options.anysql_uri)
     ami_logger.cur = ami_logger.conn.cursor()
     ami_logger.last_transaction = time.time()