Example #1
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)
Example #2
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)

    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)
Example #3
0
def _ParseInstanceReinstallRequest(name, data):
    """Parses a request for reinstalling an instance.

  """
    if not isinstance(data, dict):
        raise http.HttpBadRequest("Invalid body contents, not a dictionary")

    ostype = baserlib.CheckParameter(data, "os", default=None)
    start = baserlib.CheckParameter(data, "start", exptype=bool, default=True)
    osparams = baserlib.CheckParameter(data, "osparams", default=None)

    ops = [
        opcodes.OpInstanceShutdown(instance_name=name),
        opcodes.OpInstanceReinstall(instance_name=name,
                                    os_type=ostype,
                                    osparams=osparams),
    ]

    if start:
        ops.append(opcodes.OpInstanceStartup(instance_name=name, force=False))

    return ops
Example #4
0
 def StopInstanceOp(instance):
     """Stop given instance."""
     return opcodes.OpInstanceShutdown(instance_name=instance)