List of usage examples for com.amazonaws.services.ec2.model Volume getAttachments
public java.util.List<VolumeAttachment> getAttachments()
Information about the volume attachments.
From source file:virtualIT.java
License:Open Source License
private void defaultVolumeRequest(int userId) { String instanceId = null;/* w w w . java2 s .co m*/ String volumeId; DescribeVolumesResult describeVolumeResult = ec2.describeVolumes(); List<Volume> volumeData = describeVolumeResult.getVolumes(); for (Volume item : volumeData) { volumeId = item.getVolumeId(); List<VolumeAttachment> volumeAttachment = item.getAttachments(); for (VolumeAttachment data : volumeAttachment) { instanceId = data.getInstanceId(); } //instanceIdList.add(instanceId); rootvolumeIdList.add(volumeId); mapUserRootVol.put(userId, volumeId); } }
From source file:com.cloudera.director.aws.ec2.ebs.EBSAllocator.java
License:Apache License
/** * Wait for the specified list of volumes to be attached within a timeout. * * @param volumeIds collection of volume ids to wait for * @return collection of volumes that were successfully attached * @throws InterruptedException if the operation is interrupted *//*from w ww . j a va2 s. c o m*/ private Collection<String> waitUntilVolumesAttached(Collection<String> volumeIds) throws InterruptedException { checkNotNull(volumeIds); if (volumeIds.isEmpty()) { LOG.info("No volumes are being attached, skipping wait"); return Collections.emptySet(); } Set<String> unattachedVolumes = Sets.newHashSet(volumeIds); Set<String> attachedVolumes = Sets.newHashSet(); LOG.info("Waiting for a maximum of {} seconds for volumes to be attached", attachTimeoutSeconds); Stopwatch watch = Stopwatch.createStarted(); while (watch.elapsed(TimeUnit.SECONDS) < attachTimeoutSeconds) { DescribeVolumesRequest volumeRequest = new DescribeVolumesRequest().withVolumeIds(unattachedVolumes); List<Volume> volumes = client.describeVolumes(volumeRequest).getVolumes(); for (Volume volume : volumes) { VolumeAttachment attachment = Iterables.getOnlyElement(volume.getAttachments()); VolumeAttachmentState state = VolumeAttachmentState.fromValue(attachment.getState()); if (state == VolumeAttachmentState.Attached) { unattachedVolumes.remove(volume.getVolumeId()); attachedVolumes.add(volume.getVolumeId()); } } if (unattachedVolumes.isEmpty()) { return attachedVolumes; } LOG.info("Waiting on {} out of {} volumes to be attached, next check in {} seconds", unattachedVolumes.size(), volumeIds.size(), WAIT_UNTIL_ATTACHED_INTERVAL_SECONDS); TimeUnit.SECONDS.sleep(WAIT_UNTIL_ATTACHED_INTERVAL_SECONDS); } LOG.error("Timed out while waiting for all volumes to be attached, {} out of {} volumes were attached", attachedVolumes.size(), volumeIds.size()); return attachedVolumes; }
From source file:com.dowdandassociates.gentoo.bootstrap.AbstractBootstrapInstanceInformationProvider.java
License:Apache License
protected void attachVolume(Optional<Instance> instance, Optional<Volume> volume) { if (instance.isPresent() && volume.isPresent()) { AttachVolumeResult result = ec2Client.attachVolume(new AttachVolumeRequest() .withInstanceId(instance.get().getInstanceId()).withVolumeId(volume.get().getVolumeId()) .withDevice(blockDeviceInformation.getSDevice())); try {//from w ww .ja v a2 s . co m DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest() .withVolumeIds(volume.get().getVolumeId()); String instanceId = instance.get().getInstanceId(); boolean waiting = true; do { log.info("Sleeping for " + sleep.get() + " ms"); Thread.sleep(sleep.get()); DescribeVolumesResult describeVolumesResult = ec2Client.describeVolumes(describeVolumesRequest); if (describeVolumesResult.getVolumes().isEmpty()) { return; } Volume bootstrapVolume = describeVolumesResult.getVolumes().get(0); List<VolumeAttachment> attachments = bootstrapVolume.getAttachments(); for (VolumeAttachment attachment : attachments) { if (!instanceId.equals(attachment.getInstanceId())) { continue; } String attachmentState = attachment.getState(); log.info("Attachment state = " + attachmentState); if ("attaching".equals(attachmentState)) { break; } if (!"attached".equals(attachmentState)) { return; } waiting = false; break; } } while (waiting); } catch (InterruptedException e) { } } }
From source file:com.netflix.simianarmy.aws.janitor.VolumeTaggingMonkey.java
License:Apache License
private void tagVolumesWithLatestAttachment(AWSClient awsClient) { List<Volume> volumes = awsClient.describeVolumes(); LOGGER.info(String.format("Trying to tag %d volumes for Janitor Monkey meta data.", volumes.size())); Date now = calendar.now().getTime(); for (Volume volume : volumes) { String owner = null, instanceId = null; Date lastDetachTime = null; List<VolumeAttachment> attachments = volume.getAttachments(); List<Tag> tags = volume.getTags(); // The volume can have a special tag is it does not want to be changed/tagged // by Janitor monkey. if ("donotmark".equals(getTagValue(JanitorMonkey.JANITOR_TAG, tags))) { LOGGER.info(/* w ww.j av a2s . com*/ String.format("The volume %s is tagged as not handled by Janitor", volume.getVolumeId())); continue; } Map<String, String> janitorMetadata = parseJanitorTag(tags); // finding the instance attached most recently. VolumeAttachment latest = null; for (VolumeAttachment attachment : attachments) { if (latest == null || latest.getAttachTime().before(attachment.getAttachTime())) { latest = attachment; } } if (latest != null) { instanceId = latest.getInstanceId(); owner = getOwnerEmail(instanceId, janitorMetadata, tags, awsClient); } if (latest == null || "detached".equals(latest.getState())) { if (janitorMetadata.get(JanitorMonkey.DETACH_TIME_TAG_KEY) == null) { // There is no attached instance and the last detached time is not set. // Use the current time as the last detached time. LOGGER.info(String.format("Setting the last detached time to %s for volume %s", now, volume.getVolumeId())); lastDetachTime = now; } else { LOGGER.debug(String.format("The volume %s was already marked as detached at time %s", volume.getVolumeId(), janitorMetadata.get(JanitorMonkey.DETACH_TIME_TAG_KEY))); } } else { // The volume is currently attached to an instance lastDetachTime = null; } String existingOwner = janitorMetadata.get(JanitorMonkey.OWNER_TAG_KEY); if (owner == null && existingOwner != null) { // Save the current owner in the tag when we are not able to find a owner. owner = existingOwner; } if (needsUpdate(janitorMetadata, owner, instanceId, lastDetachTime)) { Event evt = updateJanitorMetaTag(volume, instanceId, owner, lastDetachTime, awsClient); if (evt != null) { context().recorder().recordEvent(evt); } } } }
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; }/* www . java 2s.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; }
From source file:com.pearson.eidetic.driver.threads.subthreads.SnapshotVolumeNoTime.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.ja va 2 s .com 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("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Volume not attached to instance: " + vol.getVolumeId()); } String description = period + "_snapshot " + vol.getVolumeId() + " by Eidetic 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.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event\"Error\", Error=\"error adding tags to snapshot\", Snapshot_id=\"" + current_snap.getVolumeId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); return false; } } catch (Exception e) { logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error, Error=\"error in snapshotCreation\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); return false; } return true; }
From source file:com.pearson.eidetic.driver.threads.subthreads.SnapshotVolumeSync.java
public boolean snapshotCreation(AmazonEC2Client ec2Client, Volume vol, Date date) { if ((date == null) || (ec2Client == null) || (vol == null)) { return false; }//from w w w . j a v a2 s. co m try { 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; if (Validator_) { description = "sync_snapshot " + vol.getVolumeId() + " called by Eidetic Validator Synchronizer at " + date.toString() + ". Volume attached to " + volumeAttachmentInstance; } else { description = "sync_snapshot " + vol.getVolumeId() + " by Eidetic Synchronizer at " + date.toString() + ". Volume attached to " + volumeAttachmentInstance; } Snapshot current_snap; try { current_snap = createSnapshotOfVolume(ec2Client, vol, description, numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); } catch (Exception e) { logger.error("awsAccountId=\"" + uniqueAwsAccountIdentifier_ + "\",Event=Error, Error=\"Malformed Eidetic Tag\", 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.error("awsAccountId=\"" + uniqueAwsAccountIdentifier_ + "\",Event\"Error\", Error=\"error adding tags to snapshot\", Snapshot_id=\"" + current_snap.getVolumeId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); return false; } } catch (Exception e) { logger.error("awsAccountId=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error, Error=\"error in snapshotCreation\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); return false; } return true; }
From source file:com.pearson.eidetic.driver.threads.subthreads.SnapshotVolumeSyncValidator.java
public boolean snapshotCreation(AmazonEC2Client ec2Client, Volume vol, Date date) { if ((date == null) || (ec2Client == null) || (vol == null)) { return false; }//from www . ja v a2 s. c om try { 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 = "sync_snapshot " + vol.getVolumeId() + " by Eidetic Synchronizer at " + date.toString() + ". Volume attached to " + volumeAttachmentInstance; Snapshot current_snap; try { current_snap = createSnapshotOfVolume(ec2Client, vol, description, numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); } catch (Exception e) { logger.error("awsAccountId=\"" + uniqueAwsAccountIdentifier_ + "\",Event=Error, Error=\"Malformed Eidetic Tag\", 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.error("awsAccountId=\"" + uniqueAwsAccountIdentifier_ + "\",Event\"Error\", Error=\"error adding tags to snapshot\", Snapshot_id=\"" + current_snap.getVolumeId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); return false; } } catch (Exception e) { logger.error("awsAccountId=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error, Error=\"error in snapshotCreation\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); return false; } return true; }
From source file:com.pearson.eidetic.driver.threads.subthreads.SnapshotVolumeTime.java
public boolean snapshotCreation(AmazonEC2Client ec2Client, Volume vol, String period, Date date) { if ((date == null) | (ec2Client == null) | (vol == null) | (period == null)) { return false; }/*from ww w. java2 s . c om*/ 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 Eidetic at " + date.toString() + ". Volume attached to " + volumeAttachmentInstance; Snapshot current_snap; try { current_snap = createSnapshotOfVolume(ec2Client, vol, description, numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); } catch (Exception e) { logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=Error, Error=\"Malformed Eidetic Tag\", 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.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event\"Error\", Error=\"error adding tags to snapshot\", Snapshot_id=\"" + current_snap.getVolumeId() + "\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); return false; } } catch (Exception e) { logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event=\"Error, Error=\"error in snapshotCreation\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); return false; } return true; }
From source file:com.pearson.eidetic.driver.threads.subthreads.TagChecker.java
private List<Volume> checkVolumes(AmazonEC2Client ec2Client, List<Instance> instances) { List<Volume> volumesWithNoTags = new ArrayList(); /*/*from ww w .ja v a2 s . c o m*/ Here is what we have: Instance: -instance.id -> String -instance.blockdevicemapping -> InstanceBlockDeviceMapping -BlockDeviceMapping: --BlockDeviceMapping.deviceName -> String == (/dev/xvdk) --BlockDeviceMapping.getEbs -> EbsInstanceBlockDevice --EbsInstanceBLockDevice: ---EbsInstanceBLockDevice.id - String == (volume.id) For instances Set<HashMap<instance.id,HashMap<deviceName,volume.id>>> Volume: -volume.id -> String -volume.getAttachments -> volumeAttachments -volumeAttachments: --getDevice -> String == (/dev/xvdk) --getInstanceId -> String == instance.id For vols Set<HashMap<instance.id,HashMap<deviceName,volume.id>>> If anything remians in the instance set */ List<Volume> volumes = getEideticVolumes(ec2Client); //if (volumes.isEmpty()) { //return volumesWithNoTags; //} //Set<HashMap<instance.id,HashMap<deviceName,volume.id>>> Set<HashMap<String, HashMap<String, String>>> volumeSet = new HashSet(); for (Volume volume : volumes) { String volumeId = volume.getVolumeId(); List<VolumeAttachment> attachments = volume.getAttachments(); for (VolumeAttachment attach : attachments) { HashMap<String, HashMap<String, String>> setMember = new HashMap(); HashMap<String, String> internalHash = new HashMap(); if (volumeId == null || attach.getDevice() == null || attach.getInstanceId() == null) { continue; } internalHash.put(attach.getDevice(), volumeId); setMember.put(attach.getInstanceId(), internalHash); volumeSet.add(setMember); } } //Set<HashMap<instance.id,HashMap<deviceName,volume.id>>> Set<HashMap<String, HashMap<String, String>>> instanceSet = new HashSet(); for (Instance instance : instances) { String instanceId = instance.getInstanceId(); List<InstanceBlockDeviceMapping> blocks = instance.getBlockDeviceMappings(); List<Tag> tags = instance.getTags(); Tag tagz = null; for (Tag t : tags) { if (t.getKey().equalsIgnoreCase("Data")) { tagz = t; break; } } if (tagz == null) { continue; } String[] stringtags = tagz.getValue().split(","); for (String tag : stringtags) { String deviceName; String volumeId; for (InstanceBlockDeviceMapping instanceBlockDeviceMapping : blocks) { deviceName = instanceBlockDeviceMapping.getDeviceName(); if (!deviceName.equalsIgnoreCase(tag)) { continue; } volumeId = instanceBlockDeviceMapping.getEbs().getVolumeId(); HashMap<String, HashMap<String, String>> setMember = new HashMap(); HashMap<String, String> internalHash = new HashMap(); if (volumeId == null | volumeId == null) { continue; } internalHash.put(deviceName, volumeId); setMember.put(instanceId, internalHash); instanceSet.add(setMember); } } } if (instanceSet.isEmpty()) { return volumesWithNoTags; } //Need to validate instanceSet.removeAll(volumeSet); if (instanceSet.isEmpty()) { return volumesWithNoTags; } //Instance keys, to get volumeIds Set<String> volumeIds = new HashSet(); for (HashMap<String, HashMap<String, String>> i : instanceSet) { Set<String> curSet = i.keySet(); for (String s : curSet) { Set<String> dvns = i.get(s).keySet(); for (String dvn : dvns) { if (!volumeIds.contains(dvn)) { volumeIds.add(i.get(s).get(dvn)); } } } } for (String i : volumeIds) { DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest().withVolumeIds(i); DescribeVolumesResult describeVolumeResult = EC2ClientMethods.describeVolumes(ec2Client, describeVolumesRequest, numRetries_, maxApiRequestsPerSecond_, uniqueAwsAccountIdentifier_); Volume volume = null; try { volume = describeVolumeResult.getVolumes().get(0); } catch (Exception e) { logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier_ + "\",Event\"Error\", Error=\"volume id does not exist\", stacktrace=\"" + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e) + "\""); } volumesWithNoTags.add(volume); } return volumesWithNoTags; }