def execute()

in gcpdiag/runbook/gce/vm_termination.py [0:0]


  def execute(self):
    """Investigate VM termination reason."""
    project = crm.get_project(op.get(flags.PROJECT_ID))
    res = gce.get_global_operations(
        project=op.get(flags.PROJECT_ID),
        filter_str=TERMINATION_OPERATION_FILTER.format(
            INSTANCE_ID=op.get(flags.INSTANCE_ID)),
        service_project_number=project.number,
        order_by='creationTimestamp desc',
        max_results=5)

    start = op.get(flags.START_TIME)
    end = op.get(flags.END_TIME)

    operations = list(filter(lambda o: is_within_window(o, start, end), res))
    if operations:
      # get the most recent termination operation
      operation_type = get_path(operations[0], ('operationType'))
      progress = get_path(operations[0], ('progress'))
      # if part of a mig
      if progress == 100 and operation_type == constants.IG_INSTANCE_REPAIR_METHOD:
        mig_recreation = ManagedInstanceGroupRecreation()
        mig_recreation.status = get_path(operations[0], ('statusMessage'))
        self.add_child(mig_recreation)
      elif progress == 100 and operation_type == constants.INSTANCE_PREMPTION_METHOD:
        preemptible_instance_termination = PreemptibleInstance()
        preemptible_instance_termination.status = get_path(
            operations[0], ('statusMessage'))
        self.add_child(preemptible_instance_termination)
      elif progress == 100 and operation_type == constants.HOST_ERROR_METHOD:
        host_error = HostError()
        host_error.status = get_path(operations[0], ('statusMessage'))
        self.add_child(host_error)
      elif progress == 100 and operation_type == constants.GUEST_TERMINATE_METHOD:
        # Coverage: Differentiate between user and system initiated shutdowns. eg: shutdown due
        # to integrity monitoring https://cloud.google.com/compute/shielded-vm/docs/
        # integrity-monitoring#updating-baseline
        # Help users to understand the PCRs and how to fix it
        guest_os_issued_shutdown = GuestOsIssuedShutdown()
        guest_os_issued_shutdown.status = get_path(operations[0],
                                                   ('statusMessage'))
        self.add_child(guest_os_issued_shutdown)
      elif progress == 100 and operation_type == constants.TERMINATE_ON_HOST_MAINTENANCE_METHOD:
        terminate_on_host_maintenance = TerminateOnHostMaintenance()
        terminate_on_host_maintenance.status = get_path(operations[0],
                                                        ('statusMessage'))
        self.add_child(terminate_on_host_maintenance)
      elif progress == 100 and operation_type == 'stop':
        stop_gateway = StopOperationGateway()
        stop_gateway.stop_account = get_path(operations[0], ('user'))
        stop_gateway.operation_name = get_path(operations[0], ('name'))
        self.add_child(stop_gateway)