private void cleanupImage()

in aws-core-server/src/main/java/jetbrains/buildServer/clouds/amazon/ami/cleanup/EC2AmiCleanupExtension.java [163:188]


  private void cleanupImage(@NotNull BuildCleanupContext cleanupContext, @NotNull SFinishedBuild build, @NotNull Ec2Client client, @NotNull Image image) {
    try {
      client.deregisterImage(DeregisterImageRequest.builder()
        .imageId(image.imageId())
        .build());
      final List<String> snapshots = image.blockDeviceMappings().stream()
                                          .map(BlockDeviceMapping::ebs)
                                          .filter(Objects::nonNull).map(EbsBlockDevice::snapshotId)
                                          .collect(Collectors.toList());
      for (String snapshot : snapshots) {
        try {
          client.deleteSnapshot(DeleteSnapshotRequest.builder()
            .snapshotId(snapshot)
            .build());
        } catch (SdkException e) {
          final String message = String.format(SNAPSHOT_ERROR, snapshot, image.imageId());
          CLEANUP.warnAndDebugDetails(message, e);
          cleanupContext.onBuildCleanupError(this, build, message);
        }
      }
    } catch (SdkException e) {
      final String message = String.format(AMI_ERROR, image.imageId());
      CLEANUP.warnAndDebugDetails(message, e);
      cleanupContext.onBuildCleanupError(this, build, message);
    }
  }