Esempio n. 1
0
    def cb_init(self, tctx):
        if self.maapi is None:
            self.maapi = Maapi()
        self.trans = self.maapi.attach(tctx)

        name = 'th-{0}'.format(tctx.th)
        wsock = take_worker_socket(self.daemon, name, self._make_key(tctx))
        try:
            init_cb = getattr(self, 'init', None)
            if callable(init_cb):
                init_cb(tctx)

            # Associate worker socket with the transaction
            trans_set_fd(tctx, wsock)

        except Exception as e:
            return_worker_socket(self.daemon, self._make_key(tctx))
            raise
Esempio n. 2
0
    def cb_action(self, uinfo, name, kp, input, output):

        def iterate(keypath, op, old_value, new_value):
            if op == ncs.MOP_DELETED and len(keypath) > 2 and kp_value(keypath[0]) == 'commit-queue':
                # /ncs:services/cq-notify-test:c-queue-test1/vrf{cust4}/plan/commit-queue
                self.log.info("Commit-queue completed for notifier {}, service instance {}.".format(
                    input.kicker_id,
                    kp_value(keypath[2])
                ))
                send_rest_notification(service=kp_value(keypath[2]), notifier=input.kicker_id)
                return ncs.ITER_STOP
            else:
                return ncs.ITER_RECURSE

        def send_rest_notification(**kwargs):
            # Retrieve configured REST parameters
            with ncs.maapi.single_read_trans(uinfo.username, "system") as read_t:
                rest_params = ncs.maagic.get_node(read_t, kp).rest
                uri = rest_params.uri
                source_data = rest_params.payload
                rest_user = rest_params.username
                read_t.maapi.install_crypto_keys()
                rest_pass = _ncs.decrypt(rest_params.password)

            # Process source_data through jinja2
            jinja_env = jinja2.Environment(autoescape=True, trim_blocks=True, lstrip_blocks=True)
            data = jinja_env.from_string(source_data).render(kwargs)

            # Send REST notification
            try:
                response = requests.post(url=uri, auth=(rest_user, rest_pass), data=data)
                response.raise_for_status()
                self.log.info("REST notification sent successfully: status code {}.".format(response.status_code))
            except requests.exceptions.RequestException as e:
                self.log.error("REST notification failed: {}".format(e))

        with Maapi() as m:
            try:
                t = m.attach(input.tid)
                t.diff_iterate(iterate, ncs.ITER_WANT_P_CONTAINER)
            finally:
                m.detach(input.tid)
Esempio n. 3
0
    def cb_action(self, uinfo, name, kp, input, output):

        def iterate(keypath, op, old_value, new_value):
            if op == ncs.MOP_CREATED and len(keypath) > 3 and str(keypath[0]) == 'request':
                # /loopback:external-resource-manager/id-pool{pool-1}/allocation{XR-0-1}/request
                allocation_id = kp_value(keypath[1])
                pool_name = kp_value(keypath[3])

                assigned_id = id_allocator.allocate(allocation_id, pool_name)
                if assigned_id is None:
                    self.log.error('Resource pool {} exhausted'.format(pool_name))
                else:
                    with ncs.maapi.single_write_trans(uinfo.username, "system", db=ncs.OPERATIONAL) as write_t:
                        ncs.maagic.get_node(write_t, keypath)._parent.response.assigned_id = assigned_id
                        write_t.apply()

                    self.log.info('Allocated: {}, {}, value: {}'.format(pool_name, allocation_id, assigned_id))

            elif op == ncs.MOP_DELETED and len(keypath) > 2 and str(keypath[1]) == 'allocation':
                # /loopback:external-resource-manager/id-pool{pool-1}/allocation{XR-0-1}
                allocation_id = kp_value(keypath[0])
                pool_name = kp_value(keypath[2])

                diff_size = id_allocator.deallocate(allocation_id, pool_name)
                self.log.info('De-allocated: {}, {}, {} changes'.format(pool_name, allocation_id, diff_size))

            else:
                return ncs.ITER_RECURSE

            return ncs.ITER_STOP

        self.log.info('Action input: kicker-id: {}, path: {}, tid: {}'.format(input.kicker_id, input.path, input.tid))
        id_allocator = FileAllocator(Config.ID_POOL)
        with Maapi() as m:
            try:
                t = m.attach(input.tid)
                t.diff_iterate(iterate, ncs.ITER_WANT_P_CONTAINER)
            finally:
                m.detach(input.tid)