Check if barbican-api.service from bootstrap is still running

In a regular system, barbican is initially started by systemd during
bootstrap as barbican-api.service and later on it should be moved to
under SM lifecycle.
With [1] the service was decoupled from sm slice in cgroups, so new
expectation would be to see openstack-barbican-api.service running.

However, it was observed on a subcloud deployment that barbican was
still being managed by systemd and running.
This caused openstack-barbican-api.service to fail due to barbican
bind port already in use, and SM to be in loop starting barbican-api
service.

This change checks if barbican-api.service initialized during bootstrap
is still running. If so, stop and disable it before initialized
barbican-api through systemd-run command.

Moreover, since there is a logrotate definition for
/var/log/barbican/barbican-api.log defined in
/etc/logrotate.d/barbican-common this change also sets the
StandardOutput and StandardError systemd-run parameters from 'file'
to 'append'.

[1] https://review.opendev.org/c/starlingx/ha/+/948675

Test Plan:
PASS: build-pkgs
PASS: Install ISO and bootstrap
PASS: barbican-api.service must be inactive after sm is started
PASS: Replicate the odd behavior of barbican-api.service:
      systemctl start barbican-api.service
      It was noticed that sm calls the <start> method on any change
      on PIDFile, so SM should stop barbican-api.service whenever it
      starts, since it changes the PIDFile. Check barbican-api.log
      for these messages:
      INFO: Barbican start
      INFO: OpenStack Key Management API (barbican-api) managed by
            systemd identified. Stopping it...
      Check barbican-api.service status, it should be inactive.

Partial-Bug: 2122366

Change-Id: I6c0470a93c03221b0c78e4a83097c06829ebd2ec
Signed-off-by: Alyson Deives Pereira <alyson.deivespereira@windriver.com>
This commit is contained in:
Alyson Deives Pereira
2025-09-08 15:22:42 -03:00
parent a9333d0b80
commit f1d132c894

View File

@@ -49,7 +49,7 @@ OCF_RESKEY_server_port_default="9311"
SERVICE="openstack-barbican-api.service" SERVICE="openstack-barbican-api.service"
SERVICE_CPUSHARES=512 SERVICE_CPUSHARES=512
SERVICE_DESC="OpenStack Key Management API Service (barbican-api)" SERVICE_DESC="OpenStack Key Management API Service (openstack-barbican-api)"
####################################################################### #######################################################################
@@ -190,19 +190,19 @@ barbican_api_validate() {
barbican_api_status() { barbican_api_status() {
local rc local rc
if [ ! -f $OCF_RESKEY_pid ]; then
ocf_log info "OpenStack Key Management API (barbican-api) is not running"
return $OCF_NOT_RUNNING
fi
systemctl is-active --quiet $SERVICE systemctl is-active --quiet $SERVICE
rc=$? rc=$?
if [ $rc -eq 0 ]; then if [ $rc -eq 0 ]; then
return $OCF_SUCCESS return $OCF_SUCCESS
else else
ocf_log info "Old PID file found, but OpenStack Key Management API \ if [ ! -f $OCF_RESKEY_pid ]; then
(barbican-api) is not running" ocf_log info "OpenStack Key Management API (openstack-barbican-api) is not running"
rm -f $OCF_RESKEY_pid else
pid=`cat $OCF_RESKEY_pid`
ocf_log info "Old PID file found with pid=(${pid}), but OpenStack Key Management API \
(openstack-barbican-api) is not running"
rm -f $OCF_RESKEY_pid
fi
return $OCF_NOT_RUNNING return $OCF_NOT_RUNNING
fi fi
} }
@@ -229,18 +229,34 @@ barbican_api_monitor() {
return $OCF_NOT_RUNNING return $OCF_NOT_RUNNING
fi fi
ocf_log debug "OpenStack Key Management API (barbican-api) monitor succeeded" ocf_log debug "OpenStack Key Management API (openstack-barbican-api) monitor succeeded"
return $OCF_SUCCESS return $OCF_SUCCESS
} }
barbican_api_stop_systemd_managed_instance() {
local rc
# Check if barbican-api.service initialized during bootstrap is still running
# This a workaround for https://bugs.launchpad.net/starlingx/+bug/2122366
systemctl is-active --quiet barbican-api.service
rc=$?
if [ $rc -eq 0 ]; then
ocf_log info "OpenStack Key Management API (barbican-api) managed by systemd identified. Stopping it..."
systemctl stop --quiet barbican-api.service
systemctl reset-failed --quiet barbican-api.service
systemctl disable --quiet barbican-api.service
fi
}
barbican_api_start() { barbican_api_start() {
local rc local rc
local host local host
barbican_api_stop_systemd_managed_instance
barbican_api_status barbican_api_status
rc=$? rc=$?
if [ $rc -eq $OCF_SUCCESS ]; then if [ $rc -eq $OCF_SUCCESS ]; then
ocf_log info "OpenStack Key Management API (barbican-api) already running" ocf_log info "OpenStack Key Management API (openstack-barbican-api) already running"
return $OCF_SUCCESS return $OCF_SUCCESS
fi fi
# run the actual barbican-api daemon. Don't use ocf_run as we're sending the tool's output # run the actual barbican-api daemon. Don't use ocf_run as we're sending the tool's output
@@ -254,10 +270,15 @@ barbican_api_start() {
-p PIDFile=${OCF_RESKEY_pid} \ -p PIDFile=${OCF_RESKEY_pid} \
-p User=barbican -p Group=barbican \ -p User=barbican -p Group=barbican \
-p RuntimeDirectory=barbican -p RuntimeDirectoryMode=770 \ -p RuntimeDirectory=barbican -p RuntimeDirectoryMode=770 \
-p StandardError=file:/var/log/barbican/barbican-api.log \ -p StandardError=append:/var/log/barbican/barbican-api.log \
-p StandardOutput=file:/var/log/barbican/barbican-api.log \ -p StandardOutput=append:/var/log/barbican/barbican-api.log \
/usr/bin/gunicorn --pid $OCF_RESKEY_pid --config /etc/barbican/gunicorn-config.py \ /usr/bin/gunicorn --pid $OCF_RESKEY_pid --config /etc/barbican/gunicorn-config.py \
--paste /etc/barbican/barbican-api-paste.ini --paste /etc/barbican/barbican-api-paste.ini
rc=$?
if [ $rc -ne 0 ]; then
ocf_log err "OpenStack Key Management API (openstack-barbican-api) failed to start (rc=$rc)"
exit $OCF_ERR_GENERIC
fi
# Restrict access to logfile # Restrict access to logfile
chmod 640 /var/log/barbican/barbican-api.log chmod 640 /var/log/barbican/barbican-api.log
@@ -268,13 +289,13 @@ barbican_api_start() {
rc=$? rc=$?
[ $rc -eq $OCF_SUCCESS ] && break [ $rc -eq $OCF_SUCCESS ] && break
if [ $rc -ne $OCF_NOT_RUNNING ]; then if [ $rc -ne $OCF_NOT_RUNNING ]; then
ocf_log err "OpenStack Key Management API (barbican-api) start failed" ocf_log err "OpenStack Key Management API (openstack-barbican-api) start failed (rc=$rc)"
exit $OCF_ERR_GENERIC exit $OCF_ERR_GENERIC
fi fi
sleep 1 sleep 1
done done
ocf_log info "OpenStack Key Management API (barbican-api) started" ocf_log info "OpenStack Key Management API (openstack-barbican-api) started"
return $OCF_SUCCESS return $OCF_SUCCESS
} }
@@ -282,6 +303,8 @@ barbican_api_confirm_stop() {
local my_bin local my_bin
local my_processes local my_processes
barbican_api_stop_systemd_managed_instance
my_processes=`pgrep -l -f "gunicorn.*master.*barbican-api"` my_processes=`pgrep -l -f "gunicorn.*master.*barbican-api"`
if [ -n "${my_processes}" ] if [ -n "${my_processes}" ]
@@ -297,7 +320,7 @@ barbican_api_stop() {
barbican_api_status barbican_api_status
rc=$? rc=$?
if [ $rc -eq $OCF_NOT_RUNNING ]; then if [ $rc -eq $OCF_NOT_RUNNING ]; then
ocf_log info "OpenStack Key Management API (barbican-api) already stopped" ocf_log info "OpenStack Key Management API (openstack-barbican-api) already stopped"
barbican_api_confirm_stop barbican_api_confirm_stop
return $OCF_SUCCESS return $OCF_SUCCESS
fi fi
@@ -325,7 +348,7 @@ barbican_api_stop() {
fi fi
count=`expr $count + 1` count=`expr $count + 1`
sleep 1 sleep 1
ocf_log debug "OpenStack Key Management API (barbican-api) still hasn't stopped yet. \ ocf_log debug "OpenStack Key Management API (openstack-barbican-api) still hasn't stopped yet. \
Waiting ..." Waiting ..."
done done
@@ -333,13 +356,13 @@ barbican_api_stop() {
rc=$? rc=$?
if [ $rc -ne $OCF_NOT_RUNNING ]; then if [ $rc -ne $OCF_NOT_RUNNING ]; then
# SIGTERM didn't help either, try SIGKILL # SIGTERM didn't help either, try SIGKILL
ocf_log info "OpenStack Key Management API (barbican-api) failed to stop after \ ocf_log info "OpenStack Key Management API (openstack-barbican-api) failed to stop after \
${shutdown_timeout}s using SIGTERM. Trying SIGKILL ..." ${shutdown_timeout}s using SIGTERM. Trying SIGKILL ..."
systemctl kill --signal=SIGKILL $SERVICE systemctl kill --signal=SIGKILL $SERVICE
fi fi
barbican_api_confirm_stop barbican_api_confirm_stop
ocf_log info "OpenStack Key Management API (barbican-api) stopped" ocf_log info "OpenStack Key Management API (openstack-barbican-api) stopped"
rm -f $OCF_RESKEY_pid rm -f $OCF_RESKEY_pid