summaryrefslogtreecommitdiff
diff options
-rwxr-xr-xbin/wifi_nmcli_backup.py79
-rw-r--r--units/wireless/jobs.pxu28
2 files changed, 107 insertions, 0 deletions
diff --git a/bin/wifi_nmcli_backup.py b/bin/wifi_nmcli_backup.py
new file mode 100755
index 0000000..7ec9c89
--- /dev/null
+++ b/bin/wifi_nmcli_backup.py
@@ -0,0 +1,79 @@
+#!/usr/bin/env python3
+# Copyright 2019 Canonical Ltd.
+# All rights reserved.
+#
+# Written by:
+# Jonathan Cave <jonathan.cave@canonical.com>
+#
+# Save/Restore NetworkManager wifi connections
+
+import os
+import shutil
+import subprocess as sp
+import sys
+
+NM_CON_DIR = '/etc/NetworkManager/system-connections'
+SAVE_DIR = os.path.join(os.path.expandvars(
+ '$PLAINBOX_SESSION_SHARE'), 'stored-system-connections')
+
+
+def get_nm_connections():
+ c = []
+ cmd = 'nmcli -t -f TYPE,NAME c'
+ output = sp.check_output(cmd, shell=True)
+ for line in output.decode(sys.stdout.encoding).splitlines():
+ con_type, name = line.strip().split(':')
+ if con_type == '802-11-wireless':
+ c.append(name)
+ return c
+
+
+def reload_nm_connections():
+ cmd = 'nmcli c reload'
+ sp.check_call(cmd, shell=True)
+
+
+def save_connections(con_list):
+ if len(con_list) == 0:
+ print('No stored 802.11 connections to save')
+ return
+ if not os.path.exists(SAVE_DIR):
+ os.makedirs(SAVE_DIR)
+ for c in con_list:
+ print('Save connection {}'.format(c))
+ c_loc = os.path.join(NM_CON_DIR, c)
+ if not os.path.exists(c_loc):
+ print(' No stored connection fount at {}'.format(c_loc))
+ continue
+ print(' Found file {}'.format(c_loc))
+ save_f = shutil.copy(c_loc, SAVE_DIR)
+ print(' Saved copy at {}'.format(save_f))
+
+
+def restore_connections():
+ saved_list = [f for f in os.listdir(
+ SAVE_DIR) if os.path.isfile(os.path.join(SAVE_DIR, f))]
+ if len(saved_list) == 0:
+ print('No stored 802.11 connections found')
+ return
+ for f in saved_list:
+ save_f = os.path.join(SAVE_DIR, f)
+ print('Restore connection {}'.format(save_f))
+ restore_f = shutil.copy(save_f, NM_CON_DIR)
+ print(' Restored file at {}'.format(restore_f))
+ os.remove(save_f)
+ print(' Removed copy from {}'.format(save_f))
+
+
+if __name__ == '__main__':
+ if len(sys.argv) != 2:
+ raise SystemExit('ERROR: please specify save or restore')
+ action = sys.argv[1]
+
+ if action == 'save':
+ save_connections(get_nm_connections())
+ elif action == 'restore':
+ restore_connections()
+ reload_nm_connections()
+ else:
+ raise SystemExit('ERROR: unrecognised action')
diff --git a/units/wireless/jobs.pxu b/units/wireless/jobs.pxu
index 52b2b01..9c25c71 100644
--- a/units/wireless/jobs.pxu
+++ b/units/wireless/jobs.pxu
@@ -442,3 +442,31 @@ command:
estimated_duration: 330.0
_description:
Tests the performance of a system's wireless connection through the iperf tool, using UDP packets.
+
+
+unit: template
+template-resource: device
+template-filter: device.category == 'WIRELESS' and device.interface != 'UNKNOWN'
+id: wireless/nm_connection_save_{category}
+category_id: com.canonical.plainbox::wireless
+_summary: Save any NetworkManager 802.11 configurations prior to testing
+plugin: shell
+user: root
+command:
+ wifi_nmcli_backup.py save
+estimated_duration: 2.0
+flags: preserve-locale also-after-suspend also-after-suspend-manual
+
+unit: template
+template-resource: device
+template-filter: device.category == 'WIRELESS' and device.interface != 'UNKNOWN'
+id: wireless/nm_connection_restore_{category}
+category_id: com.canonical.plainbox::wireless
+_summary: Restore any NetworkManager 802.11 configurations after testing
+plugin: shell
+user: root
+command:
+ wifi_nmcli_backup.py restore
+estimated_duration: 2.0
+depends: wireless/nm_connection_save_{category}
+flags: preserve-locale also-after-suspend also-after-suspend-manual \ No newline at end of file