Example #1
0
    def __init__(self, dctx, key, thread_map, control=False):
        super(PollingThread, self).__init__()
        self.dctx = dctx
        self.stop_flag = False
        self.daemon = True

        # create the socket
        self.sock = socket.socket(family=socket.AF_INET,
                                  type=socket.SOCK_STREAM,
                                  proto=0)

        thr_type = dp.WORKER_SOCKET
        if control:
            thr_type = dp.CONTROL_SOCKET

        dp.connect(dx=dctx,
                   sock=self.sock,
                   type=thr_type,
                   ip=CONFD_ADDR,
                   port=_confd.CONFD_PORT)

        self.key = key
        self.thread_map = thread_map
        # add thread reference to global map for potential abort etc.
        print('[thr# ' + key + '] new thread')
        if key in self.thread_map:
            print('WARN - got recycled worker')
            del self.thread_map[key]
        self.thread_map[self.key] = self
Example #2
0
def run():
    # In C we use confd_init() which sets the debug-level, but for Python the
    # call to confd_init() is done when we do 'import confd'.
    # Therefore we need to set the debug level here:
    _confd.set_debug(_confd.TRACE, sys.stderr)

    ctx = dp.init_daemon("arpe_daemon")
    maapisock = socket.socket()
    ctlsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    wrksock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    try:
        maapi.connect(maapisock, '127.0.0.1', _confd.CONFD_PORT)
        dp.connect(ctx, ctlsock, dp.CONTROL_SOCKET, '127.0.0.1',
                   _confd.CONFD_PORT, '/')
        dp.connect(ctx, wrksock, dp.WORKER_SOCKET, '127.0.0.1',
                   _confd.CONFD_PORT, '/')

        maapi.load_schemas(maapisock)

        arp = ArpRunner()

        tcb = TransCbs(wrksock, arp)
        dp.register_trans_cb(ctx, tcb)
        dcb = DataCbs(arp)
        dp.register_data_cb(ctx, arpe_ns.ns.callpoint_arpe, dcb)

        dp.register_done(ctx)

        try:
            _r = [ctlsock, wrksock]
            _w = []
            _e = []

            while (True):
                (r, w, e) = select.select(_r, _w, _e, 1)
                for rs in r:
                    if rs.fileno() == ctlsock.fileno():
                        try:
                            dp.fd_ready(ctx, ctlsock)
                        except (_confd.error.Error) as e:
                            if e.confd_errno is not _confd.ERR_EXTERNAL:
                                raise e
                    elif rs.fileno() == wrksock.fileno():
                        try:
                            dp.fd_ready(ctx, wrksock)
                        except (_confd.error.Error) as e:
                            if e.confd_errno is not _confd.ERR_EXTERNAL:
                                raise e

        except KeyboardInterrupt:
            print("\nCtrl-C pressed\n")

    finally:
        ctlsock.close()
        wrksock.close()
        dp.release_daemon(ctx)
Example #3
0
def init(dname="ncpy"):
    global ctrsock, dx, sock  # must not be deallocated
    ctrsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    dx = dp.init_daemon(dname)
    dp.connect(dx, ctrsock, dp.CONTROL_SOCKET, '127.0.0.1', 4565)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    dp.connect(dx, sock, dp.WORKER_SOCKET, '127.0.0.1', 4565)
    stream = dp.register_notification_stream(dx, object(), sock, 'test-stream')
    dp.register_done(dx)
    return stream
Example #4
0
def connect(dctx, csock, wsock):
    """
    Connect the sockets
    """
    # Create the first control socket, all requests to
    # create new transactions arrive here
    dp.connect(dx=dctx,
               sock=csock,
               type=dp.CONTROL_SOCKET,
               ip='127.0.0.1',
               port=4565)

    # Also establish a workersocket, this is the most simple
    # case where we have just one ctlsock and one workersock
    dp.connect(dx=dctx,
               sock=wsock,
               type=dp.WORKER_SOCKET,
               ip='127.0.0.1',
               port=4565)
def init_confd_daemon():
    log.info("==>")
    global maapisock, maapisock_commit, ctlsock, workersock, workersock_commit,\
        dctx
    # In C we use confd_init() which sets the debug-level, but for Python the
    # call to confd_init() is done when we do 'import confd'.
    # Therefore we need to set the ConfD debug level here (if we want it to be
    # different from the default debug level - CONFD_SILENT):
    _confd.set_debug(confd_debug_level, sys.stderr)

    maapisock = socket.socket()
    maapi.connect(sock=maapisock, ip=CONFD_ADDR, port=_confd.CONFD_PORT)
    maapisock_commit = socket.socket()
    maapi.connect(sock=maapisock_commit, ip=CONFD_ADDR, port=_confd.CONFD_PORT)
    maapi.load_schemas(maapisock)  # in Python load schemas through maapi

    ctlsock = socket.socket()
    workersock = socket.socket()
    workersock_commit = socket.socket()
    dctx = dp.init_daemon("actions_daemon")
    dp.connect(dx=dctx,
               sock=ctlsock,
               type=dp.CONTROL_SOCKET,
               ip=CONFD_ADDR,
               port=_confd.CONFD_PORT)

    dp.connect(dx=dctx,
               sock=workersock,
               type=dp.WORKER_SOCKET,
               ip=CONFD_ADDR,
               port=_confd.CONFD_PORT)

    dp.connect(dx=dctx,
               sock=workersock_commit,
               type=dp.WORKER_SOCKET,
               ip=CONFD_ADDR,
               port=_confd.CONFD_PORT)

    dp.register_trans_cb(dctx, TransCbs())

    # validation
    dp.register_trans_validate_cb(dctx, TransValCbs())
    dp.register_valpoint_cb(dctx, maapi_example_ns.ns.validate_val_items,
                            ValCbs())

    # data provider
    dp.register_data_cb(dctx, maapi_example_ns.ns.callpoint_items, DataCbs())

    # clispec command actions
    dp.register_action_cbs(dctx, "start_count_cp", ActStartCountCbs())
    dp.register_action_cbs(dctx, "show_items_with_value_cp", ActShowItemsCbs())
    dp.register_action_cbs(dctx, "show_items_with_smaller_than_value_cp",
                           ActShowItemsSmallerCbs())
    dp.register_action_cbs(dctx, "start_confirmed_commit",
                           ActConfirmedCommit())
    dp.register_done(dctx)
    log.info("<== Initialization complete")
Example #6
0
def main():
    """ Main execution of the transformer daemon.
    """
    global maapi_socket

    _confd.set_debug(debug_level, sys.stderr)

    daemon_ctx = dp.init_daemon(daemon_name)

    maapi_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    control_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    worker_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)

    try:
        maapi.connect(maapi_socket, dest_ip, dest_port, path=ROOT_PATH)
        maapi.load_schemas(maapi_socket)

        dp.connect(daemon_ctx,
                   control_sock,
                   dp.CONTROL_SOCKET,
                   dest_ip,
                   dest_port,
                   path=ROOT_PATH)

        dp.connect(daemon_ctx,
                   worker_sock,
                   dp.WORKER_SOCKET,
                   dest_ip,
                   dest_port,
                   path=ROOT_PATH)

        transaction_cb = TransactionCallbackImpl(worker_sock)
        dp.register_trans_cb(daemon_ctx, transaction_cb)

        transform_cb = TransformCallbackImpl()
        dp.register_data_cb(daemon_ctx, ns_folders.callpoint_transcp,
                            transform_cb)

        dp.register_done(daemon_ctx)

        try:
            read_list = [control_sock, worker_sock]
            write_list = []
            error_list = []

            fd_map = {
                control_sock.fileno(): control_sock,
                worker_sock.fileno(): worker_sock
            }

            print('entering poll loop')
            while True:
                read_socks = select.select(read_list, write_list, error_list,
                                           1)[0]
                for rs in read_socks:
                    sock = fd_map[rs.fileno()]
                    try:
                        dp.fd_ready(daemon_ctx, sock)
                    except _confd.error.Error as ex:
                        traceback.print_exc()
                        if ex.confd_errno is not confd.ERR_EXTERNAL:
                            raise ex

        except KeyboardInterrupt:
            print("Ctrl-C pressed\n")
    finally:
        control_sock.close()
        worker_sock.close()
        dp.release_daemon(daemon_ctx)
Example #7
0
def run(debuglevel):

    # In C we use confd_init() which sets the debug-level, but for Python the
    # call to confd_init() is done when we do 'import confd'.
    # Therefore we need to set the debug level here:
    _confd.set_debug(debuglevel, sys.stderr)

    # init library
    ctx = dp.init_daemon("hosts_daemon")

    # initialize our simple database
    db = Dlist()
    if db.restore("RUNNING.ckp"):
        print("Restoring from checkpoint\n")
    elif db.restore("RUNNING.db"):
        print("Restoring from RUNNING.db\n")
    else:
        print("Starting with empy DB\n")

    db.show()

    maapisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    ctlsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    wrksock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    try:
        maapi.connect(maapisock, '127.0.0.1', _confd.CONFD_PORT)
        dp.connect(ctx, ctlsock, dp.CONTROL_SOCKET, '127.0.0.1',
                   _confd.CONFD_PORT, '/')
        dp.connect(ctx, wrksock, dp.WORKER_SOCKET, '127.0.0.1',
                   _confd.CONFD_PORT, '/')

        maapi.load_schemas(maapisock)

        # Transaction and db callbacks
        tcb = TransCbs(wrksock)
        dcb = DbCbs(wrksock, db)
        dp.register_trans_cb(ctx, tcb)
        dp.register_db_cb(ctx, dcb)

        # Read/Write callbacks
        host_cbks = HostDataCbs('hcp', db)
        iface_cbks = IfaceDataCbs('icp', db)
        if dp.register_data_cb(ctx, 'hcp', host_cbks) == _confd.CONFD_ERR:
            print("Failed to register host cb\n")
            exit(1)
        if dp.register_data_cb(ctx, 'icp', iface_cbks):
            print("Failed to register interface cb\n")
            exit(1)

        dp.register_done(ctx)

        p = Prompt(db)
        try:
            _r = [sys.stdin, ctlsock, wrksock]
            _w = []
            _e = []

            while (True):
                (r, w, e) = select.select(_r, _w, _e, 1)
                for rs in r:
                    if rs.fileno() == ctlsock.fileno():
                        try:
                            dp.fd_ready(ctx, ctlsock)
                        except (_confd.error.Error) as e:
                            if e.confd_errno is not _confd.ERR_EXTERNAL:
                                raise e
                    elif rs.fileno() == wrksock.fileno():
                        try:
                            dp.fd_ready(ctx, wrksock)
                        except (_confd.error.Error) as e:
                            if e.confd_errno is not _confd.ERR_EXTERNAL:
                                raise e
                    elif rs == sys.stdin:
                        p.handle_stdin(sys.stdin.readline().rstrip())

        except KeyboardInterrupt:
            print("\nCtrl-C pressed\n")

    finally:
        ctlsock.close()
        wrksock.close()
        dp.release_daemon(ctx)
Example #8
0
def run(debuglevel):

    # In C we use confd_init() which sets the debug-level, but for Python the
    # call to confd_init() is done when we do 'import confd'.
    # Therefore we need to set the debug level here:
    _confd.set_debug(debuglevel, sys.stderr)

    # init library
    daemon_ctx = dp.init_daemon('hosts_daemon')

    # initialize our simple database
    custom_db = CustomDatabase()
    if custom_db.restore_db(DBFILE_CHECKPOINT):
        print('Restored from checkpoint\n')
    elif custom_db.restore_db(DBFILE_RUNNING):
        print('Restored from RUNNING.db\n')
    else:
        print('Starting with empty DB\n')

    custom_db.show()

    confd_addr = '127.0.0.1'
    confd_port = _confd.PORT
    managed_path = '/'

    maapisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    ctlsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    wrksock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    try:
        dp.connect(daemon_ctx, ctlsock, dp.CONTROL_SOCKET, confd_addr,
                   confd_port, managed_path)
        dp.connect(daemon_ctx, wrksock, dp.WORKER_SOCKET, confd_addr,
                   confd_port, managed_path)

        maapi.connect(maapisock, confd_addr, confd_port, managed_path)
        maapi.load_schemas(maapisock)

        transaction_cb = TransCbs(wrksock, custom_db)
        dp.register_trans_cb(daemon_ctx, transaction_cb)

        database_cb = DatabaseCbs(wrksock, custom_db)
        dp.register_db_cb(daemon_ctx, database_cb)

        host_data_cb = HostDataCbs(custom_db)
        dp.register_data_cb(daemon_ctx, model_ns.callpoint_hcp, host_data_cb)

        iface_data_cb = IfaceDataCbs(custom_db)
        dp.register_data_cb(daemon_ctx, model_ns.callpoint_icp, iface_data_cb)

        dp.register_done(daemon_ctx)

        p = Prompt(custom_db)
        try:
            _r = [sys.stdin, ctlsock, wrksock]
            _w = []
            _e = []

            while True:
                (r, w, e) = select.select(_r, _w, _e, 1)

                for rs in r:

                    if rs.fileno() == ctlsock.fileno():
                        try:
                            dp.fd_ready(daemon_ctx, ctlsock)
                        except _confd.error.Error as e:
                            if e.confd_errno is not _confd.ERR_EXTERNAL:
                                raise e
                    elif rs.fileno() == wrksock.fileno():
                        try:
                            dp.fd_ready(daemon_ctx, wrksock)
                        except _confd.error.Error as e:
                            if e.confd_errno is not _confd.ERR_EXTERNAL:
                                raise e
                    elif rs == sys.stdin:
                        p.handle_stdin(sys.stdin.readline().rstrip())

        except KeyboardInterrupt:
            print('\nCtrl-C pressed\n')

    finally:
        ctlsock.close()
        wrksock.close()
        dp.release_daemon(daemon_ctx)