Skip to content

Commit e84934d

Browse files
author
Alex J Lennon
committed
improv: Fix nmcli connection save command for NetworkManager 1.46.0
NetworkManager 1.46.0 does not support 'nmcli connection save' command. Connections are automatically saved when created with nmcli.connection.add(). Changes: - Replace invalid 'nmcli connection save' with 'nmcli connection reload' - Add verification that connection file exists and has psk-flags=0 - Fix asyncio.get_event_loop() deprecation warning - Change all save-related logging to DEBUG level (non-critical) - Add os import for connection file verification Applied to both: - Generic onboarding-server.py - imx93-jaguar-eink/onboarding-server-eink.py This fixes the WARNING about 'save' command not being understood.
1 parent 2091ae2 commit e84934d

File tree

2 files changed

+83
-20
lines changed

2 files changed

+83
-20
lines changed

recipes-devtools/python/python3-improv/imx93-jaguar-eink/onboarding-server-eink.py

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,14 @@ def get_board_id():
122122
INTERFACE = os.getenv("IMPROV_WIFI_INTERFACE", "wlan0") # imx93-jaguar-eink uses wlan0
123123
TIMEOUT = int(os.getenv("IMPROV_CONNECTION_TIMEOUT", "10000"))
124124

125-
loop = asyncio.get_event_loop()
125+
# Use new_event_loop() or get_event_loop() depending on Python version
126+
# get_event_loop() is deprecated in Python 3.10+ but still works
127+
try:
128+
loop = asyncio.get_running_loop()
129+
except RuntimeError:
130+
# No running loop, create new one
131+
loop = asyncio.new_event_loop()
132+
asyncio.set_event_loop(loop)
126133
server = BlessServer(name=SERVICE_NAME, loop=loop)
127134

128135
def wifi_connect(ssid: str, passwd: str) -> Optional[list[str]]:
@@ -165,21 +172,45 @@ def wifi_connect(ssid: str, passwd: str) -> Optional[list[str]]:
165172
print(f'Could not add new connection {CON_NAME}: {e}')
166173
return None
167174

168-
# Save connection to keyfile to ensure secrets are persisted
169-
# This is REQUIRED to persist psk-flags=0 setting
170-
# Use subprocess since Python nmcli library doesn't have save() method
175+
# NetworkManager automatically saves connections when created/modified
176+
# In NetworkManager 1.46.0+, connections are saved automatically to
177+
# /etc/NetworkManager/system-connections/ when created with nmcli.connection.add()
178+
# The psk-flags=0 setting is persisted automatically.
179+
# Use 'reload' to ensure NetworkManager picks up the connection immediately
171180
try:
172-
subprocess.run(['nmcli', 'connection', 'save', f"{CON_NAME}"],
181+
subprocess.run(['nmcli', 'connection', 'reload'],
173182
check=True, capture_output=True, timeout=5)
174-
logger.info(f"Successfully saved connection {CON_NAME} to keyfile")
183+
logger.debug(f"Reloaded NetworkManager connections")
175184
except subprocess.CalledProcessError as e:
176-
logger.warning(f"Could not save connection {CON_NAME} to keyfile (exit code {e.returncode}): {e.stderr.decode() if e.stderr else 'unknown error'}")
185+
logger.debug(f"Could not reload NetworkManager connections (exit code {e.returncode}): {e.stderr.decode() if e.stderr else 'unknown error'}")
186+
# Non-critical - connection is already saved automatically
177187
except subprocess.TimeoutExpired as e:
178-
logger.warning(f"Timeout saving connection {CON_NAME} to keyfile: {e}")
188+
logger.debug(f"Timeout reloading NetworkManager connections: {e}")
189+
# Non-critical - connection is already saved automatically
179190
except FileNotFoundError:
180-
logger.error(f"nmcli command not found - cannot save connection {CON_NAME} to keyfile")
191+
logger.debug(f"nmcli command not found - cannot reload connections")
192+
# Non-critical - connection is already saved automatically
193+
except Exception as e:
194+
logger.debug(f"Unexpected error reloading connections: {e}")
195+
# Non-critical - connection is already saved automatically
196+
197+
# Verify connection file exists and has correct settings
198+
connection_file = f"/etc/NetworkManager/system-connections/{CON_NAME}.nmconnection"
199+
try:
200+
if os.path.exists(connection_file):
201+
logger.debug(f"Connection file exists: {connection_file}")
202+
# Read file to verify psk-flags=0 is set
203+
with open(connection_file, 'r') as f:
204+
content = f.read()
205+
if 'psk-flags=0' in content or 'psk-flags=0\n' in content:
206+
logger.debug(f"Verified psk-flags=0 in connection file")
207+
else:
208+
logger.warning(f"psk-flags=0 not found in connection file - connection may not work correctly")
209+
else:
210+
logger.warning(f"Connection file not found at {connection_file} - connection may not persist")
181211
except Exception as e:
182-
logger.warning(f"Unexpected error saving connection {CON_NAME} to keyfile: {e}")
212+
logger.debug(f"Could not verify connection file: {e}")
213+
# Non-critical - just for verification
183214

184215
try:
185216
nmcli.connection.up(f"{CON_NAME}", TIMEOUT)

recipes-devtools/python/python3-improv/onboarding-server.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import uuid
1818
import nmcli
1919
import subprocess
20+
import os
2021

2122
logging.basicConfig(level=logging.DEBUG)
2223
logger = logging.getLogger(name=__name__)
@@ -82,7 +83,14 @@ def build_gatt():
8283
INTERFACE = "wlan0"
8384
TIMEOUT = 10000
8485

85-
loop = asyncio.get_event_loop()
86+
# Use new_event_loop() or get_event_loop() depending on Python version
87+
# get_event_loop() is deprecated in Python 3.10+ but still works
88+
try:
89+
loop = asyncio.get_running_loop()
90+
except RuntimeError:
91+
# No running loop, create new one
92+
loop = asyncio.new_event_loop()
93+
asyncio.set_event_loop(loop)
8694
server = BlessServer(name=SERVICE_NAME, loop=loop)
8795

8896
def wifi_connect(ssid: str, passwd: str) -> Optional[list[str]]:
@@ -120,21 +128,45 @@ def wifi_connect(ssid: str, passwd: str) -> Optional[list[str]]:
120128
print(f'Could not add new connection {CON_NAME}: {e}')
121129
return None
122130

123-
# Save connection to keyfile to ensure secrets are persisted
124-
# This is REQUIRED to persist psk-flags=0 setting
125-
# Use subprocess since Python nmcli library doesn't have save() method
131+
# NetworkManager automatically saves connections when created/modified
132+
# In NetworkManager 1.46.0+, connections are saved automatically to
133+
# /etc/NetworkManager/system-connections/ when created with nmcli.connection.add()
134+
# The psk-flags=0 setting is persisted automatically.
135+
# Use 'reload' to ensure NetworkManager picks up the connection immediately
126136
try:
127-
subprocess.run(['nmcli', 'connection', 'save', f"{CON_NAME}"],
137+
subprocess.run(['nmcli', 'connection', 'reload'],
128138
check=True, capture_output=True, timeout=5)
129-
logger.info(f"Successfully saved connection {CON_NAME} to keyfile")
139+
logger.debug(f"Reloaded NetworkManager connections")
130140
except subprocess.CalledProcessError as e:
131-
logger.warning(f"Could not save connection {CON_NAME} to keyfile (exit code {e.returncode}): {e.stderr.decode() if e.stderr else 'unknown error'}")
141+
logger.debug(f"Could not reload NetworkManager connections (exit code {e.returncode}): {e.stderr.decode() if e.stderr else 'unknown error'}")
142+
# Non-critical - connection is already saved automatically
132143
except subprocess.TimeoutExpired as e:
133-
logger.warning(f"Timeout saving connection {CON_NAME} to keyfile: {e}")
144+
logger.debug(f"Timeout reloading NetworkManager connections: {e}")
145+
# Non-critical - connection is already saved automatically
134146
except FileNotFoundError:
135-
logger.error(f"nmcli command not found - cannot save connection {CON_NAME} to keyfile")
147+
logger.debug(f"nmcli command not found - cannot reload connections")
148+
# Non-critical - connection is already saved automatically
149+
except Exception as e:
150+
logger.debug(f"Unexpected error reloading connections: {e}")
151+
# Non-critical - connection is already saved automatically
152+
153+
# Verify connection file exists and has correct settings
154+
connection_file = f"/etc/NetworkManager/system-connections/{CON_NAME}.nmconnection"
155+
try:
156+
if os.path.exists(connection_file):
157+
logger.debug(f"Connection file exists: {connection_file}")
158+
# Read file to verify psk-flags=0 is set
159+
with open(connection_file, 'r') as f:
160+
content = f.read()
161+
if 'psk-flags=0' in content or 'psk-flags=0\n' in content:
162+
logger.debug(f"Verified psk-flags=0 in connection file")
163+
else:
164+
logger.warning(f"psk-flags=0 not found in connection file - connection may not work correctly")
165+
else:
166+
logger.warning(f"Connection file not found at {connection_file} - connection may not persist")
136167
except Exception as e:
137-
logger.warning(f"Unexpected error saving connection {CON_NAME} to keyfile: {e}")
168+
logger.debug(f"Could not verify connection file: {e}")
169+
# Non-critical - just for verification
138170

139171
try:
140172
nmcli.connection.up(f"{CON_NAME}", TIMEOUT)

0 commit comments

Comments
 (0)