cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3205
Views
10
Helpful
4
Replies

Commit and Rollback usint Action (Python code)

Fantolino
Level 1
Level 1

I want to configure a service using an action with code written in Python.

To be able to rollback that configuration would like to get the commit ID to be passed as result (output) of the action. Then, using that ID, I want to rollback that specific commit using in another action.

So what I need is somethiong like that

Configuring Action

with ncs.maapi.Maapi() as m:
    with ncs.maapi.Session(m, uinfo.username, uinfo.context):
        with m.start_write_trans() as t:
            root = ncs.maagic.get_root(t)
            Some code
            t.apply()

        output.commitID = ?????

Rollbacl Action

with ncs.maapi.Maapi() as m:
    with ncs.maapi.Session(m, uinfo.username, uinfo.context):
        with m.start_write_trans() as t:

            Somecommand(input.commitID)

 

 

2 Accepted Solutions

Accepted Solutions

Before we had this new API, the way to implement it was not direct.
You would need to:

1) Commit using a commit-tag:
set_label(sock, thandle, label) -> None

Set the Label that is stored in the rollback file when a transaction
towards running is committed.

Keyword arguments:
sock -- a python socket instance
thandle -- transaction handle
label -- the Label

2) When you would like to rollback, you need to search through your rollback files to find your label:

list_rollbacks(sock, rp_size) -> list

Get a list of rollbacks, at most rp_size big.

Keyword arguments:
sock -- a python socket instance
rp_size -- maximum number of rollback files to list

Something like this:
items=m.list_rollbacks(10)
for item in items:
print(item.label)
print(item.fixed_nr)

3) with the target fixed_nr, you can load the rollback.


View solution in original post

mohamkh7
Cisco Employee
Cisco Employee

Adding rollback example using commit label:

def _rollback(username, commit_label):
    success = False
    rollback_num = __get_rollback_num(username, commit_label)
    print('Rollback Number:', rollback_num)
    try:
        with ncs.maapi.single_write_trans(username, 'system') as write_trans:
            # perform rollback
            _ncs.maapi.load_rollback_fixed(write_trans.maapi.msock, write_trans.th, rollback_num)
            write_trans.apply()
            print(f'Rollback {rollback_num} success')
            success = True
    except Exception as e:
        print(str(e))
    return success

def __get_rollback_num(username, commit_label):
    with ncs.maapi.single_read_trans(username, 'system') as read_trans:
        rollbacks = _ncs.maapi.list_rollbacks(read_trans.maapi.msock, 10)
        for rollback in rollbacks:
            if rollback.label == commit_label:
                return rollback.fixed_nr
    return -1



View solution in original post

4 Replies 4

rogaglia
Cisco Employee
Cisco Employee

Hi,

In NSO5.4, we added support for receiving the rollback id in the commit:

- ncs: Add support for retrieving id of the rollback file created during a
transaction commit. The id returned can later be used to do selective
rollback. See API documentation for details. C, Python, Java, and Erlang
MAAPI interfaces supported. CLI, RESTCONF, NETCONF, and JSON-RPC
supported.

 

I believe the new python method is:

_ncs.maapi.get_rollback_id(sock, thandle) -> int
 
Get rollback id from a committed transaction. Returns int with fixed id,
where -1 indicates an error or no rollback id available.
 
Keyword arguments:
sock -- a python socket instance
thandle -- transaction handle

Fantolino
Level 1
Level 1

Thanks for the information.

 

I undestand that with the release 4.6.5 that I'm using there is non possibility to accomplish that task.

As a work-around is it possible to set a label an later have an action that rollback selective that commit?

 

 

Before we had this new API, the way to implement it was not direct.
You would need to:

1) Commit using a commit-tag:
set_label(sock, thandle, label) -> None

Set the Label that is stored in the rollback file when a transaction
towards running is committed.

Keyword arguments:
sock -- a python socket instance
thandle -- transaction handle
label -- the Label

2) When you would like to rollback, you need to search through your rollback files to find your label:

list_rollbacks(sock, rp_size) -> list

Get a list of rollbacks, at most rp_size big.

Keyword arguments:
sock -- a python socket instance
rp_size -- maximum number of rollback files to list

Something like this:
items=m.list_rollbacks(10)
for item in items:
print(item.label)
print(item.fixed_nr)

3) with the target fixed_nr, you can load the rollback.


mohamkh7
Cisco Employee
Cisco Employee

Adding rollback example using commit label:

def _rollback(username, commit_label):
    success = False
    rollback_num = __get_rollback_num(username, commit_label)
    print('Rollback Number:', rollback_num)
    try:
        with ncs.maapi.single_write_trans(username, 'system') as write_trans:
            # perform rollback
            _ncs.maapi.load_rollback_fixed(write_trans.maapi.msock, write_trans.th, rollback_num)
            write_trans.apply()
            print(f'Rollback {rollback_num} success')
            success = True
    except Exception as e:
        print(str(e))
    return success

def __get_rollback_num(username, commit_label):
    with ncs.maapi.single_read_trans(username, 'system') as read_trans:
        rollbacks = _ncs.maapi.list_rollbacks(read_trans.maapi.msock, 10)
        for rollback in rollbacks:
            if rollback.label == commit_label:
                return rollback.fixed_nr
    return -1



Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the NSO Developer community: