Skip to content

Commit

Permalink
cephadm: use enum for tracking redeploy/reconfig
Browse files Browse the repository at this point in the history
Since the options are mutually exclusive, using
an enum is preferable to having multiple bools
to track each of them

Signed-off-by: Adam King <adking@redhat.com>
  • Loading branch information
adk3798 committed Jun 1, 2023
1 parent 40a9817 commit 7081759
Showing 1 changed file with 61 additions and 44 deletions.
105 changes: 61 additions & 44 deletions src/cephadm/cephadm.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ def __eq__(self, other: Any) -> bool:
and self.version == other.version)


class DeploymentType(Enum):
# Fresh deployment of a daemon.
DEFAULT = 'Deploy'
# Redeploying a daemon. Works the same as fresh
# deployment minus port checking.
REDEPLOY = 'Redeploy'
# Reconfiguring a daemon. Rewrites config
# files and potentially restarts daemon.
RECONFIG = 'Reconfig'


class BaseConfig:

def __init__(self) -> None:
Expand Down Expand Up @@ -3376,13 +3387,13 @@ def deploy_daemon(ctx: CephadmContext, fsid: str, daemon_type: str,
daemon_id: Union[int, str], c: Optional['CephContainer'],
uid: int, gid: int, config: Optional[str] = None,
keyring: Optional[str] = None, osd_fsid: Optional[str] = None,
reconfig: Optional[bool] = False, redeploy: Optional[bool] = False,
deployment_type: DeploymentType = DeploymentType.DEFAULT,
ports: Optional[List[int]] = None) -> None:

ports = ports or []
# only check port in use if not reconfig or redeploy since service
# only check port in use if fresh deployment since service
# we are redeploying/reconfiguring will already be using the port
if not reconfig and not redeploy:
if deployment_type == DeploymentType.DEFAULT:
if any([port_in_use(ctx, port) for port in ports]):
if daemon_type == 'mgr':
# non-fatal for mgr when we are in mgr_standby_modules=false, but we can't
Expand All @@ -3394,7 +3405,7 @@ def deploy_daemon(ctx: CephadmContext, fsid: str, daemon_type: str,
raise Error("TCP Port(s) '{}' required for {} already in use".format(','.join(map(str, ports)), daemon_type))

data_dir = get_data_dir(fsid, ctx.data_dir, daemon_type, daemon_id)
if reconfig and not os.path.exists(data_dir):
if deployment_type == DeploymentType.RECONFIG and not os.path.exists(data_dir):
raise Error('cannot reconfig, data path %s does not exist' % data_dir)
if daemon_type == 'mon' and not os.path.exists(data_dir):
assert config
Expand Down Expand Up @@ -3441,7 +3452,9 @@ def deploy_daemon(ctx: CephadmContext, fsid: str, daemon_type: str,
uid, gid,
config, keyring)

if not reconfig:
# only write out unit files and start daemon
# with systemd if this is not a reconfig
if deployment_type != DeploymentType.RECONFIG:
if daemon_type == CephadmAgent.daemon_type:
if ctx.config_json == '-':
config_js = get_parm('-')
Expand Down Expand Up @@ -3477,7 +3490,9 @@ def deploy_daemon(ctx: CephadmContext, fsid: str, daemon_type: str,
fw.open_ports(ports + fw.external_ports.get(daemon_type, []))
fw.apply_rules()

if reconfig and daemon_type not in Ceph.daemons:
# If this was a reconfig and the daemon is not a Ceph daemon, restart it
# so it can pick up potential changes to its configuration files
if deployment_type == DeploymentType.RECONFIG and daemon_type not in Ceph.daemons:
# ceph daemons do not need a restart; others (presumably) do to pick
# up the new config
call_throws(ctx, ['systemctl', 'reset-failed',
Expand Down Expand Up @@ -6133,6 +6148,24 @@ def get_deployment_container(ctx: CephadmContext,
return c


def get_deployment_type(ctx: CephadmContext, daemon_type: str, daemon_id: str) -> DeploymentType:
deployment_type: DeploymentType = DeploymentType.DEFAULT
if ctx.reconfig:
deployment_type = DeploymentType.RECONFIG
unit_name = get_unit_name(ctx.fsid, daemon_type, daemon_id)
(_, state, _) = check_unit(ctx, unit_name)
if state == 'running' or is_container_running(ctx, CephContainer.for_daemon(ctx, ctx.fsid, daemon_type, daemon_id, 'bash')):
# if reconfig was set, that takes priority over redeploy. If
# this is considered a fresh deployment at this stage,
# mark it as a redeploy to avoid port checking
if deployment_type == DeploymentType.DEFAULT:
deployment_type = DeploymentType.REDEPLOY

logger.info(f'{deployment_type.value} daemon {ctx.name} ...')

return deployment_type


@default_image
def command_deploy(ctx):
# type: (CephadmContext) -> None
Expand All @@ -6144,19 +6177,7 @@ def command_deploy(ctx):
if daemon_type not in get_supported_daemons():
raise Error('daemon type %s not recognized' % daemon_type)

reconfig = ctx.reconfig
redeploy = False
unit_name = get_unit_name(ctx.fsid, daemon_type, daemon_id)
(_, state, _) = check_unit(ctx, unit_name)
if state == 'running' or is_container_running(ctx, CephContainer.for_daemon(ctx, ctx.fsid, daemon_type, daemon_id, 'bash')):
redeploy = True

if reconfig:
logger.info('%s daemon %s ...' % ('Reconfig', ctx.name))
elif redeploy:
logger.info('%s daemon %s ...' % ('Redeploy', ctx.name))
else:
logger.info('%s daemon %s ...' % ('Deploy', ctx.name))
deployment_type: DeploymentType = get_deployment_type(ctx, daemon_type, daemon_id)

# Migrate sysctl conf files from /usr/lib to /etc
migrate_sysctl_dir(ctx, ctx.fsid)
Expand Down Expand Up @@ -6190,8 +6211,7 @@ def command_deploy(ctx):
deploy_daemon(ctx, ctx.fsid, daemon_type, daemon_id, c, uid, gid,
config=config, keyring=keyring,
osd_fsid=ctx.osd_fsid,
reconfig=reconfig,
redeploy=redeploy,
deployment_type=deployment_type,
ports=daemon_ports)

elif daemon_type in Monitoring.components:
Expand All @@ -6213,12 +6233,12 @@ def command_deploy(ctx):
uid, gid = extract_uid_gid_monitoring(ctx, daemon_type)
c = get_deployment_container(ctx, ctx.fsid, daemon_type, daemon_id)
deploy_daemon(ctx, ctx.fsid, daemon_type, daemon_id, c, uid, gid,
reconfig=reconfig,
redeploy=redeploy,
deployment_type=deployment_type,
ports=daemon_ports)

elif daemon_type == NFSGanesha.daemon_type:
if not reconfig and not redeploy and not daemon_ports:
# only check ports if this is a fresh deployment
if deployment_type == DeploymentType.DEFAULT and not daemon_ports:
daemon_ports = list(NFSGanesha.port_map.values())

config, keyring = get_config_and_keyring(ctx)
Expand All @@ -6227,8 +6247,7 @@ def command_deploy(ctx):
c = get_deployment_container(ctx, ctx.fsid, daemon_type, daemon_id)
deploy_daemon(ctx, ctx.fsid, daemon_type, daemon_id, c, uid, gid,
config=config, keyring=keyring,
reconfig=reconfig,
redeploy=redeploy,
deployment_type=deployment_type,
ports=daemon_ports)

elif daemon_type == CephIscsi.daemon_type:
Expand All @@ -6237,63 +6256,58 @@ def command_deploy(ctx):
c = get_deployment_container(ctx, ctx.fsid, daemon_type, daemon_id)
deploy_daemon(ctx, ctx.fsid, daemon_type, daemon_id, c, uid, gid,
config=config, keyring=keyring,
reconfig=reconfig,
redeploy=redeploy,
deployment_type=deployment_type,
ports=daemon_ports)
elif daemon_type in Tracing.components:
uid, gid = 65534, 65534
c = get_container(ctx, ctx.fsid, daemon_type, daemon_id)
deploy_daemon(ctx, ctx.fsid, daemon_type, daemon_id, c, uid, gid,
reconfig=reconfig,
redeploy=redeploy,
deployment_type=deployment_type,
ports=daemon_ports)
elif daemon_type == HAproxy.daemon_type:
haproxy = HAproxy.init(ctx, ctx.fsid, daemon_id)
uid, gid = haproxy.extract_uid_gid_haproxy()
c = get_deployment_container(ctx, ctx.fsid, daemon_type, daemon_id)
deploy_daemon(ctx, ctx.fsid, daemon_type, daemon_id, c, uid, gid,
reconfig=reconfig,
redeploy=redeploy,
deployment_type=deployment_type,
ports=daemon_ports)

elif daemon_type == Keepalived.daemon_type:
keepalived = Keepalived.init(ctx, ctx.fsid, daemon_id)
uid, gid = keepalived.extract_uid_gid_keepalived()
c = get_deployment_container(ctx, ctx.fsid, daemon_type, daemon_id)
deploy_daemon(ctx, ctx.fsid, daemon_type, daemon_id, c, uid, gid,
reconfig=reconfig,
redeploy=redeploy,
deployment_type=deployment_type,
ports=daemon_ports)

elif daemon_type == CustomContainer.daemon_type:
cc = CustomContainer.init(ctx, ctx.fsid, daemon_id)
if not reconfig and not redeploy:
# only check ports if this is a fresh deployment
if deployment_type == DeploymentType.DEFAULT:
daemon_ports.extend(cc.ports)
c = get_deployment_container(ctx, ctx.fsid, daemon_type, daemon_id,
privileged=cc.privileged,
ptrace=ctx.allow_ptrace)
deploy_daemon(ctx, ctx.fsid, daemon_type, daemon_id, c,
uid=cc.uid, gid=cc.gid, config=None,
keyring=None, reconfig=reconfig,
redeploy=redeploy, ports=daemon_ports)
keyring=None, deployment_type=deployment_type,
ports=daemon_ports)

elif daemon_type == CephadmAgent.daemon_type:
# get current user gid and uid
uid = os.getuid()
gid = os.getgid()
deploy_daemon(ctx, ctx.fsid, daemon_type, daemon_id, None,
uid, gid,
reconfig=reconfig,
redeploy=redeploy,
deployment_type=deployment_type,
ports=daemon_ports)

elif daemon_type == SNMPGateway.daemon_type:
sc = SNMPGateway.init(ctx, ctx.fsid, daemon_id)
c = get_deployment_container(ctx, ctx.fsid, daemon_type, daemon_id)
deploy_daemon(ctx, ctx.fsid, daemon_type, daemon_id, c,
sc.uid, sc.gid,
reconfig=reconfig,
redeploy=redeploy,
deployment_type=deployment_type,
ports=daemon_ports)

else:
Expand Down Expand Up @@ -7243,7 +7257,8 @@ def command_adopt_prometheus(ctx, daemon_id, fsid):

make_var_run(ctx, fsid, uid, gid)
c = get_container(ctx, fsid, daemon_type, daemon_id)
deploy_daemon(ctx, fsid, daemon_type, daemon_id, c, uid, gid, redeploy=True, ports=ports)
deploy_daemon(ctx, fsid, daemon_type, daemon_id, c, uid, gid,
deployment_type=DeploymentType.REDEPLOY, ports=ports)
update_firewalld(ctx, daemon_type)


Expand Down Expand Up @@ -7300,7 +7315,8 @@ def command_adopt_grafana(ctx, daemon_id, fsid):

make_var_run(ctx, fsid, uid, gid)
c = get_container(ctx, fsid, daemon_type, daemon_id)
deploy_daemon(ctx, fsid, daemon_type, daemon_id, c, uid, gid, redeploy=True, ports=ports)
deploy_daemon(ctx, fsid, daemon_type, daemon_id, c, uid, gid,
deployment_type=DeploymentType.REDEPLOY, ports=ports)
update_firewalld(ctx, daemon_type)


Expand Down Expand Up @@ -7333,7 +7349,8 @@ def command_adopt_alertmanager(ctx, daemon_id, fsid):

make_var_run(ctx, fsid, uid, gid)
c = get_container(ctx, fsid, daemon_type, daemon_id)
deploy_daemon(ctx, fsid, daemon_type, daemon_id, c, uid, gid, redeploy=True, ports=ports)
deploy_daemon(ctx, fsid, daemon_type, daemon_id, c, uid, gid,
deployment_type=DeploymentType.REDEPLOY, ports=ports)
update_firewalld(ctx, daemon_type)


Expand Down

0 comments on commit 7081759

Please sign in to comment.