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")
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)
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("<==")
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)
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)
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)
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()
def destroy_daemon(demon_data): # terminate all the running threads for key in demon_data.thread_map: print('terminating thread #' + key) demon_data.thread_map[key].stop_flag = True demon_data.thread_map[key].join() # main execution if __name__ == "__main__": # 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) dd = DaemonData() while True: attempts = 0 while create_daemon(dd) != _confd.OK: attempts += 1 if attempts > MAX_ATTEMPTS: eprint('Failed to create daemon, giving up') exit(2) else: eprint('Failed to create daemon, will retry...') time.sleep(3)
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("<==")