Exemplo n.º 1
0
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")
Exemplo n.º 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)
Exemplo n.º 3
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
Exemplo n.º 4
0
def run():
    log.info("==>")

    # 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)
    subsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    # maapi socket for load schemas
    maapisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    maapi.connect(maapisock, CONFD_ADDR, _confd.CONFD_PORT)
    maapi.load_schemas(maapisock)

    cdb.connect(subsock, cdb.SUBSCRIPTION_SOCKET, CONFD_ADDR, _confd.CONFD_PORT)
    spoint = cdb.subscribe(subsock, 3, user_folders_ns.ns.hash, ROOT_PATH)
    cdb.subscribe_done(subsock)
    log.debug("Subscribed to path %s spoint=%i" % (ROOT_PATH, spoint))
    log.info("CDB subscriber initialized!")
    try:
        _r = [subsock]
        _w = []
        _e = []
        log.debug("subscok connected, starting ConfD loop")
        while True:
            (r, w, e) = select.select(_r, _w, _e, 1)
            for rs in r:
                log.debug("rs.fileno=%i subscok.fileno=%i" % (
                    rs.fileno(), subsock.fileno()))
                if rs.fileno() == subsock.fileno():
                    log.debug("subsock triggered")
                    try:
                        process_subscriptions(spoint, subsock)
                    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:
        subsock.close()

    log.info("<==")
Exemplo n.º 5
0
    def exec_totals(self, uinfo):
        print('exec totals')

        key = get_thread_key(uinfo)
        print('[thr# ' + key + '] watch thread for abort request')
        thr = self.thread_map[key]

        # import pdb; pdb.set_trace()

        # open own MAAPI connection for reading values
        msock = socket.socket()
        maapi.connect(msock, CONFD_ADDR, _confd.CONFD_PORT)
        th = maapi.start_trans2(msock, _confd.RUNNING, _confd.READ, uinfo.usid)

        path = "/model:dm/proc"

        mc = maapi.init_cursor(msock, th, path)

        # gather data from ConfD
        cpu_sum = 0
        cpu_cnt = 0
        keys = maapi.get_next(mc)
        while not thr.stop_flag and keys is not False:
            cpu = maapi.get_elem(msock, th,
                                 path + '{' + str(keys[0]) + '}/cpu')
            cpu_sum += int(cpu)
            cpu_cnt += 1
            keys = maapi.get_next(mc)

        # clean the used MAAPI session
        maapi.destroy_cursor(mc)
        maapi.close(msock)

        # and dispatch result to ConfD
        result = [
            make_tag_value(ns.hash, ns.model_num_procs, int(cpu_cnt),
                           _confd.C_UINT32),
            make_tag_value(ns.hash, ns.model_total_cpu, int(cpu_sum),
                           _confd.C_UINT32)
        ]

        dp.action_reply_values(uinfo, result)
        return _confd.OK
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 9
0
<?xml version="1.0" encoding="UTF-8"?>
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="7">
  <cancel-commit>
    <persist-id>sync_commit</persist-id>
  </cancel-commit>
</rpc>
]]>]]>
<?xml version="1.0" encoding="UTF-8"?>
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="8">
  <close-session/>
</rpc>
]]>]]>'''


ms = socket.socket()
maapi.connect(ms, CONFD_ADDR, confd.PORT)
maapi.load_schemas(ms)
ms.close


class StdoutError(Exception):
    def __init__(self, message):
        super(StdoutError, self).__init__(self, message)
        self.message = message


class StderrError(Exception):
    def __init__(self, message):
        super(StderrError, self).__init__(self, message)
        self.message = message
Exemplo n.º 10
0
def run():
    """ Main subscriber thread execution. """
    confd_addr = '127.0.0.1'
    confd_port = _confd.PORT

    _confd.set_debug(_confd.SILENT, sys.stderr)

    sub_path = "/system/ip/route"

    # MAAPI socket to load schemas for a nicer print() calls
    maapi_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    maapi.connect(maapi_sock, confd_addr, confd_port, sub_path)
    maapi.load_schemas(maapi_sock)

    # socket for subscription data iteration
    data_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    cdb.connect(data_sock, cdb.DATA_SOCKET, confd_addr, confd_port, sub_path)

    # tailf:cdb-oper subscription socket
    oper_sub_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    cdb.connect(oper_sub_sock, cdb.SUBSCRIPTION_SOCKET, confd_addr, confd_port,
                sub_path)

    oper_sub_point = cdb.oper_subscribe(oper_sub_sock, routes_ns.ns.hash,
                                        sub_path)
    cdb.subscribe_done(oper_sub_sock)

    try:
        read_list = [oper_sub_sock]
        write_list = []
        error_list = []

        print('entering poll loop')
        while True:
            read_socks = select.select(read_list, write_list, error_list, 1)[0]
            for rs in read_socks:
                # process only our cdb-oper subscription socket here...
                if rs.fileno() is not oper_sub_sock.fileno():
                    continue
                try:
                    sub_points = cdb.read_subscription_socket(oper_sub_sock)
                    for s in sub_points:
                        if s != oper_sub_point:
                            continue
                        print("CDB operational subscription point triggered")
                        cdb.start_session(data_sock, cdb.OPERATIONAL)
                        cdb.set_namespace(data_sock, routes_ns.ns.hash)
                        cdb.diff_iterate(oper_sub_sock, oper_sub_point,
                                         iterate_changes, 0, None)
                        cdb.end_session(data_sock)

                    cdb.sync_subscription_socket(oper_sub_sock,
                                                 cdb.DONE_OPERATIONAL)
                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:
        data_sock.close()
        oper_sub_sock.close()
Exemplo n.º 11
0
def connect_maapi():
    s = socket.socket()
    maapi.connect(s, '127.0.0.1', _confd.PORT)
    maapi.load_schemas(s)
    return s
Exemplo n.º 12
0
def run():
    log.info("==>")
    # 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)

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    subsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    # maapi socket for load schemas
    maapisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    maapi.connect(maapisock, CONFD_ADDR, _confd.CONFD_PORT)
    maapi.load_schemas(maapisock)

    cdb.connect(sock, cdb.DATA_SOCKET, CONFD_ADDR, _confd.CONFD_PORT)
    sub_path = "/root/NodeB/RFHead"
    cdb.connect(subsock, cdb.SUBSCRIPTION_SOCKET, CONFD_ADDR,
                _confd.CONFD_PORT)
    spoint = cdb.subscribe(subsock, 3, root_ns.ns.hash, sub_path)
    cdb.subscribe_done(subsock)
    log.debug("Subscribed to path %s spoint=%i" % (sub_path, spoint))
    read_db(sock)

    try:
        _r = [subsock]
        _w = []
        _e = []
        log.debug("subscok connected, starting ConfD loop")
        while (True):
            (r, w, e) = select.select(_r, _w, _e, 1)
            # log.debug("select triggered r=%r" % r)
            for rs in r:
                log.debug("rs.fileno=%i subscok.fileno=%i" %
                          (rs.fileno(), subsock.fileno()))
                if rs.fileno() == subsock.fileno():
                    log.debug("subsock triggered")
                    try:
                        sub_points = cdb.read_subscription_socket(subsock)
                        for s in sub_points:
                            if s == spoint:
                                log.debug("our spoint=%i triggered" % spoint)
                                cdb.start_session(sock, cdb.RUNNING)
                                cdb.set_namespace(sock, root_ns.ns.hash)
                                cdb.diff_iterate(subsock, spoint, iter,
                                                 _confd.ITER_WANT_PREV, sock)
                                cdb.end_session(sock)

                                # variant with diff_match not yet available
                                # in python

                                cdb.sync_subscription_socket(
                                    subsock, cdb.DONE_PRIORITY)
                    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:
        sock.close()
        subsock.close()
    log.info("<==")