Skip to content

Commit

Permalink
fixup! Add function to check if service can be started
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Sandakov committed Mar 20, 2024
1 parent 84a5827 commit 2ba6b94
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
6 changes: 3 additions & 3 deletions pleskdistup/actions/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __init__(self):
"sw-engine.service",
]
self.plesk_systemd_services = [
service for service in plesk_known_systemd_services if systemd.is_service_can_be_started(service)
service for service in plesk_known_systemd_services if systemd.is_service_startable(service)
]

# Oneshot services are special, so they shouldn't be started on revert or after conversion, just enabled
Expand All @@ -113,9 +113,9 @@ def __init__(self):

# We don't remove postfix service when remove it during qmail installation
# so we should choose the right smtp service, otherwise they will conflict
if systemd.is_service_can_be_started("qmail.service"):
if systemd.is_service_startable("qmail.service"):
self.plesk_systemd_services.append("qmail.service")
elif systemd.is_service_can_be_started("postfix.service"):
elif systemd.is_service_startable("postfix.service"):
self.plesk_systemd_services.append("postfix.service")

def _prepare_action(self) -> action.ActionResult:
Expand Down
23 changes: 14 additions & 9 deletions pleskdistup/common/src/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,34 @@ def is_service_active(service: str):

def get_required_services(service: str) -> typing.List[str]:
res = subprocess.run(
[SYSTEMCTL_BIN_PATH, 'cat', service],
[SYSTEMCTL_BIN_PATH, 'show', '--property', 'Requires', service],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
universal_newlines=True
)

required_services = []
for line in res.stdout.splitlines():
if line.startswith('Requires='):
required_services = line.split('s=')[1].split()
break
required_services = [service for service in res.stdout.split('s=')[1].split() if '.service' in service]
return required_services


def is_service_can_be_started(service: str) -> bool:
def is_service_startable(service: str, already_checked: typing.Set[str] = None) -> bool:
if not is_service_exists(service):
log.debug(f"Service '{service}' not exists")
return False

if already_checked is not None and service in already_checked:
return True

if already_checked is None:
already_checked = {service}
else:
already_checked.add(service)

required_services = get_required_services(service)
for required_service in required_services:
if not is_service_exists(required_service):
log.debug("Service '{}' can't be started because required service '{}' doesn't exist".format(service, required_service))
if not is_service_startable(required_service, already_checked):
log.debug(f"Service '{service}' can't be started because required service '{required_service}' doesn't exist")
return False
return True

Expand Down

0 comments on commit 2ba6b94

Please sign in to comment.