Skip to content

Commit 3797fb5

Browse files
committed
cephadm: move logrotate config to jinja2 template
This moves both the cluster and cephadm logrotate configs into jinja2 templates. It looks a bit silly right now to have the cephadm one in a template given it has no variables, but it may allow us to implement custom cephadm log logrotate configs later down the line Signed-off-by: Adam King <[email protected]>
1 parent 473eada commit 3797fb5

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
@@ -147,7 +147,12 @@
147147
from cephadmlib.locking import FileLock
148148
from cephadmlib.daemon_identity import DaemonIdentity, DaemonSubIdentity
149149
from cephadmlib.packagers import create_packager, Packager
150-
from cephadmlib.logging import cephadm_init_logging, Highlight, LogDestination
150+
from cephadmlib.logging import (
151+
cephadm_init_logging,
152+
Highlight,
153+
LogDestination,
154+
write_cluster_logrotate_config,
155+
)
151156
from cephadmlib.systemd import check_unit, check_units
152157
from cephadmlib.container_types import (
153158
CephContainer,
@@ -3419,42 +3424,7 @@ def install_base_units(ctx, fsid):
34193424
if os.path.exists(ctx.logrotate_dir + f'/ceph-{fsid}'):
34203425
return
34213426

3422-
# logrotate for the cluster
3423-
with write_new(ctx.logrotate_dir + f'/ceph-{fsid}', perms=None) as f:
3424-
"""
3425-
This is a bit sloppy in that the killall/pkill will touch all ceph daemons
3426-
in all containers, but I don't see an elegant way to send SIGHUP *just* to
3427-
the daemons for this cluster. (1) systemd kill -s will get the signal to
3428-
podman, but podman will exit. (2) podman kill will get the signal to the
3429-
first child (bash), but that isn't the ceph daemon. This is simpler and
3430-
should be harmless.
3431-
"""
3432-
targets: List[str] = [
3433-
'ceph-mon',
3434-
'ceph-mgr',
3435-
'ceph-mds',
3436-
'ceph-osd',
3437-
'ceph-fuse',
3438-
'radosgw',
3439-
'rbd-mirror',
3440-
'cephfs-mirror',
3441-
'tcmu-runner'
3442-
]
3443-
3444-
f.write("""# created by cephadm
3445-
/var/log/ceph/%s/*.log {
3446-
rotate 7
3447-
daily
3448-
compress
3449-
sharedscripts
3450-
postrotate
3451-
killall -q -1 %s || pkill -1 -x '%s' || true
3452-
endscript
3453-
missingok
3454-
notifempty
3455-
su root root
3456-
}
3457-
""" % (fsid, ' '.join(targets), '|'.join(targets)))
3427+
write_cluster_logrotate_config(ctx, fsid)
34583428

34593429

34603430
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
@@ -17,6 +17,8 @@ class Templates(str, enum.Enum):
1717

1818
ceph_service = 'ceph.service.j2'
1919
agent_service = 'agent.service.j2'
20+
cluster_logrotate_config = 'cluster.logrotate.config.j2'
21+
cephadm_logrotate_config = 'cephadm.logrotate.config.j2'
2022

2123
def __str__(self) -> str:
2224
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)