summaryrefslogtreecommitdiff
path: root/scripts/update-configd-include-file
blob: 6615e21ff54138c2dc5c2e1fcbbc4a5d643b22f1 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env python3
###
# A simple script for safely editing configd-include.json, the list of
# scripts which are off-loaded to be run by the daemon.
# Usage:
# update-configd-include-file --add script1.py script2.py ...
#                             --remove scriptA.py scriptB.py ...
#
# Additionally, it offers optional sanity checks by examining the signatures
# of functions and placement of Config instance for consistency with configd
# requirements.
# Usage:
# update-configd-include-file --check-current
#   to check the current include list
# update-configd-include-file --check-file
#   to check arbitrary conf_mode scripts
#
# Note that this feature is the basis for the configd smoketest, but it is of
# limited use in this script, as it requires an environment that has all script
# (python) dependencies installed (e.g. installed image) so that the script may
# be imported for introspection. Nonetheless, for testing and development, it has
# its uses.

import os
import sys
import json
import argparse
import datetime
import importlib.util
from inspect import signature, getsource

from vyos.defaults import directories
from vyos.version import get_version
from vyos.util import cmd

# Defaults

installed_image = False

include_file = 'configd-include.json'
build_relative_include_file = '../data/configd-include.json'
dirname = os.path.dirname(__file__)

build_location_include_file = os.path.join(dirname, build_relative_include_file)
image_location_include_file = os.path.join(directories['data'], include_file)

build_relative_conf_dir = '../src/conf_mode'

build_location_conf_dir = os.path.join(dirname, build_relative_conf_dir)
image_location_conf_dir = directories['conf_mode']

# Get arguments

parser = argparse.ArgumentParser(description='Add or remove scripts from the list of scripts to be run be daemon')
parser.add_argument('--add', nargs='*', default=[],
                    help='scripts to add to configd include list')
parser.add_argument('--remove', nargs='*', default=[],
                    help='scripts to remove from configd include list')
parser.add_argument('--show-diff', action='store_true',
                    help='show list of conf_mode scripts not in include list')
parser.add_argument('--check-file', nargs='*', default=[],
                    help='check files for suitability to run under daemon')
parser.add_argument('--check-current', action="store_true",
                    help='check current include list for suitability to run under daemon')

args = vars(parser.parse_args())

# Check if we are running within installed image; since this script is not
# part of the distribution, there is no need to check if live cd
if get_version():
    installed_image = True

if installed_image:
    include_file = image_location_include_file
    conf_dir = image_location_conf_dir
else:
    include_file = build_location_include_file
    conf_dir = build_location_conf_dir

# Utilities for checking function signature and body
def import_script(s: str):
    """
    A compact form of the import code in vyos-configd
    """
    path = os.path.join(conf_dir, s)
    if not os.path.exists(path):
        print(f"script {s} is not in conf_mode directory")
        return None

    name = os.path.splitext(s)[0].replace('-', '_')

    spec = importlib.util.spec_from_file_location(name, path)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)

    return module

funcs = { 'get_config': False,
          'verify':     False,
          'generate':   False,
          'apply':      False
        }

def check_signatures(s: str) -> bool:
    """
    Basic sanity check: script standard functions should all take one
    argument, including get_config(config=None).
    """
    funcd = dict(funcs)
    for i in list(funcd):
        m = import_script(s)
        f = getattr(m, i, None)
        if not f:
            funcd[i] = True
            continue
        sig = signature(f)
        params = sig.parameters
        if len(params) != 1:
            continue
        if i == 'get_config':
            for p in params.values():
                funcd[i] = True if (p.default is None) else False
        else:
            funcd[i] = True

    res = True

    for k, v in funcd.items():
        if v is False:
            if k == 'get_config':
                print(f"function '{k}' will need the standard modification")
            else:
                print(f"function '{k}' in script '{s}' has wrong signature")
            res = False

    return res

def check_instance_per_function(s: str) -> bool:
    """
    The standard function 'get_config' should have one instantiation of Config;
    all other standard functions, zero.
    """
    funcd = dict(funcs)
    for i in list(funcd):
        m = import_script(s)
        f = getattr(m, i, None)
        if not f:
            funcd[i] = True
            continue
        str_f = getsource(f)
        n = str_f.count('Config()')
        if n == 1 and i == 'get_config':
            funcd[i] = True
        if n == 0 and i != 'get_config':
            funcd[i] = True

    res = True

    for k, v in funcd.items():
        if v is False:
            fi = 'zero' if k == 'get_config' else 'non-zero'
            print(f"function '{k}' in script '{s}' has {fi} instances of Config")
            res = False

    return res

def check_instance_total(s: str) -> bool:
    """
    A script should have at most one instantiation of Config.
    """
    m = import_script(s)
    str_m = getsource(m)
    n = str_m.count('Config()')
    if n != 1:
        print(f"instance of Config outside of 'get_config' in script '{s}'")
        return False

    return True

def check_config_modification(s: str) -> bool:
    """
    Modification to the session config from within a script is necessary in
    certain cases, but the script should then run as stand-alone.
    """
    m = import_script(s)
    str_m = getsource(m)
    n = str_m.count('my_set')
    if n != 0:
        print(f"modification of config within script")
        return False

    return True

def check_viability(s: str) -> bool:
    """
    Check existence, and if on installed image, signatures, instances of
    Config, and modification of session config
    """
    path = os.path.join(conf_dir, s)
    if not os.path.exists(path):
        print(f"script {s} is not in conf_mode directory")
        return False

    if not installed_image:
        if args['check_file'] or args['check_current']:
            print(f"In order to check script viability for offload, run this script on installed image")
        return True

    r1 = check_signatures(s)
    r2 = check_instance_per_function(s)
    r3 = check_instance_total(s)
    r4 = check_config_modification(s)

    if not r1 or not r2 or not r3 or not r4:
        return False

    return True

def check_file(s: str) -> bool:
    if not check_viability(s):
        return False
    return True

def check_files(l: list) -> int:
    check_list = l[:]
    res = 0
    for s in check_list:
        if not check_file(s):
            res = 1
    return res

# Status

def show_diff(l: list):
    print(conf_dir)
    (_, _, filenames) = next(iter(os.walk(conf_dir)))
    filenames.sort()
    res = [i for i in filenames if i not in l]
    print(res)

# Read configd-include.json and add/remove/check/show scripts

with open(include_file, 'r') as f:
    try:
        include_list = json.load(f)
    except OSError as e:
        print(f"configd include file error: {e}")
        sys.exit(1)
    except json.JSONDecodeError as e:
        print(f"JSON load error: {e}")
        sys.exit(1)

if args['show_diff']:
    show_diff(include_list)
    sys.exit(0)

if args['check_file']:
    l = args['check_file']
    ret = check_files(l)
    if not ret:
        print('pass')
    sys.exit(ret)

if args['check_current']:
    ret = check_files(include_list)
    if not ret:
        print('pass')
    sys.exit(ret)

add_list = args['add']
# drop redundencies
add_list = [i for i in add_list if i not in include_list]
# prune entries that don't pass check
add_list = [i for i in add_list if check_file(i)]

remove_list = args['remove']

if not add_list and not remove_list:
    sys.exit(0)

separator = '.'
backup_file_name = separator.join([include_file,
    '{0:%Y-%m-%d-%H%M%S}'.format(datetime.datetime.now()), 'bak'])

cmd(f'cp -p {include_file} {backup_file_name}')

if add_list:
    include_list.extend(add_list)
    include_list.sort()
if remove_list:
    include_list = [i for i in include_list if i not in remove_list]

with open(include_file, 'w') as f:
    try:
        json.dump(include_list, f, indent=0)
    except OSError as e:
        print(f"error writing configd include file: {e}")
        sys.exit(1)