summaryrefslogtreecommitdiff
path: root/src/migration-scripts/system/13-to-14
blob: fbbecbcd34a97e1799939f82db78caaf7ad71fc0 (plain)
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
# Copyright 2019-2024 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library.  If not, see <http://www.gnu.org/licenses/>.

# 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

from vyos.configtree import ConfigTree
from vyos.utils.process import cmd


tz_base = ['system', 'time-zone']

def migrate(config: ConfigTree) -> None:
    if not config.exists(tz_base):
        # Nothing to do
        return

    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)