Beispiel #1
0
def configuration_post(request):
    for setting in ("imsiUpdate", "smsUpdate", "silentSms", "welcomeMessage", "replyMessage"):
        obj = DBSession.query(Settings).filter_by(name=setting).one()
        obj.value = request.POST.get(setting)
        DBSession.add(obj)

    return configuration_get(request)
    def start_new_session(self):
        with transaction.manager:
            DBSession.query(Measure).delete()
            HLRDBSession.query(Sms).delete()
            HLRDBSession.query(Subscriber).filter(Subscriber.extension != self.pf_phone_number).delete()

        return True
Beispiel #3
0
 def __init__(self, resource, playbook_path, req_id, model):
     super(MyPlaybook, self).__init__(resource)
     self.results_raw = None
     self.playbook_path = playbook_path
     self.req = req_id
     self.model = model
     self.count = 0
     self.session = DBSession()
    def start_new_session(self):
        with transaction.manager:
            DBSession.query(Measure).delete()
            HLRDBSession.query(Sms).delete()
            HLRDBSession.query(Subscriber).filter(
                Subscriber.extension != self.pf_phone_number).delete()

        return True
Beispiel #5
0
def gen_save_idsession(session, path):
    value = session
    if 'id' in session:
        print('Registra no banco a requisição')
    else:
        print('ID da session gerado.')
        value['id'] = str(uuid4())

    # DBSession.begin()
    DBSession.add(LogModel(idsession=value['id'], endpoint=path))
    transaction.commit()

    return value
Beispiel #6
0
    def post(self):
        self.set_header('Content-Type', 'application/json')
        try:
            print self.request.body
            data = json.loads(self.request.body)
        except ValueError as e:
            self.set_status(400)
            return self.write(
                json.dumps({"message": "username and password are required!"}))

        username = data.get("username", None)
        password = data.get("password", None)

        if username and password:
            session = DBSession()
            try:
                user = session.query(User).filter(
                    User.username == username).one()
            except Exception as e:
                logger.error("query db failed, %s" % e.message)
                user = None
                self.set_status(403)
                self.write(
                    json.dumps({"message": "user %s not exist!" % username}))
            finally:
                session.close()

            if user:
                if hashlib.sha256(password).hexdigest() == user.password:
                    payload = {
                        'id':
                        user.id,
                        'username':
                        username,
                        'exp':
                        datetime.datetime.utcnow() +
                        datetime.timedelta(seconds=43200),
                    }
                    token = jwt.encode(payload, SECRET_KEY, algorithm='HS256')
                    response = {'token': token}
                    self.write(response)
                else:
                    self.set_status(403)
                    self.write(
                        json.dumps({"message": 'authentication failed!'}))
        else:
            self.set_status(400)
            self.write(
                json.dumps({"message": 'username and password are required.'}))
Beispiel #7
0
def get_imsi_circles(request):
    imsi = request.matchdict['imsi']
    ts_begin = request.GET.get('timestamp_begin')
    ts_end = request.GET.get('timestamp_end')

    query = DBSession.query(
        Measure.distance, Measure.gps_lat, Measure.gps_lon,
        Measure.timestamp).filter(
            (Measure.imsi == imsi) & (Measure.gps_lat != None)
            & (Measure.gps_lon != None)
            & (Measure.timestamp <= datetime.fromtimestamp(
                float(ts_end) / 1000))).order_by(Measure.timestamp.asc())

    if ts_begin is not None:
        query = query.filter(Measure.timestamp >= datetime.fromtimestamp(
            float(ts_begin) / 1000))

    result = {'imsi': imsi, 'circles': []}

    for circle in query.all():
        result['circles'].append({
            'center': [circle.gps_lat, circle.gps_lon],
            'radius':
            circle.distance,
            'ts':
            time.mktime(circle.timestamp.timetuple())
        })

    return result
    def get_last_measure(self, imsi):
        last_measures = DBSession.query(Measure).filter(
            Measure.imsi == imsi).order_by(Measure.id.desc()).limit(1).all()

        if len(last_measures) == 0:
            return None
        return last_measures[0]
Beispiel #9
0
def get_imsi_list(request):
    result = []
    messages = []

    query = DBSession.query(
        Measure.id,
        Measure.imsi,
        func.max(Measure.timestamp).label('last')
    ).group_by(Measure.imsi).all()

    for measure in query:
        dtime = datetime.now() - measure.last
        result.append({
            'id': measure.id,
            'imsi': measure.imsi,
            'last_lur': dtime.total_seconds() // 60
        })


    # Calculate message count for each IMSI
    imsi_list = map(lambda item: item['imsi'], result);
    query = HLRDBSession.query(
        Subscriber.imsi,
        func.count(Sms.id).label('sms_count')
    ).filter((
        (Sms.src_addr == Subscriber.extension) |
        (Sms.dest_addr == Subscriber.extension)) &
        (Sms.protocol_id != 64) &
        (Subscriber.imsi.in_(imsi_list))
    ).group_by(
        Subscriber.imsi
    ).all()

    for record in query:
        messages.append({
            'imsi': int(record.imsi),
            'count': record.sms_count
        })


    if 'jtSorting' in request.GET:
        sorting_params = request.GET['jtSorting'].split(' ')
        sorting_field = sorting_params[0]
        reverse = sorting_params[1] == 'DESC'
        result.sort(key=lambda x: x[sorting_field], reverse=reverse)

    try:
        gps_status = all(request.xmlrpc.get_current_gps())
        gps_status = 'yes' if gps_status else 'no'
    except (socket.error, xmlrpclib.Error) as e:
        gps_status = 'failed'


    return {
        'Result': 'OK',
        'Records': result,
        'Messages': messages,
        'GpsStatus': gps_status
    }
    def get_formated_reply_message(self, **wargs):
        wargs["ph_phone_number"] = self.pf_phone_number
        reply_message_res = DBSession.query(Settings.value).filter(Settings.name == "replyMessage").all()
        if len(reply_message_res) != 1:
            self.logger.error("Settings table not have replyMessage value!")
            return None

        return reply_message_res[0][0].format(**wargs)
Beispiel #11
0
def configuration_get(request):
    query = DBSession.query(Settings.name, Settings.value)

    result = {}
    for setting in query.all():
        result[setting.name] = setting.value

    return result
    def save_measure_to_db(self, meas, extension):
        self.logger.debug("Save measure to DB!")

        distance = self.__calculate_distance(long(meas['meas_rep']['L1_TA']))

        self.logger.debug("distance: {0}".format(distance))

        with transaction.manager:
            obj = Measure(imsi=meas['imsi'],
                          timestamp=datetime.datetime.fromtimestamp(meas['time']),
                          timing_advance=meas['meas_rep']['L1_TA'],
                          distance=distance,
                          phone_number=extension,
                          gps_lat=meas['lat'],
                          gps_lon=meas['lon'],
                          )
            self.logger.info("Add measure: imsi={0}, ta={1}, lat={2}, lon={3}".format(meas['imsi'], meas['meas_rep']['L1_TA'], meas['lat'], meas['lon']))
            DBSession.add(obj)
    def get_formated_reply_message(self, **wargs):
        wargs["ph_phone_number"] = self.pf_phone_number
        reply_message_res = DBSession.query(
            Settings.value).filter(Settings.name == "replyMessage").all()
        if len(reply_message_res) != 1:
            self.logger.error("Settings table not have replyMessage value!")
            return None

        return reply_message_res[0][0].format(**wargs)
Beispiel #14
0
def main(argv=sys.argv):
    if len(argv) != 2:
        usage(argv)
    config_uri = argv[1]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.pf.')
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)

    hlr_engine = engine_from_config(settings, 'sqlalchemy.hlr.')
    HLRDBSession.configure(bind=hlr_engine)

    # initial settings
    with transaction.manager:
        DBSession.add_all([
            Settings(name='imsiUpdate', value=3000),
            Settings(name='smsUpdate', value=3000),
            Settings(name='silentSms', value=3000),
            Settings(
                name='welcomeMessage',
                value='You are connected to a mobile search and rescue team. ' + \
                      'Please SMS to {ph_phone_number} to communicate. ' + \
                      'Your temporary phone number is {ms_phone_number}'
            ),
            Settings(
                name='replyMessage',
                value='Your SMSs are being sent to a mobile search and rescue team. ' + \
                      'Reply to this message to communicate.'
            ),
        ])

        HLRDBSession.add_all([
            Subscriber(
                created=datetime.datetime.fromtimestamp(time.time()),
                updated=datetime.datetime.fromtimestamp(time.time()),
                imsi=pf_subscriber_imsi,
                name=pf_subscriber_name,
                extension=pf_subscriber_extension,
                authorized=1,
                lac=0
            )
        ])
    def get_last_measure(self, imsi):
        last_measures = DBSession.query(
            Measure
        ).filter(
            Measure.imsi == imsi
        ).order_by(
            Measure.id.desc()
        ).limit(1).all()

        if len(last_measures) == 0:
            return None
        return last_measures[0]
Beispiel #16
0
def main(global_config, **settings):
    engine = engine_from_config(settings, 'sqlalchemy.pf.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine

    engine = engine_from_config(settings, 'sqlalchemy.hlr.')
    HLRDBSession.configure(bind=engine)

    xmlrpc_url = 'http://%(xmlrpc.host)s:%(xmlrpc.port)s' % settings
    xmlrpc = lambda request: xmlrpclib.ServerProxy(xmlrpc_url)
    tile_dir = lambda request: settings.get('tile_dir')

    config = Configurator(settings=settings)
    config.include('pyramid_mako')
    config.add_static_view('static', 'static', cache_max_age=3600)

    config.add_route('home', '/')
    config.add_route('configuration', '/configuration')

    config.add_route('start_tracking', '/tracking/start')
    config.add_route('stop_tracking', '/tracking/stop')

    config.add_route('get_imsi_list', '/imsi/list')
    config.add_route('get_imsi_messages', 'imsi/{imsi}/messages')
    config.add_route('get_imsi_circles', 'imsi/{imsi}/circles')
    config.add_route('send_imsi_message', 'imsi/{imsi}/message')

    config.add_route('download_tiles_start', '/tiles/download/start')
    config.add_route('download_tiles_stop', '/tiles/download/stop')
    config.add_route('download_tiles_status', '/tiles/download/status')
    config.add_static_view(name='tiles', path=settings.get('tile_dir'))

    config.add_route('clear_data', '/data/clear/all')

    config.add_request_method(xmlrpc, 'xmlrpc', reify=True)
    config.add_request_method(tile_dir, 'tile_dir', reify=True)

    config.scan()
    return config.make_wsgi_app()
    def save_measure_to_db(self, meas, extension):
        self.logger.debug("Save measure to DB!")

        distance = self.__calculate_distance(long(meas['meas_rep']['L1_TA']))

        self.logger.debug("distance: {0}".format(distance))

        with transaction.manager:
            obj = Measure(
                imsi=meas['imsi'],
                timestamp=datetime.datetime.fromtimestamp(meas['time']),
                timing_advance=meas['meas_rep']['L1_TA'],
                distance=distance,
                phone_number=extension,
                gps_lat=meas['lat'],
                gps_lon=meas['lon'],
            )
            self.logger.info(
                "Add measure: imsi={0}, ta={1}, lat={2}, lon={3}".format(
                    meas['imsi'], meas['meas_rep']['L1_TA'], meas['lat'],
                    meas['lon']))
            DBSession.add(obj)
Beispiel #18
0
def main(global_config, **settings):
    engine = engine_from_config(settings, 'sqlalchemy.pf.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine

    engine = engine_from_config(settings, 'sqlalchemy.hlr.')
    HLRDBSession.configure(bind=engine)

    xmlrpc_url = 'http://%(xmlrpc.host)s:%(xmlrpc.port)s' % settings
    xmlrpc = lambda request: xmlrpclib.ServerProxy(xmlrpc_url)
    tile_dir = lambda request: settings.get('tile_dir')

    config = Configurator(settings=settings)
    config.include('pyramid_mako')
    config.add_static_view('static', 'static', cache_max_age=3600)

    config.add_route('home', '/')
    config.add_route('configuration', '/configuration')

    config.add_route('start_tracking', '/tracking/start')
    config.add_route('stop_tracking', '/tracking/stop')

    config.add_route('get_imsi_list', '/imsi/list')
    config.add_route('get_imsi_messages', 'imsi/{imsi}/messages')
    config.add_route('get_imsi_circles', 'imsi/{imsi}/circles')
    config.add_route('send_imsi_message', 'imsi/{imsi}/message')

    config.add_route('download_tiles_start', '/tiles/download/start')
    config.add_route('download_tiles_stop', '/tiles/download/stop')
    config.add_route('download_tiles_status', '/tiles/download/status')
    config.add_static_view(name='tiles', path=settings.get('tile_dir'))

    config.add_route('clear_data', '/data/clear/all')

    config.add_request_method(xmlrpc, 'xmlrpc', reify=True)
    config.add_request_method(tile_dir, 'tile_dir', reify=True)

    config.scan()
    return config.make_wsgi_app()
Beispiel #19
0
def get_imsi_list(request):
    result = []
    messages = []

    query = DBSession.query(Measure.id, Measure.imsi,
                            func.max(
                                Measure.timestamp).label('last')).group_by(
                                    Measure.imsi).all()

    for measure in query:
        dtime = datetime.now() - measure.last
        result.append({
            'id': measure.id,
            'imsi': measure.imsi,
            'last_lur': dtime.total_seconds() // 60
        })

    # Calculate message count for each IMSI
    imsi_list = map(lambda item: item['imsi'], result)
    query = HLRDBSession.query(
        Subscriber.imsi,
        func.count(Sms.id).label(
            'sms_count')).filter(((Sms.src_addr == Subscriber.extension)
                                  | (Sms.dest_addr == Subscriber.extension))
                                 & (Sms.protocol_id != 64)
                                 & (Subscriber.imsi.in_(imsi_list))).group_by(
                                     Subscriber.imsi).all()

    for record in query:
        messages.append({'imsi': int(record.imsi), 'count': record.sms_count})

    if 'jtSorting' in request.GET:
        sorting_params = request.GET['jtSorting'].split(' ')
        sorting_field = sorting_params[0]
        reverse = sorting_params[1] == 'DESC'
        result.sort(key=lambda x: x[sorting_field], reverse=reverse)

    try:
        gps_status = all(request.xmlrpc.get_current_gps())
        gps_status = 'yes' if gps_status else 'no'
    except (socket.error, xmlrpclib.Error) as e:
        gps_status = 'failed'

    return {
        'Result': 'OK',
        'Records': result,
        'Messages': messages,
        'GpsStatus': gps_status
    }
    def prepare_ready_for_silent_sms(self):
        while self.__stop_trackin_event.is_set() is False:
            sub_sms = HLRDBSession.query(
                Subscriber.imsi,
                Sms.dest_addr,
                Sms.sent,
                func.max(Sms.created)
            ).select_from(
                Subscriber
            ).outerjoin(
                Sms,
                (
                    (Sms.dest_addr == Subscriber.extension) and
                    (Sms.protocol_id == 64)
                )
            ).group_by(
                Sms.dest_addr
            ).filter(
                Subscriber.extension != self.pf_phone_number
            ).all()

            for (imsi, dest_addr, sent, created) in sub_sms:
                if dest_addr is not None:
                    #if (sent is not None) or (self.__tracking_cicle_number == 0):
                    if (sent is not None):
                        self.__imis_reday_for_silent_sms_list.put(imsi)
                        self.logger.debug("Tracking. Put imsi {0} to queue for send silent sms!".format(imsi))
                    else:
                        self.logger.debug("Tracking. Don't put imsi {0} to queue for send silent sms - not answer previous one!".format(imsi))
                else:
                    self.logger.debug("Tracking. Put imsi {0} to queue for send silent sms!".format(imsi))
                    self.__imis_reday_for_silent_sms_list.put(imsi)

            silent_sms_interval = DBSession.query(Settings.value).filter(Settings.name == "silentSms").all()
            if len(silent_sms_interval) != 1:
                self.logger.error("Settings table not have silentSms value!")
                silent_sms_interval = self.measure_update_period
            else:
                silent_sms_interval = int(silent_sms_interval[0][0]) / 1000

            time.sleep(silent_sms_interval)
            self.__tracking_cicle_number += 1
Beispiel #21
0
class TaskAsyncHandler(RequestHandler):
    executor = ThreadPoolExecutor(THREAD_POOL_MAX)

    def __init__(self, *args, **kwargs):
        self.session = DBSession()
        super(TaskAsyncHandler, self).__init__(*args, **kwargs)

    @gen.coroutine
    def get(self):
        self.set_header('Content-Type', 'application/json')
        req_id = self.get_argument('id')
        if req_id:
            results = yield self.get_data(req_id)
            results
            self.write(json.dumps(results))
        else:
            self.write("id are required!")
        self.finish()

    @run_on_executor
    def get_data(self, req_id):
        """
        执行ansible playbook
        Args:
            resource:  ditto
            playbook: ditto
            variable_dict: playbook variable dict
        """
        results_objs = self.session.query(Record).filter(
            Record.req == str(req_id)).all()
        results_list = [{
            'submit_time':
            obj.submit_time.strftime('"%Y-%m-%d %H:%M:%S'),
            'states':
            obj.states,
            'step':
            obj.step,
            'msg':
            obj.msg
        } for obj in results_objs]
        return results_list
Beispiel #22
0
class SSHPrivateKey(RequestHandler, CommonMiXin):
    executor = ThreadPoolExecutor(THREAD_POOL_MAX)

    def __init__(self, *args, **kwargs):
        self.session = DBSession()
        super(SSHPrivateKey, self).__init__(*args, **kwargs)

    @gen.coroutine
    def get(self, id):
        """
        download ssh private key
        """
        try:
            try:
                ssh_key = self.session.query(SSHKey).get(id)
                private_file = os.path.join(ssh_key.cache_dir, 'id_rsa')
                username = ssh_key.username
            except Exception as e:
                raise WorkFlowError("query database error, %s" % e.message)

            self.set_header('Content-Type', 'application/force-download')
            self.set_header(
                'Content-Disposition',
                'attachment; filename=%s-%s' % (username, 'id_rsa'))
            with open(private_file, "rb") as f:
                try:
                    while True:
                        _buffer = f.read(4096)
                        if _buffer:
                            self.write(_buffer)
                        else:
                            f.close()
                            self.finish()
                            return
                except Exception as e:
                    raise HTTPError(500,
                                    reason='read file failed: %s' % e.message)
        except Exception as e:
            logger.error(e.message)
            self.my_response(400, e.message)
Beispiel #23
0
def get_imsi_circles(request):
    imsi = request.matchdict['imsi']
    ts_begin = request.GET.get('timestamp_begin')
    ts_end = request.GET.get('timestamp_end')

    query = DBSession.query(
        Measure.distance,
        Measure.gps_lat,
        Measure.gps_lon,
        Measure.timestamp
    ).filter(
        (Measure.imsi == imsi) &
        (Measure.gps_lat != None) &
        (Measure.gps_lon != None) &
        (Measure.timestamp <= datetime.fromtimestamp(float(ts_end) / 1000))
    ).order_by(Measure.timestamp.asc())

    if ts_begin is not None:
        query = query.filter(
            Measure.timestamp >= datetime.fromtimestamp(float(ts_begin) / 1000)
        )

    result = {
        'imsi': imsi,
        'circles': []
    }

    for circle in query.all():
        result['circles'].append({
            'center': [
                circle.gps_lat,
                circle.gps_lon
            ],
            'radius': circle.distance,
            'ts': time.mktime(circle.timestamp.timetuple())
        })

    return result
    def prepare_ready_for_silent_sms(self):
        while self.__stop_trackin_event.is_set() is False:
            sub_sms = HLRDBSession.query(
                Subscriber.imsi, Sms.dest_addr, Sms.sent,
                func.max(Sms.created)).select_from(Subscriber).outerjoin(
                    Sms,
                    ((Sms.dest_addr == Subscriber.extension) and
                     (Sms.protocol_id == 64))).group_by(Sms.dest_addr).filter(
                         Subscriber.extension != self.pf_phone_number).all()

            for (imsi, dest_addr, sent, created) in sub_sms:
                if dest_addr is not None:
                    #if (sent is not None) or (self.__tracking_cicle_number == 0):
                    if (sent is not None):
                        self.__imis_reday_for_silent_sms_list.put(imsi)
                        self.logger.debug(
                            "Tracking. Put imsi {0} to queue for send silent sms!"
                            .format(imsi))
                    else:
                        self.logger.debug(
                            "Tracking. Don't put imsi {0} to queue for send silent sms - not answer previous one!"
                            .format(imsi))
                else:
                    self.logger.debug(
                        "Tracking. Put imsi {0} to queue for send silent sms!".
                        format(imsi))
                    self.__imis_reday_for_silent_sms_list.put(imsi)

            silent_sms_interval = DBSession.query(
                Settings.value).filter(Settings.name == "silentSms").all()
            if len(silent_sms_interval) != 1:
                self.logger.error("Settings table not have silentSms value!")
                silent_sms_interval = self.measure_update_period
            else:
                silent_sms_interval = int(silent_sms_interval[0][0]) / 1000

            time.sleep(silent_sms_interval)
            self.__tracking_cicle_number += 1
Beispiel #25
0
 def __init__(self, *args, **kwargs):
     super(SysUserHandler, self).__init__(*args, **kwargs)
     self.validate_data = Dict()
     self.session = DBSession()
Beispiel #26
0
class MyPlaybook(ResourceBase):
    """
    this is my playbook object for execute playbook.
    Attributes:
        resource: resource dict ,see ResourceBase class
        playbook_path: relational playbook path, the default playbook directory is: <PLAYBOOK_DIR>
        req: required id
        model: sqlalchemy data model
        ws_function: web socket write message function
    """
    def __init__(self, resource, playbook_path, req_id, model):
        super(MyPlaybook, self).__init__(resource)
        self.results_raw = None
        self.playbook_path = playbook_path
        self.req = req_id
        self.model = model
        self.count = 0
        self.session = DBSession()

    def save(self, msg):
        if isinstance(msg, dict):
            msg = json.dumps(msg)
            states = 'SUCCESS'
        else:
            states = 'STARTED'
            try:
                msg = msg.decode('utf-8')
            except UnicodeEncodeError:
                msg = msg
        record = self.model(step=self.count,
                            req=self.req,
                            msg=msg,
                            states=states)
        self.session.add(record)
        self.session.commit()
        self.session.close()

    def send_web(self, msg, color):
        pass

    def display(self,
                msg,
                color=None,
                stderr=False,
                screen_only=False,
                log_only=False,
                runner=None):
        self.count += 1
        self.save(msg)
        self.send_web(msg, color)

    def run(self, extra_vars=None):
        """
        run ansible playbook, only surport relational path.
        Args:
            extra_vars: playbook extra variables.
        """
        task_results = []
        stats = CustomAggregateStats(task_results)
        callbacks.display = self.display
        playbook_cb = CustomePlaybookCallbacks(verbose=utils.VERBOSITY,
                                               task_results=task_results)
        runner_cb = PlaybookRunnerCallbacks(stats=stats,
                                            verbose=utils.VERBOSITY)
        playbook_path = os.path.join(PLAYBOOK_DIR, self.playbook_path)

        pb = PlayBook(playbook=playbook_path,
                      stats=stats,
                      callbacks=playbook_cb,
                      runner_callbacks=runner_cb,
                      inventory=self.inventory,
                      extra_vars=extra_vars,
                      module_path=C.DEFAULT_MODULE_PATH,
                      check=False)

        self.results_raw = pb.run()
        self.count += 1
        self.save(self.results_raw)
        return self.results_raw
Beispiel #27
0
 def __init__(self, *args, **kwargs):
     self.session = DBSession()
     super(TaskAsyncHandler, self).__init__(*args, **kwargs)
Beispiel #28
0
 def __init__(self, *args, **kwargs):
     self.session = DBSession()
     super(SSHPrivateKey, self).__init__(*args, **kwargs)
Beispiel #29
0
    logger = logging_utils.get_logger("main")
    logger.info("Comm interface started! pid: {0}".format(os.getpid()))

    # Init DB ================================================================
    pf_db_conn_str = None
    try:
        pf_db_conn_str = configuration.get('app:main', 'sqlalchemy.pf.url')
    except ConfigParser.Error as err:
        logger.error("Identification People Finder DB fail: {0}".format(err.message))
        sys.exit(1)

    logger.info("PF db sqlite path: {0}".format(pf_db_conn_str))
    try:
        bind_session(pf_db_conn_str)
        DBSession.query(Measure).count()
        DBSession.query(Settings).count()
    except:
        logger.error("People finder DB connection err")
        raise

    hlr_db_conn_str = None
    try:
        hlr_db_conn_str = configuration.get('app:main', 'sqlalchemy.hlr.url')
    except ConfigParser.Error as err:
        logger.error("Identification HLR fail: {0}".format(err.message))
        sys.exit(1)

    logger.info("HLR db sqlite path: {0}".format(hlr_db_conn_str))
    try:
        bind_hlr_session(hlr_db_conn_str)
Beispiel #30
0
from wsgiref.simple_server import make_server
from pyramid.session import SignedCookieSessionFactory
from pyramid.config import Configurator
from sqlalchemy import engine_from_config
from model.models import DBSession, Base

my_session_factory = SignedCookieSessionFactory ('kvbjoeifHG4woi7rtc3ndxhq3x0xj398HR47yroq3dj93r98yriwxhiuhd7teu7e')
settings = {'sqlalchemy.url': 'sqlite:///./database/logurlaccess.db', 'sqlalchemy.echo': 'True'}

if __name__ == '__main__':
    with Configurator ( ) as config:
        engine = engine_from_config (settings, 'sqlalchemy.')
        DBSession.configure (bind=engine)
        Base.metadata.bind = engine
        Base.metadata.create_all (engine)

        config.add_route ('home', '/')
        config.add_route ('quotes', '/quotes')
        config.add_route ('values', '/quotes/{value}')
        config.add_route ('consultas', '/consultas')

        config.set_session_factory (my_session_factory)
        config.include ('pyramid_jinja2')
        config.scan ('views')
        app = config.make_wsgi_app ( )

    server = make_server ('0.0.0.0', 8080, app)
    server.serve_forever ( )
Beispiel #31
0
def consultas(request):
    retorno = DBSession.query(LogModel).all()
    json_string = json.dumps([ob.__dict__ for ob in retorno], default=datetime_handler)
    return Response(json_body=json.loads(json_string))
Beispiel #32
0
class SysUserHandler(RequestHandler, CommonMiXin):
    executor = ThreadPoolExecutor(THREAD_POOL_MAX)

    def __init__(self, *args, **kwargs):
        super(SysUserHandler, self).__init__(*args, **kwargs)
        self.validate_data = Dict()
        self.session = DBSession()

    def _validate_json(self):
        try:
            data = json.loads(self.request.body)
            self.validate_data.data = data
        except ValueError as e:
            logger.error("dump input data error: %s" % e.message)
            raise WorkFlowError("input data must be json data structure!")

    def _validate_required(self):
        required_field = ['resource', 'username']
        for required in required_field:
            if required not in self.validate_data.data:
                raise WorkFlowError("%s must be required!" % required)
        self.validate_data.resource = self.validate_data.data['resource']
        self.validate_data.username = self.validate_data.data['username']
        self.validate_data.key_pass = self.validate_data.data.get(
            "key_pass", None)

    def validate(self):
        try:
            self._validate_json()
            self._validate_required()
        except Exception as e:
            return False, e.message
        return True, None

    @gen.coroutine
    def post(self):
        self.add_my_header()
        status, message = self.validate()
        if status:
            response = yield self.add_system_user(self.validate_data.resource,
                                                  self.validate_data.username,
                                                  self.validate_data.key_pass)
            code = 200
        else:
            code = 400
            response = message
        self.my_response(code, response)

    @gen.coroutine
    def put(self):
        self.ImplementError()

    @gen.coroutine
    def get(self):
        self.ImplementError()

    @gen.coroutine
    def delete(self, *args, **kwargs):
        self.add_my_header()
        status, message = self.validate()
        if status:
            response = yield self.del_system_user(self.validate_data.resource,
                                                  self.validate_data.username)
            code = 200
        else:
            code = 400
            response = message
        self.my_response(code, response)

    @run_on_executor
    def add_system_user(self, resource, username, key_password):
        """
        use ansible shell module to execute command on inventory.

        Args:
            resource: inventory resource, see Resource Class
            username: which user you want to add
            key_password: the ssh private key password
        Returns:
            AnsibleReuslt: AnsibleResult instance, contain the all ansible return information.
        """
        task = Task(resource)
        key_dir = os.path.join(SSH_KEY_DIR, uuid4().hex)
        private, public = gen_keys(key_password)
        pri_file, pub_file = save_key_to_file(private, public, key_password,
                                              key_dir)
        add_user = task.add_user(username)
        add_key = task.push_key(username, pub_file)

        try:
            ssh_key = SSHKey(username=username,
                             private_key=private,
                             public_key=public,
                             key_password=key_password,
                             cache_dir=key_dir)
            self.session.add(ssh_key)
            self.session.commit()
        except Exception as e:
            self.session.rollback()
            logger.error("save ssh key instance to database failed!, %s" %
                         e.message)
        finally:
            self.session.close()

        result = {
            "add_user": add_user.result_deal,
            "add_key": add_key.result_deal
        }
        return result

    @run_on_executor
    def del_system_user(self, resource, username):
        """
        use ansible shell module to execute command on inventory.

        Args:
            resource: inventory resource, see Resource Class
            username: which user you want to add
        Returns:
            AnsibleReuslt: AnsibleResult instance, contain the all ansible return information.
        """
        task = Task(resource)
        del_user = task.del_user(username)
        return del_user.result_deal