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 #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 create_daemon(dd):
    ret_val = _confd.OK

    dd.dctx = dp.init_daemon("threads_daemon")
    dd.thread_map = {}

    try:
        # maapi socket for auxiliary tasks
        maapisock = socket.socket()
        maapi.connect(maapisock, CONFD_ADDR, _confd.CONFD_PORT)
        maapi.load_schemas(maapisock)

        # common control socket
        control_thread = PollingThread(dd.dctx,
                                       THR_KEY_CONTROL,
                                       dd.thread_map,
                                       control=True)

        # common control socket
        worker_thread = PollingThread(dd.dctx, THR_KEY_WORKER, dd.thread_map)

        # register transaction callbacks
        tcb = TransCbs(worker_thread.sock)
        dp.register_trans_cb(dd.dctx, tcb)

        # the common data provider for config false data
        dcb = DataCbs()
        dp.register_data_cb(dd.dctx, ns.callpoint_proc, dcb)

        # and the action handler callback object
        acb = ActionCallback(dd.dctx, control_thread.sock)
        dp.register_action_cbs(dd.dctx, ns.actionpoint_sleep, acb)
        dp.register_action_cbs(dd.dctx, ns.actionpoint_totals, acb)

        dp.register_done(dd.dctx)

        # don't forget to start the servicing threads
        control_thread.start()
        worker_thread.start()
    except (KeyboardInterrupt, SystemExit):
        sys.exit(0)
    except Exception as ex:
        eprint(ex.__str__())
        ret_val = _confd.ERR
    finally:
        pass
        # ctlsock.close()
        # wrksock.close()
        # dp.release_daemon(dctx)
    return ret_val
Example #5
0
def action_main():
    ctrl_sock = socket.socket()
    worker_sock = socket.socket()

    dctx = dp.init_daemon("actions_daemon")

    connect(dctx=dctx, csock=ctrl_sock, wsock=worker_sock)

    # register the action handler callback object
    acb = ActionCallbacks(worker_sock=worker_sock)
    dp.register_action_cbs(dctx, 'reboot-point', acb)

    dp.register_done(dctx)
    print("register_done called")

    try:
        poll_loop(dctx, ctrl_sock, worker_sock)
    finally:
        worker_sock.close()
        ctrl_sock.close()
        dp.release_daemon(dctx)
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)