def __init__(self, action: 'ActionBase') -> None: self.uinfo = action.uinfo self.cli_log_file: Optional[TextIO] if action.cli_log_filename is not None: self.cli_log_file = open(action.cli_log_filename, 'a') self.cli_log_file.write(action.log_header()) else: self.cli_log_file = None self.maapi = maapi.Maapi() self.maapi.start_user_session('admin', 'system') self.trans = maapi.Transaction(self.maapi, rw=_ncs.READ, db=_ncs.OPERATIONAL) root = maagic.get_root(self.trans) apoint = root.ncs_state.internal.callpoints.actionpoint[ns.actionpoint_xmnr_cli_log] if apoint.daemon.id is not None: self.cli_log_action = root.drned_xmnr.cli_log_message self.cli_log_params = self.cli_log_action.get_input() self.cli_log_params.device = action.dev_name else: self.cli_log_action = self.cli_log_params = None
def cb_action(self, uinfo, name, kp, input, output): self.log.info('invoke action %s with template %s' % (name, input.name)) def apply_template(t, i): template = ncs.template.Template(t, i.context_node) vars = ncs.template.Variables() for v in i.variable: vars.add(v.name, v.value) template.apply(i.name, vars) do_apply = False m = maapi.Maapi() if uinfo.actx_thandle != -1: # When invoked from the CLI we get a transaction # Note: unless we are in configure mode it will be read-only m.attach2(0, 0, uinfo.actx_thandle) trans = maapi.Transaction(m, uinfo.actx_thandle) else: # Start write transaction and apply it at end trans = m.start_write_trans(usid=uinfo.usid) do_apply = True try: apply_template(trans, input) except _ncs.error.Error as e: if e.confd_errno == ncs.ERR_NOT_WRITABLE: # Happens when invoked from the CLI and not in configure mode, # assume user wants us to start a separate transaction and # apply the template there. m.detach(uinfo.actx_thandle) trans = m.start_write_trans(usid=uinfo.usid) do_apply = True apply_template(trans, input) else: raise e if do_apply: trans.apply()
def single_trans(readWrite): with maapi.Maapi() as m: with maapi.Session(m, 'admin', 'system') as s: with maapi.Transaction(m, rw=readWrite) as t: yield t