|
Packit Service |
0535c1 |
#
|
|
Packit Service |
0535c1 |
# Copyright (c) 2018-2020 Red Hat, Inc.
|
|
Packit Service |
0535c1 |
#
|
|
Packit Service |
0535c1 |
# This file is part of nmstate
|
|
Packit Service |
0535c1 |
#
|
|
Packit Service |
0535c1 |
# This program is free software: you can redistribute it and/or modify
|
|
Packit Service |
0535c1 |
# it under the terms of the GNU Lesser General Public License as published by
|
|
Packit Service |
0535c1 |
# the Free Software Foundation, either version 2.1 of the License, or
|
|
Packit Service |
0535c1 |
# (at your option) any later version.
|
|
Packit Service |
0535c1 |
#
|
|
Packit Service |
0535c1 |
# This program is distributed in the hope that it will be useful,
|
|
Packit Service |
0535c1 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
0535c1 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
0535c1 |
# GNU Lesser General Public License for more details.
|
|
Packit Service |
0535c1 |
#
|
|
Packit Service |
0535c1 |
# You should have received a copy of the GNU Lesser General Public License
|
|
Packit Service |
0535c1 |
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
Packit Service |
0535c1 |
#
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
import copy
|
|
Packit Service |
0535c1 |
import time
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
from libnmstate import validator
|
|
Packit Service |
0535c1 |
from libnmstate.error import NmstateVerificationError
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
from .nmstate import create_checkpoints
|
|
Packit Service |
0535c1 |
from .nmstate import destroy_checkpoints
|
|
Packit Service |
0535c1 |
from .nmstate import plugin_context
|
|
Packit Service |
0535c1 |
from .nmstate import plugins_capabilities
|
|
Packit Service |
0535c1 |
from .nmstate import rollback_checkpoints
|
|
Packit Service |
0535c1 |
from .nmstate import show_with_plugins
|
|
Packit Service |
0535c1 |
from .net_state import NetState
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
MAINLOOP_TIMEOUT = 35
|
|
Packit Service |
0535c1 |
VERIFY_RETRY_INTERNAL = 1
|
|
Packit Service |
0535c1 |
VERIFY_RETRY_TIMEOUT = 5
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
def apply(
|
|
Packit Service |
0535c1 |
desired_state,
|
|
Packit Service |
0535c1 |
*,
|
|
Packit Service |
0535c1 |
verify_change=True,
|
|
Packit Service |
0535c1 |
commit=True,
|
|
Packit Service |
0535c1 |
rollback_timeout=60,
|
|
Packit Service |
0535c1 |
save_to_disk=True,
|
|
Packit Service |
0535c1 |
):
|
|
Packit Service |
0535c1 |
"""
|
|
Packit Service |
0535c1 |
Apply the desired state
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
:param verify_change: Check if the outcome state matches the desired state
|
|
Packit Service |
0535c1 |
and rollback if not.
|
|
Packit Service |
0535c1 |
:param commit: Commit the changes after verification if the state matches.
|
|
Packit Service |
0535c1 |
:param rollback_timeout: Revert the changes if they are not commited within
|
|
Packit Service |
0535c1 |
this timeout (specified in seconds).
|
|
Packit Service |
0535c1 |
:type verify_change: bool
|
|
Packit Service |
0535c1 |
:type commit: bool
|
|
Packit Service |
0535c1 |
:type rollback_timeout: int (seconds)
|
|
Packit Service |
0535c1 |
:returns: Checkpoint identifier
|
|
Packit Service |
0535c1 |
:rtype: str
|
|
Packit Service |
0535c1 |
"""
|
|
Packit Service |
0535c1 |
desired_state = copy.deepcopy(desired_state)
|
|
Packit Service |
0535c1 |
with plugin_context() as plugins:
|
|
Packit Service |
0535c1 |
validator.schema_validate(desired_state)
|
|
Packit Service |
0535c1 |
current_state = show_with_plugins(plugins, include_status_data=True)
|
|
Packit Service |
0535c1 |
validator.validate_capabilities(
|
|
Packit Service |
0535c1 |
desired_state, plugins_capabilities(plugins)
|
|
Packit Service |
0535c1 |
)
|
|
Packit Service |
0535c1 |
net_state = NetState(desired_state, current_state, save_to_disk)
|
|
Packit Service |
0535c1 |
checkpoints = create_checkpoints(plugins, rollback_timeout)
|
|
Packit Service |
0535c1 |
_apply_ifaces_state(plugins, net_state, verify_change, save_to_disk)
|
|
Packit Service |
0535c1 |
if commit:
|
|
Packit Service |
0535c1 |
destroy_checkpoints(plugins, checkpoints)
|
|
Packit Service |
0535c1 |
else:
|
|
Packit Service |
0535c1 |
return checkpoints
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
def commit(*, checkpoint=None):
|
|
Packit Service |
0535c1 |
"""
|
|
Packit Service |
0535c1 |
Commit a checkpoint that was received from `apply()`.
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
:param checkpoint: Checkpoint to commit. If not specified, a checkpoint
|
|
Packit Service |
0535c1 |
will be selected and committed.
|
|
Packit Service |
0535c1 |
:type checkpoint: str
|
|
Packit Service |
0535c1 |
"""
|
|
Packit Service |
0535c1 |
with plugin_context() as plugins:
|
|
Packit Service |
0535c1 |
destroy_checkpoints(plugins, checkpoint)
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
def rollback(*, checkpoint=None):
|
|
Packit Service |
0535c1 |
"""
|
|
Packit Service |
0535c1 |
Roll back a checkpoint that was received from `apply()`.
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
:param checkpoint: Checkpoint to roll back. If not specified, a checkpoint
|
|
Packit Service |
0535c1 |
will be selected and rolled back.
|
|
Packit Service |
0535c1 |
:type checkpoint: str
|
|
Packit Service |
0535c1 |
"""
|
|
Packit Service |
0535c1 |
with plugin_context() as plugins:
|
|
Packit Service |
0535c1 |
rollback_checkpoints(plugins, checkpoint)
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
def _apply_ifaces_state(plugins, net_state, verify_change, save_to_disk):
|
|
Packit Service |
0535c1 |
for plugin in plugins:
|
|
Packit Service |
0535c1 |
plugin.apply_changes(net_state, save_to_disk)
|
|
Packit Service |
0535c1 |
verified = False
|
|
Packit Service |
0535c1 |
if verify_change:
|
|
Packit Service |
0535c1 |
for _ in range(VERIFY_RETRY_TIMEOUT):
|
|
Packit Service |
0535c1 |
try:
|
|
Packit Service |
0535c1 |
_verify_change(plugins, net_state)
|
|
Packit Service |
0535c1 |
verified = True
|
|
Packit Service |
0535c1 |
break
|
|
Packit Service |
0535c1 |
except NmstateVerificationError:
|
|
Packit Service |
0535c1 |
time.sleep(VERIFY_RETRY_INTERNAL)
|
|
Packit Service |
0535c1 |
if not verified:
|
|
Packit Service |
0535c1 |
_verify_change(plugins, net_state)
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
|
|
Packit Service |
0535c1 |
def _verify_change(plugins, net_state):
|
|
Packit Service |
0535c1 |
current_state = show_with_plugins(plugins)
|
|
Packit Service |
0535c1 |
net_state.verify(current_state)
|