List of usage examples for com.amazonaws.services.ec2.model Snapshot getSnapshotId
public String getSnapshotId()
The ID of the snapshot.
From source file:virtualIT.java
License:Open Source License
private void createExtraSnapShot(int userId) throws Exception { /****************************************** * Creating Snap Shots before detaching volume */// w ww. j ava2s . c o m System.out.println("Creating Snap Shots before detaching volume\n"); String volumeId = mapUserExtraVolu.get(userId); CreateSnapshotResult snapRes = ec2 .createSnapshot(new CreateSnapshotRequest(volumeId, "Test snapshot" + userId)); Snapshot snap = snapRes.getSnapshot(); System.out.println("Snapshot request sent."); System.out.println("Waiting for snapshot to be created"); String snapState = snap.getState(); System.out.println("snapState is " + snapState); System.out.print("Waiting for snapshot to be created"); // Wait for the snapshot to be created while (snapState.equals("pending")) { Thread.sleep(500); System.out.print("."); DescribeSnapshotsResult describeSnapRes = ec2 .describeSnapshots(new DescribeSnapshotsRequest().withSnapshotIds(snap.getSnapshotId())); snapState = describeSnapRes.getSnapshots().get(0).getState(); } mapUserExtraSnap.put(userId, snap.getSnapshotId()); System.out.println("\nSnap shot Done."); return; }
From source file:virtualIT.java
License:Open Source License
private void createRootSnapShot(int userId) throws Exception { String volumeId = mapUserRootVol.get(userId); CreateSnapshotResult snapRes = ec2//from w ww.j a va 2s . co m .createSnapshot(new CreateSnapshotRequest(volumeId, "Test snapshot" + userId)); Snapshot snap = snapRes.getSnapshot(); System.out.println("Snapshot request sent."); System.out.println("Waiting for snapshot to be created"); String snapState = snap.getState(); System.out.println("snapState is " + snapState); System.out.print("Waiting for snapshot to be created"); // Wait for the snapshot to be created while (snapState.equals("pending")) { Thread.sleep(500); System.out.print("."); DescribeSnapshotsResult describeSnapRes = ec2 .describeSnapshots(new DescribeSnapshotsRequest().withSnapshotIds(snap.getSnapshotId())); snapState = describeSnapRes.getSnapshots().get(0).getState(); } mapUserRootSnap.put(userId, snap.getSnapshotId()); System.out.println("\nSnap shot Done."); return; }
From source file:com.axemblr.yab.YaB.java
License:Apache License
/** * De-register AMI and delete related snapshot *//*from www. ja v a 2 s. c o 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.eucalyptus.tests.awssdk.CloudCleaner.java
License:Open Source License
@Test public void clean() throws Exception { testInfo(this.getClass().getSimpleName()); getCloudInfo();// w w w .j a va 2s . c o m //Terminate All instances List<String> instancesToTerminate = new ArrayList<String>(); DescribeInstancesResult result = ec2.describeInstances(); List<Reservation> reservations = result.getReservations(); if (reservations.size() > 0) { print("Found instances to terminate"); for (Reservation reservation : reservations) { List<Instance> instances = reservation.getInstances(); for (Instance instance : instances) { print("Terminating: " + instance.getInstanceId()); instancesToTerminate.add(instance.getInstanceId()); } } TerminateInstancesRequest term = new TerminateInstancesRequest(); term.setInstanceIds(instancesToTerminate); ec2.terminateInstances(term); } else { print("No instances found"); } // delete all keypairs if (getKeyPairCount() > 0) { print("Found Keypairs to delete"); DescribeKeyPairsResult describeKeyPairsResult = ec2.describeKeyPairs(); for (KeyPairInfo keypair : describeKeyPairsResult.getKeyPairs()) { deleteKeyPair(keypair.getKeyName()); } } else { print("No keypairs found"); } // delete all groups except default group List<SecurityGroup> groups = describeSecurityGroups(); if (groups.size() > 1) { print("Found security groups to delete"); for (SecurityGroup group : groups) { if (!group.getGroupName().equals("default")) { deleteSecurityGroup(group.getGroupName()); } } } else { print("No Security Groups found (other than default)"); } // delete all policies List<ScalingPolicy> policies = describePolicies(); if (policies.size() > 0) { print("Found Policies to delete"); for (ScalingPolicy policy : policies) { deletePolicy(policy.getPolicyName()); } } else { print("No auto scaling policies found"); } // delete launch configs List<LaunchConfiguration> lcs = describeLaunchConfigs(); if (lcs.size() > 0) { print("Found Launch Configs to delete"); for (LaunchConfiguration lc : lcs) { deleteLaunchConfig(lc.getLaunchConfigurationName()); } } else { print("No launch configs found"); } // delete autoscaling groups List<AutoScalingGroup> asGroups = describeAutoScalingGroups(); if (asGroups.size() > 0) { print("Found Auto Scaling Groups to delete"); for (AutoScalingGroup asg : asGroups) { deleteAutoScalingGroup(asg.getAutoScalingGroupName(), true); } } else { print("No auto scaling groups found"); } // delete volumes List<Volume> volumes = ec2.describeVolumes().getVolumes(); if (volumes.size() > 0) { print("Found volumes to delete"); for (Volume vol : volumes) { deleteVolume(vol.getVolumeId()); } } else { print("No volumes found"); } //delete snapshots List<Snapshot> snapshots = ec2.describeSnapshots().getSnapshots(); if (snapshots.size() > 0) { print("Found snapshots to delete"); for (Snapshot snap : snapshots) { deleteSnapshot(snap.getSnapshotId()); } } else { print("No volumes found"); } }
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 . j ava2 s. c o m 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.EideticSubThreadMethods.java
@Override public void setResourceTags(AmazonEC2Client ec2Client, Snapshot snapshot, Collection<Tag> tags, Integer numRetries, Integer maxApiRequestsPerSecond, String uniqueAwsAccountIdentifier) { CreateTagsRequest createTagsRequest = new CreateTagsRequest().withResources(snapshot.getSnapshotId()) .withTags(tags);//from w w w. j a v a 2 s . co m EC2ClientMethods.createTags(ec2Client, createTagsRequest, numRetries, maxApiRequestsPerSecond, uniqueAwsAccountIdentifier); snapshot.setTags(tags); }
From source file:com.pearson.eidetic.driver.threads.EideticSubThreadMethods.java
public void deleteSnapshot(AmazonEC2Client ec2Client, Snapshot snapshot, Integer numRetries, Integer maxApiRequestsPerSecond, String uniqueAwsAccountIdentifier) { String snapshotId = snapshot.getSnapshotId(); DeleteSnapshotRequest deleteSnapshotRequest = new DeleteSnapshotRequest().withSnapshotId(snapshotId); EC2ClientMethods.deleteSnapshot(ec2Client, deleteSnapshotRequest, numRetries, maxApiRequestsPerSecond, uniqueAwsAccountIdentifier); }
From source file:com.pearson.eidetic.driver.threads.subthreads.ErrorChecker.java
private void loggerOuputSnapshotErrors(List<Snapshot> error_snapshots, Region region) { for (Snapshot snapshot : error_snapshots) { logger.info("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"SnapshotInErrorState\", region=\"" + region.toString() + ", snapshot_id=\"" + snapshot.getSnapshotId() + "\""); }// ww w . j a v a 2 s. c o m }
From source file:com.pearson.eidetic.driver.threads.subthreads.ErrorChecker.java
private void deleteSnapshotErrors(AmazonEC2Client ec2Client, List<Snapshot> error_snapshots) { for (Snapshot snapshot : error_snapshots) { try {/*from w w w . jav a 2 s. c om*/ deleteSnapshot(ec2Client, snapshot, numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); } catch (Exception e) { logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error\", Error=\"error deleting snapshot\", Snapshot_id=\"" + snapshot.getSnapshotId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); } } }
From source file:com.pearson.eidetic.driver.threads.subthreads.SnapshotChecker.java
public boolean snapshotCreation(AmazonEC2Client ec2Client, Volume vol, String period, Date date) { if ((date == null) || (ec2Client == null) || (vol == null) || (period == null)) { return false; }/*ww w .j a v a 2 s . co m*/ try { if ("day".equalsIgnoreCase(period)) { } else if ("hour".equalsIgnoreCase(period)) { } else if ("week".equalsIgnoreCase(period)) { } else if ("month".equalsIgnoreCase(period)) { } else { logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=Error, Error=\"Malformed Eidetic Tag\", Volume_id=\"" + vol.getVolumeId() + "\""); return false; } Collection<Tag> tags_volume = getResourceTags(vol); String volumeAttachmentInstance = "none"; try { volumeAttachmentInstance = vol.getAttachments().get(0).getInstanceId(); } catch (Exception e) { logger.debug("Volume not attached to instance: " + vol.getVolumeId()); } String description = period + "_snapshot " + vol.getVolumeId() + " by snapshot checker at " + date.toString() + ". Volume attached to " + volumeAttachmentInstance; Snapshot current_snap; try { current_snap = createSnapshotOfVolume(ec2Client, vol, description, numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); } catch (Exception e) { logger.info("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error\", Error=\"error creating snapshot from volume\", Volume_id=\"" + vol.getVolumeId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); return false; } try { setResourceTags(ec2Client, current_snap, tags_volume, numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); } catch (Exception e) { logger.info("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error\", Error=\"error adding tags to snapshot\", Snapshot_id=\"" + current_snap.getSnapshotId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); return false; } } catch (Exception e) { logger.info("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error\", Error=\"error in snapshotCreation\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); return false; } return true; }