diff options
author | Christian Poessinger <christian@poessinger.com> | 2019-12-25 17:51:29 +0100 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2019-12-26 14:05:52 +0100 |
commit | bc36bb5a9db4143696b96ab74eee40539de3541d (patch) | |
tree | ca9323f9d12c9501a657a6e69b87239098ef949c | |
parent | 1bf918791c5d39da7d93fe02dafc04e756559031 (diff) | |
download | vyos-1x-bc36bb5a9db4143696b96ab74eee40539de3541d.tar.gz vyos-1x-bc36bb5a9db4143696b96ab74eee40539de3541d.zip |
time-zone: T1906: migrate to XML/Python
The current node.def based implementtion should be migrated from
vyatta-cfg-system to vyos-1x. During the migration also provide a migration
script which transforms some ole timezones like "Los_Angeles" into a proper
IANA assigned timezone which should be "America/Los_Angeles".
-rw-r--r-- | interface-definitions/system-time-zone.xml.in | 19 | ||||
-rwxr-xr-x | src/conf_mode/timezone.py | 55 | ||||
-rwxr-xr-x | src/migration-scripts/system/11-to-12 | 2 | ||||
-rwxr-xr-x | src/migration-scripts/system/12-to-13 | 56 | ||||
-rwxr-xr-x | src/validators/timezone | 43 |
5 files changed, 174 insertions, 1 deletions
diff --git a/interface-definitions/system-time-zone.xml.in b/interface-definitions/system-time-zone.xml.in new file mode 100644 index 000000000..1ef739735 --- /dev/null +++ b/interface-definitions/system-time-zone.xml.in @@ -0,0 +1,19 @@ +<?xml version="1.0"?> +<interfaceDefinition> + <node name="system"> + <children> + <leafNode name="time-zone" owner="${vyos_conf_scripts_dir}/timezone.py"> + <properties> + <help>Local time zone (default UTC)</help> + <priority>100</priority> + <completionHelp> + <script>find /usr/share/zoneinfo/posix -type f -or -type l | sed -e s:/usr/share/zoneinfo/posix/:: | sort</script> + </completionHelp> + <constraint> + <validator name="timezone" argument="--validate"/> + </constraint> + </properties> + </leafNode> + </children> + </node> +</interfaceDefinition> diff --git a/src/conf_mode/timezone.py b/src/conf_mode/timezone.py new file mode 100755 index 000000000..d715bd27e --- /dev/null +++ b/src/conf_mode/timezone.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2019 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import sys +import os + +from copy import deepcopy +from vyos.config import Config +from vyos import ConfigError + +default_config_data = { + 'name': 'UTC' +} + +def get_config(): + tz = deepcopy(default_config_data) + conf = Config() + if conf.exists('system time-zone'): + tz['name'] = conf.return_value('system time-zone') + + return tz + +def verify(tz): + pass + +def generate(tz): + pass + +def apply(tz): + cmd = '/usr/bin/timedatectl set-timezone {}'.format(tz['name']) + os.system(cmd) + pass + +if __name__ == '__main__': + try: + c = get_config() + verify(c) + generate(c) + apply(c) + except ConfigError as e: + print(e) + sys.exit(1) diff --git a/src/migration-scripts/system/11-to-12 b/src/migration-scripts/system/11-to-12 index 64425e2b9..0c92a0746 100755 --- a/src/migration-scripts/system/11-to-12 +++ b/src/migration-scripts/system/11-to-12 @@ -27,7 +27,7 @@ for host in config.list_nodes(cbase): if re.search(':[0-9]{1,5}$',host): h = re.search('^[a-zA-Z\-0-9\.]+', host).group(0) p = re.sub(':', '', re.search(':[0-9]+$', host).group(0)) - config.set(cbase + [h]) + config.set(cbase + [h]) config.set(cbase + [h, 'port'], value=p) for fac in config.list_nodes(cbase + [host, 'facility']): config.set(cbase + [h, 'facility', fac]) diff --git a/src/migration-scripts/system/12-to-13 b/src/migration-scripts/system/12-to-13 new file mode 100755 index 000000000..deb17e44a --- /dev/null +++ b/src/migration-scripts/system/12-to-13 @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +# Fixup non existent time-zones. Some systems have time-zone set to: +# - Los* (Los_Angeles) +# - Den* (Denver) +# ... but thos are no real IANA assigned time zones. In the past the have been +# silently remapped. Time to clean it up! + +import re +import sys + +from vyos.configtree import ConfigTree + +if (len(sys.argv) < 1): + print("Must specify file name!") + sys.exit(1) + +file_name = sys.argv[1] + +with open(file_name, 'r') as f: + config_file = f.read() + +config = ConfigTree(config_file) +tz_base = ['system', 'time-zone'] +if not config.exists(tz_base): + # Nothing to do + sys.exit(0) +else: + tz = config.return_value(tz_base) + + if re.match(r'[Ll][Oo][Ss].+', tz): + tz = 'America/Los_Angeles' + elif re.match(r'[Dd][Ee][Nn].+', tz): + tz = 'America/Denver' + elif re.match(r'[Hh][Oo][Nn][Oo].+', tz): + tz = 'Pacific/Honolulu' + elif re.match(r'[Nn][Ee][Ww].+', tz): + tz = 'America/New_York' + elif re.match(r'[Cc][Hh][Ii][Cc]*.+', tz): + tz = 'America/Chicago' + elif re.match(r'[Aa][Nn][Cc].+', tz): + tz = 'America/Anchorage' + elif re.match(r'[Pp][Hh][Oo].+', tz): + tz = 'America/Phoenix' + elif re.match(r'GMT(.+)?', tz): + tz = 'Etc/' + tz + + # replace timezone data is required + config.set(tz_base, value=tz) + + try: + with open(file_name, 'w') as f: + f.write(config.to_string()) + except OSError as e: + print("Failed to save the modified config: {}".format(e)) + sys.exit(1) diff --git a/src/validators/timezone b/src/validators/timezone new file mode 100755 index 000000000..d7a8f64c4 --- /dev/null +++ b/src/validators/timezone @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2019 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import argparse + +from sys import exit +from subprocess import Popen, PIPE, STDOUT + +def _cmd(cmd): + p = Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=True) + tmp = p.communicate()[0].strip() + return tmp.decode() + +parser = argparse.ArgumentParser() +parser.add_argument("--validate", action="store", help="Check if timezone is valid") + +if __name__ == '__main__': + args = parser.parse_args() + + if args.validate: + tz_data = _cmd('find /usr/share/zoneinfo/posix -type f -or -type l | sed -e s:/usr/share/zoneinfo/posix/::') + tz_data = tz_data.split('\n') + # if timezone can't be found in list it's invalid + if args.validate not in tz_data: + exit(1) + else: + parser.print_help() + exit(1) + + exit(0) |