Merge pull request #56075 from phlogistonjohn/jjm-enable-black-cephadm

cephadm: enforce black formatting for cephadmlib

Reviewed-by: Adam King <adking@redhat.com>
This commit is contained in:
Adam King 2024-03-19 13:46:11 -04:00 committed by GitHub
commit 6fb1026668
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 131 additions and 65 deletions

View File

@ -6,13 +6,14 @@ import logging
logger = logging.getLogger()
def http_query(addr: str = '',
port: str = '',
data: Optional[bytes] = None,
endpoint: str = '',
ssl_ctx: Optional[Any] = None,
timeout: Optional[int] = 10) -> Tuple[int, str]:
def http_query(
addr: str = '',
port: str = '',
data: Optional[bytes] = None,
endpoint: str = '',
ssl_ctx: Optional[Any] = None,
timeout: Optional[int] = 10,
) -> Tuple[int, str]:
url = f'https://{addr}:{port}{endpoint}'
logger.debug(f'sending query to {url}')
try:

View File

@ -40,7 +40,9 @@ class ContainerDaemonForm(DaemonForm):
"""
return []
def sidecar_containers(self, ctx: CephadmContext) -> List[SidecarContainer]:
def sidecar_containers(
self, ctx: CephadmContext
) -> List[SidecarContainer]:
"""Returns a list of sidecar containers that should be executed along
with the primary service container.
"""
@ -167,7 +169,9 @@ def daemon_to_container(
ctx.container_engine.update_mounts(ctx, container_mounts)
if auto_podman_args and _is_podman:
container_args.extend(
ctx.container_engine.service_args(ctx, daemon.identity.service_name)
ctx.container_engine.service_args(
ctx, daemon.identity.service_name
)
)
return CephContainer.for_daemon(

View File

@ -147,13 +147,7 @@ class BasicContainer:
[],
)
return (
cmd_args
+ self.container_args
+ envs
+ vols
+ binds
)
return cmd_args + self.container_args + envs + vols + binds
def build_run_cmd(self) -> List[str]:
return (
@ -190,7 +184,7 @@ class BasicContainer:
cls,
other: 'BasicContainer',
*,
ident: Optional[DaemonIdentity] = None
ident: Optional[DaemonIdentity] = None,
) -> 'BasicContainer':
return cls(
other.ctx,
@ -582,7 +576,6 @@ def extract_uid_gid(
img: str = '',
file_path: Union[str, List[str]] = '/var/lib/ceph',
) -> Tuple[int, int]:
if not img:
img = ctx.image

View File

@ -327,7 +327,9 @@ class CephExporter(ContainerDaemonForm):
def validate(self) -> None:
if not os.path.isdir(self.sock_dir):
raise Error(f'Desired sock dir for ceph-exporter is not directory: {self.sock_dir}')
raise Error(
f'Desired sock dir for ceph-exporter is not directory: {self.sock_dir}'
)
def container(self, ctx: CephadmContext) -> CephContainer:
ctr = daemon_to_container(ctx, self)

View File

@ -81,9 +81,17 @@ class NodeProxy(ContainerDaemonForm):
data_dir = self.identity.data_dir(ctx.data_dir)
# TODO: update this when we have the actual location
# in the ceph container we are going to keep node-proxy
mounts.update({os.path.join(data_dir, 'node-proxy.json'): '/usr/share/ceph/node-proxy.json:z'})
mounts.update(
{
os.path.join(
data_dir, 'node-proxy.json'
): '/usr/share/ceph/node-proxy.json:z'
}
)
def customize_process_args(self, ctx: CephadmContext, args: List[str]) -> None:
def customize_process_args(
self, ctx: CephadmContext, args: List[str]
) -> None:
# TODO: this corresponds with the mount location of
# the config in _get_container_mounts above. They
# will both need to be updated when we have a proper

View File

@ -14,7 +14,6 @@ logger = logging.getLogger()
class Firewalld(object):
# for specifying ports we should always open when opening
# ports for a daemon of that type. Main use case is for ports
# that we should open when deploying the daemon type but that
@ -51,26 +50,44 @@ class Firewalld(object):
def enable_service_for(self, svc: str) -> None:
assert svc, 'service name not provided'
if not self.available:
logger.debug('Not possible to enable service <%s>. firewalld.service is not available' % svc)
logger.debug(
'Not possible to enable service <%s>. firewalld.service is not available'
% svc
)
return
if not self.cmd:
raise RuntimeError('command not defined')
out, err, ret = call(self.ctx, [self.cmd, '--permanent', '--query-service', svc], verbosity=CallVerbosity.DEBUG)
out, err, ret = call(
self.ctx,
[self.cmd, '--permanent', '--query-service', svc],
verbosity=CallVerbosity.DEBUG,
)
if ret:
logger.info('Enabling firewalld service %s in current zone...' % svc)
out, err, ret = call(self.ctx, [self.cmd, '--permanent', '--add-service', svc])
logger.info(
'Enabling firewalld service %s in current zone...' % svc
)
out, err, ret = call(
self.ctx, [self.cmd, '--permanent', '--add-service', svc]
)
if ret:
raise RuntimeError(
'unable to add service %s to current zone: %s' % (svc, err))
'unable to add service %s to current zone: %s'
% (svc, err)
)
else:
logger.debug('firewalld service %s is enabled in current zone' % svc)
logger.debug(
'firewalld service %s is enabled in current zone' % svc
)
def open_ports(self, fw_ports):
# type: (List[int]) -> None
if not self.available:
logger.debug('Not possible to open ports <%s>. firewalld.service is not available' % fw_ports)
logger.debug(
'Not possible to open ports <%s>. firewalld.service is not available'
% fw_ports
)
return
if not self.cmd:
@ -78,20 +95,36 @@ class Firewalld(object):
for port in fw_ports:
tcp_port = str(port) + '/tcp'
out, err, ret = call(self.ctx, [self.cmd, '--permanent', '--query-port', tcp_port], verbosity=CallVerbosity.DEBUG)
out, err, ret = call(
self.ctx,
[self.cmd, '--permanent', '--query-port', tcp_port],
verbosity=CallVerbosity.DEBUG,
)
if ret:
logger.info('Enabling firewalld port %s in current zone...' % tcp_port)
out, err, ret = call(self.ctx, [self.cmd, '--permanent', '--add-port', tcp_port])
logger.info(
'Enabling firewalld port %s in current zone...' % tcp_port
)
out, err, ret = call(
self.ctx,
[self.cmd, '--permanent', '--add-port', tcp_port],
)
if ret:
raise RuntimeError('unable to add port %s to current zone: %s' %
(tcp_port, err))
raise RuntimeError(
'unable to add port %s to current zone: %s'
% (tcp_port, err)
)
else:
logger.debug('firewalld port %s is enabled in current zone' % tcp_port)
logger.debug(
'firewalld port %s is enabled in current zone' % tcp_port
)
def close_ports(self, fw_ports):
# type: (List[int]) -> None
if not self.available:
logger.debug('Not possible to close ports <%s>. firewalld.service is not available' % fw_ports)
logger.debug(
'Not possible to close ports <%s>. firewalld.service is not available'
% fw_ports
)
return
if not self.cmd:
@ -99,13 +132,22 @@ class Firewalld(object):
for port in fw_ports:
tcp_port = str(port) + '/tcp'
out, err, ret = call(self.ctx, [self.cmd, '--permanent', '--query-port', tcp_port], verbosity=CallVerbosity.DEBUG)
out, err, ret = call(
self.ctx,
[self.cmd, '--permanent', '--query-port', tcp_port],
verbosity=CallVerbosity.DEBUG,
)
if not ret:
logger.info('Disabling port %s in current zone...' % tcp_port)
out, err, ret = call(self.ctx, [self.cmd, '--permanent', '--remove-port', tcp_port])
out, err, ret = call(
self.ctx,
[self.cmd, '--permanent', '--remove-port', tcp_port],
)
if ret:
raise RuntimeError('unable to remove port %s from current zone: %s' %
(tcp_port, err))
raise RuntimeError(
'unable to remove port %s from current zone: %s'
% (tcp_port, err)
)
else:
logger.info(f'Port {tcp_port} disabled')
else:

View File

@ -16,10 +16,13 @@ from .file_utils import write_new
logger = logging.getLogger()
def install_sysctl(ctx: CephadmContext, fsid: str, daemon: DaemonForm) -> None:
def install_sysctl(
ctx: CephadmContext, fsid: str, daemon: DaemonForm
) -> None:
"""
Set up sysctl settings
"""
def _write(conf: Path, lines: List[str]) -> None:
lines = [
'# created by cephadm',
@ -54,11 +57,14 @@ def sysctl_get(ctx: CephadmContext, variable: str) -> Union[str, None]:
return out or None
def filter_sysctl_settings(ctx: CephadmContext, lines: List[str]) -> List[str]:
def filter_sysctl_settings(
ctx: CephadmContext, lines: List[str]
) -> List[str]:
"""
Given a list of sysctl settings, examine the system's current configuration
and return those which are not currently set as described.
"""
def test_setting(desired_line: str) -> bool:
# Remove any comments
comment_start = desired_line.find('#')
@ -67,11 +73,14 @@ def filter_sysctl_settings(ctx: CephadmContext, lines: List[str]) -> List[str]:
desired_line = desired_line.strip()
if not desired_line or desired_line.isspace():
return False
setting, desired_value = map(lambda s: s.strip(), desired_line.split('='))
setting, desired_value = map(
lambda s: s.strip(), desired_line.split('=')
)
if not setting or not desired_value:
return False
actual_value = sysctl_get(ctx, setting)
return desired_value != actual_value
return list(filter(test_setting, lines))
@ -81,36 +90,50 @@ def migrate_sysctl_dir(ctx: CephadmContext, fsid: str) -> None:
This moves it to '/etc/sysctl.d'.
"""
deprecated_location: str = '/usr/lib/sysctl.d'
deprecated_confs: List[str] = glob(f'{deprecated_location}/90-ceph-{fsid}-*.conf')
deprecated_confs: List[str] = glob(
f'{deprecated_location}/90-ceph-{fsid}-*.conf'
)
if not deprecated_confs:
return
file_count: int = len(deprecated_confs)
logger.info(f'Found sysctl {file_count} files in deprecated location {deprecated_location}. Starting Migration.')
logger.info(
f'Found sysctl {file_count} files in deprecated location {deprecated_location}. Starting Migration.'
)
for conf in deprecated_confs:
try:
shutil.move(conf, ctx.sysctl_dir)
file_count -= 1
except shutil.Error as err:
if str(err).endswith('already exists'):
logger.warning(f'Destination file already exists. Deleting {conf}.')
logger.warning(
f'Destination file already exists. Deleting {conf}.'
)
try:
os.unlink(conf)
file_count -= 1
except OSError as del_err:
logger.warning(f'Could not remove {conf}: {del_err}.')
else:
logger.warning(f'Could not move {conf} from {deprecated_location} to {ctx.sysctl_dir}: {err}')
logger.warning(
f'Could not move {conf} from {deprecated_location} to {ctx.sysctl_dir}: {err}'
)
# Log successful migration
if file_count == 0:
logger.info(f'Successfully migrated sysctl config to {ctx.sysctl_dir}.')
logger.info(
f'Successfully migrated sysctl config to {ctx.sysctl_dir}.'
)
return
# Log partially successful / unsuccessful migration
files_processed: int = len(deprecated_confs)
if file_count < files_processed:
status: str = f'partially successful (failed {file_count}/{files_processed})'
status: str = (
f'partially successful (failed {file_count}/{files_processed})'
)
elif file_count == files_processed:
status = 'unsuccessful'
logger.warning(f'Migration of sysctl configuration {status}. You may want to perform a migration manually.')
logger.warning(
f'Migration of sysctl configuration {status}. You may want to perform a migration manually.'
)

View File

@ -78,7 +78,10 @@ def _write_init_containers_unit_file(
def _write_sidecar_unit_file(
dest: IO, ctx: CephadmContext, primary: DaemonIdentity, sidecar: DaemonSubIdentity
dest: IO,
ctx: CephadmContext,
primary: DaemonIdentity,
sidecar: DaemonSubIdentity,
) -> None:
has_docker_engine = isinstance(ctx.container_engine, Docker)
has_podman_engine = isinstance(ctx.container_engine, Podman)
@ -128,9 +131,7 @@ def _install_extended_systemd_services(
difh = estack.enter_context(
write_new(pinfo.drop_in_file, perms=None)
)
_write_drop_in(
difh, ctx, identity, enable_init_containers, sids
)
_write_drop_in(difh, ctx, identity, enable_init_containers, sids)
def _get_unit_file(ctx: CephadmContext, fsid: str) -> str:

View File

@ -1,9 +1,9 @@
[tox]
envlist =
py3
mypy
fix
flake8
mypy
check-black
py3
skipsdist = true
[flake8]
@ -50,14 +50,6 @@ deps =
-c{toxinidir}/../mypy-constrains.txt
commands = mypy --config-file ../mypy.ini {posargs:cephadm.py cephadmlib}
[testenv:fix]
basepython = python3
deps =
autopep8
commands =
python --version
autopep8 {[autopep8]addopts} {posargs: cephadm.py}
[testenv:flake8]
basepython = python3
allowlist_externals = bash