Prefix SystemD service by "tripleo_"

We now prefix the SystemD service so we can identify them better:
  e.g. systemctl list-unit-files | grep tripleo

It'll help to not conflict when rpms are installed on the host and
have the same service name as their container name.

For example haproxy rpm and haproxy container would have the same
service name so the prefix will help to not having this conflict
when removing the rpms during a cleanup by the operator.

Change-Id: I4bec5cfdb34c7422816c2d79ccaa5a499c783631
This commit is contained in:
Emilien Macchi 2018-12-06 11:23:51 -05:00
parent 669f1d173b
commit aa42360b85
3 changed files with 29 additions and 11 deletions

View File

@ -27,12 +27,13 @@ class TestUtilsSystemd(base.TestCase):
@mock.patch('os.chmod')
def test_service_create(self, mock_chmod, mock_subprocess_call):
container = 'my_app'
service = 'tripleo_' + container
cconfig = {'depends_on': ['something'], 'restart': 'unless-stopped',
'stop_grace_period': '15'}
tempdir = tempfile.mkdtemp()
systemd.service_create(container, cconfig, tempdir)
sysd_unit_f = tempdir + container + '.service'
sysd_unit_f = tempdir + service + '.service'
unit = open(sysd_unit_f, 'rt').read()
self.assertIn('Wants=something.service', unit)
self.assertIn('Restart=always', unit)
@ -40,7 +41,7 @@ class TestUtilsSystemd(base.TestCase):
mock_chmod.assert_has_calls([mock.call(sysd_unit_f, 420)])
mock_subprocess_call.assert_has_calls([
mock.call(['systemctl', 'enable', '--now', container]),
mock.call(['systemctl', 'enable', '--now', service]),
mock.call(['systemctl', 'daemon-reload']),
])
@ -52,9 +53,10 @@ class TestUtilsSystemd(base.TestCase):
def test_service_delete(self, mock_subprocess_call, mock_isfile, mock_rm):
mock_isfile.return_value = True
container = 'my_app'
service = 'tripleo_' + container
systemd.service_delete(container)
mock_subprocess_call.assert_has_calls([
mock.call(['systemctl', 'stop', container]),
mock.call(['systemctl', 'disable', container]),
mock.call(['systemctl', 'stop', service]),
mock.call(['systemctl', 'disable', service]),
mock.call(['systemctl', 'daemon-reload']),
])

View File

@ -36,6 +36,14 @@ def service_create(container, cconfig, sysdir='/etc/systemd/system/',
:type log: logging.RootLogger
"""
log = log or common.configure_logging(__name__)
# We prefix the SystemD service so we can identify them better:
# e.g. systemctl list-unit-files | grep paunch
# It'll help to not conflict when rpms are installed on the host and
# have the same service name as their container name.
# For example haproxy rpm and haproxy container would have the same
# service name so the prefix will help to not having this conflict
# when removing the rpms during a cleanup by the operator.
service = 'tripleo_' + container
wants = " ".join(str(x) + '.service' for x in
cconfig.get('depends_on', []))
@ -49,7 +57,7 @@ def service_create(container, cconfig, sysdir='/etc/systemd/system/',
if restart == 'unless-stopped':
restart = 'always'
sysd_unit_f = sysdir + container + '.service'
sysd_unit_f = sysdir + service + '.service'
log.debug('Creating systemd unit file: %s' % sysd_unit_f)
s_config = {
'name': container,
@ -70,7 +78,7 @@ ExecStop=/usr/bin/podman stop -t %(stop_grace_period)s %(name)s
KillMode=process
[Install]
WantedBy=multi-user.target""" % s_config)
subprocess.call(['systemctl', 'enable', '--now', container])
subprocess.call(['systemctl', 'enable', '--now', service])
subprocess.call(['systemctl', 'daemon-reload'])
@ -84,14 +92,16 @@ def service_delete(container, log=None):
:type log: logging.RootLogger
"""
log = log or common.configure_logging(__name__)
# prefix is explained in the service_create().
service = 'tripleo_' + container
sysd_unit_f = '/etc/systemd/system/' + container + '.service'
sysd_unit_f = '/etc/systemd/system/' + service + '.service'
if os.path.isfile(sysd_unit_f):
log.debug('Stopping and disabling systemd service for %s' % container)
subprocess.call(['systemctl', 'stop', container])
subprocess.call(['systemctl', 'disable', container])
log.debug('Stopping and disabling systemd service for %s' % service)
subprocess.call(['systemctl', 'stop', service])
subprocess.call(['systemctl', 'disable', service])
log.debug('Removing systemd unit file %s' % sysd_unit_f)
os.remove(sysd_unit_f)
subprocess.call(['systemctl', 'daemon-reload'])
else:
log.warning('No systemd unit file was found for %s' % container)
log.warning('No systemd unit file was found for %s' % service)

View File

@ -5,3 +5,9 @@ features:
so the containers automatically start at boot and restart at failure.
When the container is removed, we'll disable and stop the service, then
remove the systemd unit file.
We prefix the SystemD service so we can identify them better.
It will help to not conflict when rpms are installed on the host and
have the same service name as their container name.
For example haproxy rpm and haproxy container would have the same
service name so the prefix will help to not having this conflict
when removing the rpms during a cleanup by the operator.