List of usage examples for com.amazonaws.services.ec2.model Snapshot getDescription
public String getDescription()
The description for the snapshot.
From source file:com.axemblr.yab.YaB.java
License:Apache License
/** * De-register AMI and delete related snapshot *//*from w w w. j av a 2 s .co m*/ public void deleteImageAndRelatedSnapshot(String imageId) { client.deregisterImage(new DeregisterImageRequest().withImageId(imageId)); final String pattern = "for " + imageId + " from vol-"; DescribeSnapshotsResult result = client.describeSnapshots(new DescribeSnapshotsRequest()); for (Snapshot candidate : result.getSnapshots()) { if (candidate.getDescription().contains(pattern)) { client.deleteSnapshot(new DeleteSnapshotRequest().withSnapshotId(candidate.getSnapshotId())); } } }
From source file:com.netflix.simianarmy.aws.janitor.crawler.EBSSnapshotJanitorCrawler.java
License:Apache License
private List<Resource> getSnapshotResources(String... snapshotIds) { refreshSnapshotToAMIs();//from w w w .ja va 2 s .com List<Resource> resources = new LinkedList<Resource>(); AWSClient awsClient = getAWSClient(); for (Snapshot snapshot : awsClient.describeSnapshots(snapshotIds)) { Resource snapshotResource = new AWSResource().withId(snapshot.getSnapshotId()) .withRegion(getAWSClient().region()).withResourceType(AWSResourceType.EBS_SNAPSHOT) .withLaunchTime(snapshot.getStartTime()).withDescription(snapshot.getDescription()); for (Tag tag : snapshot.getTags()) { LOGGER.debug(String.format("Adding tag %s = %s to resource %s", tag.getKey(), tag.getValue(), snapshotResource.getId())); snapshotResource.setTag(tag.getKey(), tag.getValue()); } snapshotResource.setOwnerEmail(getOwnerEmailForResource(snapshotResource)); ((AWSResource) snapshotResource).setAWSResourceState(snapshot.getState()); Collection<String> amis = snapshotToAMIs.get(snapshotResource.getId()); if (amis != null) { snapshotResource.setAdditionalField(SNAPSHOT_FIELD_AMIS, StringUtils.join(amis, ",")); } resources.add(snapshotResource); } return resources; }
From source file:com.pearson.eidetic.driver.threads.MonitorSnapshotVolumeTime.java
public boolean snapshotDecision(AmazonEC2Client ec2Client, Volume vol, String period) { if ((ec2Client == null) || (vol == null) || (period == null)) { return false; }//from w ww.ja va 2s . c o m try { List<Snapshot> int_snapshots = getAllSnapshotsOfVolume(ec2Client, vol, ApplicationConfiguration.getAwsCallRetryAttempts(), awsAccount_.getMaxApiRequestsPerSecond(), awsAccount_.getUniqueAwsAccountIdentifier()); List<Snapshot> comparelist = new ArrayList(); for (Snapshot snapshot : int_snapshots) { String desc = snapshot.getDescription(); if ("week".equalsIgnoreCase(period) && desc.startsWith("week_snapshot")) { comparelist.add(snapshot); } else if ("day".equalsIgnoreCase(period) && desc.startsWith("day_snapshot")) { if (!desc.contains("snapshot checker")) { comparelist.add(snapshot); } } else if ("hour".equalsIgnoreCase(period) && desc.startsWith("hour_snapshot")) { comparelist.add(snapshot); } else if ("month".equalsIgnoreCase(period) && desc.startsWith("month_snapshot")) { comparelist.add(snapshot); } } List<Snapshot> sortedCompareList = new ArrayList<>(comparelist); sortSnapshotsByDate(sortedCompareList); int hours = getHoursBetweenNowAndNewestSnapshot(sortedCompareList); int days = getDaysBetweenNowAndNewestSnapshot(sortedCompareList); if (("week".equalsIgnoreCase(period) && days < 0) || ("week".equalsIgnoreCase(period) && days >= 7)) { } else if (("hour".equalsIgnoreCase(period) && hours < 0) || ("hour".equalsIgnoreCase(period) && hours >= 1)) { } else if (("day".equalsIgnoreCase(period) && days < 0) || ("day".equalsIgnoreCase(period) && days >= 1)) { } else if (("month".equalsIgnoreCase(period) && days < 0) || ("month".equalsIgnoreCase(period) && days >= 30)) { } else { return false; } } catch (Exception e) { logger.info("awsAccountNickname=\"" + awsAccount_.getUniqueAwsAccountIdentifier() + "\",Event=\"Error\", Error=\"error in snapshotDecision\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); return false; } return true; }
From source file:com.pearson.eidetic.driver.threads.subthreads.SnapshotChecker.java
public boolean snapshotDecision(AmazonEC2Client ec2Client, Volume vol, String period) { if ((ec2Client == null) || (vol == null) || (period == null)) { return false; }/*from ww w .ja v a2s . c o m*/ try { List<Snapshot> int_snapshots = getAllSnapshotsOfVolume(ec2Client, vol, numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); List<Snapshot> comparelist = new ArrayList(); for (Snapshot snapshot : int_snapshots) { String sndesc = snapshot.getDescription(); if ("week".equalsIgnoreCase(period) && sndesc.startsWith("week_snapshot")) { comparelist.add(snapshot); } else if ("day".equalsIgnoreCase(period) && sndesc.startsWith("day_snapshot")) { comparelist.add(snapshot); } else if ("hour".equalsIgnoreCase(period) && sndesc.startsWith("hour_snapshot")) { comparelist.add(snapshot); } else if ("month".equalsIgnoreCase(period) && sndesc.startsWith("month_snapshot")) { comparelist.add(snapshot); } } List<Snapshot> sortedCompareList = new ArrayList<>(comparelist); sortSnapshotsByDate(sortedCompareList); int hours = getHoursBetweenNowAndNewestSnapshot(sortedCompareList); int days = getDaysBetweenNowAndNewestSnapshot(sortedCompareList); if (("week".equalsIgnoreCase(period) && days < 0) || ("week".equalsIgnoreCase(period) && days >= 14)) { } else if (("hour".equalsIgnoreCase(period) && hours < 0) || ("hour".equalsIgnoreCase(period) && hours >= 2)) { } else if (("day".equalsIgnoreCase(period) && days < 0) || ("day".equalsIgnoreCase(period) && hours >= 25)) { } else if (("month".equalsIgnoreCase(period) && days < 0) || ("month".equalsIgnoreCase(period) && days >= 60)) { } else { return false; } } catch (Exception e) { logger.info("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error\", Error=\"error in snapshotDecision\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); return false; } return true; }
From source file:com.pearson.eidetic.driver.threads.subthreads.SnapshotChecker.java
public boolean snapshotDeletion(AmazonEC2Client ec2Client, Volume vol, String period, Integer keep) { if ((keep == null) || (ec2Client == null) || (vol == null) || (period == null)) { return false; }// w ww .j av a 2s.co m try { List<Snapshot> del_snapshots = getAllSnapshotsOfVolume(ec2Client, vol, numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); List<Snapshot> deletelist = new ArrayList(); for (Snapshot snapshot : del_snapshots) { String desc = snapshot.getDescription(); if ("week".equals(period) && desc.startsWith("week_snapshot")) { deletelist.add(snapshot); } else if ("day".equals(period) && desc.startsWith("day_snapshot")) { deletelist.add(snapshot); } else if ("hour".equals(period) && desc.startsWith("hour_snapshot")) { deletelist.add(snapshot); } else if ("month".equals(period) && desc.startsWith("month_snapshot")) { deletelist.add(snapshot); } } List<Snapshot> sortedDeleteList = new ArrayList<>(deletelist); sortSnapshotsByDate(sortedDeleteList); int delta = sortedDeleteList.size() - keep; for (int i : range(0, delta - 1)) { try { deleteSnapshot(ec2Client, sortedDeleteList.get(i), numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); } catch (Exception e) { logger.info("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error\", Error=\"error deleting snapshot\", Snapshot_id=\"" + sortedDeleteList.get(i).getSnapshotId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); } } } catch (Exception e) { logger.info("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error\", Error=\"error in snapshotDeletion\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); } return true; }
From source file:com.pearson.eidetic.driver.threads.subthreads.SnapshotVolumeNoTime.java
public boolean snapshotDecision(AmazonEC2Client ec2Client, Volume vol, String period) { if ((ec2Client == null) || (vol == null) || (period == null)) { return false; }/*from www. j a v a 2 s . co m*/ try { List<Snapshot> int_snapshots = getAllSnapshotsOfVolume(ec2Client, vol, numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); List<Snapshot> comparelist = new ArrayList(); for (Snapshot snapshot : int_snapshots) { String sndesc = snapshot.getDescription(); if ("week".equalsIgnoreCase(period) && sndesc.startsWith("week_snapshot")) { comparelist.add(snapshot); } else if ("day".equalsIgnoreCase(period) && sndesc.startsWith("day_snapshot")) { comparelist.add(snapshot); } else if ("hour".equalsIgnoreCase(period) && sndesc.startsWith("hour_snapshot")) { comparelist.add(snapshot); } else if ("month".equalsIgnoreCase(period) && sndesc.startsWith("month_snapshot")) { comparelist.add(snapshot); } } List<Snapshot> sortedCompareList = new ArrayList<>(comparelist); sortSnapshotsByDate(sortedCompareList); int hours = getHoursBetweenNowAndNewestSnapshot(sortedCompareList); int days = getDaysBetweenNowAndNewestSnapshot(sortedCompareList); if (("week".equalsIgnoreCase(period) && days < 0) || ("week".equalsIgnoreCase(period) && days >= 7)) { } else if (("hour".equalsIgnoreCase(period) && hours < 0) || ("hour".equalsIgnoreCase(period) && hours >= 1)) { } else if (("day".equalsIgnoreCase(period) && days < 0) || ("day".equalsIgnoreCase(period) && days >= 1)) { } else if (("month".equalsIgnoreCase(period) && days < 0) || ("month".equalsIgnoreCase(period) && days >= 30)) { } else { return false; } } catch (Exception e) { logger.info("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error\", Error=\"error in snapshotDecision\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); return false; } return true; }
From source file:com.pearson.eidetic.driver.threads.subthreads.SnapshotVolumeNoTime.java
public boolean snapshotDeletion(AmazonEC2Client ec2Client, Volume vol, String period, Integer keep) { if ((keep == null) || (ec2Client == null) || (vol == null) || (period == null)) { return false; }//from w w w.ja v a 2 s . c o m try { List<Snapshot> del_snapshots = getAllSnapshotsOfVolume(ec2Client, vol, numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); List<Snapshot> deletelist = new ArrayList(); for (Snapshot snapshot : del_snapshots) { String desc = snapshot.getDescription(); if ("week".equals(period) && desc.startsWith("week_snapshot")) { deletelist.add(snapshot); } else if ("day".equals(period) && desc.startsWith("day_snapshot")) { deletelist.add(snapshot); } else if ("hour".equals(period) && desc.startsWith("hour_snapshot")) { deletelist.add(snapshot); } else if ("month".equals(period) && desc.startsWith("month_snapshot")) { deletelist.add(snapshot); } } List<Snapshot> sortedDeleteList = new ArrayList<>(deletelist); sortSnapshotsByDate(sortedDeleteList); int delta = sortedDeleteList.size() - keep; for (int i : range(0, delta - 1)) { try { deleteSnapshot(ec2Client, sortedDeleteList.get(i), numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); } catch (Exception e) { logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error\", Error=\"error deleting snapshot\", Snapshot_id=\"" + sortedDeleteList.get(i).getSnapshotId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); } } } catch (Exception e) { logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error\", Error=\"error in snapshotDeletion\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); } return true; }
From source file:com.pearson.eidetic.driver.threads.subthreads.SnapshotVolumeSync.java
public boolean snapshotDeletion(AmazonEC2Client ec2Client, Volume vol, Integer keep) { if ((keep == null) || (ec2Client == null) || (vol == null)) { return false; }/*www .ja v a 2 s .co m*/ try { List<Snapshot> del_snapshots = getAllSnapshotsOfVolume(ec2Client, vol, numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); List<Snapshot> deletelist = new ArrayList(); for (Snapshot snapshot : del_snapshots) { String desc = snapshot.getDescription(); if (desc.startsWith("sync_snapshot")) { deletelist.add(snapshot); } } List<Snapshot> sortedDeleteList = new ArrayList<>(deletelist); sortSnapshotsByDate(sortedDeleteList); int delta = sortedDeleteList.size() - keep; for (int i : range(0, delta - 1)) { try { deleteSnapshot(ec2Client, sortedDeleteList.get(i), numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); } catch (Exception e) { logger.error("awsAccountId=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error\", Error=\"error deleting snapshot\", Snapshot_id=\"" + sortedDeleteList.get(i).getSnapshotId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); } } } catch (Exception e) { logger.error("awsAccountId=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error\", Error=\"error in snapshotDeletion\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); } return true; }
From source file:com.pearson.eidetic.driver.threads.subthreads.SnapshotVolumeSyncValidator.java
public boolean snapshotDecision(AmazonEC2Client ec2Client, Volume vol, Integer createAfter) { if ((ec2Client == null) || (vol == null) || (createAfter == null)) { return false; }//from w w w .j a v a 2 s . c om try { List<Snapshot> int_snapshots = getAllSnapshotsOfVolume(ec2Client, vol, numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); List<Snapshot> comparelist = new ArrayList(); for (Snapshot snapshot : int_snapshots) { String sndesc = snapshot.getDescription(); if (sndesc.startsWith("sync_snapshot")) { comparelist.add(snapshot); } } List<Snapshot> sortedCompareList = new ArrayList<>(comparelist); sortSnapshotsByDate(sortedCompareList); int hours = getHoursBetweenNowAndNewestSnapshot(sortedCompareList); int days = getDaysBetweenNowAndNewestSnapshot(sortedCompareList); if (hours <= createAfter) { return false; } } catch (Exception e) { logger.info("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error\", Error=\"error in snapshotDecision\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); return false; } return true; }
From source file:fr.xebia.demo.amazon.aws.AmazonAwsShutdowner.java
License:Apache License
public void test() { String ownerId = "self"; boolean dryRun = true; // RETRIEVE TAGS Map<String, Map<String, String>> tagsByResourceId = new MapMaker() .makeComputingMap(new Function<String, Map<String, String>>() { @Override/*w ww .j av a 2s .c o m*/ public Map<String, String> apply(String input) { return Maps.newHashMap(); } }); for (TagDescription tagDescription : ec2.describeTags().getTags()) { tagsByResourceId.get(tagDescription.getResourceId()).put(tagDescription.getKey(), tagDescription.getValue()); } // RDS INSTANCEs for (DBInstance dbInstance : rds.describeDBInstances().getDBInstances()) { Map<String, String> instanceTags = tagsByResourceId.get(dbInstance.getDBInstanceIdentifier()); logger.debug("Tags for " + dbInstance + ": " + instanceTags); } // EC2 INSTANCES List<Instance> instancesAlreadyNotStarted = Lists.newArrayList(); List<Instance> instancesToStop = Lists.newArrayList(); List<Instance> instancesToTerminate = Lists.newArrayList(); List<Instance> instancesToKeepUnchanged = Lists.newArrayList(); for (Reservation reservation : ec2.describeInstances().getReservations()) { for (Instance instance : reservation.getInstances()) { Map<String, String> instanceTags = tagsByResourceId.get(instance.getInstanceId()); logger.debug("Tags for {}: {}", instance, instanceTags); if ("terminated".equals(instance.getState().getName())) { instancesToKeepUnchanged.add(instance); } else if (instanceTags.containsKey(TAG_DO_NOT_STOP)) { instancesToKeepUnchanged.add(instance); } else if (instanceTags.containsKey(TAG_DO_NOT_TERMINATE)) { if ("started".equals(instance.getState().getName())) { instancesToStop.add(instance); } else { instancesAlreadyNotStarted.add(instance); } } else { instancesToTerminate.add(instance); } } } System.out.println("EC2 INSTANCES"); if (dryRun) { System.out.println("DRY RUN Terminate:" + instancesToTerminate); System.out.println("DRY RUN Stop:" + instancesToStop); System.out.println("DRY RUN No need to stop:" + instancesAlreadyNotStarted); System.out.println("DRY RUN Keep unchanged:" + instancesToKeepUnchanged); } else { System.out.println("Terminate:" + instancesToTerminate); if (!instancesToTerminate.isEmpty()) { ec2.terminateInstances(new TerminateInstancesRequest( Lists.transform(instancesToTerminate, TO_INSTANCE_ID_FUNCTION))); } System.out.println("Stop:" + instancesToStop); if (!instancesToStop.isEmpty()) { ec2.stopInstances( new StopInstancesRequest(Lists.transform(instancesToStop, TO_INSTANCE_ID_FUNCTION))); } System.out.println("No need to stop:" + instancesAlreadyNotStarted); System.out.println("Keep unchanged:" + instancesToKeepUnchanged); } // AMIs System.out.println("AMIs"); List<Image> imagesToDeRegister = Lists.newArrayList(); List<Image> imagesToKeep = Lists.newArrayList(); for (Image image : ec2.describeImages(new DescribeImagesRequest().withOwners(ownerId)).getImages()) { Map<String, String> imageTags = tagsByResourceId.get(image.getImageId()); logger.debug("Tags for {}: {}", image, imageTags); if (imageTags.containsKey(TAG_DO_NOT_DEREGISTER)) { imagesToKeep.add(image); } else { imagesToDeRegister.add(image); } } if (dryRun) { System.out.println("DRY RUN Deregister:" + imagesToDeRegister); System.out.println("DRY RUN Keep:" + imagesToKeep); } else { System.out.println("Deregister:" + imagesToDeRegister); for (Image image : imagesToDeRegister) { ec2.deregisterImage(new DeregisterImageRequest(image.getImageId())); } System.out.println("Keep:" + imagesToKeep); } List<String> imageIdsToKeep = Lists.transform(imagesToKeep, TO_IMAGE_ID_FUNCTION); // SNAPSHOTS System.out.println("SNAPSHOTs"); for (Snapshot snapshot : ec2.describeSnapshots(new DescribeSnapshotsRequest().withOwnerIds(ownerId)) .getSnapshots()) { if (snapshot.getDescription().contains("Created by CreateImage")) { boolean associatedWithAnImageToKeep = false; for (String imageIdToKeep : imageIdsToKeep) { if (snapshot.getDescription().contains(imageIdToKeep)) { associatedWithAnImageToKeep = true; break; } } if (associatedWithAnImageToKeep) { System.out.println("Keep: " + snapshot); } else { if (dryRun) { System.out.println("DRY RUN delete: " + snapshot); } else { System.out.println("Delete: " + snapshot); ec2.deleteSnapshot(new DeleteSnapshotRequest(snapshot.getSnapshotId())); } } } } // ELASTIC LOAD BALANCERs // no tags on elb }