From 8532f668f70c3c0c3f3a6dc5d6cddcd73b7023e1 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Thu, 26 Dec 2019 14:01:40 +0100 Subject: time-zone: T1906: migrate unknown timezones to UTC If - for whatever reason - a timezone specified is invalid ... migrate it to UTC, just in case! --- src/migration-scripts/system/12-to-13 | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'src/migration-scripts/system/12-to-13') diff --git a/src/migration-scripts/system/12-to-13 b/src/migration-scripts/system/12-to-13 index deb17e44a..5f7413d46 100755 --- a/src/migration-scripts/system/12-to-13 +++ b/src/migration-scripts/system/12-to-13 @@ -1,15 +1,23 @@ #!/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! +# Fixup non existent time-zones. Some systems have time-zone set to: Los* +# (Los_Angeles), Den* (Denver), New* (New_York) ... but those are no real IANA +# assigned time zones. In the past they have been silently remapped. +# +# Time to clean it up! +# +# Migrate all configured timezones to real IANA assigned timezones! import re import sys from vyos.configtree import ConfigTree +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() if (len(sys.argv) < 1): print("Must specify file name!") @@ -28,6 +36,10 @@ if not config.exists(tz_base): else: tz = config.return_value(tz_base) + # retrieve all valid timezones + 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 re.match(r'[Ll][Oo][Ss].+', tz): tz = 'America/Los_Angeles' elif re.match(r'[Dd][Ee][Nn].+', tz): @@ -44,6 +56,9 @@ else: tz = 'America/Phoenix' elif re.match(r'GMT(.+)?', tz): tz = 'Etc/' + tz + elif tz not in tz_data: + # assign default UTC timezone + tz = 'UTC' # replace timezone data is required config.set(tz_base, value=tz) -- cgit v1.2.3