Merge "Configure code to use log format from software.conf"

This commit is contained in:
Zuul
2025-05-14 20:08:06 +00:00
committed by Gerrit Code Review
8 changed files with 81 additions and 27 deletions

View File

@@ -1,11 +1,12 @@
# #
# Copyright (c) 2023-2024 Wind River Systems, Inc. # Copyright (c) 2023-2025 Wind River Systems, Inc.
# #
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
# #
# This is an utility module used by standalone USM upgrade scripts # This is an utility module used by standalone USM upgrade scripts
# that runs on the FROM-side context but using TO-side code base # that runs on the FROM-side context but using TO-side code base
# #
import configparser import configparser
import json import json
import logging import logging
@@ -17,10 +18,19 @@ import sys
import time import time
import yaml import yaml
from oslo_config import cfg
from keystoneauth1 import exceptions from keystoneauth1 import exceptions
from keystoneauth1 import identity from keystoneauth1 import identity
from keystoneauth1 import session from keystoneauth1 import session
LOG = logging.getLogger('main_logger')
CONF = cfg.CONF
logging_default_format_string = None
software_conf_mtime = 0
software_conf = '/etc/software/software.conf'
def get_token_endpoint(config, service_type="platform"): def get_token_endpoint(config, service_type="platform"):
"""Returns an endpoint and a token for a service """Returns an endpoint and a token for a service
@@ -169,13 +179,18 @@ def get_system_info(sysinv_client):
def configure_logging(filename, log_level=logging.INFO): def configure_logging(filename, log_level=logging.INFO):
read_log_config()
my_exec = os.path.basename(sys.argv[0]) my_exec = os.path.basename(sys.argv[0])
log_format = ('%(asctime)s: ' + my_exec + '[%(process)s]: ' log_format = logging_default_format_string
'%(filename)s(%(lineno)s): %(levelname)s: %(message)s') log_format = log_format.replace('%(exec)s', my_exec)
log_datefmt = "%FT%T" formatter = logging.Formatter(log_format, datefmt="%FT%T")
logging.basicConfig(filename=filename, format=log_format, level=log_level, datefmt=log_datefmt) LOG.setLevel(log_level)
main_log_handler = logging.FileHandler(filename)
main_log_handler.setFormatter(formatter)
LOG.addHandler(main_log_handler)
def get_platform_conf(key): def get_platform_conf(key):
@@ -222,6 +237,27 @@ def get_secret_data_yaml(name, namespace):
return None return None
def read_log_config():
global software_conf_mtime
global software_conf
if software_conf_mtime == os.stat(software_conf).st_mtime:
# The file has not changed since it was last read
return
global logging_default_format_string
# TODO(lbonatti) Remove this default_format when logging_default_format_string is present in stx11,
# when this becomes the N release.
default_format = ('%(asctime)s.%(msecs)03d USM - %(exec)s [%(process)s:%(thread)d]: %(filename)s(%(lineno)s): '
'%(levelname)s: %(message)s')
config = configparser.ConfigParser(interpolation=None)
config.read(software_conf)
software_conf_mtime = os.stat(software_conf).st_mtime
logging_default_format_string = config.get("DEFAULT", "logging_default_format_string", fallback=default_format)
def get_available_gib_in_vg(): def get_available_gib_in_vg():
"""Get the free space for cgts-vg volume group """Get the free space for cgts-vg volume group
returns: Free space in GiB returns: Free space in GiB

View File

@@ -1,3 +1,6 @@
[DEFAULT]
logging_default_format_string = %(asctime)s.%(msecs)03d USM - %(exec)s [%(process)s:%(thread)d]: %(filename)s(%(lineno)s): %(levelname)s: %(message)s
[runtime] [runtime]
controller_multicast = 239.1.1.3 controller_multicast = 239.1.1.3
agent_multicast = 239.1.1.4 agent_multicast = 239.1.1.4

View File

@@ -1,5 +1,5 @@
""" """
Copyright (c) 2023 Wind River Systems, Inc. Copyright (c) 2023-2025 Wind River Systems, Inc.
SPDX-License-Identifier: Apache-2.0 SPDX-License-Identifier: Apache-2.0
@@ -25,6 +25,7 @@ alt_postgresql_port = 0
mgmt_if = None mgmt_if = None
nodetype = None nodetype = None
package_feed = None package_feed = None
logging_default_format_string = None
platform_conf_mtime = 0 platform_conf_mtime = 0
software_conf_mtime = 0 software_conf_mtime = 0
software_conf = constants.SOFTWARE_CONFIG_FILE_LOCAL software_conf = constants.SOFTWARE_CONFIG_FILE_LOCAL
@@ -98,8 +99,9 @@ def read_config():
global agent_port global agent_port
global alt_postgresql_port global alt_postgresql_port
global package_feed global package_feed
global logging_default_format_string
config = configparser.ConfigParser(defaults) config = configparser.ConfigParser(defaults, interpolation=None)
config.read(software_conf) config.read(software_conf)
software_conf_mtime = os.stat(software_conf).st_mtime software_conf_mtime = os.stat(software_conf).st_mtime
@@ -113,6 +115,8 @@ def read_config():
agent_port = config.getint('runtime', 'agent_port') agent_port = config.getint('runtime', 'agent_port')
alt_postgresql_port = config.getint('runtime', 'alt_postgresql_port') alt_postgresql_port = config.getint('runtime', 'alt_postgresql_port')
package_feed = config.get("runtime", "package_feed") package_feed = config.get("runtime", "package_feed")
logging_default_format_string = config.get("DEFAULT", "logging_default_format_string",
fallback=constants.LOG_DEFAULT_FORMAT)
# The platform.conf file has no section headers, which causes problems # The platform.conf file has no section headers, which causes problems
# for ConfigParser. So we'll fake it out. # for ConfigParser. So we'll fake it out.

View File

@@ -231,3 +231,7 @@ MAX_OSTREE_DEPLOY_RETRIES = 5
# Precheck timeout # Precheck timeout
PRECHECK_RESULT_VALID_PERIOD = 300 PRECHECK_RESULT_VALID_PERIOD = 300
# Logging
LOG_DEFAULT_FORMAT = ('%(asctime)s.%(msecs)03d USM - %(exec)s [%(process)s:%(thread)d]: '
'%(filename)s(%(lineno)s): %(levelname)s: %(message)s')

View File

@@ -1033,10 +1033,10 @@ class PatchAgent(PatchService):
def main(): def main():
global pa global pa
configure_logging()
cfg.read_config() cfg.read_config()
configure_logging()
pa = PatchAgent() pa = PatchAgent()
if os.path.isfile(constants.INSTALL_LOCAL_FLAG): if os.path.isfile(constants.INSTALL_LOCAL_FLAG):
pa.install_local = True pa.install_local = True

View File

@@ -12,6 +12,7 @@ sys.modules['osprofiler'] = None
import configparser import configparser
import gc import gc
import json import json
import logging
import os import os
from packaging import version from packaging import version
import re import re
@@ -82,7 +83,6 @@ from software.software_functions import package_dir
from software.software_functions import repo_dir from software.software_functions import repo_dir
from software.software_functions import root_scripts_dir from software.software_functions import root_scripts_dir
from software.software_functions import SW_VERSION from software.software_functions import SW_VERSION
from software.software_functions import LOG
from software.software_functions import audit_log_info from software.software_functions import audit_log_info
from software.software_functions import repo_root_dir from software.software_functions import repo_root_dir
from software.software_functions import is_deploy_state_in_sync from software.software_functions import is_deploy_state_in_sync
@@ -118,6 +118,8 @@ import xml.etree.ElementTree as ET
CONF = oslo_cfg.CONF CONF = oslo_cfg.CONF
LOG = logging.getLogger('main_logger')
pidfile_path = "/var/run/patch_controller.pid" pidfile_path = "/var/run/patch_controller.pid"
sc = None sc = None
@@ -4887,10 +4889,10 @@ def main():
default_config_files=['/etc/software/software.conf', ] default_config_files=['/etc/software/software.conf', ]
) )
configure_logging()
cfg.read_config() cfg.read_config()
configure_logging()
# daemon.pidlockfile.write_pid_to_pidfile(pidfile_path) # daemon.pidlockfile.write_pid_to_pidfile(pidfile_path)
global thread_death global thread_death

View File

@@ -25,6 +25,7 @@ from lxml import etree as ElementTree
from xml.dom import minidom from xml.dom import minidom
import software.apt_utils as apt_utils import software.apt_utils as apt_utils
import software.config as cfg
from software.db.api import get_instance from software.db.api import get_instance
from software.release_verify import verify_files from software.release_verify import verify_files
from software.release_verify import cert_type_all from software.release_verify import cert_type_all
@@ -84,11 +85,8 @@ def configure_logging(logtofile=True, level=logging.INFO):
if logtofile: if logtofile:
my_exec = os.path.basename(sys.argv[0]) my_exec = os.path.basename(sys.argv[0])
log_format = '%(asctime)s: ' \ log_format = cfg.logging_default_format_string
+ my_exec + '[%(process)s:%(thread)d]: ' \ log_format = log_format.replace('%(exec)s', my_exec)
+ '%(filename)s(%(lineno)s): ' \
+ '%(levelname)s: %(message)s'
formatter = logging.Formatter(log_format, datefmt="%FT%T") formatter = logging.Formatter(log_format, datefmt="%FT%T")
LOG.setLevel(level) LOG.setLevel(level)
@@ -96,16 +94,13 @@ def configure_logging(logtofile=True, level=logging.INFO):
main_log_handler.setFormatter(formatter) main_log_handler.setFormatter(formatter)
LOG.addHandler(main_log_handler) LOG.addHandler(main_log_handler)
try:
os.chmod(logfile, 0o640)
except Exception:
pass
auditLOG.setLevel(level) auditLOG.setLevel(level)
api_log_handler = logging.FileHandler(apilogfile) api_log_handler = logging.FileHandler(apilogfile)
api_log_handler.setFormatter(formatter) api_log_handler.setFormatter(formatter)
auditLOG.addHandler(api_log_handler) auditLOG.addHandler(api_log_handler)
try: try:
os.chmod(logfile, 0o640)
os.chmod(apilogfile, 0o640) os.chmod(apilogfile, 0o640)
except Exception: except Exception:
pass pass

View File

@@ -1,5 +1,5 @@
# #
# Copyright (c) 2024 Wind River Systems, Inc. # Copyright (c) 2023-2025 Wind River Systems, Inc.
# #
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
# #
@@ -10,6 +10,7 @@ import os
import psycopg2 import psycopg2
from psycopg2.extras import RealDictCursor from psycopg2.extras import RealDictCursor
import subprocess import subprocess
import sys
import tempfile import tempfile
import yaml import yaml
@@ -20,6 +21,7 @@ from software.utilities.constants import PLATFORM_PATH
from software.utilities.constants import KEYRING_PERMDIR from software.utilities.constants import KEYRING_PERMDIR
from software.utilities import constants from software.utilities import constants
import software.config as cfg
LOG = logging.getLogger('main_logger') LOG = logging.getLogger('main_logger')
SOFTWARE_LOG_FILE = "/var/log/software.log" SOFTWARE_LOG_FILE = "/var/log/software.log"
@@ -39,10 +41,18 @@ ACTION_ACTIVATE_ROLLBACK = "activate-rollback"
def configure_logging(): def configure_logging():
log_format = ('%(asctime)s: ' + __name__ + '[%(process)s]: ' cfg.read_config()
'%(filename)s(%(lineno)s): %(levelname)s: %(message)s')
log_datefmt = "%FT%T" my_exec = os.path.basename(sys.argv[0])
logging.basicConfig(filename=SOFTWARE_LOG_FILE, format=log_format, level=logging.INFO, datefmt=log_datefmt)
log_format = cfg.logging_default_format_string
log_format = log_format.replace('%(exec)s', my_exec)
formatter = logging.Formatter(log_format, datefmt="%FT%T")
LOG.setLevel(logging.INFO)
main_log_handler = logging.FileHandler(SOFTWARE_LOG_FILE)
main_log_handler.setFormatter(formatter)
LOG.addHandler(main_log_handler)
def get_migration_scripts(migration_script_dir): def get_migration_scripts(migration_script_dir):