Пример #1
0
    def add_imei(self, otckey, imei, custid, tid):
        ''' Add to database. If IMEI exists, other fields are updated. '''

        if _keycheck(otckey) == False:
            return "NOP"

        dbconn()

        imei = imei.replace(' ', '')
        custid = custid.replace(' ', '')
        tid = tid.replace(' ', '')

        try:
            o = Otap.get(Otap.imei == imei)

            o.custid = custid
            o.tid = tid
            o.block = 0
            o.save()
        except Otap.DoesNotExist:
            item = {
                'imei': imei,
                'custid': custid,
                'tid': tid,
                'block': 0,
            }
            try:
                o = Otap(**item)
                o.save()

                log.info("Stored OTAP IMEI {0} in database".format(imei))
            except Exception, e:
                log.error("Cannot store OTAP record for {0} in DB: {1}".format(
                    imei, str(e)))
Пример #2
0
def otap_get(custid):
    device, imei = agentinfo()

    dbconn()

    log.info('OTAP request for cust={0} / {1} IMEI={2}'.format(
        custid, device, imei))

    tid = ""
    deliver = None
    try:
        o = Otap.get(Otap.imei == imei, Otap.custid == custid)

        tid = o.tid or "??"

        if o.block == 0 and o.deliver is not None:
            deliver = o.deliver

    except Otap.DoesNotExist:
        log.info("Requested OTAP cust={0}/IMEI={1} doesn't exist in database".
                 format(custid, imei))
        return bottle.HTTPResponse(status=404, body="NOTFOUND")
    except Exception, e:
        log.error("Cannot get OTAP record for {0} from DB: {1}".format(
            imei, str(e)))
        return bottle.HTTPResponse(status=404, body="NOTFOUND")
Пример #3
0
def otap_notify(custid, clientid):
    device, imei = agentinfo()
    otap_result = bottle.request.body.read()
    otap_result = otap_result.rstrip()

    item = {
        'tid': "",
        'device': device,
        'imei': imei,
        'result': otap_result,
        'tstamp': time.strftime('%Y-%m-%d %H:%M:%S',
                                time.gmtime(int(time.time()))),
        'custid': custid,
    }
    try:
        o = Otap.get(Otap.imei == imei, Otap.custid == custid)

        item['tid'] = o.tid or "??"

        if device != 'SIMU':
            o.lastcheck = time.strftime('%Y-%m-%d %H:%M:%S',
                                        time.gmtime(int(time.time())))
            o.save()
    except:
        pass

    message = "OTAP upgrade result for {custid}/{tid} ({device}) {imei}: {result}  {tstamp}".format(
        **item)
    log.info(message)
    notify(item.get('tid', 'xxx'), message)

    return ""
Пример #4
0
    def setflags(self, otckey, imei, flagstring):
        ''' set flags on IMEI '''

        dbconn()

        res = "not set"

        if _keycheck(otckey) == True:
            try:
                imei = imei.strip()
                flagstring = flagstring.strip()

                o = Otap.get(Otap.imei == imei)

                oldflags = o.flags
                custid = o.custid or ''
                tid = o.tid or ''

                if len(flagstring) < 1:
                    flagstring = None
                    operation = 'unset'

                o.flags = flagstring
                o.save()

                res = "Flags on {0}/{1}/{2} changed from {3} to {4}".format(
                    custid, tid, imei, oldflags, flagstring)
                log.info(res)

            except Otap.DoesNotExist:
                res = "Flags not set for {0}: IMEI does not exist".format(imei)

            except Exception, e:
                res = "Flags not set for {0}: {1}".format(imei, str(e))
                log.error(res)
Пример #5
0
    def setcomment(self, otckey, imei, text):
        ''' set a plain text comment for IMEI '''

        dbconn()

        res = "not set"

        if _keycheck(otckey) == True:
            try:
                o = Otap.get(Otap.imei == imei)

                operation = 'set'
                if len(text) < 1:
                    text = None
                    operation = 'unset'

                o.comment = text
                o.save()

                res = "Comment {0} for {1}".format(operation, imei)
                log.info(res)

            except Exception, e:
                res = "Comment not set for {0}: {1}".format(imei, str(e))
                log.error(res)
Пример #6
0
    def find(self, otckey, word):
        ''' Find a word (tid or custid) in the database and show info. '''

        results = []
        dbconn()

        if _keycheck(otckey) == True:

            # Lousy, but no other way at the moment ... See comment re JOIN above
            snames = {}
            query = Imeiset.select()
            for q in query.naive():
                snames[q.imei] = q.sname

            query = Otap.select()
            query = query.where((Otap.tid == word) | (Otap.custid == word))
            query = query.order_by(Otap.tid.asc())
            for q in query.naive():
                results.append({
                    'imei': q.imei,
                    'custid': q.custid,
                    'tid': q.tid,
                    'block': q.block,
                    'reported': q.reported,
                    'deliver': q.deliver,
                    'sname': snames.get(q.imei, ""),
                    'lastcheck': utc_to_localtime(q.lastcheck),
                    'comment': q.comment or '',
                    'flags': q.flags or '',
                })

        return results
Пример #7
0
    def imei(self, otckey, custid, tid):
        ''' Find a TID (and optional custid) in the database and return it's IMEI. '''

        result = ""
        dbconn()

        if _keycheck(otckey) == True:

            try:
                query = Otap.select(Otap.imei).where(Otap.tid == tid)
                if custid is not None:
                    query = query.where(Otap.custid == custid)
                query = query.get()
                result = query.imei
            except Otap.DoesNotExist:
                pass

        return result
Пример #8
0
def imei_getflags(imei):
    ''' Get the flags for IMEI and return as string or "" if no flags '''

    dbconn()

    flagstring = ""

    try:
        imei = imei.strip()

        o = Otap.get(Otap.imei == imei)
        flagstring = o.flags or ""

    except Otap.DoesNotExist:
        flagstring = ""
    except Exception, e:
        log.error("Cannot imei_getflags({0}): {1}".format(imei, str(e)))
        pass
Пример #9
0
    def deliver(self, otckey, imei, version):
        ''' Update IMEI in database and set version to be delivered.
            n.nn.nn means that version, "latest" means current highest version,
            and "*" means highest version existing even if that is introduced
            at a later point in time. '''

        if _keycheck(otckey) == False:
            return "NOP"

        dbconn()

        imei = imei.replace(' ', '')
        version = version.replace(' ', '')

        if version == 'ANY':
            version = '*'

        if version == 'latest':
            # Get sorted versions and take highest
            version = list_jars()[-1]

        if version != '*':
            jarfile = "{0}/{1}.jar".format(cf.jardir, version)
            if not os.path.isfile(jarfile):
                return "No such version here"

        custid = ""
        tid = ""
        try:
            o = Otap.get(Otap.imei == imei)
            o.deliver = version

            custid = o.custid or ""
            tid = o.tid or ""

            o.save()
        except:
            return "Can't find IMEI {0} in DB".format(imei)

        message = "{0}/{1}/{2} will get {3} at next OTAP".format(
            custid, tid, imei, version)
        log.info(message)
        notify('deliver', message)
        return message
Пример #10
0
    def block(self, otckey, imei, bl):
        ''' Set block in db for IMEI. If IMEI == 'ALL', then for all '''

        if _keycheck(otckey) == False:
            return "NOP"

        dbconn()

        imei = imei.replace(' ', '')
        nrecs = None
        try:
            query = Otap.update(block=bl)
            if imei != 'ALL':
                query = query.where(Otap.imei == imei)
            nrecs = query.execute()
        except Exception, e:
            s = "Cannot update db: {0}".format(str(e))
            log.error(s)
            return s
Пример #11
0
    def purge(self, otckey, version):
        ''' purge JAR version from filesystem, ensuring that version is
            not configured as 'deliver' for devices. '''

        if _keycheck(otckey) == False:
            return "NOP"

        dbconn()

        version = version.replace(' ', '')

        n = None
        try:
            query = (Otap.select(fn.COUNT(
                Otap.imei).alias('numdeliver')).where(
                    Otap.deliver == version).get())
            n = query.numdeliver or 0
        except Exception, e:
            raise
Пример #12
0
    def show(self, otckey, imei):
        ''' Show content of database. If IMEI specified, then just that one. '''

        if _keycheck(otckey) == False:
            return "NOP"

        dbconn()

        results = []

        # This join works in dev but not in prod; same Peewee versions (!?!)
        #        query = (Otap
        ##            .select(Otap, Settings.sname.alias('sname'))
        #            .select(Otap, Settings.sname)
        #            .join(Imeiset, JOIN_LEFT_OUTER, on=(Otap.imei == Imeiset.imei))
        #            )

        # Lousy, but no other way at the moment ...
        snames = {}
        query = Imeiset.select()
        for q in query.naive():
            snames[q.imei] = q.sname

        query = Otap.select()
        if imei is not None:
            query = query.where(Otap.imei == imei)
        query = query.order_by(Otap.tid.asc())
        for q in query.naive():
            results.append({
                'imei': q.imei,
                'custid': q.custid,
                'tid': q.tid,
                'block': q.block,
                'reported': q.reported,
                'deliver': q.deliver,
                'sname': snames.get(q.imei, ""),
                'lastcheck': utc_to_localtime(q.lastcheck),
                'comment': q.comment or '',
                'flags': q.flags or '',
            })

        return results
Пример #13
0
def versioncheck(custid, word):

    device, imei = agentinfo()

    current_version = bottle.request.body.read()

    dbconn()

    flags = imei_getflags(imei)

    upgrade = 0
    settings = []
    new_version = ""
    tid = ""

    try:
        o = Otap.get(Otap.imei == imei, Otap.custid == custid)

        tid = o.tid or '??'
        new_version = o.deliver
        if new_version == '*':
            new_version = list_jars()[-1]

        o.reported = current_version
        if device != 'SIMU':
            o.lastcheck = time.strftime('%Y-%m-%d %H:%M:%S',
                                        time.gmtime(int(time.time())))
            o.save()

        if o.block == 0 and o.deliver is not None and current_version != new_version:
            upgrade = 1

        if o.block == 0:
            # Device is not being blocked, but it may have settings we want to push. Do it

            settings_str = imei_settings(imei)
            if settings_str is not None:
                settings = expand_settings(settings_str)

            if device == 'SIMU':
                log.info("NOT clearing settings-delivery because SIMUlator")
            else:
                # Check if settings are once-only. If so, delete the record
                try:
                    q = Imeiset.select().where(Imeiset.imei == imei).get()
                    if q.once == 1:
                        query = Imeiset.delete().where(Imeiset.imei == imei)
                        nrows = query.execute()
                        message = "Imeiset deleted for {0} because once-only configured".format(
                            imei)
                        log.info(message)
                        notify('versioncheck', message)
                except:
                    pass

    except Otap.DoesNotExist:
        log.info(
            "Requested OTAP IMEI {0}/{1} doesn't exist in database".format(
                custid, imei))
    except Exception, e:
        log.error("Cannot get OTAP record for {0} from DB: {1}".format(
            imei, str(e)))