Ejemplo n.º 1
0
def set_message_sent(id_message, svc_port):
    log.info('Set {} sent'.format(id_message))

    import json
    import datetime
    from base_svc.comm import BaseAPIRequestHandler

    n = str(datetime.datetime.now())[:19]

    rh = BaseAPIRequestHandler()
    data = {'id_message': id_message, 'sent_time': n}
    rh.set_argument('data', json.dumps(data, ensure_ascii=False))
    kwargs = {}
    kwargs['request_handler'] = rh

    from base_svc.comm import call
    import base_api.mail_api.sent_mail

    try:
        res, status = call(
            'localhost',
            # base_config.settings.APP_PORT,
            svc_port,
            base_api.mail_api.sent_mail.location,
            data,
            base_api.mail_api.sent_mail.set_mail_sent.__api_method_type__)
    except ConnectionRefusedError as e:
        log.critical('Servis not working: {}'.format(e))
        return False

    if status != 204:
        log.error('Error set message {} sent: {}'.format(id_message, res))

    return True
Ejemplo n.º 2
0
def set_message_sent(id_message, svc_port):
    log.info('Set {} sent'.format(id_message))

    import json
    import datetime
    from base_svc.comm import BaseAPIRequestHandler

    n = str(datetime.datetime.now())[:19]

    rh = BaseAPIRequestHandler()
    data = {'id_message': id_message, 'sent_time': n}
    rh.set_argument('data', json.dumps(data))
    kwargs = {}
    kwargs['request_handler'] = rh

    from base_svc.comm import call
    import base_api.mail_api.sent_mail

    try:
        res, status = call(
            'localhost',
            # base_config.settings.APP_PORT,
            svc_port,
            base_api.mail_api.sent_mail.location,
            data,
            base_api.mail_api.sent_mail.set_mail_sent.__api_method_type__)
    except ConnectionRefusedError as e:
        log.critical('Servis not working: {}'.format(e))
        return False

    if status != 204:
        log.error('Error set message {} sent: {}'.format(id_message, res))

    return True
Ejemplo n.º 3
0
    def initialize(self, apimodule_map, allowed=None, denied=None):

        self.apimodule_map = apimodule_map
        self.apimodule = self.apimodule_map['module']
        self.api_module_name = self.apimodule.name
        self.allowed = allowed
        self.denied = denied
        log.info(self.api_module_name)
Ejemplo n.º 4
0
    def initialize(self, apimodule_map, allowed=None, denied=None):

        self.apimodule_map = apimodule_map
        self.apimodule = self.apimodule_map['module']
        self.api_module_name = self.apimodule.name
        self.allowed = allowed
        self.denied = denied
        log.info(self.api_module_name)
Ejemplo n.º 5
0
def do_post(message, **kwargs):
    """
    Post method of echo API call - test
    """

    log.info('echo.do_post')

    return base_common.msg.post_ok({'echo': message})
    return base_common.msg.post_ok({'echo': message}, http_status=201)
Ejemplo n.º 6
0
def do_delete(message, **kwargs):
    """
    Delete method of echo API call - test
    """

    log.info('echo.do_delete')

    return base_common.msg.delete_ok({'echo': str(message)})
    return base_common.msg.delete_ok({'echo': 'delete echo'}, http_status=202)
Ejemplo n.º 7
0
def do_put(message, **kwargs):
    """
    Put method of echo API call - test
    """

    log.info('echo.put')

    #return base_common.msg.put_ok()
    return base_common.msg.put_ok({'echo': str(message)})
    return base_common.msg.put_ok({'echo': str(message)}, http_status=202)
Ejemplo n.º 8
0
def do_patch(message, **kwargs):
    """
    Patch method of echo API call - test
    """

    log.info('echo.do_patch')

    # return base_common.msg.put_ok()
    return base_common.msg.patch_ok({'echo': message})
    return base_common.msg.patch_ok({'echo': message}, http_status=201)
Ejemplo n.º 9
0
def entry_point(api_path, api_module_map, allowed=None, denied=None):

    api_module = api_module_map['module']

    log.info("Registering {}".format(api_module.name))

    base_pkg = hasattr(api_module, 'BASE') and api_module.BASE

    _uri = "^/{}{}".format("" if base_pkg else "{}/".format(api_module.PREFIX), api_path)

    return _uri, base_svc.comm.GeneralPostHandler, dict(allowed=allowed, denied=denied, apimodule_map=api_module_map)
Ejemplo n.º 10
0
def start_tests(app_started):

    import subprocess
    import base_tests.basetest

    s = subprocess.Popen(
        ["python3", base_tests.basetest.__file__, app_started],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        stdin=subprocess.PIPE)
    log.info('Tests started on PID: {}'.format(s.pid))
Ejemplo n.º 11
0
def do_post(message, **kwargs):
    """
    Post method of echo API call - title
    :description:
    Post method of echo API call - description

    """

    log.info("echo.do_post")

    return base_common.msg.post_ok({"echo": message})
    return base_common.msg.post_ok({"echo": message}, http_status=201)
Ejemplo n.º 12
0
def do_post(message, **kwargs):
    """
    Post method of echo API call - title
    :description:
    Post method of echo API call - description

    """

    log.info('echo.do_post')

    return base_common.msg.post_ok({'echo': message})
    return base_common.msg.post_ok({'echo': message}, http_status=201)
Ejemplo n.º 13
0
def do_delete(message, **kwargs):
    """
    Delete method of echo API call - title
    :description:
    Delete method of echo API call - description

    """

    log.info('echo.do_delete')

    return base_common.msg.delete_ok({'echo': str(message)})
    return base_common.msg.delete_ok({'echo': 'delete echo'}, http_status=202)
Ejemplo n.º 14
0
def do_delete(message, **kwargs):
    """
    Delete method of echo API call - title
    :description:
    Delete method of echo API call - description

    """

    log.info("echo.do_delete")

    return base_common.msg.delete_ok({"echo": str(message)})
    return base_common.msg.delete_ok({"echo": "delete echo"}, http_status=202)
Ejemplo n.º 15
0
def do_put(message, **kwargs):
    """
    Put method of echo API call - title
    :description:
    Put method of echo API call - description

    """

    log.info("echo.put")

    # return base_common.msg.put_ok()
    return base_common.msg.put_ok({"echo": str(message)})
    return base_common.msg.put_ok({"echo": str(message)}, http_status=202)
Ejemplo n.º 16
0
def do_put(message, **kwargs):
    """
    Put method of echo API call - title
    :description:
    Put method of echo API call - description

    """

    log.info('echo.put')

    #return base_common.msg.put_ok()
    return base_common.msg.put_ok({'echo': str(message)})
    return base_common.msg.put_ok({'echo': str(message)}, http_status=202)
Ejemplo n.º 17
0
def do_patch(message, **kwargs):
    """
    Patch method of echo API call - title
    :description:
    Patch method of echo API call - description

    """

    log.info('echo.do_patch')

    # return base_common.msg.put_ok()
    return base_common.msg.patch_ok({'echo': message})
    return base_common.msg.patch_ok({'echo': message}, http_status=201)
Ejemplo n.º 18
0
def do_patch(message, **kwargs):
    """
    Patch method of echo API call - title
    :description:
    Patch method of echo API call - description

    """

    log.info("echo.do_patch")

    # return base_common.msg.put_ok()
    return base_common.msg.patch_ok({"echo": message})
    return base_common.msg.patch_ok({"echo": message}, http_status=201)
Ejemplo n.º 19
0
def start_tests(app_started, t_stage, t_keep):
    import subprocess
    import base_tests.basetest

    test_cmd = ["python3", base_tests.basetest.__file__, app_started, csettings.APP_DB.db,
                          csettings.APP_DB.user, csettings.APP_DB.passwd]

    if t_stage:
        test_cmd.append('-s')
        test_cmd.append(t_stage)

    if t_keep:
        test_cmd.append('-k')
        test_cmd.append(t_keep)

    s_pid = os.getpid()
    test_cmd.append('-p')
    test_cmd.append('{}'.format(s_pid))

    s = subprocess.Popen(test_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
    log.info('Tests started on PID: {}'.format(s.pid))
Ejemplo n.º 20
0
def send_message(sender, _to, subject, message):

    receiver = [{'email': _to, 'name': _to, 'type': 'to'}]
    msg = {
        'from_email': sender,
        'from_name': sender,
        'html': message,
        'subject': subject,
        'to': receiver,
        # 'auto_html': None,        # automatic html generating if html not provided
        # 'auto_text': None,        # automatic text generating if text not provided
        # 'bcc_address': '*****@*****.**', # bcc address
        # 'headers': {'Reply-To': '*****@*****.**'}, # additional headers
        # 'inline_css': None,       # if css has to be inline (only for <=256 line messages)
        # 'preserve_recipients': None,  # if every receiver has to see other receivers in header
        # 'text': 'Example text content',   # message plain text
        # 'view_content_link': None     # False for disable content logging for sensitive mails
    }
    # if _site:
    #     msg['metadata'] = {'website': _site}
    # if tags:
    #     msg['tags'] = tags

    m = get_mail_api()
    try:
        res = m.messages.send(message=msg, async=False)
    except mandrill.Error as e:
        log.critical('MANDRILL send message error: {}'.format(e))
        return False

    if 'status' not in res or res['status'] != 'sent':
        log.warning('MANDRILL send mail error status: {}'.format(res))
        return False

    log.info('Receiver: {}, status: {}'.format(_to, res))
    return True
Ejemplo n.º 21
0
def send_message(sender, _to, subject, message):

    receiver = [{'email': _to, 'name': _to, 'type': 'to'}]
    msg = {
        'from_email': sender,
        'from_name': sender,
        'html': message,
        'subject': subject,
        'to': receiver,
        # 'auto_html': None,        # automatic html generating if html not provided
        # 'auto_text': None,        # automatic text generating if text not provided
        # 'bcc_address': '*****@*****.**', # bcc address
        # 'headers': {'Reply-To': '*****@*****.**'}, # additional headers
        # 'inline_css': None,       # if css has to be inline (only for <=256 line messages)
        # 'preserve_recipients': None,  # if every receiver has to see other receivers in header
        # 'text': 'Example text content',   # message plain text
        # 'view_content_link': None     # False for disable content logging for sensitive mails
    }
    # if _site:
    #     msg['metadata'] = {'website': _site}
    # if tags:
    #     msg['tags'] = tags

    m = get_mail_api()
    try:
        res = m.messages.send(message=msg, async=False)
    except mandrill.Error as e:
        log.critical('MANDRILL send message error: {}'.format(e))
        return False

    if 'status' not in res or res['status'] != 'sent':
        log.warning('MANDRILL send mail error status: {}'.format(res))
        return False

    log.info('Receiver: {}, status: {}'.format(_to, res))
    return True
Ejemplo n.º 22
0
def do_post(username, password, **kwargs):
    """
    User login
    """

    _db = get_db()
    dbc = _db.cursor()

    log.info('User {} trying to login'.format(username))
    username = username.lower()
    q = apphooks.prepare_login_query(username)

    ip = kwargs['r_ip']

    dbc.execute(q)
    if dbc.rowcount != 1:
        msg = '{} user not found: {}'.format(username, dbc.rowcount)
        log.critical(msg)
        apphooks.action_log_hook(None, ip, 'login', msg)
        return base_common.msg.error(msgs.USER_NOT_FOUND)

    us = dbc.fetchone()
    u_id = us['id']
    u_pwd = us['password']

    upwd = None
    try:
        with open('/tmp/upwd.base') as f:
            upwd = f.read()

    except Exception as e:
        pass

    if not upwd or upwd != password:

        if not check_password(u_pwd, username, password):
            msg = 'Username {} wrong password: {}'.format(username, password)
            log.critical(msg)
            apphooks.action_log_hook(None, ip, 'login', msg)
            return base_common.msg.error(msgs.USER_NOT_FOUND)

    if hasattr(apphooks, 'login_expansion') and not apphooks.login_expansion(us):
        return base_common.msg.error(msgs.ERROR_LOGIN_USER)

    # ASSIGN TOKEN
    tk = get_token(u_id, dbc)
    if not tk:
        return base_common.msg.error(msgs.ERROR_LOGIN_USER)

    _db.commit()

    res = {'token': tk}

    if hasattr(apphooks, 'post_login_digest'):
        post_d = apphooks.post_login_digest(_db, u_id, username, password, tk)
        if post_d == False:
            log.critical('Error user post login digest')
            return base_common.msg.error(msgs.ERROR_POST_LOGIN)

        if isinstance(post_d, dict):
            res.update(post_d)

    apphooks.action_log_hook(u_id,ip, 'login', 'user {} successfuly logged in'.format(username))
    log.info('User {} successfully logged in'.format(username))
    return base_common.msg.post_ok(res)
Ejemplo n.º 23
0
    def call_api_fun(self, method):

        self.set_header('Access-Control-Allow-Origin', '*')

        j, ip = self.check()

        try:
            if method in self.apimodule_map:

                fun = self.apimodule_map[method]

                if hasattr(fun, '__api_return_type__'):
                    self.set_api_header(getattr(fun, '__api_return_type__'))
                else:
                    self.set_header('Content-Type', 'application/json')

                if not hasattr(
                        fun,
                        '__api_method_call__') or not fun.__api_method_call__:
                    self.set_status(500)
                    self.write(
                        json.dumps(base_common.msg.error(amsgs.NOT_API_CALL,
                                                         forget_status=True),
                                   ensure_ascii=False))
                    return

                # k = fun.__name__
                # p = fun.__parent__
                _fun_method = getattr(fun, '__api_method_type__')
                _fun_method = http_map[_fun_method]
                if method != _fun_method:
                    log.critical(
                        'Trying to call {} method on {} function from {}'.
                        format(method, fun.__name__, self.apimodule.__name__))

                    self.set_status(500)
                    self.write(
                        json.dumps(base_common.msg.error(self.e_msgs[method],
                                                         forget_status=True),
                                   ensure_ascii=False))
                    return

                if csettings.LB:

                    global _c
                    _server = csettings.BALANCE[_c % len(csettings.BALANCE)]
                    _c += 1

                    self._a_p = _server[-4:]
                    log.info('CALLING SERVER: {}'.format(self._a_p))
                    self.a_call(_server, method)
                    return

                else:

                    result = fun(request_handler=self,
                                 logged_user_dict=j,
                                 r_ip=ip,
                                 auth_token=self.auth_token)

                if 'redirect' in result and result['redirect']:
                    if not 'redirect_url' in result:
                        self.set_status(500)
                        self.set_header('Content-Type', 'application/json')
                        self.write(
                            json.dumps(base_common.msg.error(
                                amsgs.MISSING_REDIRECTION_URL,
                                forget_status=True),
                                       ensure_ascii=False))

                    _redirection_status = result[
                        'redirection_http_status'] if 'redirection_http_status' in result else 302
                    _permanent = _redirection_status == 301 or 'permanent' in result and result[
                        'permanent']
                    self.redirect(result['redirect_url'],
                                  permanent=_permanent,
                                  status=_redirection_status)
                    return

                self.set_status(200)
                if 'http_status' in result:
                    self.set_status(result['http_status'])
                    del result['http_status']

                if result != {}:
                    self.write(json.dumps(result, ensure_ascii=False))

            else:
                log.error("ip: {}, {} not implemented".format(
                    ip, http_rev_map[method]))
                log.error(
                    "ip: {}, {} ({}) not implemented, with module map: {}".
                    format(ip, http_rev_map[method], method,
                           self.apimodule_map))
                self.set_status(404)
                self.set_header('Content-Type', 'application/json')
                self.write(
                    json.dumps(base_common.msg.error(self.e_msgs[method],
                                                     forget_status=True),
                               ensure_ascii=False))

        except Exception as e:

            log.error(
                "ip: {}, module: {}, function: {}, exception: e:{}".format(
                    ip, self.apimodule.__name__, method, e))
            self.set_status(500)
            self.set_header('Content-Type', 'application/json')
            self.write(
                json.dumps(base_common.msg.error(e, forget_status=True),
                           ensure_ascii=False))
Ejemplo n.º 24
0
def start_base_service():
    close_stdout(csettings.DEBUG)

    imported_modules = {}
    installed_apps = {}

    try:
        get_installed_apps(installed_apps)
    except ApplicationNameUsed as e:
        log.critical('Application name collision, check installed applications: {}'.format(e))
        print('Application name collision, check installed applications: {}'.format(e))
        sys.exit(4)

    b_args = check_args(installed_apps)

    svc_port = b_args.port if b_args.port else installed_apps[b_args.app]['svc_port']
    check_test_port_is_used(svc_port, b_args.app)

    if not svc_port:
        log.critical('No svc port provided, lok in app config or add in commandline')
        print('Please provide svc_port in app init or trough commandline')
        sys.exit(3)

    from base_common.dbaexc import BalancingAppException
    try:
        import_from_settings(imported_modules, b_args.app)
    except BalancingAppException as e:
        log.critical('Error loading balance servers: {}'.format(e))
        sys.exit(5)

    entry_points = [entry_point(p, m) for p, m in imported_modules.items()]

    if b_args.test:
        setattr(csettings, 'TEST_MODE', True)
        prepare_test_env()
        svc_port = csettings.TEST_PORT
        start_tests(b_args.app, b_args.test, b_args.keep)

    baseapi_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
    tpl_dir = os.path.join(baseapi_dir, 'templates')
    st_dir = os.path.join(baseapi_dir, 'static')

    application = tornado.web.Application([
        (r'^/$', base_svc.comm.MainHandler),
        (r'^/spec.*$', base_svc.comm.MainHandler),
        *entry_points
    ],
        template_path=tpl_dir,
        static_path=st_dir,
        cookie_secret="d1g1t4l", debug=csettings.DEBUG)

    try:
        application.listen(svc_port)
        log.info('Starting base_svc v({}) on port {}'.format(csettings.__VERSION__, svc_port))
        tornado.ioloop.IOLoop.instance().start()

    except gaierror:
        log.critical("Missing port for service, port = {}".format(svc_port))
        print("Missing port for service, port = {}".format(svc_port))

    except Exception as e:
        log.critical("Exception: {}".format(e))
        print("Critical exception: {}".format(e), file=sys.stderr)
Ejemplo n.º 25
0
import sys
import json
from base_config.service import log
import base_common.dbacommon
import base_config.settings
import base_common.send_mail_common as smcommon

__SVC_PORT = int(sys.argv[1])


if __name__ == '__main__':

    dbr = base_common.dbacommon.get_redis_db()

    blocked = False
    while not blocked:
        task = dbr.brpop(base_config.settings.MAIL_CHANNEL, base_config.settings.MAIL_CHANNEL_TIMEOUT)

        if task is not None:

            task = task[1].decode('utf-8')  # tuple 0-channel, 1-task
            task = json.loads(task)
            log.info('got: {}'.format(task['id']))

            if not smcommon.send_message(task['sender'], task['receiver'], task['subject'], task['message']):
                log.critical('Error sending message: {}'.format(task['id']))
                continue

            if not smcommon.set_message_sent(task['id'], __SVC_PORT):
                blocked = True
Ejemplo n.º 26
0
    def call_api_fun(self, method):

        self.set_header('Access-Control-Allow-Origin', '*')

        j, ip = self.check()

        try:
            if method in self.apimodule_map:

                fun = self.apimodule_map[method]

                if hasattr(fun,'__api_return_type__'):
                    self.set_api_header(getattr(fun,'__api_return_type__'))
                else:
                    self.set_header('Content-Type', 'application/json')

                if not hasattr(fun, '__api_method_call__') or not fun.__api_method_call__:
                    self.set_status(500)
                    self.write(json.dumps(base_common.msg.error(amsgs.NOT_API_CALL)))
                    return

                # k = fun.__name__
                # p = fun.__parent__
                _fun_method = getattr(fun, '__api_method_type__')
                _fun_method = http_map[_fun_method]
                if method != _fun_method:
                    log.critical('Trying to call {} method on {} function from {}'.format(
                        method, fun.__name__, self.apimodule.__name__))

                    self.set_status(500)
                    self.write(json.dumps(base_common.msg.error(self.e_msgs[method])))
                    return

                if csettings.LB:

                    global _c
                    _server = csettings.BALANCE[ _c % len(csettings.BALANCE) ]
                    _c += 1

                    self._a_p = _server[-4:]
                    log.info('CALLING SERVER: {}'.format(self._a_p))
                    self.a_call(_server, method)
                    return

                else:

                    result = fun(request_handler=self, logged_user_dict=j, r_ip=ip, auth_token=self.auth_token)

                self.set_status(200)
                if 'http_status' in result:
                    self.set_status(result['http_status'])
                    del result['http_status']

                if result != {}:
                    self.write(json.dumps(result))

            else:
                log.error("ip: {}, {} not implemented".format(ip, http_rev_map[method]))
                self.set_status(404)
                self.set_header('Content-Type', 'application/json')
                self.write(json.dumps(base_common.msg.error(self.e_msgs[method])))

        except Exception as e:

            log.error(
                "ip: {}, module: {}, function: {}, exception: e:{}".format(ip, self.apimodule.__name__, method, e))
            self.set_status(500)
            self.set_header('Content-Type', 'application/json')
            self.write(json.dumps(base_common.msg.error(e)))