private void removeTags(final String imageId) throws Exception { final ImageInfo image = Images.lookupImage(imageId); final String imageOwnerId = image.getOwnerUserId(); DeleteTagsTask task = new DeleteTagsTask( imageOwnerId, Lists.newArrayList(image.getDisplayName()), Lists.newArrayList(TAG_KEY_STATE, TAG_KEY_MESSAGE)); CheckedListenableFuture<Boolean> result = task.dispatch(); if (result.get()) {; } final List<VmInstance> instances = this.lookupInstances(imageId); for (final VmInstance instance : instances) { final String instanceId = instance.getInstanceId(); final String instanceOwnerId = instance.getOwnerUserId(); try { task = new DeleteTagsTask( instanceOwnerId, Lists.newArrayList(instanceId), Lists.newArrayList(TAG_KEY_STATE, TAG_KEY_MESSAGE)); result = task.dispatch(); if (result.get()) {; } } catch (final Exception ex) {; } } }
private void updateTags(final List<String> images) throws Exception { for (final String imageId : images) { try { final ImageInfo image = Images.lookupImage(imageId); final ImageMetadata.State imgState = image.getState(); final String taskId = ((MachineImageInfo) image).getImageConversionId(); if (ImageMetadata.State.pending_available.equals( imgState)) {; // do nothing for images not yet in conversion } else if (ImageMetadata.State.pending_conversion.equals(imgState)) { String message = ""; try { Optional<DiskImageConversionTask> task = conversionTaskCache.get(taskId); if (task.isPresent()) { message = task.get().getStatusMessage(); } } catch (final Exception ex) {; } // if needed, we can add messages as well; not sure yet if the messages are clear this.tagResources(imageId, "active", message); taggedImages.add(imageId); } else if (ImageMetadata.State.available.equals(imgState) && taggedImages.contains(imageId)) { try { this.removeTags(imageId); } catch (final Exception ex) {; } finally { taggedImages.remove(imageId); } } } catch (final Exception ex) { LOG.error("Failed to update tags for resources in conversion", ex); } } }
private void resetImagePendingAvailable(final String imageId, final String reason) { String taskMessage = ""; /// tag image and instances with proper message try { final ImageInfo image = Images.lookupImage(imageId); final String taskId = ((MachineImageInfo) image).getImageConversionId(); if (taskId != null) { conversionTaskCache.invalidate(taskId); Optional<DiskImageConversionTask> task = conversionTaskCache.get(taskId); if (task.isPresent()) taskMessage = task.get().getStatusMessage(); } final String tagMessage = reason != null ? reason : taskMessage; this.tagResources(imageId, "failed", tagMessage); } catch (final Exception ex) {; } finally { taggedImages.remove(imageId); } try (final TransactionResource db = Entities.transactionFor(ImageInfo.class)) { try { final ImageInfo entity = Entities.uniqueResult(Images.exampleWithImageId(imageId)); entity.setState(ImageMetadata.State.pending_available); entity.setImageFormat(ImageMetadata.ImageFormat.partitioned.name()); ((MachineImageInfo) entity).setImageConversionId(null); Entities.persist(entity); db.commit(); } catch (final Exception ex) { LOG.error("Failed to mark the image state available for conversion: " + imageId, ex); } } }
private void updateTags(final List<String> images) throws Exception { for (final String imageId : images) { try { final ImageInfo image = Images.lookupImage(imageId); final ImageMetadata.State imgState = image.getState(); final String taskId = ((MachineImageInfo) image).getImageConversionId(); if (ImageMetadata.State.pending_available.equals( imgState)) {; // do nothing for images not yet in conversion } else if (ImageMetadata.State.pending_conversion.equals(imgState)) { String message = ""; try { Optional<DiskImageConversionTask> task = conversionTaskCache.get(taskId); if (task.isPresent()) { message = task.get().getStatusMessage(); } } catch (final Exception ex) {; } // if needed, we can add messages as well; not sure yet if the messages are clear this.tagResources(imageId, "active", message); taggedImages.add(imageId); } else if (ImageMetadata.State.available.equals(imgState) && taggedImages.contains(imageId)) { try { this.removeTags(imageId); } catch (final Exception ex) {; } finally { taggedImages.remove(imageId); } } else if (ImageMetadata.State.failed.equals(imgState) && taggedImages.contains(imageId)) { String message = ""; try { conversionTaskCache.invalidate(taskId); Optional<DiskImageConversionTask> task = conversionTaskCache.get(taskId); if (task.isPresent()) message = task.get().getStatusMessage(); } catch (final Exception ex) {; } finally { taggedImages.remove(imageId); } this.tagResources(imageId, "failed", message); try (final TransactionResource db = Entities.transactionFor(ImageInfo.class)) { try { final ImageInfo entity = Entities.uniqueResult(Images.exampleWithImageId(imageId)); entity.setState(ImageMetadata.State.pending_available); entity.setImageFormat(ImageMetadata.ImageFormat.partitioned.name()); ((MachineImageInfo) entity).setImageConversionId(null); Entities.persist(entity); db.commit(); } catch (final Exception ex) { LOG.error("Failed to mark the image state available for conversion", ex); } } } } catch (final Exception ex) { LOG.error("Failed to update tags for resources in conversion", ex); } } }
private void tagResources(final String imageId, final String state, String statusMessage) throws Exception { final ImageInfo image = Images.lookupImage(imageId); final String imageOwnerId = image.getOwnerUserId(); final List<VmInstance> instances = this.lookupInstances(imageId); if (tagState.containsKey(imageId) && state.equals(tagState.get(imageId))) {; } else { resetTag(imageOwnerId, imageId, TAG_KEY_STATE, state); tagState.put(imageId, state); } for (final VmInstance instance : instances) { final String instanceId = instance.getInstanceId(); final String instanceOwnerId = instance.getOwnerUserId(); if (tagState.containsKey(instanceId) && state.equals(tagState.get(instanceId))) {; } else { resetTag(instanceOwnerId, instanceId, TAG_KEY_STATE, state); tagState.put(instanceId, state); } } if (statusMessage == null) statusMessage = ""; if (tagMessage.containsKey(imageId) && statusMessage.equals(tagMessage.get(imageId))) {; } else { resetTag(imageOwnerId, imageId, TAG_KEY_MESSAGE, statusMessage); tagMessage.put(imageId, statusMessage); } for (final VmInstance instance : instances) { final String instanceId = instance.getInstanceId(); final String instanceOwnerId = instance.getOwnerUserId(); if (tagMessage.containsKey(instanceId) && statusMessage.equals(tagMessage.get(instanceId))) {; } else { resetTag(instanceOwnerId, instanceId, TAG_KEY_MESSAGE, statusMessage); tagMessage.put(instanceId, statusMessage); } } }