From ba483643bd7340960d72dc424e7d57876d99b0cd Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sat, 14 Nov 2020 07:15:23 +0100 Subject: options: keyboard: T3038: use proper XML over hardcoded Python value We should not use hardcoded Python values whenever possible. vyos.xml provides an abstraction of the XML CLI definitions providing default values from the CLI specified via the node. This increases consistency among all XML/Python wrappers. Additional small fixes in this commit (besides the bad practice incorporating unrelated changes into the same commit) contain: - Keyboard layout shout be explicitly set for /dev/console - Added missing Debian dependency on console-data - When looking for a key in a dict, we do not need to specify dict.keys() --- src/conf_mode/system-options.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/conf_mode/system-options.py b/src/conf_mode/system-options.py index bf0bd56ec..92d60df1b 100755 --- a/src/conf_mode/system-options.py +++ b/src/conf_mode/system-options.py @@ -20,9 +20,11 @@ from netifaces import interfaces from sys import exit from vyos.config import Config +from vyos.configdict import dict_merge from vyos.template import render from vyos.util import cmd from vyos.validate import is_addr_assigned +from vyos.xml import defaults from vyos import ConfigError from vyos import airbag airbag.enable() @@ -38,6 +40,12 @@ def get_config(config=None): conf = Config() base = ['system', 'options'] options = conf.get_config_dict(base, key_mangling=('-', '_'), get_first_key=True) + + # We have gathered the dict representation of the CLI, but there are default + # options which we need to update into the dictionary retrived. + default_values = defaults(base) + options = dict_merge(default_values, options) + return options def verify(options): @@ -70,7 +78,7 @@ def generate(options): def apply(options): # Beep action - if 'beep_if_fully_booted' in options.keys(): + if 'beep_if_fully_booted' in options: cmd('systemctl enable vyos-beep.service') else: cmd('systemctl disable vyos-beep.service') @@ -95,7 +103,7 @@ def apply(options): # Reboot system on kernel panic with open('/proc/sys/kernel/panic', 'w') as f: - if 'reboot_on_panic' in options.keys(): + if 'reboot_on_panic' in options: f.write('60') else: f.write('0') @@ -107,14 +115,10 @@ def apply(options): else: cmd('systemctl disable tuned.service') - # Keyboard layout - if 'keyboard_layout' in options.keys(): - try: - cmd('loadkeys {}'.format(options['keyboard_layout'])) - except OSError: - raise ConfigError('Dos not possible to set {} as keyboard layout'.format(options['keyboard_layout'])) - else: - cmd('loadkeys us') + # Keyboard layout - there will be always the default key inside the dict + # but we check for key existence anyway + if 'keyboard_layout' in options: + cmd('loadkeys -C /dev/console {keyboard_layout}'.format(**options)) if __name__ == '__main__': try: -- cgit v1.2.3