1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
#!/usr/bin/env python3
# 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 vyos.util import cmd
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)
# retrieve all valid timezones
try:
tz_datas = cmd('timedatectl list-timezones')
except OSError:
tz_datas = ''
tz_data = tz_datas.split('\n')
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
elif tz not in tz_data:
# assign default UTC timezone
tz = 'UTC'
# 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)
|