Skip to content

Commit e8d1ca4

Browse files
committed
Feat: first draft of yaml config
1 parent 092209f commit e8d1ca4

10 files changed

Lines changed: 296 additions & 86 deletions

File tree

debian/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Standards-Version: 5.0.0
77

88
Package: linuxmuster-linuxclient7
99
Architecture: all
10-
Depends: python3, python3-ldap, cifs-utils, ldb-tools, bind9-host, ipcalc, hxtools, network-manager, krb5-user, keyutils, samba, sssd, sssd-tools, libsss-sudo, adcli, libpam-sss, sudo, realmd, cups (>= 2.3.0), coreutils, libcap2-bin
10+
Depends: python3, python3-ldap, python3-yaml, cifs-utils, ldb-tools, bind9-host, ipcalc, hxtools, network-manager, krb5-user, keyutils, samba, sssd, sssd-tools, libsss-sudo, adcli, libpam-sss, sudo, realmd, cups (>= 2.3.0), coreutils, libcap2-bin
1111
Description: Package for Ubuntu clients to connect to the linuxmuster.net 7 active directory server.
1212
Conflicts: linuxmuster-client-adsso, linuxmuster-client-adsso7, ni-lmn-client-adsso
Lines changed: 78 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import configparser, re
2-
from linuxmusterLinuxclient7 import logging, constants
1+
import configparser, os, yaml
2+
from linuxmusterLinuxclient7 import logging, constants, fileHelper
33

44
def network():
55
"""
@@ -8,59 +8,36 @@ def network():
88
:return: Tuple (success, dict of keys)
99
:rtype: tuple
1010
"""
11-
rc, rawNetworkConfig = _readNetworkConfig()
12-
if not rc:
11+
config = _readConfig()
12+
if config is None or not "network" in config:
1313
return False, None
1414

15-
if not _checkNetworkConfigVersion(rawNetworkConfig)[0]:
16-
return False, None
17-
18-
networkConfig = {}
19-
20-
try:
21-
networkConfig["serverHostname"] = rawNetworkConfig["serverHostname"]
22-
networkConfig["domain"] = rawNetworkConfig["domain"]
23-
networkConfig["realm"] = rawNetworkConfig["realm"]
24-
except KeyError as e:
25-
logging.error("Error when reading network.conf (2)")
26-
logging.exception(e)
15+
networkConfig = config["network"]
16+
if not _validateNetworkConfig(networkConfig):
17+
logging.error("Error when reading network.conf")
2718
return False, None
2819

2920
return True, networkConfig
3021

3122
def writeNetworkConfig(newNetworkConfig):
3223
"""
33-
Write the network configuration in `/etc/linuxmuster-linuxclient7/network.conf`
24+
Write the network configuration in `/etc/linuxmuster-linuxclient7/config.yml`.
25+
Preserves other configurations.
3426
3527
:param newNetworkConfig: The new config
3628
:type newNetworkConfig: dict
3729
:return: True or False
3830
:rtype: bool
3931
"""
40-
networkConfig = configparser.ConfigParser(interpolation=None)
41-
42-
try:
43-
networkConfig["network"] = {}
44-
networkConfig["network"]["version"] = str(_networkConfigVersion())
45-
networkConfig["network"]["serverHostname"] = newNetworkConfig["serverHostname"]
46-
networkConfig["network"]["domain"] = newNetworkConfig["domain"]
47-
networkConfig["network"]["realm"] = newNetworkConfig["realm"]
48-
except Exception as e:
49-
logging.error("Error when preprocessing new network configuration!")
50-
logging.exception(e)
32+
if not _validateNetworkConfig(newNetworkConfig):
5133
return False
5234

53-
try:
54-
logging.info("Writing new network Configuration")
55-
with open(constants.networkConfigFilePath, 'w') as networkConfigFile:
56-
networkConfig.write(networkConfigFile)
35+
config = _readConfig()
36+
if config is None:
37+
config = {}
38+
config["network"] = newNetworkConfig
5739

58-
except Exception as e:
59-
logging.error("Failed!")
60-
logging.exception(e)
61-
return False
62-
63-
return True
40+
return _writeConfig(config)
6441

6542
def upgrade():
6643
"""
@@ -70,67 +47,87 @@ def upgrade():
7047
:return: True or False
7148
:rtype: bool
7249
"""
73-
return _upgradeNetworkConfig()
50+
return _upgrade()
51+
52+
def delete():
53+
"""
54+
Delete the network configuration file.
55+
56+
:return: True or False
57+
:rtype: bool
58+
"""
59+
legacyNetworkConfigFleDeleted = fileHelper.deleteFile(constants.legacyNetworkConfigFilePath)
60+
configFileDeleted = fileHelper.deleteFile(constants.configFilePath)
61+
return legacyNetworkConfigFleDeleted and configFileDeleted
62+
7463

7564
# --------------------
7665
# - Helper functions -
7766
# --------------------
7867

79-
def _readNetworkConfig():
68+
def _readConfig():
69+
if not os.path.exists(constants.configFilePath):
70+
networkConfig = _readLegacyNetworkConfig()
71+
return {"network": networkConfig} if networkConfig is not None else None
72+
73+
try:
74+
with open(constants.configFilePath, "r") as configFile:
75+
yamlContent = configFile.read()
76+
config = yaml.safe_load(yamlContent)
77+
return config
78+
except Exception as e:
79+
logging.error("Error when reading config.yml")
80+
logging.exception(e)
81+
return None
82+
83+
def _readLegacyNetworkConfig():
8084
configParser = configparser.ConfigParser()
81-
configParser.read(constants.networkConfigFilePath)
85+
configParser.read(constants.legacyNetworkConfigFilePath)
8286
try:
8387
rawNetworkConfig = configParser["network"]
84-
return True, rawNetworkConfig
88+
networkConfig = {
89+
"serverHostname": rawNetworkConfig["serverHostname"],
90+
"domain": rawNetworkConfig["domain"],
91+
"realm": rawNetworkConfig["realm"]
92+
}
93+
return networkConfig
8594
except KeyError as e:
8695
logging.error("Error when reading network.conf (1)")
8796
logging.exception(e)
88-
return False, None
89-
return configParser
90-
91-
def _networkConfigVersion():
92-
return 1
97+
return None
9398

94-
def _checkNetworkConfigVersion(rawNetworkConfig):
99+
def _writeConfig(config):
95100
try:
96-
networkConfigVersion = int(rawNetworkConfig["version"])
97-
except KeyError as e:
98-
logging.warning("The network.conf version could not be identified, assuming 0")
99-
networkConfigVersion = 0
100-
101-
if networkConfigVersion != _networkConfigVersion():
102-
logging.warning("The network.conf Version is a mismatch!")
103-
return False, networkConfigVersion
104-
105-
return True, networkConfigVersion
106-
107-
def _upgradeNetworkConfig():
108-
logging.info("Upgrading network config.")
109-
rc, rawNetworkConfig = _readNetworkConfig()
110-
if not rc:
101+
with open(constants.configFilePath, "w") as configFile:
102+
yamlContent = yaml.dump(config)
103+
configFile.write(yamlContent)
104+
return True
105+
except Exception as e:
106+
logging.error("Error when writing config.yml")
107+
logging.exception(e)
111108
return False
112109

113-
rc, networkConfigVersion = _checkNetworkConfigVersion(rawNetworkConfig)
114-
if rc:
110+
def _validateNetworkConfig(networkConfig):
111+
requiredKeys = {"serverHostname", "domain", "realm"}
112+
return networkConfig is not None and networkConfig.keys() >= requiredKeys
113+
114+
def _upgrade():
115+
logging.info("Upgrading config.")
116+
if os.path.exists(constants.configFilePath):
115117
logging.info("No need to upgrade, already up-to-date.")
116118
return True
117119

118-
logging.info("Upgrading network config from {0} to {1}".format(networkConfigVersion, _networkConfigVersion()))
119-
120-
if networkConfigVersion > _networkConfigVersion():
121-
logging.error("Cannot upgrade from a newer version to an older one!")
122-
return False
120+
logging.info("Upgrading config from network.conf to config.yml")
123121

124-
try:
125-
if networkConfigVersion == 0:
126-
newNetworkConfig = {}
127-
newNetworkConfig["serverHostname"] = rawNetworkConfig["serverHostname"] + "." + rawNetworkConfig["domain"]
128-
newNetworkConfig["domain"] = rawNetworkConfig["domain"]
129-
newNetworkConfig["realm"] = rawNetworkConfig["domain"].upper()
130-
return writeNetworkConfig(newNetworkConfig)
131-
except Exception as e:
132-
logging.error("Error when upgrading network config!")
133-
logging.exception(e)
122+
config = _readConfig()
123+
if not "network" in config or not _validateNetworkConfig(config["network"]):
124+
logging.error("Error when upgrading config, network.conf is invalid")
134125
return False
135-
126+
127+
if not _writeConfig(config):
128+
logging.error("Error when upgrading config, could not write config.yml")
129+
return False
130+
131+
fileHelper.deleteFile(constants.legacyNetworkConfigFilePath)
132+
logging.info("Config upgrade successfull")
136133
return True

usr/lib/python3/dist-packages/linuxmusterLinuxclient7/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
configFileTemplateDir = shareBaseDir + "/templates"
1717
scriptDir = shareBaseDir + "/scripts"
1818

19-
networkConfigFilePath = etcBaseDir + "/network.conf"
19+
legacyNetworkConfigFilePath = etcBaseDir + "/network.conf"
20+
configFilePath = etcBaseDir + "/config.yml"
2021
# {} will be substituted for the username
2122
tmpEnvironmentFilePath = "/home/{}/.linuxmuster-linuxclient7-environment.sh"
2223

usr/lib/python3/dist-packages/linuxmusterLinuxclient7/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ pytest==7.1.1
1111
pytest-cov==3.0.0
1212
python-ldap==3.4.0
1313
tomli==2.0.1
14+
pyyaml==6.0

usr/lib/python3/dist-packages/linuxmusterLinuxclient7/setup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ def isSetup():
179179
:return: True if setup, False otherwise
180180
:rtype: bool
181181
"""
182-
return os.path.isfile(constants.networkConfigFilePath)
182+
rc, networkConfig = config.network()
183+
return rc and networkConfig is not None
183184

184185
# --------------------
185186
# - Helper functions -
@@ -210,8 +211,8 @@ def _cleanOldDomainJoins():
210211
return False
211212

212213
# remove network.conf
213-
logging.info(f"Deleting {constants.networkConfigFilePath} if exists ...")
214-
if not fileHelper.deleteFile(constants.networkConfigFilePath):
214+
logging.info(f"Deleting configuration files if exist ...")
215+
if not config.delete():
215216
return False
216217

217218
return True
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
network:
2+
sserverHostname: server.linuxmuster.lan
3+
ddomain: linuxmuster.lan
4+
rrealm: LINUXMUSTER.LAN
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*-?
2+
network:
3+
serverHostname: server.linuxmuster.lan
4+
domain: linuxmuster.lan
5+
realm: LINUXMUSTER.LAN
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
network:
2+
serverHostname: server.linuxmuster.lan
3+
domain: linuxmuster.lan
4+
realm: LINUXMUSTER.LAN
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[network]
2+
version = 1
3+
serverhostname = server.linuxmuster.legacy
4+
domain = linuxmuster.legacy
5+
realm = LINUXMUSTER.LEGACY

0 commit comments

Comments
 (0)