Allow to pass some of systemd.exec flags

It may happen we have some shared directory mounted within
the container - if it is located in /run or in some other
tmpfs location, it won't persist across reboots.

SystemD provides the capability to create those directories, and
this patch allows to take advantage of this capacity.

Basically, we might add as many flag as we want.

Closes-Bug: #1814764
Change-Id: I5d2ef49ec205b7a43c488d4ceef0724e5ef2b6cc
This commit is contained in:
Cédric Jeanneret 2019-02-06 11:24:07 +01:00
parent 4f7e8d5178
commit b452a33030
3 changed files with 32 additions and 0 deletions

View File

@ -47,6 +47,28 @@ class TestUtilsSystemd(base.TestCase):
os.rmdir(tempdir)
@mock.patch('subprocess.check_call', autospec=True)
@mock.patch('os.chmod')
def test_svc_extended_create(self, mock_chmod, mock_subprocess_check_call):
container = 'my_app'
service = 'tripleo_' + container
cconfig = {'depends_on': ['something'], 'restart': 'unless-stopped',
'stop_grace_period': '15',
'systemd_exec_flags': {'RootDirectory': '/srv',
'LimitCPU': '60',
'RuntimeDirectory': 'my_app foo/bar'}
}
tempdir = tempfile.mkdtemp()
systemd.service_create(container, cconfig, tempdir)
sysd_unit_f = tempdir + service + '.service'
unit = open(sysd_unit_f, 'rt').read()
self.assertIn('RootDirectory=/srv', unit)
self.assertIn('LimitCPU=60', unit)
self.assertIn('RuntimeDirectory=my_app foo/bar', unit)
os.rmdir(tempdir)
@mock.patch('os.remove', autospec=True)
@mock.patch('os.path.isfile', autospec=True)
@mock.patch('subprocess.check_call', autospec=True)

View File

@ -51,6 +51,11 @@ def service_create(container, cconfig, sysdir=constants.SYSTEMD_DIR,
restart = cconfig.get('restart', 'always')
stop_grace_period = cconfig.get('stop_grace_period', '10')
# Please refer to systemd.exec documentation for those entries
# https://www.freedesktop.org/software/systemd/man/systemd.exec.html
sys_exec = cconfig.get('systemd_exec_flags', {})
# SystemD doesn't have the equivalent of docker unless-stopped.
# Let's force 'always' so containers aren't restarted when stopped by
# systemd, but restarted when in failure. Also this code is only for
@ -65,6 +70,7 @@ def service_create(container, cconfig, sysdir=constants.SYSTEMD_DIR,
'wants': wants,
'restart': restart,
'stop_grace_period': stop_grace_period,
'sys_exec': '\n'.join(['%s=%s' % (x, y) for x, y in sys_exec.items()]),
}
with open(sysd_unit_f, 'w') as unit_file:
os.chmod(unit_file.name, 0o644)
@ -77,6 +83,7 @@ Restart=%(restart)s
ExecStart=/usr/bin/podman start -a %(name)s
ExecStop=/usr/bin/podman stop -t %(stop_grace_period)s %(name)s
KillMode=process
%(sys_exec)s
[Install]
WantedBy=multi-user.target""" % s_config)
try:

View File

@ -0,0 +1,3 @@
---
features:
- Adds a new systemd_exec_flags parameter for paunch-managed systemd unit