MDCBuilder.buildMdcContext(stack);
Credential credential = stack.getCredential();
String emailAsFolder = azureStackUtil.emailAsFolder(stack.getOwner());
String filePath = AzureCertificateService.getUserJksFileName(credential, emailAsFolder);
AzureClient azureClient = azureStackUtil.createAzureClient(credential, filePath);
if (!azureClient.isImageAvailable(azureStackUtil.getOsImageName(credential))) {
String affinityGroupName = ((AzureCredential) credential).getCommonName();
createAffinityGroup(stack, azureClient, affinityGroupName);
String storageName = String.format("%s%s", VM_COMMON_NAME, stack.getId());
createStorage(stack, azureClient, affinityGroupName);
String targetBlobContainerUri = "http://" + affinityGroupName + ".blob.core.windows.net/vm-images";
String targetImageUri = targetBlobContainerUri + '/' + storageName + ".vhd";
Map<String, String> params = new HashMap<>();
params.put(AzureStackUtil.NAME, affinityGroupName);
String keyJson = (String) azureClient.getStorageAccountKeys(params);
JsonNode actualObj = null;
try {
actualObj = MAPPER.readValue(keyJson, JsonNode.class);
} catch (IOException e) {
LOGGER.info("Can not read Json node: ", e);
throw new InternalServerException("Can not read Json node: ", e);
}
String storageAccountKey = actualObj.get("StorageService").get("StorageServiceKeys").get("Primary").asText();
AzureClientUtil.createBlobContainer(storageAccountKey, targetBlobContainerUri);
AzureClientUtil.copyOsImage(storageAccountKey, baseImageUri, targetImageUri);
String copyStatus = PENDING;
while (PENDING.equals(copyStatus)) {
Map<String, String> copyStatusFromServer = (Map<String, String>) AzureClientUtil.getCopyOsImageProgress(storageAccountKey, targetImageUri);
copyStatus = copyStatusFromServer.get("status");
Long copied = Long.valueOf(copyStatusFromServer.get("copiedBytes"));
Long total = Long.valueOf(copyStatusFromServer.get("totalBytes"));
double copyPercentage = (long) ((float) copied / total * ONE_HUNDRED);
LOGGER.info(String.format("copy progress=%s / %s percentage: %s%%.",
copyStatusFromServer.get("copiedBytes"),
copyStatusFromServer.get("totalBytes"),
copyPercentage));
websocketService.sendToTopicUser(stack.getOwner(), WebsocketEndPoint.COPY_IMAGE,
new StatusMessage(stack.getId(), stack.getName(), PENDING, String.format("The copy status is: %s%%.", copyPercentage)));
retryingStackUpdater.updateStackStatusReason(stack.getId(), String.format("The copy status is: %s%%.", copyPercentage));
try {
Thread.sleep(MILLIS);
} catch (InterruptedException e) {
LOGGER.info("Interrupted exception occured during sleep.", e);
Thread.currentThread().interrupt();
}
}
if (!SUCCESS.equals(copyStatus)) {
throw new InternalServerException("Copy OS image failed with status: " + copyStatus);
}
params = new HashMap<>();
params.put(AzureStackUtil.NAME, azureStackUtil.getOsImageName(credential));
params.put(OS, "Linux");
params.put(MEDIALINK, targetImageUri);
azureClient.addOsImage(params);
}
LOGGER.info("Publishing {} event [StackId: '{}']", ReactorConfig.PROVISION_SETUP_COMPLETE_EVENT, stack.getId());
reactor.notify(ReactorConfig.PROVISION_SETUP_COMPLETE_EVENT,
Event.wrap(new ProvisionSetupComplete(getCloudPlatform(), stack.getId())
.withSetupProperty(CREDENTIAL, stack.getCredential())