diff --git a/paunch/tests/test_utils_systemd.py b/paunch/tests/test_utils_systemd.py index 4a46b61..0cb4825 100644 --- a/paunch/tests/test_utils_systemd.py +++ b/paunch/tests/test_utils_systemd.py @@ -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) diff --git a/paunch/utils/systemd.py b/paunch/utils/systemd.py index 2447c29..dc9c75c 100644 --- a/paunch/utils/systemd.py +++ b/paunch/utils/systemd.py @@ -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: diff --git a/releasenotes/notes/systemd-flags-1a8b3ade8a5c4615.yaml b/releasenotes/notes/systemd-flags-1a8b3ade8a5c4615.yaml new file mode 100644 index 0000000..19d91d5 --- /dev/null +++ b/releasenotes/notes/systemd-flags-1a8b3ade8a5c4615.yaml @@ -0,0 +1,3 @@ +--- +features: + - Adds a new systemd_exec_flags parameter for paunch-managed systemd unit