Skip to content

Commit

Permalink
Merge pull request #37 from binod33/master
Browse files Browse the repository at this point in the history
fix for imaging windows server
  • Loading branch information
greese committed May 8, 2013
2 parents 9e41e42 + 59f5360 commit 7cecfe9
Showing 1 changed file with 57 additions and 58 deletions.
115 changes: 57 additions & 58 deletions src/main/java/org/dasein/cloud/aws/compute/AMI.java
Expand Up @@ -94,7 +94,7 @@ public void addPublicShare(@Nonnull String providerImageId) throws CloudExceptio
}

private @Nonnull MachineImage captureImage(@Nonnull ProviderContext ctx, @Nonnull ImageCreateOptions options, @Nullable AsynchronousTask<MachineImage> task) throws CloudException, InternalException {
APITrace.begin(provider, "Image.captureImage");
APITrace.begin(provider, "captureImage");
try {
if( task != null ) {
task.setStartTime(System.currentTimeMillis());
Expand All @@ -110,12 +110,21 @@ public void addPublicShare(@Nonnull String providerImageId) throws CloudExceptio
if( vm == null || VmState.TERMINATED.equals(vm.getCurrentState()) ) {
break;
}
if( !vm.isPersistent() ) {
throw new OperationNotSupportedException("You cannot capture instance-backed virtual machines");
}

if( VmState.RUNNING.equals(vm.getCurrentState()) || VmState.STOPPED.equals(vm.getCurrentState()) ) {
break;
}

if( !vm.isPersistent() ) {
if( vm.getPlatform().isWindows() ) {
String bucket = provider.getStorageServices().getBlobStoreSupport().createBucket("dsnwin" + (System.currentTimeMillis() % 10000), true).getBucketName();
if( bucket == null ) {
throw new CloudException("There is no bucket");
}
return captureWindows(provider.getContext(), options, bucket, task);
}
}

}
catch( Throwable ignore ) {
// ignore
Expand All @@ -130,68 +139,58 @@ public void addPublicShare(@Nonnull String providerImageId) throws CloudExceptio
int attempts = 5;

while( attempts > 0 ) {
if( vm.getPlatform().isWindows() ) {
String bucket = provider.getStorageServices().getBlobStoreSupport().createBucket("dsnwin" + (System.currentTimeMillis() % 10000), true).getBucketName();
Map<String,String> parameters = provider.getStandardParameters(provider.getContext(), EC2Method.CREATE_IMAGE);
NodeList blocks;
EC2Method method;
Document doc;

if( bucket == null ) {
throw new CloudException("There is no bucket");
}
return captureWindows(ctx, options, bucket, task);
parameters.put("InstanceId", options.getVirtualMachineId());
parameters.put("Name", options.getName());
parameters.put("Description", options.getDescription());
method = new EC2Method(provider, provider.getEc2Url(), parameters);
try {
doc = method.invoke();
}
else {
Map<String,String> parameters = provider.getStandardParameters(provider.getContext(), EC2Method.CREATE_IMAGE);
NodeList blocks;
EC2Method method;
Document doc;

parameters.put("InstanceId", options.getVirtualMachineId());
parameters.put("Name", options.getName());
parameters.put("Description", options.getDescription());
method = new EC2Method(provider, provider.getEc2Url(), parameters);
try {
doc = method.invoke();
}
catch( EC2Exception e ) {
logger.error(e.getSummary());
throw new CloudException(e);
}
blocks = doc.getElementsByTagName("imageId");
if( blocks.getLength() > 0 ) {
Node imageIdNode = blocks.item(0);
String id = imageIdNode.getFirstChild().getNodeValue().trim();
MachineImage img = getImage(id);

if( img == null ) {
for( int i=0; i<5; i++ ) {
try { Thread.sleep(5000L * i); }
catch( InterruptedException ignore ) { }
img = getImage(id);
if( img != null ) {
break;
}
}
if( img == null ) {
throw new CloudException("No image exists for " + id + " as created during the capture process");
catch( EC2Exception e ) {
logger.error(e.getSummary());
throw new CloudException(e);
}
blocks = doc.getElementsByTagName("imageId");
if( blocks.getLength() > 0 ) {
Node imageIdNode = blocks.item(0);
String id = imageIdNode.getFirstChild().getNodeValue().trim();
MachineImage img = getImage(id);

if( img == null ) {
for( int i=0; i<5; i++ ) {
try { Thread.sleep(5000L * i); }
catch( InterruptedException ignore ) { }
img = getImage(id);
if( img != null ) {
break;
}
}
if( MachineImageState.DELETED.equals(img.getCurrentState()) ) {
String errorMessage = (String)img.getTag("stateReason");

if( errorMessage != null ) {
if( errorMessage.contains("try again") ) {
lastMessage = errorMessage;
attempts--;
try { Thread.sleep(CalendarWrapper.MINUTE); }
catch( InterruptedException ignore ) { }
continue;
}
throw new CloudException(errorMessage);
if( img == null ) {
throw new CloudException("No image exists for " + id + " as created during the capture process");
}
}
if( MachineImageState.DELETED.equals(img.getCurrentState()) ) {
String errorMessage = (String)img.getTag("stateReason");

if( errorMessage != null ) {
if( errorMessage.contains("try again") ) {
lastMessage = errorMessage;
attempts--;
try { Thread.sleep(CalendarWrapper.MINUTE); }
catch( InterruptedException ignore ) { }
continue;
}
throw new CloudException(errorMessage);
}
return img;
}
throw new CloudException("No error occurred during imaging, but no machine image was specified");
return img;
}
throw new CloudException("No error occurred during imaging, but no machine image was specified");
}
if( lastMessage == null ) {
lastMessage = "Unknown error";
Expand Down

0 comments on commit 7cecfe9

Please sign in to comment.