|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +This command is here mainly to be used by running Pilots. |
| 4 | +Its goal is to add a PilotReference in PilotAgentsDB, and to update its status. |
| 5 | +
|
| 6 | +While SiteDirectors normally add pilots in PilotAgentsDB, |
| 7 | +the same can't be true for pilots started in the vacuum (i.e. without SiteDirectors involved). |
| 8 | +This script is here to solve specifically this issue, even though it can be used for other things too. |
| 9 | +
|
| 10 | +Example: |
| 11 | + $ dirac-admin-add-pilot htcondor:123456 user_DN user_group DIRAC A11D8D2E-60F8-17A6-5520-E2276F41 --Status=Running |
| 12 | +
|
| 13 | +""" |
| 14 | + |
| 15 | +from DIRAC import S_OK, S_ERROR, gLogger, exit as DIRACExit |
| 16 | +from DIRAC.Core.Base.Script import Script |
| 17 | + |
| 18 | + |
| 19 | +class Params: |
| 20 | + """ |
| 21 | + Class holding the parameters, and callbacks for their respective switches. |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__(self): |
| 25 | + """C'or""" |
| 26 | + self.status = False |
| 27 | + self.taskQueueID = 0 |
| 28 | + self.switches = [ |
| 29 | + ("", "status=", "sets the pilot status", self.setStatus), |
| 30 | + ("t:", "taskQueueID=", "sets the taskQueueID", self.setTaskQueueID), |
| 31 | + ] |
| 32 | + |
| 33 | + def setStatus(self, value): |
| 34 | + """sets self.status |
| 35 | +
|
| 36 | + :param value: option argument |
| 37 | +
|
| 38 | + :return: S_OK()/S_ERROR() |
| 39 | + """ |
| 40 | + from DIRAC.WorkloadManagementSystem.Client.PilotStatus import PILOT_STATES |
| 41 | + |
| 42 | + if value not in PILOT_STATES: |
| 43 | + return S_ERROR(f"{value} is not a valid pilot status") |
| 44 | + self.status = value |
| 45 | + return S_OK() |
| 46 | + |
| 47 | + def setTaskQueueID(self, value): |
| 48 | + """sets self.taskQueueID |
| 49 | +
|
| 50 | + :param value: option argument |
| 51 | +
|
| 52 | + :return: S_OK()/S_ERROR() |
| 53 | + """ |
| 54 | + try: |
| 55 | + self.taskQueueID = int(value) |
| 56 | + except ValueError: |
| 57 | + return S_ERROR("TaskQueueID has to be a number") |
| 58 | + return S_OK() |
| 59 | + |
| 60 | + |
| 61 | +@Script() |
| 62 | +def main(): |
| 63 | + """ |
| 64 | + This is the script main method, which holds all the logic. |
| 65 | + """ |
| 66 | + params = Params() |
| 67 | + |
| 68 | + Script.registerSwitches(params.switches) |
| 69 | + Script.registerArgument("pilotRef: pilot reference") |
| 70 | + Script.registerArgument("ownerDN: pilot owner DN") |
| 71 | + Script.registerArgument("ownerGroup: pilot owner group") |
| 72 | + Script.registerArgument("gridType: grid type") |
| 73 | + Script.registerArgument("pilotStamp: DIRAC pilot stamp") |
| 74 | + |
| 75 | + Script.parseCommandLine(ignoreErrors=False) |
| 76 | + |
| 77 | + # Get grouped positional arguments |
| 78 | + pilotRef, ownerDN, ownerGroup, gridType, pilotStamp = Script.getPositionalArgs(group=True) |
| 79 | + |
| 80 | + # Import the required DIRAC modules |
| 81 | + from DIRAC.Core.Utilities import DErrno |
| 82 | + from DIRAC.WorkloadManagementSystem.Client.PilotManagerClient import PilotManagerClient |
| 83 | + |
| 84 | + pmc = PilotManagerClient() |
| 85 | + |
| 86 | + # Check if pilot is not already registered |
| 87 | + res = pmc.getPilotInfo(pilotRef) |
| 88 | + if not res["OK"]: |
| 89 | + if not DErrno.cmpError(res, DErrno.EWMSNOPILOT): |
| 90 | + gLogger.error(res["Message"]) |
| 91 | + DIRACExit(1) |
| 92 | + res = pmc.addPilotTQReference( |
| 93 | + [pilotRef], params.taskQueueID, ownerDN, ownerGroup, "Unknown", gridType, {pilotRef: pilotStamp} |
| 94 | + ) |
| 95 | + if not res["OK"]: |
| 96 | + gLogger.error(res["Message"]) |
| 97 | + DIRACExit(1) |
| 98 | + |
| 99 | + if params.status: |
| 100 | + res = pmc.setPilotStatus(pilotRef, params.status) |
| 101 | + if not res["OK"]: |
| 102 | + gLogger.error(res["Message"]) |
| 103 | + DIRACExit(1) |
| 104 | + |
| 105 | + DIRACExit(0) |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + main() |
0 commit comments