[enh] Log special upgrade into operation_logger

This commit is contained in:
ljf 2019-03-22 15:17:39 +01:00
parent 096c2a7d7b
commit ad0f65aad7
2 changed files with 23 additions and 9 deletions

View file

@ -318,14 +318,27 @@ class OperationLogger(object):
self.flush() self.flush()
self._register_log() self._register_log()
@property
def md_path(self):
"""
Metadata path file
"""
return os.path.join(self.path, self.name + METADATA_FILE_EXT)
@property
def log_path(self):
"""
Log path file
"""
return os.path.join(self.path, self.name + LOG_FILE_EXT)
def _register_log(self): def _register_log(self):
""" """
Register log with a handler connected on log system Register log with a handler connected on log system
""" """
# TODO add a way to not save password on app installation # TODO add a way to not save password on app installation
filename = os.path.join(self.path, self.name + LOG_FILE_EXT) self.file_handler = FileHandler(self.log_path)
self.file_handler = FileHandler(filename)
self.file_handler.formatter = Formatter('%(asctime)s: %(levelname)s - %(message)s') self.file_handler.formatter = Formatter('%(asctime)s: %(levelname)s - %(message)s')
# Listen to the root logger # Listen to the root logger
@ -337,8 +350,7 @@ class OperationLogger(object):
Write or rewrite the metadata file with all metadata known Write or rewrite the metadata file with all metadata known
""" """
filename = os.path.join(self.path, self.name + METADATA_FILE_EXT) with open(self.md_path, 'w') as outfile:
with open(filename, 'w') as outfile:
yaml.safe_dump(self.metadata, outfile, default_flow_style=False) yaml.safe_dump(self.metadata, outfile, default_flow_style=False)
@property @property

View file

@ -660,17 +660,19 @@ def tools_upgrade(operation_logger, auth, ignore_apps=False, ignore_packages=Fal
# likely to kill/restart the api which is in turn likely to kill this # likely to kill/restart the api which is in turn likely to kill this
# command before it ends...) # command before it ends...)
# #
logfile = operation_logger.log_path
logfile = "/var/log/yunohost/special_upgrade_%s.log" % datetime.utcnow().strftime("%Y%m%d_%H%M%S")
command = dist_upgrade + " 2>&1 | tee -a {}".format(logfile) command = dist_upgrade + " 2>&1 | tee -a {}".format(logfile)
MOULINETTE_LOCK = "/var/run/moulinette_yunohost.lock" MOULINETTE_LOCK = "/var/run/moulinette_yunohost.lock"
wait_until_end_of_yunohost_command = "(while [ -f {} ]; do sleep 2; done)".format(MOULINETTE_LOCK) wait_until_end_of_yunohost_command = "(while [ -f {} ]; do sleep 2; done)".format(MOULINETTE_LOCK)
update_log_metadata = "sed -i \"s/ended_at: .*$/ended_at: $(date -u +'%Y-%m-%d %H:%M:%S.%N')/\" {}"
update_log_metadata = update_log_metadata.format(operation_logger.md_path)
# TODO : i18n # TODO : i18n
upgrade_completed = "YunoHost package upgrade completed ! Press [enter] to get the command line back" upgrade_completed = "YunoHost package upgrade completed ! Press [enter] to get the command line back"
command = "({} && {}; echo '{}') &".format(wait_until_end_of_yunohost_command, command = "({} && {} && {}; echo '{}') &".format(wait_until_end_of_yunohost_command,
command, command,
update_log_metadata,
upgrade_completed) upgrade_completed)
logger.debug("Running command :\n{}".format(command)) logger.debug("Running command :\n{}".format(command))