Skip to content

Commit

Permalink
Module/NM: Try to reapply connections
Browse files Browse the repository at this point in the history
  • Loading branch information
tyll committed Apr 17, 2020
1 parent da35b81 commit d6a1c59
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 6 deletions.
37 changes: 37 additions & 0 deletions library/network_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,13 @@ def check_deactivated_cb():
finally:
ac.handler_disconnect(ac_id)

def reapply(self, device, connection=None):
version_id = 0
flags = 0
return Util.call_async_method(
device, "reapply", [connection, version_id, flags]
)


###############################################################################

Expand Down Expand Up @@ -2133,6 +2140,9 @@ def run_action_up(self, idx):
)
self.connections_data_set_changed(idx)
if self.check_mode == CheckMode.REAL_RUN:
if self._try_reapply(idx, con):
return

try:
ac = self.nmutil.connection_activate(con)
except MyError as e:
Expand All @@ -2147,6 +2157,33 @@ def run_action_up(self, idx):
except MyError as e:
self.log_error(idx, "up connection failed while waiting: %s" % (e))

def _try_reapply(self, idx, con):
""" Try to reapply a connection
If there is exactly one active connection with the same UUID activated
on exactly one device, ask the device to reapply the connection.
:returns: `True`, when the connection was reapplied, `False` otherwise
:rtype: bool
"""
NM = Util.NM()

acons = list(self.nmutil.active_connection_list(connections=[con]))
if len(acons) != 1:
return False

active_connection = acons[0]
if active_connection.get_state() == NM.ActiveConnectionState.ACTIVATED:
devices = active_connection.get_devices()
if len(devices) == 1:
try:
self.nmutil.reapply(devices[0])
self.log_info(idx, "connection reapplied")
return True
except MyError as error:
self.log_info(idx, "connection reapply failed: %s" % (error))
return False

def run_action_down(self, idx):
connection = self.connections[idx]

Expand Down
13 changes: 7 additions & 6 deletions tests/ensure_non_running_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@
OTHER_PROVIDER_SUFFIX = "_other_provider.yml"

IGNORE = [
"tests_802_1x_nm.yml",
"tests_default_initscripts.yml",
"tests_default_nm.yml",
"tests_default.yml",
"tests_ethtool_features_initscripts.yml",
"tests_ethtool_features_nm.yml",
"tests_helpers-and-asserts.yml",
"tests_reapply_nm.yml",
"tests_states.yml",
"tests_unit.yml",
"tests_vlan_mtu_initscripts.yml",
"tests_vlan_mtu_nm.yml",
"tests_ethtool_features_initscripts.yml",
"tests_ethtool_features_nm.yml",
"tests_default_nm.yml",
"tests_default_initscripts.yml",
"tests_default.yml",
"tests_802_1x_nm.yml",
]

OTHER_PLAYBOOK = """
Expand Down
62 changes: 62 additions & 0 deletions tests/playbooks/tests_reapply.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# SPDX-License-Identifier: BSD-3-Clause
---
- hosts: all
vars:
interface: rpltstbr
profile: "{{ interface }}"
network_provider: nm
pre_tasks:
- debug:
msg: Inside states tests
- include_tasks: tasks/show-interfaces.yml
- include_tasks: tasks/assert-device_absent.yml
roles:
- linux-system-roles.network
tasks:
- block:
# create test profile
- include_role:
name: linux-system-roles.network
vars:
network_connections:
- name: "{{ interface }}"
state: up
type: bridge
ip:
dhcp4: false
auto6: false
- include_tasks: tasks/assert-device_present.yml
- include_tasks: tasks/assert-profile_present.yml
# Use internal module to get output
- network_connections:
provider: nm
connections:
- name: "{{ interface }}"
state: up
type: bridge
ip:
address:
- 192.0.2.72/31
dhcp4: false
auto6: false
ignore_errors: true
register: test_module_run
- debug:
var: test_module_run
- name: Assert that reapply is found in log output
assert:
fail_msg: Reapply not found in log output
that: "{{ 'connection reapplied' in test_module_run.warnings[2] }}"
always:
- block:
# Use internal module directly for speedup
- network_connections:
provider: nm
connections:
- name: "{{ interface }}"
persistent_state: absent
state: down
- command: ip link del "{{ interface }}"
ignore_errors: true
tags:
- "tests::cleanup"
17 changes: 17 additions & 0 deletions tests/tests_reapply_nm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
# set network provider and gather facts
- hosts: all
tasks:
- name: Set network provider to 'nm'
set_fact:
network_provider: nm

# workaround for: https://github.com/ansible/ansible/issues/27973
# There is no way in Ansible to abort a playbook hosts with specific OS
# releases Therefore we include the playbook with the tests only if the hosts
# would support it.
# The test should run with NetworkManager, therefore it cannot run on
# RHEL/CentOS 6
- import_playbook: playbooks/tests_reapply.yml
when:
- ansible_distribution_major_version != '6'

0 comments on commit d6a1c59

Please sign in to comment.