Skip to content

Commit

Permalink
fix: monitoring the installation takes a user
Browse files Browse the repository at this point in the history
  • Loading branch information
fstagni committed Feb 25, 2025
1 parent 250e19e commit a32a20b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
31 changes: 25 additions & 6 deletions src/DIRAC/Core/scripts/dirac_install_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"""
Create a new DB in the MySQL server
"""
from DIRAC import exit as DIRACExit
from DIRAC import gConfig, gLogger
from DIRAC.Core.Base.Script import Script


Expand All @@ -12,7 +14,8 @@ def main():
_, args = Script.parseCommandLine()

# Script imports
from DIRAC import gConfig
from DIRAC.ConfigurationSystem.Client.Helpers.CSGlobals import useServerCertificate
from DIRAC.Core.Security.ProxyInfo import getProxyInfo
from DIRAC.FrameworkSystem.Client.ComponentInstaller import gComponentInstaller
from DIRAC.FrameworkSystem.Utilities import MonitoringUtilities

Expand All @@ -21,15 +24,31 @@ def main():
for db in args:
result = gComponentInstaller.installDatabase(db)
if not result["OK"]:
print(f"ERROR: failed to correctly install {db}", result["Message"])
continue
gLogger.error(f"ERROR: failed to correctly install {db}", result["Message"])
DIRACExit(1)
extension, system = result["Value"]
gComponentInstaller.addDatabaseOptionsToCS(gConfig, system, db, overwrite=True)
result = gComponentInstaller.addDatabaseOptionsToCS(gConfig, system, db, overwrite=True)
if not result["OK"]:
gLogger.error(f"ERROR: failed to add database options to CS: {result['Message']}")
DIRACExit(1)

if db != "InstalledComponentsDB":
result = MonitoringUtilities.monitorInstallation("DB", system, db)

# get the user that installed the DB
if useServerCertificate():
user = "DIRAC"
else:
result = getProxyInfo()
if not result["OK"]:
return result
proxyInfo = result["Value"]
if "username" in proxyInfo:
user = proxyInfo["username"]

result = MonitoringUtilities.monitorInstallation("DB", system, db, user=user)
if not result["OK"]:
print(f"ERROR: failed to register installation in database: {result['Message']}")
gLogger.error(f"ERROR: failed to register installation in database: {result['Message']}")
DIRACExit(1)


if __name__ == "__main__":
Expand Down
17 changes: 11 additions & 6 deletions src/DIRAC/FrameworkSystem/Client/SystemAdministratorClientCLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import time

from DIRAC import gConfig, gLogger
from DIRAC.ConfigurationSystem.Client.Helpers import CSGlobals
from DIRAC.Core.Base.CLI import CLI, colorize
from DIRAC.Core.Security.ProxyInfo import getProxyInfo
from DIRAC.Core.Utilities import List
Expand Down Expand Up @@ -623,6 +622,11 @@ def do_install(self, args):
install agent <system> <agent> [-m <ModuleName>] [-p <Option>=<Value>] [-p <Option>=<Value>] ...
install executor <system> <executor> [-m <ModuleName>] [-p <Option>=<Value>] [-p <Option>=<Value>] ...
"""
result = getProxyInfo()
if not result["OK"]:
self._errMsg(result["Message"])
user = result["Value"]["username"]

argss = args.split()
hostSetup = extension = None
if not argss:
Expand Down Expand Up @@ -673,7 +677,7 @@ def do_install(self, args):

if database != "InstalledComponentsDB":
result = MonitoringUtilities.monitorInstallation(
"DB", system.replace("System", ""), database, cpu=cpu, hostname=hostname
"DB", system.replace("System", ""), database, cpu=cpu, hostname=hostname, user=user
)
if not result["OK"]:
self._errMsg(result["Message"])
Expand Down Expand Up @@ -786,14 +790,14 @@ def do_install(self, args):
return

result = MonitoringUtilities.monitorInstallation(
"DB", system, "InstalledComponentsDB", cpu=cpu, hostname=hostname
"DB", system, "InstalledComponentsDB", cpu=cpu, hostname=hostname, user=user
)
if not result["OK"]:
self._errMsg(f"Error registering installation into database: {result['Message']}")
return

result = MonitoringUtilities.monitorInstallation(
option, system, component, module, cpu=cpu, hostname=hostname
option, system, component, module, cpu=cpu, hostname=hostname, user=user
)
if not result["OK"]:
self._errMsg(f"Error registering installation into database: {result['Message']}")
Expand All @@ -820,6 +824,7 @@ def do_uninstall(self, args):
result = getProxyInfo()
if not result["OK"]:
self._errMsg(result["Message"])
user = result["Value"]["username"]

option = argss[0]
if option == "db":
Expand All @@ -842,7 +847,7 @@ def do_uninstall(self, args):
self._errMsg(result["Message"])
return
system = result["Value"][component]["System"]
result = MonitoringUtilities.monitorUninstallation(system, component, hostname=hostname, cpu=cpu)
result = MonitoringUtilities.monitorUninstallation(system, component, hostname=hostname, cpu=cpu, user=user)
if not result["OK"]:
self._errMsg(result["Message"])
return
Expand Down Expand Up @@ -937,7 +942,7 @@ def do_uninstall(self, args):
else:
cpu = result["Value"]["CPUModel"]
hostname = self.host
result = MonitoringUtilities.monitorUninstallation(system, component, hostname=hostname, cpu=cpu)
result = MonitoringUtilities.monitorUninstallation(system, component, hostname=hostname, cpu=cpu, user=user)
if not result["OK"]:
return result

Expand Down
21 changes: 2 additions & 19 deletions src/DIRAC/FrameworkSystem/Utilities/MonitoringUtilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

from DIRAC import S_OK
from DIRAC.FrameworkSystem.Client.ComponentMonitoringClient import ComponentMonitoringClient
from DIRAC.Core.Security.ProxyInfo import getProxyInfo


def monitorInstallation(componentType, system, component, module=None, cpu=None, hostname=None):
def monitorInstallation(componentType, system, component, module=None, cpu=None, hostname=None, user=None):
"""
Register the installation of a component in the InstalledComponentsDB
"""
Expand All @@ -19,14 +18,6 @@ def monitorInstallation(componentType, system, component, module=None, cpu=None,
module = component

# Retrieve user installing the component
user = None
result = getProxyInfo()
if result["OK"]:
proxyInfo = result["Value"]
if "username" in proxyInfo:
user = proxyInfo["username"]
else:
return result
if not user:
user = "unknown"

Expand Down Expand Up @@ -61,21 +52,13 @@ def monitorInstallation(componentType, system, component, module=None, cpu=None,
return result


def monitorUninstallation(system, component, cpu=None, hostname=None):
def monitorUninstallation(system, component, cpu=None, hostname=None, user=None):
"""
Register the uninstallation of a component in the InstalledComponentsDB
"""
monitoringClient = ComponentMonitoringClient()

# Retrieve user uninstalling the component
user = None
result = getProxyInfo()
if result["OK"]:
proxyInfo = result["Value"]
if "username" in proxyInfo:
user = proxyInfo["username"]
else:
return result
if not user:
user = "unknown"

Expand Down

0 comments on commit a32a20b

Please sign in to comment.