-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathconfig.py
More file actions
155 lines (126 loc) · 4.54 KB
/
config.py
File metadata and controls
155 lines (126 loc) · 4.54 KB
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
import configparser, os, yaml
from linuxmusterLinuxclient7 import logging, constants, fileHelper
def network():
"""
Get the network configuration in `/etc/linuxmuster-linuxclient7/network.conf`
:return: Tuple (success, dict of keys)
:rtype: tuple
"""
config = _readConfig()
if config is None or not "network" in config:
return False, None
networkConfig = config["network"]
if not _validateNetworkConfig(networkConfig):
logging.error("Error when reading network.conf")
return False, None
return True, networkConfig
def shares():
"""
Get the shares configuration in `/etc/linuxmuster-linuxclient7/config.yml`
:return: Tuple (success, dict of keys)
:rtype: tuple
"""
config = _readConfig()
sharesConfig = {}
if config is not None and "shares" in config:
sharesConfig = config["shares"]
if not "letterTemplate" in sharesConfig:
sharesConfig["letterTemplate"] = constants.defaultShareLetterTemplate
return sharesConfig
def writeNetworkConfig(newNetworkConfig):
"""
Write the network configuration in `/etc/linuxmuster-linuxclient7/config.yml`.
Preserves other configurations.
:param newNetworkConfig: The new config
:type newNetworkConfig: dict
:return: True or False
:rtype: bool
"""
if not _validateNetworkConfig(newNetworkConfig):
return False
config = _readConfig()
if config is None:
config = {}
config["network"] = newNetworkConfig
return _writeConfig(config)
def upgrade():
"""
Upgrade the format of the network configuration in `/etc/linuxmuster-linuxclient7/network.conf`
This is done automatically on package upgrades.
:return: True or False
:rtype: bool
"""
return _upgrade()
def deleteNetworkConfig():
"""
Delete the network configuration file.
:return: True or False
:rtype: bool
"""
config = _readConfig()
if config is None or "network" not in config:
return True
del config["network"]
return _writeConfig(config)
# --------------------
# - Helper functions -
# --------------------
def _readConfig():
if not os.path.exists(constants.configFilePath):
networkConfig = _readLegacyNetworkConfig()
return {"network": networkConfig} if networkConfig is not None else None
try:
with open(constants.configFilePath, "r") as configFile:
yamlContent = configFile.read()
config = yaml.safe_load(yamlContent)
return config
except Exception as e:
logging.error("Error when reading config.yml")
logging.exception(e)
return None
def _readLegacyNetworkConfig():
if not os.path.exists(constants.legacyNetworkConfigFilePath):
return None
configParser = configparser.ConfigParser()
configParser.read(constants.legacyNetworkConfigFilePath)
try:
rawNetworkConfig = configParser["network"]
networkConfig = {
"serverHostname": rawNetworkConfig["serverHostname"],
"domain": rawNetworkConfig["domain"],
"realm": rawNetworkConfig["realm"]
}
return networkConfig
except KeyError as e:
logging.error("Error when reading network.conf. It exists but is invalid.")
logging.exception(e)
return None
def _writeConfig(config):
try:
with open(constants.configFilePath, "w") as configFile:
yamlContent = yaml.dump(config)
configFile.write(yamlContent)
return True
except Exception as e:
logging.error("Error when writing config.yml")
logging.exception(e)
return False
def _validateNetworkConfig(networkConfig):
requiredKeys = {"serverHostname", "domain", "realm"}
return networkConfig is not None and networkConfig.keys() >= requiredKeys
def _upgrade():
logging.info("Upgrading config.")
if os.path.exists(constants.configFilePath):
logging.info("No need to upgrade, already up-to-date.")
return True
logging.info("Upgrading config from network.conf to config.yml")
config = _readConfig()
if config is None or not "network" in config or not _validateNetworkConfig(config["network"]):
logging.error("Error when upgrading config, network.conf is invalid")
return False
if not _writeConfig(config):
logging.error("Error when upgrading config, could not write config.yml")
return False
fileHelper.deleteFile(constants.legacyNetworkConfigFilePath)
logging.info("Config upgrade successfull")
return True