Skip to content

Commit ec21a43

Browse files
authored
Merge pull request ceph#54378 from adk3798/cephadm-jinja2-logrotate
cephadm: move logrotate configs to jinja2 template Reviewed-by: John Mulligan <[email protected]> Reviewed-by: Michael Fritch <[email protected]>
2 parents 95a800c + 3797fb5 commit ec21a43

File tree

6 files changed

+138
-52
lines changed

6 files changed

+138
-52
lines changed

src/cephadm/cephadm.py

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,12 @@
128128
from cephadmlib.locking import FileLock
129129
from cephadmlib.daemon_identity import DaemonIdentity
130130
from cephadmlib.packagers import create_packager, Packager
131-
from cephadmlib.logging import cephadm_init_logging, Highlight, LogDestination
131+
from cephadmlib.logging import (
132+
cephadm_init_logging,
133+
Highlight,
134+
LogDestination,
135+
write_cluster_logrotate_config,
136+
)
132137
from cephadmlib.systemd import check_unit, check_units
133138
from cephadmlib.container_types import (
134139
CephContainer,
@@ -1421,42 +1426,7 @@ def install_base_units(ctx, fsid):
14211426
if os.path.exists(ctx.logrotate_dir + f'/ceph-{fsid}'):
14221427
return
14231428

1424-
# logrotate for the cluster
1425-
with write_new(ctx.logrotate_dir + f'/ceph-{fsid}', perms=None) as f:
1426-
"""
1427-
This is a bit sloppy in that the killall/pkill will touch all ceph daemons
1428-
in all containers, but I don't see an elegant way to send SIGHUP *just* to
1429-
the daemons for this cluster. (1) systemd kill -s will get the signal to
1430-
podman, but podman will exit. (2) podman kill will get the signal to the
1431-
first child (bash), but that isn't the ceph daemon. This is simpler and
1432-
should be harmless.
1433-
"""
1434-
targets: List[str] = [
1435-
'ceph-mon',
1436-
'ceph-mgr',
1437-
'ceph-mds',
1438-
'ceph-osd',
1439-
'ceph-fuse',
1440-
'radosgw',
1441-
'rbd-mirror',
1442-
'cephfs-mirror',
1443-
'tcmu-runner'
1444-
]
1445-
1446-
f.write("""# created by cephadm
1447-
/var/log/ceph/%s/*.log {
1448-
rotate 7
1449-
daily
1450-
compress
1451-
sharedscripts
1452-
postrotate
1453-
killall -q -1 %s || pkill -1 -x '%s' || true
1454-
endscript
1455-
missingok
1456-
notifempty
1457-
su root root
1458-
}
1459-
""" % (fsid, ' '.join(targets), '|'.join(targets)))
1429+
write_cluster_logrotate_config(ctx, fsid)
14601430

14611431

14621432
def get_unit_file(ctx: CephadmContext, fsid: str) -> str:

src/cephadm/cephadmlib/logging.py

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
from .context import CephadmContext
1313
from .constants import QUIET_LOG_LEVEL, LOG_DIR
1414

15+
from cephadmlib.file_utils import write_new
16+
17+
from cephadmlib import templating
18+
1519

1620
class _ExcludeErrorsFilter(logging.Filter):
1721
def filter(self, record: logging.LogRecord) -> bool:
@@ -145,18 +149,6 @@ def format(self, record: Any) -> str:
145149
}
146150

147151

148-
_logrotate_data = """# created by cephadm
149-
/var/log/ceph/cephadm.log {
150-
rotate 7
151-
daily
152-
compress
153-
missingok
154-
notifempty
155-
su root root
156-
}
157-
"""
158-
159-
160152
_VERBOSE_HANDLERS = [
161153
'console',
162154
'console_stdout',
@@ -222,9 +214,7 @@ def cephadm_init_logging(
222214

223215
logger.setLevel(QUIET_LOG_LEVEL)
224216

225-
if not os.path.exists(ctx.logrotate_dir + '/cephadm'):
226-
with open(ctx.logrotate_dir + '/cephadm', 'w') as f:
227-
f.write(_logrotate_data)
217+
write_cephadm_logrotate_config(ctx)
228218

229219
for handler in logger.handlers:
230220
# the following little hack ensures that no matter how cephadm is named
@@ -239,3 +229,48 @@ def cephadm_init_logging(
239229
if ctx.verbose and handler.name in _VERBOSE_HANDLERS:
240230
handler.setLevel(QUIET_LOG_LEVEL)
241231
logger.debug('%s\ncephadm %s' % ('-' * 80, args))
232+
233+
234+
def write_cephadm_logrotate_config(ctx: CephadmContext) -> None:
235+
if not os.path.exists(ctx.logrotate_dir + '/cephadm'):
236+
with open(ctx.logrotate_dir + '/cephadm', 'w') as f:
237+
cephadm_logrotate_config = templating.render(
238+
ctx, templating.Templates.cephadm_logrotate_config
239+
)
240+
f.write(cephadm_logrotate_config)
241+
242+
243+
def write_cluster_logrotate_config(ctx: CephadmContext, fsid: str) -> None:
244+
# logrotate for the cluster
245+
with write_new(ctx.logrotate_dir + f'/ceph-{fsid}', perms=None) as f:
246+
"""
247+
See cephadm/cephadmlib/templates/cluster.logrotate.config.j2 to
248+
get a better idea what this comment is referring to
249+
250+
This is a bit sloppy in that the killall/pkill will touch all ceph daemons
251+
in all containers, but I don't see an elegant way to send SIGHUP *just* to
252+
the daemons for this cluster. (1) systemd kill -s will get the signal to
253+
podman, but podman will exit. (2) podman kill will get the signal to the
254+
first child (bash), but that isn't the ceph daemon. This is simpler and
255+
should be harmless.
256+
"""
257+
targets: List[str] = [
258+
'ceph-mon',
259+
'ceph-mgr',
260+
'ceph-mds',
261+
'ceph-osd',
262+
'ceph-fuse',
263+
'radosgw',
264+
'rbd-mirror',
265+
'cephfs-mirror',
266+
'tcmu-runner',
267+
]
268+
269+
logrotate_config = templating.render(
270+
ctx,
271+
templating.Templates.cluster_logrotate_config,
272+
fsid=fsid,
273+
targets=targets,
274+
)
275+
276+
f.write(logrotate_config)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# created by cephadm
2+
/var/log/ceph/cephadm.log {
3+
rotate 7
4+
daily
5+
compress
6+
missingok
7+
notifempty
8+
su root root
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# created by cephadm
2+
/var/log/ceph/{{ fsid }}/*.log {
3+
rotate 7
4+
daily
5+
compress
6+
sharedscripts
7+
postrotate
8+
killall -q -1 {{ targets|join(' ') }} || pkill -1 -x '{{ targets|join('|') }}' || true
9+
endscript
10+
missingok
11+
notifempty
12+
su root root
13+
}

src/cephadm/cephadmlib/templating.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class Templates(str, enum.Enum):
2121

2222
ceph_service = 'ceph.service.j2'
2323
agent_service = 'agent.service.j2'
24+
cluster_logrotate_config = 'cluster.logrotate.config.j2'
25+
cephadm_logrotate_config = 'cephadm.logrotate.config.j2'
2426

2527
def __str__(self) -> str:
2628
return self.value
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from unittest import mock
2+
3+
import pytest
4+
5+
from tests.fixtures import import_cephadm, cephadm_fs
6+
7+
from cephadmlib import logging
8+
9+
10+
_cephadm = import_cephadm()
11+
12+
def test_cluster_logrotate_config(cephadm_fs):
13+
ctx = _cephadm.CephadmContext()
14+
ctx.logrotate_dir = '/my/log/dir'
15+
fsid = '5dcc9af0-7cd3-11ee-9e84-525400babd0a'
16+
17+
cephadm_fs.create_dir(ctx.logrotate_dir)
18+
19+
expected_cluster_logrotate_file = """# created by cephadm
20+
/var/log/ceph/5dcc9af0-7cd3-11ee-9e84-525400babd0a/*.log {
21+
rotate 7
22+
daily
23+
compress
24+
sharedscripts
25+
postrotate
26+
killall -q -1 ceph-mon ceph-mgr ceph-mds ceph-osd ceph-fuse radosgw rbd-mirror cephfs-mirror tcmu-runner || pkill -1 -x 'ceph-mon|ceph-mgr|ceph-mds|ceph-osd|ceph-fuse|radosgw|rbd-mirror|cephfs-mirror|tcmu-runner' || true
27+
endscript
28+
missingok
29+
notifempty
30+
su root root
31+
}"""
32+
33+
logging.write_cluster_logrotate_config(ctx, fsid)
34+
35+
with open(ctx.logrotate_dir + f'/ceph-{fsid}', 'r') as f:
36+
assert f.read() == expected_cluster_logrotate_file
37+
38+
def test_cephadm_logrotate_config(cephadm_fs):
39+
ctx = _cephadm.CephadmContext()
40+
ctx.logrotate_dir = '/my/log/dir'
41+
42+
cephadm_fs.create_dir(ctx.logrotate_dir)
43+
44+
expected_cephadm_logrotate_file = """# created by cephadm
45+
/var/log/ceph/cephadm.log {
46+
rotate 7
47+
daily
48+
compress
49+
missingok
50+
notifempty
51+
su root root
52+
}"""
53+
54+
logging.write_cephadm_logrotate_config(ctx)
55+
56+
with open(ctx.logrotate_dir + f'/cephadm', 'r') as f:
57+
assert f.read() == expected_cephadm_logrotate_file

0 commit comments

Comments
 (0)