summaryrefslogtreecommitdiff
path: root/src/helpers/vyos-frr-render.py
blob: bffdda4a2602fe562e0b07274e36a9b959df6c1b (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
68
69
70
71
72
73
#!/usr/bin/env python3
#
# Copyright (C) VyOS Inc.
#
# 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/>.

"""Render the FRR configuration and reload FRR without vyos-configd.

Only vyos-configd renders FRR, and only at the end of a commit it handled
itself. Two paths need a render without it: a DHCP lease event, which changes
the rendered configuration without any CLI change, and a commit which fell back
to running the conf-mode scripts directly."""

import sys

from argparse import ArgumentParser
from time import sleep

from vyos.config import Config
from vyos.frrender import FRRender
from vyos.frrender import frr_applied_config_file
from vyos.frrender import frr_config_file
from vyos.frrender import frr_render_lock
from vyos.frrender import get_frrender_dict
from vyos.utils.commit import commit_in_progress2
from vyos.utils.file import read_file
from vyos import ConfigError


def render() -> None:
    """Reload FRR if the rendered configuration differs from the one FRR was
    last reloaded with.

    A fresh instance has no cached configuration, thus its own change detection
    can not be used here. The rendered file is no substitute for it - a render
    which failed before the reload leaves it unapplied."""
    frr = FRRender()
    frr.generate(get_frrender_dict(Config()))

    if read_file(frr_config_file) == read_file(frr_applied_config_file, None):
        return

    frr.apply()


if __name__ == '__main__':
    parser = ArgumentParser()
    # The active configuration only changes once a commit is complete. Reading
    # it earlier renders the pre-commit state, which the commit then never
    # corrects as its own FRR render has already run. Not for callers which
    # are part of a commit - they would wait for themselves.
    parser.add_argument('--after-commit', action='store_true',
                        help='Wait for a running commit to complete first')
    args = parser.parse_args()

    while args.after_commit and commit_in_progress2():
        sleep(0.25)
    try:
        with frr_render_lock():
            render()
    except ConfigError as e:
        print(e)
        sys.exit(1)