1- import configparser , re
2- from linuxmusterLinuxclient7 import logging , constants
1+ import configparser , os , yaml
2+ from linuxmusterLinuxclient7 import logging , constants , fileHelper
33
44def 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
3122def 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
6542def 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
0 commit comments