diff options
-rw-r--r-- | doc/network.rst | 13 | ||||
-rw-r--r-- | ncserver/module/nms_ifupdownng.py | 24 |
2 files changed, 37 insertions, 0 deletions
diff --git a/doc/network.rst b/doc/network.rst index 7291249..eef0e68 100644 --- a/doc/network.rst +++ b/doc/network.rst @@ -174,6 +174,19 @@ Saves all queued operations to configuration storage and configures network interfaces. +``rollback()`` +~~~~~~~~~~~~~~ + +Erases all queued operations and restores the configuration from when +the transaction began. + + +``transaction()`` +~~~~~~~~~~~~~~~~~ + +A Python context manager designed to be used with the ``with`` keyword. + + Parameters ---------- diff --git a/ncserver/module/nms_ifupdownng.py b/ncserver/module/nms_ifupdownng.py index 8c92717..0c03fd9 100644 --- a/ncserver/module/nms_ifupdownng.py +++ b/ncserver/module/nms_ifupdownng.py @@ -10,6 +10,8 @@ with this source distribution for more information. SPDX-License-Identifier: NCSA """ +from contextlib import contextmanager + import logging import pathlib import socket @@ -573,6 +575,28 @@ def commit(): _TRANSACTION = False +def rollback(): + """Roll back outstanding operations.""" + global _TRANSACTION # pylint: disable=W0603 + + _load_config() + _load_sysctl_cfg() + + _TRANSACTION = False + + +@contextmanager +def transaction(): + begin_transaction() + try: + yield None + except: + rollback() + raise + else: + commit() + + def get_param(iface: str, parameter: str): """Retrieve the parameter for the specified interface.""" _load_config() |