Restart rsyslog from cloud-init

Octavia uploads a rsyslog configuration file using cloud-init but
doesn't ensure that rsyslog is restarted. In some cases (CentOS 9
Stream), rsyslog is started before cloud-init and doesn't use the
provided configuration file.
This commit ensures that rsyslog is restarted by cloud-init.

Change-Id: I550c7d2563a2d3ada07e2162383f59f104bcbbd5
(cherry picked from commit cbffae6eb6)
This commit is contained in:
Gregory Thiemonge 2022-01-27 15:19:00 +01:00
parent f9af20e80a
commit 220f13f537
5 changed files with 54 additions and 22 deletions

View File

@ -22,13 +22,18 @@
#
# Example:
# {'/root/path/to/file.cfg': 'I'm a file, write things in me'}
{% if user_data -%}
write_files:
{%- for key, value in user_data.items() %}
- path: {{ key }}
content: |
{{ value|indent(8) }}
{%- endfor -%}
{%- endif -%}
{# restart agent now that configurations are in place #}
runcmd:
- systemctl restart rsyslog
{% if user_data -%}
- service amphora-agent restart
{%- endif %}

View File

@ -107,10 +107,10 @@ class ComputeCreate(BaseComputeTask):
config_drive_files['/etc/rsyslog.d/10-rsyslog.conf'] = (
logging_cfg.build_logging_config())
udtemplater = user_data_jinja_cfg.UserDataJinjaCfg()
user_data = udtemplater.build_user_data_config(
config_drive_files if user_data_config_drive else {})
if user_data_config_drive:
udtemplater = user_data_jinja_cfg.UserDataJinjaCfg()
user_data = udtemplater.build_user_data_config(
config_drive_files)
config_drive_files = None
compute_id = self.compute.build(

View File

@ -36,10 +36,11 @@ BASE_CFG = ('#cloud-config\n'
'#\n'
'# Example:\n'
'# {\'/root/path/to/file.cfg\': \'I\'m a file, '
'write things in me\'}\n'
'write_files:\n')
'write things in me\'}\n')
WRITE_FILES_CFG = ('write_files:\n')
RUN_CMD = ('runcmd:\n'
'- service amphora-agent restart')
'- systemctl restart rsyslog\n')
WRITE_FILES_CMD = ('- service amphora-agent restart')
class TestUserDataJinjaCfg(base.TestCase):
@ -48,8 +49,15 @@ class TestUserDataJinjaCfg(base.TestCase):
def test_build_user_data_config(self):
udc = user_data_jinja_cfg.UserDataJinjaCfg()
expected_config = (BASE_CFG +
expected_config = (BASE_CFG + WRITE_FILES_CFG +
'- path: /test/config/path\n'
' content: |\n' + EXPECTED_TEST_CONFIG + RUN_CMD)
' content: |\n' + EXPECTED_TEST_CONFIG +
RUN_CMD + WRITE_FILES_CMD)
ud_cfg = udc.build_user_data_config({'/test/config/path': TEST_CONFIG})
self.assertEqual(expected_config, ud_cfg)
def test_build_user_data_config_no_files(self):
udc = user_data_jinja_cfg.UserDataJinjaCfg()
expected_config = (BASE_CFG + '\n' + RUN_CMD)
ud_cfg = udc.build_user_data_config({})
self.assertEqual(expected_config, ud_cfg)

View File

@ -102,9 +102,12 @@ class TestComputeTasks(base.TestCase):
@mock.patch('octavia.amphorae.backends.agent.'
'agent_jinja_cfg.AgentJinjaTemplater.'
'build_agent_config', return_value='test_conf')
@mock.patch('octavia.common.jinja.'
'user_data_jinja_cfg.UserDataJinjaCfg.'
'build_user_data_config', return_value='user_data_conf')
@mock.patch('stevedore.driver.DriverManager.driver')
def test_compute_create(self, mock_driver, mock_conf, mock_jinja,
mock_log_cfg):
def test_compute_create(self, mock_driver, mock_ud_conf,
mock_conf, mock_jinja, mock_log_cfg):
image_owner_id = uuidutils.generate_uuid()
self.conf.config(
@ -131,7 +134,7 @@ class TestComputeTasks(base.TestCase):
config_drive_files={'/etc/octavia/'
'amphora-agent.conf': 'test_conf',
'/etc/rsyslog.d/10-rsyslog.conf': 'FAKE CFG'},
user_data=None,
user_data='user_data_conf',
server_group_id=SERVER_GRPOUP_ID,
availability_zone=None
)
@ -167,14 +170,13 @@ class TestComputeTasks(base.TestCase):
'build_agent_config', return_value='test_conf')
@mock.patch('octavia.common.jinja.'
'user_data_jinja_cfg.UserDataJinjaCfg.'
'build_user_data_config', return_value='test_conf')
'build_user_data_config', return_value='user_data_conf')
@mock.patch('stevedore.driver.DriverManager.driver')
def test_compute_create_user_data(self, mock_driver,
mock_ud_conf, mock_conf, mock_jinja):
self.conf.config(
group="controller_worker", user_data_config_drive=True)
mock_ud_conf.return_value = 'test_ud_conf'
createcompute = compute_tasks.ComputeCreate()
mock_driver.build.return_value = COMPUTE_ID
@ -193,7 +195,7 @@ class TestComputeTasks(base.TestCase):
network_ids=AMP_NET,
port_ids=[PORT_ID],
config_drive_files=None,
user_data='test_ud_conf',
user_data='user_data_conf',
server_group_id=None,
availability_zone=None)
@ -228,9 +230,13 @@ class TestComputeTasks(base.TestCase):
@mock.patch('octavia.amphorae.backends.agent.'
'agent_jinja_cfg.AgentJinjaTemplater.'
'build_agent_config', return_value='test_conf')
@mock.patch('octavia.common.jinja.'
'user_data_jinja_cfg.UserDataJinjaCfg.'
'build_user_data_config', return_value='user_data_conf')
@mock.patch('stevedore.driver.DriverManager.driver')
def test_compute_create_availability_zone(self, mock_driver, mock_conf,
mock_jinja, mock_log_cfg):
def test_compute_create_availability_zone(self, mock_driver, mock_ud_conf,
mock_conf, mock_jinja,
mock_log_cfg):
image_owner_id = uuidutils.generate_uuid()
compute_zone = uuidutils.generate_uuid()
@ -261,7 +267,7 @@ class TestComputeTasks(base.TestCase):
config_drive_files={'/etc/octavia/'
'amphora-agent.conf': 'test_conf',
'/etc/rsyslog.d/10-rsyslog.conf': 'FAKE CFG'},
user_data=None,
user_data='user_data_conf',
server_group_id=SERVER_GRPOUP_ID,
availability_zone=compute_zone)
@ -296,9 +302,13 @@ class TestComputeTasks(base.TestCase):
@mock.patch('octavia.amphorae.backends.agent.'
'agent_jinja_cfg.AgentJinjaTemplater.'
'build_agent_config', return_value='test_conf')
@mock.patch('octavia.common.jinja.'
'user_data_jinja_cfg.UserDataJinjaCfg.'
'build_user_data_config', return_value='user_data_conf')
@mock.patch('stevedore.driver.DriverManager.driver')
def test_compute_create_without_ssh_access(
self, mock_driver, mock_conf, mock_jinja, mock_log_cfg):
self, mock_driver, nock_user_data_conf,
mock_conf, mock_jinja, mock_log_cfg):
createcompute = compute_tasks.ComputeCreate()
@ -324,7 +334,7 @@ class TestComputeTasks(base.TestCase):
config_drive_files={'/etc/octavia/'
'amphora-agent.conf': 'test_conf',
'/etc/rsyslog.d/10-rsyslog.conf': 'FAKE CFG'},
user_data=None,
user_data='user_data_conf',
server_group_id=SERVER_GRPOUP_ID,
availability_zone=None)
@ -358,9 +368,12 @@ class TestComputeTasks(base.TestCase):
@mock.patch('octavia.amphorae.backends.agent.'
'agent_jinja_cfg.AgentJinjaTemplater.'
'build_agent_config', return_value='test_conf')
@mock.patch('octavia.common.jinja.'
'user_data_jinja_cfg.UserDataJinjaCfg.'
'build_user_data_config', return_value='user_data_conf')
@mock.patch('stevedore.driver.DriverManager.driver')
def test_compute_create_cert(self, mock_driver, mock_conf, mock_jinja,
mock_log_cfg):
def test_compute_create_cert(self, mock_driver, mock_ud_conf,
mock_conf, mock_jinja, mock_log_cfg):
createcompute = compute_tasks.CertComputeCreate()
key = utils.get_compatible_server_certs_key_passphrase()
fer = fernet.Fernet(key)
@ -387,7 +400,7 @@ class TestComputeTasks(base.TestCase):
sec_groups=AMP_SEC_GROUPS,
network_ids=AMP_NET,
port_ids=[],
user_data=None,
user_data='user_data_conf',
config_drive_files={
'/etc/rsyslog.d/10-rsyslog.conf': 'FAKE CFG',
'/etc/octavia/certs/server.pem': fer.decrypt(

View File

@ -0,0 +1,6 @@
---
fixes:
- |
Ensure that the provided rsyslog configuration file is used by the rsyslog
by restarting the service, it fixes the log offloading feature on
distributions that start rsyslog before cloud-init.