def _VerifyDisks(cl, uuid, nodes, instances): """Run a per-group "gnt-cluster verify-disks". """ op = opcodes.OpGroupVerifyDisks(group_name=uuid, priority=constants.OP_PRIO_LOW) op.reason = [(constants.OPCODE_REASON_SRC_WATCHER, "Verifying disks of group %s" % uuid, utils.EpochNano())] job_id = cl.SubmitJob([op]) ((_, offline_disk_instances, _), ) = \ cli.PollJob(job_id, cl=cl, feedback_fn=logging.debug) try: cl.ArchiveJob(job_id) except Exception as err: logging.exception("Error while archiving job %d" % job_id) if not offline_disk_instances: # nothing to do logging.debug("Verify-disks reported no offline disks, nothing to do") return logging.debug("Will activate disks for instance(s) %s", utils.CommaJoin(offline_disk_instances)) # We submit only one job, and wait for it. Not optimal, but this puts less # load on the job queue. job = [] for name in offline_disk_instances: try: inst = instances[name] except KeyError: logging.info("Can't find instance '%s', maybe it was ignored", name) continue if inst.status in HELPLESS_STATES or _CheckForOfflineNodes( nodes, inst): logging.info( "Skipping instance '%s' because it is in a helpless state" " or has offline secondaries", name) continue op = opcodes.OpInstanceActivateDisks(instance_name=name) op.reason = [(constants.OPCODE_REASON_SRC_WATCHER, "Activating disks for instance %s" % name, utils.EpochNano())] job.append(op) if job: job_id = cli.SendJob(job, cl=cl) try: cli.PollJob(job_id, cl=cl, feedback_fn=logging.debug) except Exception: # pylint: disable=W0703 logging.exception("Error while activating disks")
def ReplaceFilter(opts, args): """Replaces a job filter rule with the given UUID, or creates it, if it doesn't exist already. @param opts: the command line options selected by the user @type args: list @param args: should contain only one element, the UUID of the filter @rtype: int @return: the desired exit code """ (uuid, ) = args reason = [] if opts.reason: reason = [(constants.OPCODE_REASON_SRC_USER, opts.reason, utils.EpochNano())] cl = GetClient() result = cl.ReplaceFilter(uuid, priority=opts.priority, predicates=opts.predicates, action=opts.action, reason=reason) print(result) # Prints the UUID of the replaced/created filter return 0
def _CleanupInstance(cl, notepad, inst, locks): n = notepad.NumberOfCleanupAttempts(inst.name) if inst.name in locks: logging.info("Not cleaning up instance '%s', instance is locked", inst.name) return if n > MAXTRIES: logging.warning("Not cleaning up instance '%s', retries exhausted", inst.name) return logging.info( "Instance '%s' was shutdown by the user, cleaning up instance", inst.name) op = opcodes.OpInstanceShutdown(instance_name=inst.name, admin_state_source=constants.USER_SOURCE) op.reason = [(constants.OPCODE_REASON_SRC_WATCHER, "Cleaning up instance %s" % inst.name, utils.EpochNano())] try: cli.SubmitOpCode(op, cl=cl) if notepad.NumberOfCleanupAttempts(inst.name): notepad.RemoveInstance(inst.name) except Exception: # pylint: disable=W0703 logging.exception("Error while cleaning up instance '%s'", inst.name) notepad.RecordCleanupAttempt(inst.name)
def Restart(self, cl): """Encapsulates the start of an instance. """ op = opcodes.OpInstanceStartup(instance_name=self.name, force=False) op.reason = [(constants.OPCODE_REASON_SRC_WATCHER, "Restarting instance %s" % self.name, utils.EpochNano())] cli.SubmitOpCode(op, cl=cl)
def _extendReasonTrail(trail, source, reason=""): """Extend the reason trail with noded information The trail is extended by appending the name of the noded functionality """ assert trail is not None trail_source = "%s:%s" % (constants.OPCODE_REASON_SRC_NODED, source) trail.append((trail_source, reason, utils.EpochNano()))
def ActivateDisks(self, cl): """Encapsulates the activation of all disks of an instance. """ op = opcodes.OpInstanceActivateDisks(instance_name=self.name) op.reason = [(constants.OPCODE_REASON_SRC_WATCHER, "Activating disks for instance %s" % self.name, utils.EpochNano())] cli.SubmitOpCode(op, cl=cl)
def testAddFilter(self): self.assertTrue( self.cl.AddFilter( priority=1, predicates=[["jobid", [">", "id", "watermark"]]], action="CONTINUE", reason_trail=["testAddFilter", "myreason", utils.EpochNano()], ) is NotImplemented)
def _GetCommonStatic(self): """Return the static parameters common to all the RAPI calls The reason is a parameter present in all the RAPI calls, and the reason trail has to be build for all of them, so the parameter is read here and used to build the reason trail, that is the actual parameter passed forward. """ trail = [] usr_reason = self._checkStringVariable("reason", default=None) if usr_reason: trail.append((constants.OPCODE_REASON_SRC_USER, usr_reason, utils.EpochNano())) reason_src = "%s:%s" % (constants.OPCODE_REASON_SRC_RLIB2, self._GetRapiOpName()) trail.append((reason_src, "", utils.EpochNano())) common_static = { "reason": trail, } return common_static
def testReplaceFilter(self): self.assertTrue( self.cl.ReplaceFilter( uuid="c6a70f02-facb-4e37-b344-54f146dd0396", priority=1, predicates=[["jobid", [">", "id", "watermark"]]], action="CONTINUE", reason_trail=[ "testReplaceFilter", "myreason", utils.EpochNano() ], ) is NotImplemented)
def TestFilters(): """Testing filter management via the remote API. """ body = { "priority": 10, "predicates": [], "action": "CONTINUE", "reason": [(constants.OPCODE_REASON_SRC_USER, "reason1", utils.EpochNano())], } body1 = copy.deepcopy(body) body1["priority"] = 20 # Query filters _DoTests([("/2/filters", [], "GET", None)]) # Add a filter via POST and delete it again uuid = _DoTests([("/2/filters", None, "POST", body)])[0] uuid_module.UUID(uuid) # Check if uuid is a valid UUID _DoTests([("/2/filters/%s" % uuid, lambda r: r is None, "DELETE", None)]) _DoTests([ # Check PUT-inserting a nonexistent filter with given UUID ("/2/filters/%s" % uuid, lambda u: u == uuid, "PUT", body), # Check PUT-inserting an existent filter with given UUID ("/2/filters/%s" % uuid, lambda u: u == uuid, "PUT", body1), # Check that the update changed the filter ("/2/filters/%s" % uuid, lambda f: f["priority"] == 20, "GET", None), # Delete it again ("/2/filters/%s" % uuid, lambda r: r is None, "DELETE", None), ]) # Add multiple filters, query and delete them uuids = _DoTests([ ("/2/filters", None, "POST", body), ("/2/filters", None, "POST", body), ("/2/filters", None, "POST", body), ]) _DoTests([("/2/filters", lambda rs: [r["uuid"] for r in rs] == uuids, "GET", None)]) for u in uuids: _DoTests([("/2/filters/%s" % u, lambda r: r is None, "DELETE", None)])
def AddFilter(opts, args): """Add a job filter rule. @param opts: the command line options selected by the user @type args: list @param args: should be an empty list @rtype: int @return: the desired exit code """ assert args == [] reason = [] if opts.reason: reason = [(constants.OPCODE_REASON_SRC_USER, opts.reason, utils.EpochNano())] cl = GetClient() result = cl.ReplaceFilter(None, opts.priority, opts.predicates, opts.action, reason) print(result) # Prints the UUID of the replaced/created filter
def GetAuthReason(self): return (constants.OPCODE_REASON_SRC_RLIB2, constants.OPCODE_REASON_AUTH_USER + self.auth_user, utils.EpochNano())