Skip to content

Commit

Permalink
The psutil library is a Salt hard depency
Browse files Browse the repository at this point in the history
  • Loading branch information
s0undt3ch committed Feb 26, 2024
1 parent 4b22459 commit ca40b31
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 204 deletions.
16 changes: 3 additions & 13 deletions salt/beacons/diskusage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,18 @@
import logging
import re

import psutil

import salt.utils.beacons
import salt.utils.platform

try:
import psutil

HAS_PSUTIL = True
except ImportError:
HAS_PSUTIL = False

log = logging.getLogger(__name__)

__virtualname__ = "diskusage"


def __virtual__():
if HAS_PSUTIL is False:
err_msg = "psutil library is missing."
log.error("Unable to load %s beacon: %s", __virtualname__, err_msg)
return False, err_msg
else:
return __virtualname__
return __virtualname__


def validate(config):
Expand Down
16 changes: 3 additions & 13 deletions salt/beacons/memusage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,17 @@
import logging
import re

import salt.utils.beacons

try:
import psutil
import psutil

HAS_PSUTIL = True
except ImportError:
HAS_PSUTIL = False
import salt.utils.beacons

log = logging.getLogger(__name__)

__virtualname__ = "memusage"


def __virtual__():
if HAS_PSUTIL is False:
err_msg = "psutil library is missing."
log.error("Unable to load %s beacon: %s", __virtualname__, err_msg)
return False, err_msg
else:
return __virtualname__
return __virtualname__


def validate(config):
Expand Down
16 changes: 3 additions & 13 deletions salt/beacons/swapusage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,17 @@
import logging
import re

import salt.utils.beacons

try:
import psutil
import psutil

HAS_PSUTIL = True
except ImportError:
HAS_PSUTIL = False
import salt.utils.beacons

log = logging.getLogger(__name__)

__virtualname__ = "swapusage"


def __virtual__():
if HAS_PSUTIL is False:
err_msg = "psutil library is missing."
log.error("Unable to load %s beacon: %s", __virtualname__, err_msg)
return False, err_msg
else:
return __virtualname__
return __virtualname__


def validate(config):
Expand Down
5 changes: 1 addition & 4 deletions salt/cli/daemons.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from salt.exceptions import SaltClientError, SaltSystemExit, get_error_message
from salt.utils import migrations
from salt.utils.platform import is_junos
from salt.utils.process import HAS_PSUTIL

# All salt related deprecation warnings should be shown once each!
warnings.filterwarnings(
Expand Down Expand Up @@ -282,9 +281,7 @@ def prepare(self):
migrations.migrate_paths(self.config)

# Bail out if we find a process running and it matches out pidfile
if (HAS_PSUTIL and not self.claim_process_responsibility()) or (
not HAS_PSUTIL and self.check_running()
):
if not self.claim_process_responsibility():
self.action_log_info("An instance is already running. Exiting")
self.shutdown(1)

Expand Down
25 changes: 3 additions & 22 deletions salt/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import urllib.parse
from copy import deepcopy

import psutil

import salt.defaults.exitcodes
import salt.exceptions
import salt.features
Expand All @@ -37,15 +39,6 @@
DFLT_LOG_FMT_LOGFILE,
)

try:
import psutil

if not hasattr(psutil, "virtual_memory"):
raise ImportError("Version of psutil too old.")
HAS_PSUTIL = True
except ImportError:
HAS_PSUTIL = False

log = logging.getLogger(__name__)

_DFLT_REFSPECS = ["+refs/heads/*:refs/remotes/origin/*", "+refs/tags/*:refs/tags/*"]
Expand Down Expand Up @@ -88,19 +81,7 @@ def _gather_buffer_space():
Result is in bytes.
"""
if HAS_PSUTIL and psutil.version_info >= (0, 6, 0):
# Oh good, we have psutil. This will be quick.
total_mem = psutil.virtual_memory().total
else:
# Avoid loading core grains unless absolutely required
import platform

import salt.grains.core

# We need to load up ``mem_total`` grain. Let's mimic required OS data.
os_data = {"kernel": platform.system()}
grains = salt.grains.core._memdata(os_data)
total_mem = grains["mem_total"]
total_mem = psutil.virtual_memory().total
# Return the higher number between 5% of the system memory and 10MiB
return max([total_mem * 0.05, 10 << 20])

Expand Down
20 changes: 2 additions & 18 deletions salt/modules/restartcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,15 @@
import sys
import time

import psutil

import salt.exceptions
import salt.utils.args
import salt.utils.files
import salt.utils.path

NILRT_FAMILY_NAME = "NILinuxRT"

HAS_PSUTIL = False
try:
import psutil

HAS_PSUTIL = True
except ImportError:
pass

LIST_DIRS = [
# We don't care about log files
"^/var/log/",
Expand Down Expand Up @@ -76,16 +70,6 @@
]


def __virtual__():
"""
Only run this module if the psutil python module is installed (package python-psutil).
"""
if HAS_PSUTIL:
return HAS_PSUTIL
else:
return (False, "Missing dependency: psutil")


def _valid_deleted_file(path):
"""
Filters file path against unwanted directories and decides whether file is marked as deleted.
Expand Down
12 changes: 2 additions & 10 deletions salt/modules/win_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import logging
import subprocess

import psutil

import salt.utils.event
import salt.utils.platform
import salt.utils.stringutils
Expand All @@ -34,13 +36,6 @@
except ImportError:
HAS_WMI = False

try:
import psutil

HAS_PSUTIL = True
except ImportError:
HAS_PSUTIL = False

if salt.utils.platform.is_windows():
ping_master = namespaced_function(ping_master, globals())
time_ = namespaced_function(time_, globals())
Expand Down Expand Up @@ -144,9 +139,6 @@ def __virtual__():
if not HAS_WMI:
return False, "win_status.py: Requires WMI and WinAPI"

if not HAS_PSUTIL:
return False, "win_status.py: Requires psutil"

return __virtualname__


Expand Down
77 changes: 24 additions & 53 deletions salt/utils/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import threading
import time

import psutil

import salt._logging
import salt.defaults.exitcodes
import salt.utils.files
Expand All @@ -30,14 +32,6 @@

log = logging.getLogger(__name__)

HAS_PSUTIL = False
try:
import psutil

HAS_PSUTIL = True
except ImportError:
pass

try:
import setproctitle

Expand Down Expand Up @@ -227,13 +221,6 @@ def claim_mantle_of_responsibility(file_name):
Return: bool
"""

# all OSs supported by salt has psutil
if not HAS_PSUTIL:
log.critical(
"Assuming no other Process has this responsibility! pidfile: %s", file_name
)
return True

# add file directory if missing
file_directory_name = os.path.dirname(file_name)
if not os.path.isdir(file_directory_name) and file_directory_name:
Expand Down Expand Up @@ -278,14 +265,6 @@ def check_mantle_of_responsibility(file_name):
file_name: str
Return: None or int
"""

# all OSs supported by salt has psutil
if not HAS_PSUTIL:
log.critical(
"Assuming no other Process has this responsibility! pidfile: %s", file_name
)
return

# get process info from file
try:
with salt.utils.files.fopen(file_name, "r") as file:
Expand Down Expand Up @@ -403,14 +382,7 @@ def os_is_running(pid):
"""
if isinstance(pid, str):
pid = int(pid)
if HAS_PSUTIL:
return psutil.pid_exists(pid)
else:
try:
os.kill(pid, 0) # SIG 0 is the "are you alive?" signal
return True
except OSError:
return False
return psutil.pid_exists(pid)


class ThreadPool:
Expand Down Expand Up @@ -1068,28 +1040,27 @@ def _handle_signals(self, signum, sigframe):
msg += "SIGTERM"
msg += ". Exiting"
log.debug(msg)
if HAS_PSUTIL:
try:
process = psutil.Process(os.getpid())
if hasattr(process, "children"):
for child in process.children(recursive=True):
try:
if child.is_running():
child.terminate()
except psutil.NoSuchProcess:
log.warning(
"Unable to kill child of process %d, it does "
"not exist. My pid is %d",
self.pid,
os.getpid(),
)
except psutil.NoSuchProcess:
log.warning(
"Unable to kill children of process %d, it does not exist."
"My pid is %d",
self.pid,
os.getpid(),
)
try:
process = psutil.Process(os.getpid())
if hasattr(process, "children"):
for child in process.children(recursive=True):
try:
if child.is_running():
child.terminate()
except psutil.NoSuchProcess:
log.warning(
"Unable to kill child of process %d, it does "
"not exist. My pid is %d",
self.pid,
os.getpid(),
)
except psutil.NoSuchProcess:
log.warning(
"Unable to kill children of process %d, it does not exist."
"My pid is %d",
self.pid,
os.getpid(),
)
# It's OK to call os._exit instead of sys.exit on forked processed
os._exit(salt.defaults.exitcodes.EX_OK)

Expand Down
15 changes: 4 additions & 11 deletions salt/utils/win_runas.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
"""
Run processes as a different user in Windows
"""

# Import Python Libraries
import ctypes
import logging
import os
import time

from salt.exceptions import CommandExecutionError

try:
import psutil
import psutil

HAS_PSUTIL = True
except ImportError:
HAS_PSUTIL = False
from salt.exceptions import CommandExecutionError

try:
import msvcrt
Expand Down Expand Up @@ -45,8 +38,8 @@ def __virtual__():
"""
Only load if Win32 Libraries are installed
"""
if not HAS_WIN32 or not HAS_PSUTIL:
return False, "This utility requires pywin32 and psutil"
if not HAS_WIN32:
return False, "This utility requires pywin32"

return "win_runas"

Expand Down
Loading

0 comments on commit ca40b31

Please sign in to comment.