Files
ha/service-mgmt-tools/sm-tools/sm_tools/sm_api_msg_utils.py
Kyale, Eliud 7e15abae2d Move sm database files for ostree compatibility
Move the following sm database files to /etc/sm/<release>
- sm.db
- sm.hb.db

update the paths in source code to point to the new database location
added a static_assert to fail if SW_VERSION macro is not defined

Test plan:

PASS - AIO-SX: iso install
               verify database files are in the correct location
                - /var/lib/sm/...
                - /etc/sm/<rel>/...
                - /var/run/sm/...
               ensure sm is running smoothly after controller-0 unlock

PASS - AIO-DX: iso install upto controller-1 unlock
               user host-swact
               uncontrolled swact by powering off active controller

Story: 2010676
Task: 50649

Change-Id: I2195c420438135c9b109060de13765b0897d7dc9
Signed-off-by: Kyale, Eliud <Eliud.Kyale@windriver.com>
2024-07-25 16:27:42 -04:00

141 lines
4.2 KiB
Python

#
# Copyright (c) 2016-2023 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
from __future__ import print_function
import time
import socket
import subprocess
platform_release = subprocess.getoutput("source /etc/build.info; echo $SW_VERSION").strip()
database_name = f"/etc/sm/{platform_release}/sm.db"
database_running_name = "/var/run/sm/sm.db"
SM_API_SERVER_ADDR = "/tmp/.sm_server_api"
SM_API_MSG_VERSION = "1"
SM_API_MSG_REVISION = "1"
SM_API_MSG_TYPE_RESTART_SERVICE = "RESTART_SERVICE"
SM_API_MSG_SKIP_DEP_CHECK = "skip-dep"
SM_API_MSG_TYPE_PROVISION_SERVICE = "PROVISION_SERVICE"
SM_API_MSG_TYPE_DEPROVISION_SERVICE = "DEPROVISION_SERVICE"
SM_API_MSG_TYPE_PROVISION_SERVICE_DOMAIN_INTERFACE = "PROVISION_SERVICE_DOMAIN_INTERFACE"
SM_API_MSG_TYPE_DEPROVISION_SERVICE_DOMAIN_INTERFACE = "DEPROVISION_SERVICE_DOMAIN_INTERFACE"
SM_API_MSG_TYPE_RELOAD_DATA = "RELOAD_DATA"
SM_API_MSG_TYPE_SDI_SET_STATE = "SERVICE_DOMAIN_INTERFACE_SET_STATE"
# offsets
SM_API_MSG_VERSION_FIELD = 0
SM_API_MSG_REVISION_FIELD = 1
SM_API_MSG_SEQNO_FIELD = 2
SM_API_MSG_TYPE_FIELD = 3
SM_API_MSG_ORIGIN_FIELD = 4
SM_API_MSG_SERVICE_NAME_FIELD = 5
# For provisioning a service domain interface, the SM API
# message expects the service domain name at the same offset
# as the service name field (used for provisioning a service)
SM_API_MSG_SERVICE_DOMAIN_NAME_FIELD = 5
SM_API_MSG_PARAM = 6
def _send_msg_to_sm(sm_api_msg):
s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
try:
s.setblocking(True)
s.sendto(sm_api_msg.encode("ascii", "ignore"), SM_API_SERVER_ADDR)
time.sleep(1)
except socket.error as e:
print("sm-api socket error: %s on %s" % (e, sm_api_msg))
def restart_service(service_name):
"""
Message SM to restart a service
"""
sm_api_msg = ("%s,%s,%i,%s,%s,%s"
% (SM_API_MSG_VERSION, SM_API_MSG_REVISION, 1,
SM_API_MSG_TYPE_RESTART_SERVICE, "sm-action",
service_name))
_send_msg_to_sm(sm_api_msg)
def restart_service_safe(service_name):
"""
Message SM to restart a service w/o checking dependency
"""
sm_api_msg = ("%s,%s,%i,%s,%s,%s,%s"
% (SM_API_MSG_VERSION, SM_API_MSG_REVISION, 1,
SM_API_MSG_TYPE_RESTART_SERVICE, "sm-action",
service_name, SM_API_MSG_SKIP_DEP_CHECK))
_send_msg_to_sm(sm_api_msg)
def provision_service(service_name, service_group_name):
"""
:param service_name:
:param service_group_name:
:return:
"""
sm_api_msg = ("%s,%s,%i,%s,%s,%s,%s"
% (SM_API_MSG_VERSION, SM_API_MSG_REVISION, 1,
SM_API_MSG_TYPE_PROVISION_SERVICE, "sm-action",
service_name, service_group_name))
_send_msg_to_sm(sm_api_msg)
def deprovision_service(service_name, service_group_name):
"""
:param service_name:
:param service_group_name:
:return:
"""
sm_api_msg = ("%s,%s,%i,%s,%s,%s,%s"
% (SM_API_MSG_VERSION, SM_API_MSG_REVISION, 1,
SM_API_MSG_TYPE_DEPROVISION_SERVICE, "sm-action",
service_name, service_group_name))
_send_msg_to_sm(sm_api_msg)
def provision_service_domain_interface(service_domain,
service_domain_interface_name):
"""
:param service_domain:
:param service_domain_interface_name:
:return:
"""
sm_api_msg = ("%s,%s,%i,%s,%s,%s,%s"
% (SM_API_MSG_VERSION, SM_API_MSG_REVISION, 1,
SM_API_MSG_TYPE_PROVISION_SERVICE_DOMAIN_INTERFACE,
"sm-action", service_domain,
service_domain_interface_name))
_send_msg_to_sm(sm_api_msg)
def deprovision_service_domain_interface(service_domain,
service_domain_interface_name):
"""
:param service_domain:
:param service_domain_interface_name:
:return:
"""
sm_api_msg = ("%s,%s,%i,%s,%s,%s,%s"
% (SM_API_MSG_VERSION, SM_API_MSG_REVISION, 1,
SM_API_MSG_TYPE_DEPROVISION_SERVICE_DOMAIN_INTERFACE,
"sm-action", service_domain,
service_domain_interface_name))
_send_msg_to_sm(sm_api_msg)