use of com.amazonaws.services.ec2.model.Volume in project SimianArmy by Netflix.
the class VolumeTaggingMonkey method tagVolumesWithLatestAttachment.
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();
// by Janitor monkey.
if ("donotmark".equals(getTagValue(JanitorMonkey.JANITOR_TAG, tags))) {
LOGGER.info(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(BasicSimianArmyContext.GLOBAL_OWNER_TAGKEY);
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);
}
}
}
}
use of com.amazonaws.services.ec2.model.Volume in project SimianArmy by Netflix.
the class AWSClient method deleteVolume.
/** {@inheritDoc} */
@Override
public void deleteVolume(String volumeId) {
Validate.notEmpty(volumeId);
LOGGER.info(String.format("Deleting volume %s in region %s.", volumeId, region));
AmazonEC2 ec2Client = ec2Client();
DeleteVolumeRequest request = new DeleteVolumeRequest().withVolumeId(volumeId);
ec2Client.deleteVolume(request);
}
use of com.amazonaws.services.ec2.model.Volume in project SimianArmy by Netflix.
the class AWSClient method describeVolumes.
/**
* Describe a set of specific EBS volumes.
*
* @param volumeIds the volume ids
* @return the volumes
*/
public List<Volume> describeVolumes(String... volumeIds) {
if (volumeIds == null || volumeIds.length == 0) {
LOGGER.info(String.format("Getting all EBS volumes in region %s.", region));
} else {
LOGGER.info(String.format("Getting EBS volumes for %d ids in region %s.", volumeIds.length, region));
}
AmazonEC2 ec2Client = ec2Client();
DescribeVolumesRequest request = new DescribeVolumesRequest();
if (volumeIds != null) {
request.setVolumeIds(Arrays.asList(volumeIds));
}
DescribeVolumesResult result = ec2Client.describeVolumes(request);
List<Volume> volumes = result.getVolumes();
LOGGER.info(String.format("Got %d EBS volumes in region %s.", volumes.size(), region));
return volumes;
}
use of com.amazonaws.services.ec2.model.Volume in project SimianArmy by Netflix.
the class TestEBSVolumeJanitorCrawler method verifyVolumeList.
private void verifyVolumeList(List<Resource> resources, List<Volume> volumeList, Date createTime) {
Assert.assertEquals(resources.size(), volumeList.size());
for (int i = 0; i < resources.size(); i++) {
Volume volume = volumeList.get(i);
verifyVolume(resources.get(i), volume.getVolumeId(), createTime);
}
}
use of com.amazonaws.services.ec2.model.Volume in project GNS by MobilityFirst.
the class AWSEC2 method createAndAttachVolume.
/**
* Creates a volume and attaches and mounts it on the instance at the specified mount point.
*
* @param ec2
* @param instanceId
* @param mountPoint
* @return the id of the volume
*/
public static String createAndAttachVolume(AmazonEC2 ec2, String instanceId, String mountPoint) {
// ATTACH A VOLUME
Instance instance = findInstance(ec2, instanceId);
String zone = instance.getPlacement().getAvailabilityZone();
CreateVolumeRequest newVolumeRequest = new CreateVolumeRequest();
//1.0GB
newVolumeRequest.setSize(1);
// set its available zone, it may change.
newVolumeRequest.setAvailabilityZone(zone);
CreateVolumeResult volumeResult = ec2.createVolume(newVolumeRequest);
Volume v1 = volumeResult.getVolume();
String volumeID = v1.getVolumeId();
//begin to attach the volume to instance
AttachVolumeRequest avr = new AttachVolumeRequest();
avr.withInstanceId(instanceId);
avr.withVolumeId(volumeID);
//mount it
avr.withDevice(mountPoint);
ec2.attachVolume(avr);
System.out.println("EBS volume has been attached and the volume ID is: " + volumeID);
return (volumeID);
}
Aggregations