From 4e21f7115b77dee461b816718686816597fdb13b Mon Sep 17 00:00:00 2001 From: Cole Walker Date: Thu, 23 Mar 2023 18:43:27 -0400 Subject: [PATCH] Update notificationservice-base-v1 startup script to handle upgrades Updated the logic in ptptracking_start.py to handle a case where the application is upgraded but helm overrides are not present. If helm overrides are present, there is no change in behaviour. If helm overrides are absent, attempt to locate ptp4l and phc2sys configs in known locations and start with those. Print log messages indicating that the configs were auto detected and that helm overrides can be set if auto-detection is incorrect or unsuccessful. Bonus fix: corrected the tox path used to auto detect unit tests, as this was missed when moving from notificationservice-base to notificationservice-base-v2 Test-plan: PASS: Build and deploy application tarball PASS: Pods start correctly when overrides are present and when they are absent Story: 2010538 Task: 47740 Signed-off-by: Cole Walker Change-Id: I25df407effbffc03e444573233058f3d3180e706 --- .../ptp-notification/helmrelease.yaml | 2 +- .../helm-charts/ptp-notification/Chart.yaml | 4 +- .../scripts/init/ptptracking_start.py | 76 ++++++++++++++++++- tox.ini | 4 +- 4 files changed, 78 insertions(+), 8 deletions(-) diff --git a/stx-ptp-notification-helm/stx-ptp-notification-helm/fluxcd-manifests/ptp-notification/helmrelease.yaml b/stx-ptp-notification-helm/stx-ptp-notification-helm/fluxcd-manifests/ptp-notification/helmrelease.yaml index b2eb287..f79fcd4 100644 --- a/stx-ptp-notification-helm/stx-ptp-notification-helm/fluxcd-manifests/ptp-notification/helmrelease.yaml +++ b/stx-ptp-notification-helm/stx-ptp-notification-helm/fluxcd-manifests/ptp-notification/helmrelease.yaml @@ -14,7 +14,7 @@ spec: chart: spec: chart: ptp-notification - version: 0.1.0 + version: 2.0.0 sourceRef: kind: HelmRepository name: stx-platform diff --git a/stx-ptp-notification-helm/stx-ptp-notification-helm/helm-charts/ptp-notification/Chart.yaml b/stx-ptp-notification-helm/stx-ptp-notification-helm/helm-charts/ptp-notification/Chart.yaml index 53d9e43..34d6ba7 100644 --- a/stx-ptp-notification-helm/stx-ptp-notification-helm/helm-charts/ptp-notification/Chart.yaml +++ b/stx-ptp-notification-helm/stx-ptp-notification-helm/helm-charts/ptp-notification/Chart.yaml @@ -4,7 +4,7 @@ # SPDX-License-Identifier: Apache-2.0 # apiVersion: v1 -appVersion: "1.0" +appVersion: "2.0" description: A Helm chart to deploy PTP Notification Service name: ptp-notification -version: 0.1.0 +version: 2.0.0 diff --git a/stx-ptp-notification-helm/stx-ptp-notification-helm/helm-charts/ptp-notification/resources/scripts/init/ptptracking_start.py b/stx-ptp-notification-helm/stx-ptp-notification-helm/helm-charts/ptp-notification/resources/scripts/init/ptptracking_start.py index a18e2f8..7bba342 100644 --- a/stx-ptp-notification-helm/stx-ptp-notification-helm/helm-charts/ptp-notification/resources/scripts/init/ptptracking_start.py +++ b/stx-ptp-notification-helm/stx-ptp-notification-helm/helm-charts/ptp-notification/resources/scripts/init/ptptracking_start.py @@ -16,6 +16,8 @@ log_helper.config_logger(LOG) import os import json import time +import glob +import re import oslo_messaging from oslo_config import cfg from trackingfunctionsdk.services.daemon import DaemonControl @@ -64,7 +66,75 @@ sqlalchemy_conf = { } sqlalchemy_conf_json = json.dumps(sqlalchemy_conf) default_daemoncontrol = DaemonControl(sqlalchemy_conf_json, json.dumps(context)) -default_daemoncontrol.refresh() -while True: - pass +if os.path.exists('/ptp/linuxptp/ptpinstance'): + LINUXPTP_CONFIG_PATH = '/ptp/linuxptp/ptpinstance/' +elif os.path.exists('/ptp/ptpinstance'): + LINUXPTP_CONFIG_PATH = '/ptp/ptpinstance/' +else: + LINUXPTP_CONFIG_PATH = '/ptp/' + +ptp4l_service_name = os.environ.get('PTP4L_SERVICE_NAME', 'ptp4l') +phc2sys_service_name = os.environ.get('PHC2SYS_SERVICE_NAME', 'phc2sys') + +pmc = False +ptp4l = False +phc2sys = False +ptp4lconf = False +phc2sysconf = False + +if os.path.isfile('/usr/sbin/pmc'): + pmc = True + +# Check ptp4l config, auto-detect if not found +if os.path.isfile('%sptp4l-%s.conf' % (LINUXPTP_CONFIG_PATH, ptp4l_service_name)): + ptp4lconf = True +else: + try: + LOG.warning("Unable to locate ptp4l config file, attempting to auto-detect") + ptp4l_detect_config = glob.glob(LINUXPTP_CONFIG_PATH + "ptp4l*.conf")[0] + pattern = '(?<=' + LINUXPTP_CONFIG_PATH + 'ptp4l-).*(?=.conf)' + match = re.search(pattern, ptp4l_detect_config) + ptp4l_service_name = match.group() + LOG.info("Using ptp4l conf: %s and ptp4l service name %s" + % (ptp4l_detect_config, ptp4l_service_name)) + LOG.info("Set Helm overrides to override auto-detection") + ptp4lconf = True + except: + LOG.warning("Unable to locate ptp4l config, auto-detect failed.") + +# Check phc2sys config, auto-detect if not found +if os.path.isfile('%sphc2sys-%s.conf' % (LINUXPTP_CONFIG_PATH, phc2sys_service_name)): + phc2sysconf = True +else: + try: + LOG.warning("Unable to locate phc2sys config file, attempting to auto-detect") + phc2sys_detect_config = glob.glob(LINUXPTP_CONFIG_PATH + "phc2sys*.conf")[0] + pattern = '(?<=' + LINUXPTP_CONFIG_PATH + 'phc2sys-).*(?=.conf)' + match = re.search(pattern, phc2sys_detect_config) + phc2sys_service_name = match.group() + LOG.info("Using phc2sys conf: %s and phc2sys service name: %s" + % (phc2sys_detect_config, phc2sys_service_name)) + LOG.info("Set Helm overrides to override auto-detection") + phc2sysconf = True + except: + LOG.warning("Unable to locate phc2sys config, auto-detect failed.") + +# Check that ptp4l and phc2sys are running +if os.path.isfile('/var/run/ptp4l-%s.pid' % ptp4l_service_name): + ptp4l = True +else: + LOG.warning("Unable to locate .pid file for %s" % ptp4l_service_name) +if os.path.isfile('/var/run/phc2sys-%s.pid' % phc2sys_service_name): + phc2sys = True +else: + LOG.warning("Unable to locate .pid file for %s" % phc2sys_service_name) + + +if pmc and ptp4l and phc2sys and ptp4lconf and phc2sysconf: + LOG.info("Located ptp4l and phc2sys configs, starting ptp-notification") + default_daemoncontrol.refresh() +else: + LOG.warning("Please configure application overrides and ensure that ptp services are running.") + while True: + pass diff --git a/tox.ini b/tox.ini index f5b049c..b0ef29b 100644 --- a/tox.ini +++ b/tox.ini @@ -21,9 +21,9 @@ allowlist_externals = [testenv:py36] basepython = python3.6 setenv = - TESTPATH=./notificationservice-base/docker/ptptrackingfunction/trackingfunctionsdk/tests/ + TESTPATH=./notificationservice-base-v2/docker/ptptrackingfunction/trackingfunctionsdk/tests/ commands = - unit2 discover -s ./notificationservice-base/docker/ptptrackingfunction + unit2 discover -s ./notificationservice-base-v2/docker/ptptrackingfunction deps = -r{toxinidir}/requirements.txt -r{toxinidir}/test-requirements.txt