public VmwareCloudImage( @NotNull final VMWareApiConnector apiConnector, @NotNull final VmwareCloudImageDetails imageDetails, @NotNull final CloudAsyncTaskExecutor asyncTaskExecutor, @NotNull final File idxStorage) { super(imageDetails.getNickname(), imageDetails.getNickname()); myImageDetails = imageDetails; myApiConnector = apiConnector; myAsyncTaskExecutor = asyncTaskExecutor; myInstances.clear(); myIdxFile = new File(idxStorage, imageDetails.getNickname() + ".idx"); if (!myIdxFile.exists()) { try { FileUtil.writeFileAndReportErrors(myIdxFile, "1"); } catch (IOException e) { LOG.warn( String.format( "Unable to write idx file '%s': %s", myIdxFile.getAbsolutePath(), e.toString())); } } Map<String, VmwareInstance> realInstances = null; try { realInstances = myApiConnector.listImageInstances(this); } catch (VmwareCheckedCloudException e) { updateErrors(TypedCloudErrorInfo.fromException(e)); return; } if (imageDetails.getBehaviour().isUseOriginal()) { final VmwareCloudInstance imageInstance = new VmwareCloudInstance( this, imageDetails.getSourceName(), VmwareConstants.CURRENT_STATE); myInstances.put(myImageDetails.getSourceName(), imageInstance); final VmwareInstance vmwareInstance = realInstances.get(imageDetails.getSourceName()); if (vmwareInstance != null) { imageInstance.setStatus(vmwareInstance.getInstanceStatus()); } else { imageInstance.setStatus(InstanceStatus.UNKNOWN); imageInstance.updateErrors( new TypedCloudErrorInfo("NoVM", "VM doesn't exist: " + imageDetails.getSourceName())); } } else { for (String instanceName : realInstances.keySet()) { final VmwareInstance instance = realInstances.get(instanceName); final String snapshotName = instance.getSnapshotName(); VmwareCloudInstance cloudInstance = new VmwareCloudInstance(this, instanceName, snapshotName); cloudInstance.setStatus(instance.getInstanceStatus()); myInstances.put(instanceName, cloudInstance); } } }
protected String generateNewVmName() { int nextIdx = 0; try { nextIdx = Integer.parseInt(FileUtil.readText(myIdxFile)); FileUtil.writeFileAndReportErrors(myIdxFile, String.valueOf(nextIdx + 1)); } catch (Exception e) { LOG.warn( "Will generate random clone index. Reason: unable to read idx file: " + e.toString()); Random r = new Random(); nextIdx = 100000 + r.nextInt(100000); } return String.format("%s-%d", getId(), nextIdx); }