summaryrefslogtreecommitdiff
path: root/src/conf_mode/system-display.py
blob: 557b7cf6a72c5f15369c2c2e221f5cec40b4e60a (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
#
# Copyright (C) 2020 Francois Mertz fireboxled at gmail.com
#
# 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 os

from sys import exit

from vyos.config import Config
from vyos import ConfigError
from vyos.util import run
from vyos.template import render

from vyos import airbag
airbag.enable()

def get_config():
    # Return a (possibly empty) configuration dictionary
    return Config().get_config_dict(['system', 'display'])

def generate(config_dict):
    if not config_dict:
        return None
    # Render config file for daemon LCDd
    render('/etc/LCDd.conf', 'system-display/LCDd.conf.tmpl', config_dict)
    # Render config file for client lcdproc
    render('/etc/lcdproc.conf', 'system-display/lcdproc.conf.tmpl', config_dict)

    return None

def verify(config_dict):
    if not config_dict:
        return None

    if 'model' not in config_dict:
        raise ConfigError('Display model is [REQUIRED]')

    if (           'show' not in config_dict
        or (      'clock' not in config_dict['show']
            and 'network' not in config_dict['show']
            and    'host' not in config_dict['show']
           )
       ):
        raise ConfigError('Display show must have a clock, host or network')

    if (      'network'     in config_dict['show']
        and 'interface' not in config_dict['show']['network']
       ):
        raise ConfigError('Display show network must have an interface')

    if (      'network' in config_dict['show']
        and 'interface' in config_dict['show']['network']
        and len(config_dict['show']['network']['interface']) > 3
       ):
        raise ConfigError('Display show network cannot have > 3 interfaces')

    return None

def apply(config_dict):
    # Stop client
    run('systemctl stop lcdproc.service')

    if not config_dict or 'disabled' in config_dict:
        # Stop server
        run('systemctl stop LCDd.service')
        return None

    # Restart server
    run('systemctl restart LCDd.service')
    # Start client
    run('systemctl start lcdproc.service')

    return None

if __name__ == '__main__':
    try:
        config_dict = get_config()
        verify(config_dict)
        generate(config_dict)
        apply(config_dict)
    except ConfigError as e:
        print(e)
        exit(1)