def get_data(clazz: Type[ClassType], tctx: Tctx, device: str, log: Log, data_cb: Callable[[ClassType], 'XmnrDeviceData.DataType']) \ -> 'XmnrDeviceData.DataType': with maapi.Maapi() as mp: with mp.attach(tctx) as trans: dd = clazz(device, log, trans) data = data_cb(dd) return data
def run_with_trans(self, callback: Callable[[Transaction], T], write: bool = False, db: int = _ncs.RUNNING) -> T: if write: # we do not want to write to the user's transaction with maapi.single_write_trans(self.uinfo.username, self.uinfo.context, db=db) as trans: return callback(trans) elif self.uinfo.actx_thandle == -1: with maapi.single_read_trans(self.uinfo.username, self.uinfo.context, db=db) as trans: return callback(trans) else: mp = maapi.Maapi() return callback(mp.attach(self.uinfo.actx_thandle))
def run_with_trans(self, callback, write=False): if write: # we do not want to write to the user's transaction with maapi.single_write_trans(self.uinfo.username, self.uinfo.context) as trans: return callback(trans) elif self.uinfo.actx_thandle == -1: with maapi.single_read_trans(self.uinfo.username, self.uinfo.context) as trans: return callback(trans) else: mp = maapi.Maapi() return callback(mp.attach(self.uinfo.actx_thandle))
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 decrypt(value): with maapi.Maapi() as m: m.install_crypto_keys() return _ncs.decrypt(value)
def __init__(self, uinfo, dev_name, params, log_obj): super(ActionBase, self).__init__(dev_name, log_obj) self.uinfo = uinfo self.maapi = maapi.Maapi() self.run_with_trans(self._setup_directories) self._init_params(params)
def get_data(clazz, tctx, device, log, data_cb): with maapi.Maapi() as mp: with mp.attach(tctx) as trans: dd = clazz(device, log, trans) data = data_cb(dd) return data
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
#!/usr/bin/env python from __future__ import print_function import _ncs import ncs.maapi as maapi import ncs.maagic as maagic import IPython if __name__ == '__main__': m = maapi.Maapi() m.load_schemas() m.start_user_session('admin', 'system', []) trans = m.start_trans(_ncs.RUNNING, _ncs.READ_WRITE) x = maagic.get_root(trans) print("Your maagic object 'x -> %s' is now prepared... go have some fun!" % (str(x))) IPython.embed(display_banner=False)
def cb_action(self, uinfo, name, kp, input, output): results = re.search('\{(.*)\}', str(kp)) sni = results.group(1) self.log.info("Reconcile VRF Service Instance [%s]" % (sni)) output.status = 'success' # Check environment setting for IPC port value port = int(os.getenv('NCS_IPC_PORT', _ncs.NCS_PORT)) self.log.info("Using NCS IPC port value [%d]" % (port)) try: with maapi.Maapi("127.0.0.1", port, path=None) as self.maapi: self.maapi.start_user_session("admin", 'VRF-REC-' + sni, []) with self.maapi.start_write_trans(db=ncs.RUNNING) as t: root = maagic.get_root(t, shared=False) sn = root.vrf__vrf[sni] ### # Apply the reconcile template ### self.template = ncs.template.Template(sn) self.vars = ncs.template.Variables() self.template.apply('vrf-oob-reconcile', self.vars) ### # Eexecute dry run to check if the reconcile was successful ### dryRun = root.ncs__services.commit_dry_run inp = dryRun.get_input() result = dryRun(inp) ### # Any device/network changes mean something outside of what we can reconile # has changed and we will fail the reconcile operation ### if (result.cli) and (result.cli.local_node.data): self.log.debug( "oob-reconile[%s] dry-run results: [%s]" % (sni, result.cli.local_node.data)) m = re.search(r'^ devices {\n device ', result.cli.local_node.data, re.MULTILINE) if (m): self.log.error('oob-reconcile [%s] failed' % (sni)) output.status = 'failed' output.error_message = result.cli.local_node.data else: ### # No output from commit dry-run, we need to check re-deploy to make # sure there aren't any unreconcilable changes in the CDB ### self.log.debug( "oob-reconile[%s] starting re-deploy dry-run" % (sni)) redeploy = sn.re_deploy inp = redeploy.get_input() inp.dry_run.create() result = redeploy(inp) if (result.cli) and (result.cli.local_node.data): self.log.debug( "oob-reconile[%s] re-deploy dry-run results: [%s]" % (sni, result.cli.local_node.data)) output.status = 'failed' output.error_message = result.cli.local_node.data if (output.status != "failed"): self.log.info( "oob-reconcile[%s]:Saving/Applying reconile changes" % (sni)) t.apply() except Exception as e: output.status = 'failed' output.error_message = "Failure during reconcillation processing" self.log.error(e) return ncs.CONFD_OK