Skip to content

Instantly share code, notes, and snippets.

@svinota
Last active April 22, 2016 10:20
Show Gist options
  • Save svinota/c3de8b32ef7986a154dc3897e474d3c2 to your computer and use it in GitHub Desktop.
Save svinota/c3de8b32ef7986a154dc3897e474d3c2 to your computer and use it in GitHub Desktop.
partial transaction session
In [1]: from pyroute2 import IPDB
In [2]: ip = IPDB()
# create future VRF port
In [3]: ip.create(ifname='v0p0', kind='dummy').commit()
Out[3]: {'family': 0, 'txqlen': 1000, 'ipdb_scope': 'system', 'index': 1118, 'operstate': 'DOWN', 'num_tx_queues': 1, 'group': 0, 'carrier_changes': 0, 'unknown': '08:00:29:00:00:00:01:00', 'ipaddr': [], 'neighbours': [], 'ifname': 'v0p0', 'promiscuity': 0, 'linkmode': 0, 'broadcast': 'ff:ff:ff:ff:ff:ff', 'address': 'f6:22:cf:07:8a:bc', 'vlans': [], 'ipdb_priority': 0, 'change': 4294967295, 'kind': 'dummy', 'qdisc': 'noop', 'mtu': 1500, 'num_rx_queues': 1, 'carrier': 1, 'flags': 130, 'ifi_type': 1, 'proto_down': 0, 'ports': []}
# create VRF interface
In [4]: ip.create(ifname='v0', kind='vrf', vrf_table=20).commit()
Out[4]: {'family': 0, 'txqlen': 1000, 'ipdb_scope': 'system', 'index': 1119, 'operstate': 'DOWN', 'num_tx_queues': 1, 'group': 0, 'carrier_changes': 0, 'unknown': '08:00:29:00:00:00:01:00', 'vrf_table': 20, 'ipaddr': [], 'neighbours': [], 'ifname': 'v0', 'promiscuity': 0, 'linkmode': 0, 'broadcast': 'ff:ff:ff:ff:ff:ff', 'address': '02:59:fa:a3:a4:be', 'vlans': [], 'ipdb_priority': 0, 'change': 4294967295, 'kind': 'vrf', 'qdisc': 'noop', 'mtu': 1500, 'num_rx_queues': 1, 'carrier': 1, 'flags': 1152, 'ifi_type': 1, 'proto_down': 0, 'ports': []}
# add to the VRF two ports: one exists (v0p0), but other still doesn't (v0p1)
In [5]: i = (ip.interfaces.v0
...: .set('mtu', 1400)
...: .add_port('v0p0')
...: .add_port('v0p1'))
# get the transaction object to alterate it
In [6]: tx = i.last()
# `partial`: allows the transaction to be applied partially:
# * do not do any rollback
# * do not exit on the first error, try to apply other changes
# * raise a specific exception
In [7]: tx.partial = True
# apply the transaction
# an exception will be raised, since the second port doesn't exist — it will stay in the transaction
In [8]: i.commit(transaction=tx)
#---------------------------------------------------------------------------
#PartialCommitException Traceback (most recent call last)
#<ipython-input-9-791afa8f0a6e> in <module>()
#----> 1 i.commit(transaction=tx)
#
#/home/peet/Projects/pyroute2/pyroute2/ipdb/interface.pyc in commit(self, tid, transaction, rollback, newif)
# 938 if error is not None:
# 939 error.transaction = transaction
#--> 940 raise error
# 941
# 942 time.sleep(config.commit_barrier)
#
#PartialCommitException: partial commit error
# check: the first port is added
In [9]: i.ports
Out[9]: [1118]
# create the second VRF port
In [10]: ip.create(ifname='v0p1', kind='dummy').commit()
Out[10]: {'family': 0, 'txqlen': 1000, 'ipdb_scope': 'system', 'index': 1120, 'operstate': 'DOWN', 'num_tx_queues': 1, 'group': 0, 'carrier_changes': 0, 'unknown': '08:00:29:00:00:00:01:00', 'ipaddr': [], 'neighbours': [], 'ifname': 'v0p1', 'promiscuity': 0, 'linkmode': 0, 'broadcast': 'ff:ff:ff:ff:ff:ff', 'address': '66:52:62:f7:a9:1d', 'vlans': [], 'ipdb_priority': 0, 'change': 4294967295, 'kind': 'dummy', 'qdisc': 'noop', 'mtu': 1500, 'num_rx_queues': 1, 'carrier': 1, 'flags': 130, 'ifi_type': 1, 'proto_down': 0, 'ports': []}
# apply again
In [11]: i.commit(transaction=tx)
Out[11]: {'family': 0, 'txqlen': 1000, 'ipdb_scope': 'system', 'index': 1119, 'operstate': 'DOWN', 'num_tx_queues': 1, 'group': 0, 'carrier_changes': 0, 'unknown': '08:00:29:00:00:00:01:00', 'vrf_table': 20, 'ipaddr': [], 'neighbours': [], 'ifname': 'v0', 'promiscuity': 0, 'linkmode': 0, 'broadcast': 'ff:ff:ff:ff:ff:ff', 'address': '02:59:fa:a3:a4:be', 'vlans': [], 'ipdb_priority': 0, 'change': 128, 'kind': 'vrf', 'qdisc': 'noop', 'mtu': 1400, 'num_rx_queues': 1, 'carrier': 1, 'flags': 1024, 'ifi_type': 1, 'proto_down': 0, 'ports': [1120, 1118]}
# the second port is here, finally
In [12]: i.ports
Out[12]: [1120, 1118]
# drop the used transaction manually
# the `commit()` call didn't do that, cause the transaction was specified in the function arguments
In [13]: i.drop(tx)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment