-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
83 lines (71 loc) · 2.2 KB
/
config.py
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
import cerberus as cer
import logging as log
import yaml
from enum import Enum
class ConfigKeys(Enum):
NAME = "name"
SSH_PORT = "ssh_port"
HOSTNAME = "hostname"
SSH_USER = "ssh_username"
ETH_ADDRESS = "ethaddr"
ENABLED = "enabled"
schema = {
'endpoints': {
'type': 'list',
'schema': {
'type': 'dict',
'schema': {
ConfigKeys.NAME.value: {
'required': True,
'type': 'string',
'minlength': 3,
'maxlength': 32
},
ConfigKeys.ETH_ADDRESS.value: {
'required': True,
'type': 'string',
'minlength': 17,
'maxlength': 17,
'regex': '[0-9a-fA-F]{2}([-:]?)[0-9a-fA-F]{2}(\\1[0-9a-fA-F]{2}){4}$'
},
ConfigKeys.HOSTNAME.value: {
'required': True,
'type': 'string',
},
ConfigKeys.SSH_USER.value: {
'required': False,
'type': 'string',
'default': 'root'
},
ConfigKeys.SSH_PORT.value: {
'required': False,
'type': 'number',
'min': 1,
'max': 65535,
'default': 22
},
ConfigKeys.ENABLED.value: {
'required': False,
'type': 'boolean',
'default': True
}
}
},
}
}
def load_config(config_file):
with open(config_file, 'r') as stream:
try:
return yaml.safe_load(stream)
except yaml.YAMLError as exception:
raise exception
def valid_config(config_file):
doc = load_config(config_file)
validator = cer.Validator(schema)
result = validator.validate(doc, schema)
if not result:
log.critical(
f"Invalid configuration file '{config_file}'. Detected problems: {validator.errors}")
return False
log.info(f"Config file '{config_file}' seems ok.")
return True