Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
lbr38 committed Jul 18, 2024
1 parent 3146906 commit a064209
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 87 deletions.
20 changes: 10 additions & 10 deletions src/controllers/App/App.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ def getVersion(self):
# Return linupdate configuration from config file
#
#-------------------------------------------------------------------------------------------------------------------
def getConf(self):
# Open YAML config file:
with open('/etc/linupdate/linupdate.yml') as stream:
try:
# Read YAML and return profile
data = yaml.safe_load(stream)
return data

except yaml.YAMLError as exception:
print(exception)
# def getConf(self):
# # Open YAML config file:
# with open('/etc/linupdate/linupdate.yml') as stream:
# try:
# # Read YAML and return profile
# data = yaml.safe_load(stream)
# return data

# except yaml.YAMLError as exception:
# print(exception)


#-------------------------------------------------------------------------------------------------------------------
Expand Down
208 changes: 140 additions & 68 deletions src/controllers/App/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,28 @@ class Config:
#
#-------------------------------------------------------------------------------------------------------------------
def getConf(self):
# Open YAML config file:
# Open main YAML config file:
with open('/etc/linupdate/linupdate.yml') as stream:
try:
# Read YAML and return configuration
return yaml.safe_load(stream)
main = yaml.safe_load(stream)

except yaml.YAMLError as e:
raise Exception(str(e))

# Open update YAML config file:
with open('/etc/linupdate/update.yml') as stream:
try:
# Read YAML and return configuration
update = yaml.safe_load(stream)

except yaml.YAMLError as e:
raise Exception(str(e))

# Merge and return both configurations
configuration = {**main, **update}

return configuration


#-------------------------------------------------------------------------------------------------------------------
Expand All @@ -28,9 +42,13 @@ def getConf(self):
#-------------------------------------------------------------------------------------------------------------------
def checkConf(self):
try:
# Check if the config file exists
# Check if main config file exists
if not Path('/etc/linupdate/linupdate.yml').is_file():
raise Exception('configuration file /etc/linupdate/linupdate.yml is missing')

# Check if update config file exists
if not Path('/etc/linupdate/update.yml').is_file():
raise Exception('configuration file /etc/linupdate/update.yml is missing')

# Retrieve configuration
configuration = self.getConf()
Expand Down Expand Up @@ -67,92 +85,120 @@ def checkConf(self):
# Check if recipient param is set in the mail_alert section
if 'recipient' not in configuration['main']['mail_alert']:
raise Exception('recipient param is missing in the mail_alert section')

# Check if modules section is set in the config file
if 'modules' not in configuration:
raise Exception('modules section is missing in the configuration file')

# Check if enabled param is set in the modules section
if 'enabled' not in configuration['modules']:
raise Exception('enabled param is missing in the modules section')

# Check if the update section is set
if 'update' not in configuration:
raise Exception('update section is missing in /etc/linupdate/update.yml')

# Check if the method param is set in the update section
if 'method' not in configuration['update']:
raise Exception('method param is missing in the update section')

if configuration['update']['method'] == None:
raise Exception('method is empty in the update section')

# Check if exit_on_package_update_error param is set in the main section
if 'exit_on_package_update_error' not in configuration['main']:
raise Exception('exit_on_package_update_error param is missing in the main section')
# Check if exit_on_package_update_error param is set in the update section
if 'exit_on_package_update_error' not in configuration['update']:
raise Exception('exit_on_package_update_error param is missing in the update section')

if configuration['main']['exit_on_package_update_error'] == None:
raise Exception('exit_on_package_update_error is empty in the main section')
if configuration['update']['exit_on_package_update_error'] == None:
raise Exception('exit_on_package_update_error is empty in the update section')

# Check if packages section is set in the config file
if 'packages' not in configuration:
if 'packages' not in configuration['update']:
raise Exception('packages section is missing in the configuration file')

# Check if exclude section is set in the packages section
if 'exclude' not in configuration['packages']:
if 'exclude' not in configuration['update']['packages']:
raise Exception('exclude section is missing in the packages section')

# Check if always param is set in the exclude section
if 'always' not in configuration['packages']['exclude']:
if 'always' not in configuration['update']['packages']['exclude']:
raise Exception('always param is missing in the exclude section')

# Check if on_major_update param is set in the exclude section
if 'on_major_update' not in configuration['packages']['exclude']:
if 'on_major_update' not in configuration['update']['packages']['exclude']:
raise Exception('on_major_update param is missing in the exclude section')

# Check if services section is set in the config file
if 'services' not in configuration:
if 'services' not in configuration['post_update']:
raise Exception('services section is missing in the configuration file')

# Check if restart param is set in the services section
if 'restart' not in configuration['services']:
if 'restart' not in configuration['post_update']['services']:
raise Exception('restart param is missing in the services section')

# Check if modules section is set in the config file
if 'modules' not in configuration:
raise Exception('modules section is missing in the configuration file')

# Check if enabled param is set in the modules section
if 'enabled' not in configuration['modules']:
raise Exception('enabled param is missing in the modules section')

except Exception as e:
raise Exception('Fatal configuration file error: ' + str(e))


#-------------------------------------------------------------------------------------------------------------------
#
# Generate main config file if not exist
# Generate config files if not exist
#
#-------------------------------------------------------------------------------------------------------------------
def generateConf(self):
# Quit if the config file already exists
if Path('/etc/linupdate/linupdate.yml').is_file():
return

# Minimal config file
data = {
'main': {
'profile': 'PC',
'environment': 'prod',
# If main config file does not exist, generate it
if not Path('/etc/linupdate/linupdate.yml').is_file():

# Content
data = {
'main': {
'profile': 'PC',
'environment': 'prod',

'mail_alert': {
'enabled': False,
'recipient': [],
}
},
'modules': {
'enabled': [],
}
}

'mail_alert': {
'enabled': False,
'recipient': [],
# Write config file
try:
with open('/etc/linupdate/linupdate.yml', 'w') as file:
yaml.dump(data, file, default_flow_style=False, sort_keys=False)
except Exception as e:
raise Exception('Could not write to configuration file /etc/linupdate/linupdate.yml: ' + str(e))

# If update config file does not exist, generate it
if not Path('/etc/linupdate/update.yml').is_file():
# Content
data = {
'update': {
'method': 'package',
'exit_on_package_update_error': True,
'packages': {
'exclude': {
'always': [],
'on_major_update': []
}
},
},
'exit_on_package_update_error': True,
},
'packages': {
'exclude': {
'always': [],
'on_major_update': []
'post_update': {
'services': {
'restart': [],
}
}
},
'services': {
'restart': [],
},
'modules': {
'enabled': [],
}
}

# Write config file
try:
with open('/etc/linupdate/linupdate.yml', 'w') as file:
yaml.dump(data, file, default_flow_style=False, sort_keys=False)
except Exception as e:
raise Exception('Could not create configuration file /etc/linupdate/linupdate.yml: ' + str(e))
# Write config file
try:
with open('/etc/linupdate/update.yml', 'w') as file:
yaml.dump(data, file, default_flow_style=False, sort_keys=False)
except Exception as e:
raise Exception('Could not write to configuration file /etc/linupdate/update.yml: ' + str(e))


#-------------------------------------------------------------------------------------------------------------------
Expand All @@ -161,12 +207,38 @@ def generateConf(self):
#
#-------------------------------------------------------------------------------------------------------------------
def writeConf(self, configuration):
# print(configuration)
main_config = {
'main': {
**configuration['main']
},
'modules': {
**configuration['modules']
}
}

update_config = {
'update': {
**configuration['update']
},
'post_update': {
**configuration['post_update']
}
}

# Write to main config file
try:
# Write config file
with open('/etc/linupdate/linupdate.yml', 'w') as file:
yaml.dump(configuration, file, default_flow_style=False, sort_keys=False)
yaml.dump(main_config, file, default_flow_style=False, sort_keys=False)
except Exception as e:
raise Exception('Could not write configuration file /etc/linupdate/linupdate.yml: ' + str(e))

# Write to update config file
try:
with open('/etc/linupdate/update.yml', 'w') as file:
yaml.dump(update_config, file, default_flow_style=False, sort_keys=False)
except Exception as e:
raise Exception('Could not write configuration file /etc/linupdate/update.yml: ' + str(e))


#-------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -258,7 +330,7 @@ def getExclude(self):
# Get current configuration
configuration = self.getConf()

return configuration['packages']['exclude']['always']
return configuration['update']['packages']['exclude']['always']


#-------------------------------------------------------------------------------------------------------------------
Expand All @@ -272,14 +344,14 @@ def setExclude(self, exclude: str = None):

# If no package to exclude, set empty list
if not exclude:
configuration['packages']['exclude']['always'] = []
configuration['update']['packages']['exclude']['always'] = []

else:
# For each package to exclude, append it to the list if not already in
for item in exclude.split(","):
if item not in configuration['packages']['exclude']['always']:
if item not in configuration['update']['packages']['exclude']['always']:
# Append exclude
configuration['packages']['exclude']['always'].append(item)
configuration['update']['packages']['exclude']['always'].append(item)

# Write config file
self.writeConf(configuration)
Expand All @@ -294,7 +366,7 @@ def getExcludeMajor(self):
# Get current configuration
configuration = self.getConf()

return configuration['packages']['exclude']['on_major_update']
return configuration['update']['packages']['exclude']['on_major_update']


#-------------------------------------------------------------------------------------------------------------------
Expand All @@ -308,14 +380,14 @@ def setExcludeMajor(self, exclude: str = None):

# If no package to exclude, set empty list
if not exclude:
configuration['packages']['exclude']['on_major_update'] = []
configuration['update']['packages']['exclude']['on_major_update'] = []

else:
# For each package to exclude, append it to the list if not already in
for item in exclude.split(","):
if item not in configuration['packages']['exclude']['on_major_update']:
if item not in configuration['update']['packages']['exclude']['on_major_update']:
# Append exclude
configuration['packages']['exclude']['on_major_update'].append(item)
configuration['update']['packages']['exclude']['on_major_update'].append(item)

# Write config file
self.writeConf(configuration)
Expand All @@ -330,7 +402,7 @@ def getServiceToRestart(self):
# Get current configuration
configuration = self.getConf()

return configuration['services']['restart']
return configuration['post_update']['services']['restart']


#-------------------------------------------------------------------------------------------------------------------
Expand All @@ -344,14 +416,14 @@ def setServiceToRestart(self, services: str = None):

# If no service to restart, set empty list
if not services:
configuration['services']['restart'] = []
configuration['post_update']['services']['restart'] = []

else:
# For each service to restart, append it to the list if not already in
for item in services.split(","):
if item not in configuration['services']['restart']:
if item not in configuration['post_update']['services']['restart']:
# Append service
configuration['services']['restart'].append(item)
configuration['post_update']['services']['restart'].append(item)

# Write config file
self.writeConf(configuration)
Expand Down
1 change: 1 addition & 0 deletions src/controllers/Args.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ def parse(self):
# If --mod-configure param has been set
#
if args.mod_configure != "null":
print(args.mod_configure)
# If module to configure is set (not 'None'), configure the module
if args.mod_configure:
try:
Expand Down
Loading

0 comments on commit a064209

Please sign in to comment.