mirror of https://github.com/apache/cloudstack.git
Fix styling and logging
This commit is contained in:
parent
813929a9de
commit
7012adf3f4
|
|
@ -41,5 +41,15 @@
|
|||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<version>2.13.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.13.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ package org.apache.cloudstack.backup;
|
|||
import java.net.URISyntaxException;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
|
@ -35,7 +34,6 @@ import org.apache.cloudstack.backup.backroll.model.BackrollBackupMetrics;
|
|||
import org.apache.cloudstack.backup.backroll.model.BackrollTaskStatus;
|
||||
import org.apache.cloudstack.backup.backroll.model.BackrollVmBackup;
|
||||
import org.apache.cloudstack.backup.dao.BackupDao;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import com.cloud.utils.Pair;
|
||||
|
|
@ -52,23 +50,22 @@ import org.apache.commons.lang3.StringUtils;
|
|||
|
||||
public class BackrollBackupProvider extends AdapterBase implements BackupProvider, Configurable {
|
||||
|
||||
private static final Logger s_logger = Logger.getLogger(BackrollBackupProvider.class);
|
||||
public static final String BACKUP_IDENTIFIER = "-CSBKP-";
|
||||
|
||||
public ConfigKey<String> BackrollUrlConfigKey = new ConfigKey<>("Advanced", String.class,
|
||||
"backup.plugin.backroll.config.url",
|
||||
"http://api.backup.demo.ccc:5050/api/v1",
|
||||
"Url for backroll plugin by DIMSI", true, ConfigKey.Scope.Zone);
|
||||
"Url for backroll plugin.", true, ConfigKey.Scope.Zone);
|
||||
|
||||
public ConfigKey<String> BackrollAppNameConfigKey = new ConfigKey<>("Advanced", String.class,
|
||||
"backup.plugin.backroll.config.appname",
|
||||
"backroll_api",
|
||||
"App Name for backroll plugin by DIMSI", true, ConfigKey.Scope.Zone);
|
||||
"App Name for backroll plugin.", true, ConfigKey.Scope.Zone);
|
||||
|
||||
public ConfigKey<String> BackrollPasswordConfigKey = new ConfigKey<>("Advanced", String.class,
|
||||
"backup.plugin.backroll.config.password",
|
||||
"VviX8dALauSyYJMqVYJqf3UyZOpO3joS",
|
||||
"Password for backroll plugin by DIMSI", true, ConfigKey.Scope.Zone);
|
||||
"Password for backroll plugin.", true, ConfigKey.Scope.Zone);
|
||||
|
||||
private BackrollClient backrollClient;
|
||||
|
||||
|
|
@ -89,10 +86,10 @@ public class BackrollBackupProvider extends AdapterBase implements BackupProvide
|
|||
|
||||
@Override
|
||||
public List<BackupOffering> listBackupOfferings(Long zoneId) {
|
||||
s_logger.debug("Listing backup policies on backroll B&R Plugin");
|
||||
logger.debug("Listing backup policies on backroll B&R Plugin");
|
||||
BackrollClient client = getClient(zoneId);
|
||||
String urlToRequest = client.getBackupOfferingUrl();
|
||||
if(!StringUtils.isEmpty(urlToRequest)){
|
||||
if (!StringUtils.isEmpty(urlToRequest)){
|
||||
return client.getBackupOfferings(urlToRequest);
|
||||
}
|
||||
return new ArrayList<BackupOffering>();
|
||||
|
|
@ -100,27 +97,26 @@ public class BackrollBackupProvider extends AdapterBase implements BackupProvide
|
|||
|
||||
@Override
|
||||
public boolean isValidProviderOffering(Long zoneId, String uuid) {
|
||||
s_logger.info("Checking if backup offering exists on the Backroll Backup Provider");
|
||||
logger.info("Checking if backup offering exists on the Backroll Backup Provider");
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean assignVMToBackupOffering(VirtualMachine vm, BackupOffering backupOffering) {
|
||||
s_logger.info("Creating VM backup for VM " + vm.getInstanceName() + " from backup offering " + backupOffering.getName());
|
||||
logger.info("Creating VM backup for VM {} from backup offering {}", vm.getInstanceName(), backupOffering.getName());
|
||||
((VMInstanceVO) vm).setBackupExternalId(backupOffering.getUuid());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean restoreVMFromBackup(VirtualMachine vm, Backup backup) {
|
||||
s_logger.debug("Restoring vm " + vm.getUuid() + "from backup " + backup.getUuid() + " on the backroll Backup Provider");
|
||||
logger.debug("Restoring vm {} from backup {} on the backroll Backup Provider", vm.getUuid(), backup.getUuid());
|
||||
boolean isSuccess = getClient(vm.getDataCenterId()).restoreVMFromBackup(vm.getUuid(), getBackupName(backup));
|
||||
return isSuccess;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<Boolean, String> restoreBackedUpVolume(Backup backup, String volumeUuid, String hostIp, String dataStoreUuid, Pair<String, VirtualMachine.State> vmNameAndState) {
|
||||
s_logger.debug("Restoring volume " + volumeUuid + "from backup " + backup.getUuid() + " on the backroll Backup Provider");
|
||||
throw new CloudRuntimeException("Backroll plugin does not support this feature");
|
||||
}
|
||||
|
||||
|
|
@ -128,12 +124,12 @@ public class BackrollBackupProvider extends AdapterBase implements BackupProvide
|
|||
public Map<VirtualMachine, Backup.Metric> getBackupMetrics(Long zoneId, List<VirtualMachine> vms) {
|
||||
final Map<VirtualMachine, Backup.Metric> metrics = new HashMap<>();
|
||||
if (CollectionUtils.isEmpty(vms)) {
|
||||
s_logger.warn("Unable to get VM Backup Metrics because the list of VMs is empty.");
|
||||
logger.warn("Unable to get VM Backup Metrics because the list of VMs is empty.");
|
||||
return metrics;
|
||||
}
|
||||
|
||||
List<String> vmUuids = vms.stream().filter(Objects::nonNull).map(VirtualMachine::getUuid).collect(Collectors.toList());
|
||||
s_logger.debug(String.format("Get Backup Metrics for VMs: [%s].", String.join(", ", vmUuids)));
|
||||
logger.debug("Get Backup Metrics for VMs: {}.", String.join(", ", vmUuids));
|
||||
|
||||
for (final VirtualMachine vm : vms) {
|
||||
if (vm == null) {
|
||||
|
|
@ -141,8 +137,8 @@ public class BackrollBackupProvider extends AdapterBase implements BackupProvide
|
|||
}
|
||||
|
||||
Metric metric = getClient(zoneId).getVirtualMachineMetrics(vm.getUuid());
|
||||
s_logger.debug(String.format("Metrics for VM [uuid: %s, name: %s] is [backup size: %s, data size: %s].", vm.getUuid(),
|
||||
vm.getInstanceName(), metric.getBackupSize(), metric.getDataSize()));
|
||||
logger.debug("Metrics for VM [uuid: {}, name: {}] is [backup size: {}, data size: {}].", vm.getUuid(),
|
||||
vm.getInstanceName(), metric.getBackupSize(), metric.getDataSize());
|
||||
metrics.put(vm, metric);
|
||||
}
|
||||
return metrics;
|
||||
|
|
@ -150,31 +146,28 @@ public class BackrollBackupProvider extends AdapterBase implements BackupProvide
|
|||
|
||||
@Override
|
||||
public boolean removeVMFromBackupOffering(VirtualMachine vm) {
|
||||
s_logger.info("Removing VM ID " + vm.getUuid() + " from Backrool backup offering ");
|
||||
logger.info("Removing VM ID {} from Backrool backup offering ", vm.getUuid());
|
||||
|
||||
boolean isAnyProblemWhileRemovingBackups = false;
|
||||
|
||||
List<Backup> backupsInCs = backupDao.listByVmId(null, vm.getId());
|
||||
|
||||
for (Backup backup : backupsInCs) {
|
||||
s_logger.debug("Trying to remove backup with id" + backup.getId());
|
||||
logger.debug("Trying to remove backup with id {}", backup.getId());
|
||||
|
||||
if (getClient(backup.getZoneId()).deleteBackup(vm.getUuid(), getBackupName(backup))) {
|
||||
var message = MessageFormat.format("Backup {0} deleted in Backroll for virtual machine {1}", backup.getId(), vm.getName());
|
||||
s_logger.info(message);
|
||||
if(!backupDao.remove(backup.getId())){
|
||||
logger.info("Backup {} deleted in Backroll for virtual machine {}", backup.getId(), vm.getName());
|
||||
if (!backupDao.remove(backup.getId())){
|
||||
isAnyProblemWhileRemovingBackups = true;
|
||||
}
|
||||
message = MessageFormat.format("Backup {0} deleted in CS for virtual machine {1}", backup.getId(), vm.getName());
|
||||
s_logger.info(message);
|
||||
logger.info("Backup {} deleted in CS for virtual machine {}", backup.getId(), vm.getName());
|
||||
} else {
|
||||
isAnyProblemWhileRemovingBackups = false;
|
||||
}
|
||||
}
|
||||
|
||||
if(isAnyProblemWhileRemovingBackups) {
|
||||
var message = MessageFormat.format("Problems occured while removing some backups for virtual machine {0}", vm.getName());
|
||||
s_logger.info(message);
|
||||
if (isAnyProblemWhileRemovingBackups) {
|
||||
logger.info("Problems occured while removing some backups for virtual machine {}", vm.getName());
|
||||
}
|
||||
return isAnyProblemWhileRemovingBackups;
|
||||
}
|
||||
|
|
@ -186,15 +179,15 @@ public class BackrollBackupProvider extends AdapterBase implements BackupProvide
|
|||
|
||||
@Override
|
||||
public boolean takeBackup(VirtualMachine vm) {
|
||||
s_logger.info("Starting backup for VM ID " + vm.getUuid() + " on backroll provider");
|
||||
logger.info("Starting backup for VM ID {} on backroll provider", vm.getUuid());
|
||||
final BackrollClient client = getClient(vm.getDataCenterId());
|
||||
|
||||
String idBackupTask = client.startBackupJob(vm.getUuid());
|
||||
if(!StringUtils.isEmpty(idBackupTask)) {
|
||||
if (!StringUtils.isEmpty(idBackupTask)) {
|
||||
BackupVO backup = new BackupVO();
|
||||
backup.setVmId(vm.getId());
|
||||
backup.setExternalId(idBackupTask);
|
||||
backup.setType("INCREMENTIAL");
|
||||
backup.setType("INCREMENTAL");
|
||||
backup.setDate(new DateTime().toDate());
|
||||
backup.setSize(0L);
|
||||
backup.setProtectedSize(0L);
|
||||
|
|
@ -210,17 +203,17 @@ public class BackrollBackupProvider extends AdapterBase implements BackupProvide
|
|||
|
||||
@Override
|
||||
public void syncBackups(VirtualMachine vm, Backup.Metric metric) {
|
||||
s_logger.info("Starting sync backup for VM ID " + vm.getUuid() + " on backroll provider");
|
||||
logger.info("Starting sync backup for VM ID " + vm.getUuid() + " on backroll provider");
|
||||
|
||||
final BackrollClient client = getClient(vm.getDataCenterId());
|
||||
List<Backup> backupsInDb = backupDao.listByVmId(null, vm.getId());
|
||||
|
||||
for (Backup backup : backupsInDb) {
|
||||
if(backup.getStatus().equals(Backup.Status.BackingUp)) {
|
||||
if (backup.getStatus().equals(Backup.Status.BackingUp)) {
|
||||
BackrollTaskStatus response = client.checkBackupTaskStatus(backup.getExternalId());
|
||||
if(response != null) {
|
||||
s_logger.debug("backroll backup id: " + backup.getExternalId());
|
||||
s_logger.debug("backroll backup status: " + response.getState());
|
||||
if (response != null) {
|
||||
logger.debug("backroll backup id: {}", backup.getExternalId());
|
||||
logger.debug("backroll backup status: {}", response.getState());
|
||||
|
||||
BackupVO backupToUpdate = new BackupVO();
|
||||
backupToUpdate.setVmId(backup.getVmId());
|
||||
|
|
@ -234,16 +227,16 @@ public class BackrollBackupProvider extends AdapterBase implements BackupProvide
|
|||
backupToUpdate.setDomainId(backup.getDomainId());
|
||||
backupToUpdate.setZoneId(backup.getZoneId());
|
||||
|
||||
if(response.getState().equals("PENDING")) {
|
||||
if (response.getState().equals("PENDING")) {
|
||||
backupToUpdate.setStatus(Backup.Status.BackingUp);
|
||||
} else if(response.getState().equals("FAILURE")) {
|
||||
} else if (response.getState().equals("FAILURE")) {
|
||||
backupToUpdate.setStatus(Backup.Status.Failed);
|
||||
} else if(response.getState().equals("SUCCESS")) {
|
||||
} else if (response.getState().equals("SUCCESS")) {
|
||||
backupToUpdate.setStatus(Backup.Status.BackedUp);
|
||||
backupToUpdate.setExternalId(backup.getExternalId() + "," + response.getInfo());
|
||||
|
||||
BackrollBackupMetrics backupMetrics = client.getBackupMetrics(vm.getUuid() , response.getInfo());
|
||||
if(backupMetrics != null) {
|
||||
if (backupMetrics != null) {
|
||||
backupToUpdate.setSize(backupMetrics.getDeduplicated()); // real size
|
||||
backupToUpdate.setProtectedSize(backupMetrics.getSize()); // total size
|
||||
}
|
||||
|
|
@ -251,15 +244,15 @@ public class BackrollBackupProvider extends AdapterBase implements BackupProvide
|
|||
backupToUpdate.setStatus(Backup.Status.BackingUp);
|
||||
}
|
||||
|
||||
if(backupDao.persist(backupToUpdate) != null) {
|
||||
s_logger.info("Backroll mise à jour enregistrée");
|
||||
if (backupDao.persist(backupToUpdate) != null) {
|
||||
logger.info("Backroll mise à jour enregistrée");
|
||||
backupDao.remove(backup.getId());
|
||||
}
|
||||
}
|
||||
} else if(backup.getStatus().equals(Backup.Status.BackedUp) && backup.getSize().equals(0L)) {
|
||||
} else if (backup.getStatus().equals(Backup.Status.BackedUp) && backup.getSize().equals(0L)) {
|
||||
String backupId = backup.getExternalId().contains(",") ? backup.getExternalId().split(",")[1] : backup.getExternalId();
|
||||
BackrollBackupMetrics backupMetrics = client.getBackupMetrics(vm.getUuid() , backupId);
|
||||
if(backupMetrics != null) {
|
||||
if (backupMetrics != null) {
|
||||
BackupVO backupToUpdate = ((BackupVO) backup);
|
||||
backupToUpdate.setSize(backupMetrics.getDeduplicated()); // real size
|
||||
backupToUpdate.setProtectedSize(backupMetrics.getSize()); // total size
|
||||
|
|
@ -279,11 +272,11 @@ public class BackrollBackupProvider extends AdapterBase implements BackupProvide
|
|||
.findAny()
|
||||
.orElse(null);
|
||||
|
||||
if(backupToFind == null) {
|
||||
if (backupToFind == null) {
|
||||
BackupVO backupToInsert = new BackupVO();
|
||||
backupToInsert.setVmId(vm.getId());
|
||||
backupToInsert.setExternalId(backupInBackroll.getId() + "," + backupInBackroll.getName());
|
||||
backupToInsert.setType("INCREMENTIAL");
|
||||
backupToInsert.setType("INCREMENTAL");
|
||||
backupToInsert.setDate(backupInBackroll.getDate());
|
||||
backupToInsert.setSize(0L);
|
||||
backupToInsert.setProtectedSize(0L);
|
||||
|
|
@ -294,11 +287,11 @@ public class BackrollBackupProvider extends AdapterBase implements BackupProvide
|
|||
backupToInsert.setZoneId(vm.getDataCenterId());
|
||||
backupDao.persist(backupToInsert);
|
||||
}
|
||||
if(backupToFind != null && backupToFind.getStatus() == Backup.Status.Removed) {
|
||||
if (backupToFind != null && backupToFind.getStatus() == Backup.Status.Removed) {
|
||||
BackupVO backupToUpdate = ((BackupVO) backupToFind);
|
||||
backupToUpdate.setStatus(Backup.Status.BackedUp);
|
||||
if(backupDao.persist(backupToUpdate) != null) {
|
||||
s_logger.info("Backroll mise à jour enregistrée");
|
||||
if (backupDao.persist(backupToUpdate) != null) {
|
||||
logger.info("Backroll update saved");
|
||||
backupDao.remove(backupToFind.getId());
|
||||
}
|
||||
}
|
||||
|
|
@ -313,11 +306,11 @@ public class BackrollBackupProvider extends AdapterBase implements BackupProvide
|
|||
.findAny()
|
||||
.orElse(null);
|
||||
|
||||
if(backupToFind == null) {
|
||||
if (backupToFind == null) {
|
||||
BackupVO backupToUpdate = ((BackupVO) backup);
|
||||
backupToUpdate.setStatus(Backup.Status.Removed);
|
||||
if(backupDao.persist(backupToUpdate) != null) {
|
||||
s_logger.debug("Backroll suppression enregistrée (sync)");
|
||||
if (backupDao.persist(backupToUpdate) != null) {
|
||||
logger.debug("Backroll delete saved (sync)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -339,18 +332,18 @@ public class BackrollBackupProvider extends AdapterBase implements BackupProvide
|
|||
|
||||
@Override
|
||||
public boolean deleteBackup(Backup backup, boolean forced) {
|
||||
s_logger.info("backroll delete backup id: " + backup.getExternalId());
|
||||
if(backup.getStatus().equals(Backup.Status.BackingUp)) {
|
||||
logger.info("backroll delete backup id: {}", backup.getExternalId());
|
||||
if (backup.getStatus().equals(Backup.Status.BackingUp)) {
|
||||
throw new CloudRuntimeException("You can't delete a backup while it still BackingUp");
|
||||
} else {
|
||||
s_logger.debug("backroll - try delete backup");
|
||||
logger.debug("backroll - try delete backup");
|
||||
VMInstanceVO vm = vmInstanceDao.findByIdIncludingRemoved(backup.getVmId());
|
||||
|
||||
if(backup.getStatus().equals(Backup.Status.Removed) || backup.getStatus().equals(Backup.Status.Failed)){
|
||||
if (backup.getStatus().equals(Backup.Status.Removed) || backup.getStatus().equals(Backup.Status.Failed)){
|
||||
return deleteBackupInDb(backup);
|
||||
} else {
|
||||
if (getClient(backup.getZoneId()).deleteBackup(vm.getUuid(), getBackupName(backup))) {
|
||||
s_logger.debug("backroll delete backup ok on backroll side");
|
||||
logger.debug("Backup deletion for backup {} complete on backroll side.", backup.getUuid());
|
||||
return deleteBackupInDb(backup);
|
||||
}
|
||||
}
|
||||
|
|
@ -361,25 +354,25 @@ public class BackrollBackupProvider extends AdapterBase implements BackupProvide
|
|||
private boolean deleteBackupInDb(Backup backup) {
|
||||
BackupVO backupToUpdate = ((BackupVO) backup);
|
||||
backupToUpdate.setStatus(Backup.Status.Removed);
|
||||
if(backupDao.persist(backupToUpdate) != null) {
|
||||
s_logger.debug("Backroll backup delete in database");
|
||||
if (backupDao.persist(backupToUpdate) != null) {
|
||||
logger.debug("Backroll backup {} deleted in database.", backup.getUuid());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected BackrollClient getClient(final Long zoneId) {
|
||||
s_logger.debug("Backroll Provider GetClient");
|
||||
logger.debug("Backroll Provider GetClient with zone id {}", zoneId);
|
||||
try {
|
||||
if(backrollClient == null){
|
||||
s_logger.debug("backroll client null - instanciation of new one ");
|
||||
if (backrollClient == null) {
|
||||
logger.debug("backroll client null - instanciation of new one ");
|
||||
backrollClient = new BackrollClient(BackrollUrlConfigKey.valueIn(zoneId), BackrollAppNameConfigKey.valueIn(zoneId), BackrollPasswordConfigKey.valueIn(zoneId), true, 300, 600);
|
||||
}
|
||||
return backrollClient;
|
||||
} catch (URISyntaxException e) {
|
||||
throw new CloudRuntimeException("Failed to parse Backroll API URL: " + e.getMessage());
|
||||
} catch (NoSuchAlgorithmException | KeyManagementException e) {
|
||||
s_logger.info("Failed to build Backroll API client due to: ", e);
|
||||
logger.info("Failed to build Backroll API client due to: ", e);
|
||||
}
|
||||
throw new CloudRuntimeException("Failed to build Backroll API client");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,7 +66,8 @@ import org.apache.http.entity.ContentType;
|
|||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.joda.time.DateTime;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
|
@ -79,7 +80,7 @@ import org.apache.cloudstack.backup.Backup.Metric;
|
|||
|
||||
public class BackrollClient {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(BackrollClient.class);
|
||||
private Logger LOG = LogManager.getLogger(BackrollClient.class);
|
||||
|
||||
private int restoreTimeout;
|
||||
|
||||
|
|
@ -121,19 +122,19 @@ public class BackrollClient {
|
|||
.build();
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(backrollToken) || !isAuthentificated()) {
|
||||
if (StringUtils.isEmpty(backrollToken) || !isAuthenticated()) {
|
||||
login(appname, password);
|
||||
}
|
||||
}
|
||||
|
||||
public String startBackupJob(final String jobId) {
|
||||
LOG.info(String.format("Trying to start backup for Backroll job: %s", jobId));
|
||||
LOG.info("Trying to start backup for Backroll job: {}", jobId);
|
||||
|
||||
try {
|
||||
loginIfAuthentificationFailed();
|
||||
loginIfAuthenticationFailed();
|
||||
|
||||
final HttpResponse response = post(String.format("/tasks/singlebackup/%s", jobId), null);
|
||||
if(isResponseOk(response)) {
|
||||
if (isResponseOk(response)) {
|
||||
HttpEntity body = response.getEntity();
|
||||
String bodyStr = EntityUtils.toString(body);
|
||||
BackrollTaskRequestResponse backupTaskRequestResponse = new ObjectMapper().readValue(bodyStr, BackrollTaskRequestResponse.class);
|
||||
|
|
@ -142,21 +143,21 @@ public class BackrollClient {
|
|||
return idBackupTask;
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
LOG.error(String.format("Failed to start Backroll backup job due to: %s", e.getMessage()));
|
||||
LOG.error("Failed to start Backroll backup job due to: {}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getBackupOfferingUrl(){
|
||||
public String getBackupOfferingUrl() {
|
||||
LOG.info("Trying to list backroll backup policies");
|
||||
|
||||
loginIfAuthentificationFailed();
|
||||
loginIfAuthenticationFailed();
|
||||
|
||||
try {
|
||||
|
||||
final HttpResponse backupPoliciesRequestResponse = get("/backup_policies");
|
||||
|
||||
if(isResponseOk(backupPoliciesRequestResponse)) {
|
||||
if (isResponseOk(backupPoliciesRequestResponse)) {
|
||||
HttpEntity body = backupPoliciesRequestResponse.getEntity();
|
||||
String bodyStr = EntityUtils.toString(body);
|
||||
BackrollTaskRequestResponse backupTaskRequestResponse = new ObjectMapper().readValue(bodyStr, BackrollTaskRequestResponse.class);
|
||||
|
|
@ -164,7 +165,7 @@ public class BackrollClient {
|
|||
return backupTaskRequestResponse.location.replace("/api/v1", "");
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
LOG.error(String.format("Failed to list Backroll jobs due to: %s", e.getMessage()));
|
||||
LOG.error("Failed to list Backroll jobs due to: {}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -172,7 +173,7 @@ public class BackrollClient {
|
|||
public List<BackupOffering> getBackupOfferings(String idTask) {
|
||||
LOG.info("Trying to list backroll backup policies");
|
||||
|
||||
loginIfAuthentificationFailed();
|
||||
loginIfAuthenticationFailed();
|
||||
|
||||
try {
|
||||
String bodyStr = waitForGetRequestResponse(idTask);
|
||||
|
|
@ -184,18 +185,16 @@ public class BackrollClient {
|
|||
policies.add(new BackrollOffering(policy.name, policy.id));
|
||||
}
|
||||
return policies;
|
||||
} catch (final IOException e) {
|
||||
LOG.error(String.format("Failed to list Backroll jobs due to: %s", e.getMessage()));
|
||||
} catch (InterruptedException e) {
|
||||
LOG.error(String.format("Failed to list Backroll jobs due to: %s", e.getMessage()));
|
||||
} catch (final IOException |InterruptedException e) {
|
||||
LOG.error("Failed to list Backroll jobs due to: {}", e.getMessage());
|
||||
}
|
||||
return new ArrayList<BackupOffering>();
|
||||
}
|
||||
|
||||
public boolean restoreVMFromBackup(final String vmId, final String backupName) {
|
||||
LOG.info(String.format("Start restore backup with backroll with backup %s for vm %s", backupName, vmId));
|
||||
LOG.info("Start restore backup with backroll with backup {} for vm {}", backupName, vmId);
|
||||
|
||||
loginIfAuthentificationFailed();
|
||||
loginIfAuthenticationFailed();
|
||||
|
||||
try {
|
||||
JSONObject jsonBody = new JSONObject();
|
||||
|
|
@ -206,13 +205,13 @@ public class BackrollClient {
|
|||
jsonBody.put("mode", "single");
|
||||
|
||||
} catch (JSONException e) {
|
||||
LOG.error(String.format("Backroll Error: %s", e.getMessage()));
|
||||
LOG.error("Backroll Error: {}", e.getMessage());
|
||||
}
|
||||
|
||||
final HttpResponse response = post(String.format("/tasks/restore/%s", vmId), jsonBody);
|
||||
LOG.debug(response.toString());
|
||||
|
||||
if(isResponseOk(response)) {
|
||||
if (isResponseOk(response)) {
|
||||
HttpEntity body = response.getEntity();
|
||||
String bodyStr = EntityUtils.toString(body);
|
||||
LOG.debug(bodyStr);
|
||||
|
|
@ -222,7 +221,7 @@ public class BackrollClient {
|
|||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
|
||||
String responseStatus = waitForGetRequestResponse(urlToRequest);
|
||||
LOG.debug("RESTORE " + responseStatus);
|
||||
LOG.debug("RESTORE {}", responseStatus);
|
||||
if(responseStatus.contains("SUCCESS")) {
|
||||
LOG.debug("RESTORE SUCCESS");
|
||||
return true;
|
||||
|
|
@ -231,27 +230,27 @@ public class BackrollClient {
|
|||
}
|
||||
}
|
||||
} catch (final IOException | InterruptedException e) {
|
||||
LOG.error(String.format("Ouch! Failed to restore VM with Backroll due to: %s", e.getMessage()));
|
||||
throw new CloudRuntimeException(String.format("Ouch! Failed to restore VM with Backroll due to: %s", e.getMessage()));
|
||||
LOG.error("Ouch! Failed to restore VM with Backroll due to: {}", e.getMessage());
|
||||
throw new CloudRuntimeException("Ouch! Failed to restore VM with Backroll due to: {}" + e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public BackrollTaskStatus checkBackupTaskStatus(String taskId) {
|
||||
LOG.info(String.format("Trying to get backup status for Backroll task: %s", taskId));
|
||||
LOG.info("Trying to get backup status for Backroll task: {}", taskId);
|
||||
|
||||
loginIfAuthentificationFailed();
|
||||
loginIfAuthenticationFailed();
|
||||
|
||||
try {
|
||||
final HttpResponse backupStatusResponse = get("/status/" + taskId);
|
||||
|
||||
if(isResponseOk(backupStatusResponse)) {
|
||||
if (isResponseOk(backupStatusResponse)) {
|
||||
BackrollTaskStatus status = new BackrollTaskStatus();
|
||||
|
||||
HttpEntity body = backupStatusResponse.getEntity();
|
||||
String bodyStr = EntityUtils.toString(body);
|
||||
|
||||
if(bodyStr.contains("FAILURE") || bodyStr.contains("PENDING")) {
|
||||
if (bodyStr.contains("FAILURE") || bodyStr.contains("PENDING")) {
|
||||
BackrollBackupStatusResponse backupStatusRequestResponse = new ObjectMapper().readValue(bodyStr, BackrollBackupStatusResponse.class);
|
||||
EntityUtils.consumeQuietly(backupStatusResponse.getEntity());
|
||||
status.setState(backupStatusRequestResponse.state);
|
||||
|
|
@ -266,23 +265,23 @@ public class BackrollClient {
|
|||
} else {
|
||||
EntityUtils.consumeQuietly(backupStatusResponse.getEntity());
|
||||
// throw new CloudRuntimeException("Failed to retrieve backups status for this VM via Backroll");
|
||||
LOG.error(String.format("Failed to retrieve backups status for this VM via Backroll"));
|
||||
LOG.error("Failed to retrieve backups status for this VM via Backroll");
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
LOG.error(String.format("Failed to check backups status due to: %s", e.getMessage()));
|
||||
LOG.error("Failed to check backups status due to: {}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean deleteBackup(final String vmId, final String backupName) {
|
||||
LOG.info(String.format("Trying to delete backup %s for vm %s using Backroll", vmId, backupName));
|
||||
LOG.info("Trying to delete backup {} for vm {} using Backroll", vmId, backupName);
|
||||
|
||||
loginIfAuthentificationFailed();
|
||||
loginIfAuthenticationFailed();
|
||||
|
||||
try {
|
||||
final HttpResponse response = delete(String.format("/virtualmachines/%s/backups/%s", vmId, backupName));
|
||||
LOG.debug(response.toString());
|
||||
if(isResponseOk(response)) {
|
||||
if (isResponseOk(response)) {
|
||||
HttpEntity body = response.getEntity();
|
||||
String bodyStr = EntityUtils.toString(body);
|
||||
LOG.debug(bodyStr);
|
||||
|
|
@ -293,7 +292,7 @@ public class BackrollClient {
|
|||
String responseStatus = waitForGetRequestResponse(urlToRequest);
|
||||
LOG.debug(responseStatus);
|
||||
BackrollBackupsFromVMResponse backrollBackupsFromVMResponse = new ObjectMapper().readValue(responseStatus, BackrollBackupsFromVMResponse.class);
|
||||
if(backrollBackupsFromVMResponse.state.equals("SUCCESS")) {
|
||||
if (backrollBackupsFromVMResponse.state.equals("SUCCESS")) {
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
return true;
|
||||
} else {
|
||||
|
|
@ -304,15 +303,15 @@ public class BackrollClient {
|
|||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
return false;
|
||||
} catch (final IOException | InterruptedException e) {
|
||||
LOG.error(String.format("Failed to delete backup using Backroll due to: %s", e.getMessage()));
|
||||
LOG.error("Failed to delete backup using Backroll due to: {}", e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Metric getVirtualMachineMetrics(final String vmId){
|
||||
LOG.info(String.format("Trying to retrieve virtual machine metric from Backroll for vm %s", vmId));
|
||||
public Metric getVirtualMachineMetrics(final String vmId) {
|
||||
LOG.info("Trying to retrieve virtual machine metric from Backroll for vm {}", vmId);
|
||||
|
||||
loginIfAuthentificationFailed();
|
||||
loginIfAuthenticationFailed();
|
||||
|
||||
Metric metric = new Metric(0L,0L);
|
||||
|
||||
|
|
@ -320,7 +319,7 @@ public class BackrollClient {
|
|||
|
||||
final HttpResponse metricsRequestResponse = get(String.format("/virtualmachines/%s/repository", vmId));
|
||||
|
||||
if(isResponseOk(metricsRequestResponse)) {
|
||||
if (isResponseOk(metricsRequestResponse)) {
|
||||
HttpEntity body = metricsRequestResponse.getEntity();
|
||||
String bodyStr = EntityUtils.toString(body);
|
||||
LOG.debug(bodyStr);
|
||||
|
|
@ -333,13 +332,13 @@ public class BackrollClient {
|
|||
|
||||
BackrollVmMetricsResponse vmMetricsResponse = new ObjectMapper().readValue(bodyStr, BackrollVmMetricsResponse.class);
|
||||
|
||||
if(vmMetricsResponse != null) {
|
||||
if (vmMetricsResponse != null) {
|
||||
|
||||
if(vmMetricsResponse.state.equals("SUCCESS")) {
|
||||
if (vmMetricsResponse.state.equals("SUCCESS")) {
|
||||
LOG.debug("SUCCESS ok");
|
||||
if(vmMetricsResponse.infos != null) {
|
||||
if(vmMetricsResponse.infos.cache != null) {
|
||||
if(vmMetricsResponse.infos.cache.stats != null) {
|
||||
if (vmMetricsResponse.infos != null) {
|
||||
if (vmMetricsResponse.infos.cache != null) {
|
||||
if (vmMetricsResponse.infos.cache.stats != null) {
|
||||
CacheStats stats = vmMetricsResponse.infos.cache.stats;
|
||||
long size = Long.parseLong(stats.totalSize);
|
||||
return new Metric(size,size);
|
||||
|
|
@ -352,21 +351,21 @@ public class BackrollClient {
|
|||
EntityUtils.consumeQuietly(metricsRequestResponse.getEntity());
|
||||
}
|
||||
} catch (final IOException | InterruptedException e) {
|
||||
LOG.error(String.format("Failed to retrieve virtual machine metrics with Backroll due to: %s", e.getMessage()));
|
||||
LOG.error("Failed to retrieve virtual machine metrics with Backroll due to: {}", e.getMessage());
|
||||
}
|
||||
|
||||
return metric;
|
||||
}
|
||||
|
||||
public BackrollBackupMetrics getBackupMetrics(String vmId, String backupId) {
|
||||
LOG.info(String.format("Trying to get backup metrics for VM: %s, and backup: %s", vmId, backupId));
|
||||
LOG.info("Trying to get backup metrics for VM: {}, and backup: {}", vmId, backupId);
|
||||
|
||||
loginIfAuthentificationFailed();
|
||||
loginIfAuthenticationFailed();
|
||||
|
||||
try {
|
||||
final HttpResponse backupMetricsResponse = get(String.format("/virtualmachines/%s/backups/%s", vmId, backupId));
|
||||
|
||||
if(isResponseOk(backupMetricsResponse)) {
|
||||
if (isResponseOk(backupMetricsResponse)) {
|
||||
HttpEntity body = backupMetricsResponse.getEntity();
|
||||
String bodyStr = EntityUtils.toString(body);
|
||||
|
||||
|
|
@ -379,7 +378,7 @@ public class BackrollClient {
|
|||
|
||||
BackrollBackupMetricsResponse metrics = new ObjectMapper().readValue(bodyStr, BackrollBackupMetricsResponse.class);
|
||||
EntityUtils.consumeQuietly(backupMetricsResponse.getEntity());
|
||||
if(metrics.info != null) {
|
||||
if (metrics.info != null) {
|
||||
return new BackrollBackupMetrics(Long.parseLong(metrics.info.originalSize), Long.parseLong(metrics.info.deduplicatedSize));
|
||||
}
|
||||
} else {
|
||||
|
|
@ -387,20 +386,20 @@ public class BackrollClient {
|
|||
throw new CloudRuntimeException("Failed to retrieve backups status for this VM via Backroll");
|
||||
}
|
||||
} catch (final IOException | InterruptedException e) {
|
||||
LOG.error(String.format("Failed to check backups status due to: %s", e.getMessage()));
|
||||
LOG.error("Failed to check backups status due to: {}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<BackrollVmBackup> getAllBackupsfromVirtualMachine(String vmId) {
|
||||
LOG.info(String.format("Trying to retrieve all backups for vm %s", vmId));
|
||||
LOG.info("Trying to retrieve all backups for vm {}", vmId);
|
||||
|
||||
List<BackrollVmBackup> backups = new ArrayList<BackrollVmBackup>();
|
||||
|
||||
try {
|
||||
HttpResponse backupsResponse = get(String.format("/virtualmachines/%s/backups", vmId));
|
||||
|
||||
if(isResponseOk(backupsResponse)) {
|
||||
if (isResponseOk(backupsResponse)) {
|
||||
HttpEntity body = backupsResponse.getEntity();
|
||||
String bodyStr = EntityUtils.toString(body);
|
||||
|
||||
|
|
@ -414,8 +413,8 @@ public class BackrollClient {
|
|||
VirtualMachineBackupsResponse response = new ObjectMapper().readValue(bodyStr, VirtualMachineBackupsResponse.class);
|
||||
EntityUtils.consumeQuietly(backupsResponse.getEntity());
|
||||
|
||||
if(response.state.equals("SUCCESS")) {
|
||||
if(response.info.archives.size() > 0) {
|
||||
if (response.state.equals("SUCCESS")) {
|
||||
if (response.info.archives.size() > 0) {
|
||||
for (BackupInfos infos : response.info.archives) {
|
||||
var dateStart = new DateTime(infos.start);
|
||||
backups.add(new BackrollVmBackup(infos.id, infos.name, dateStart.toDate()));
|
||||
|
|
@ -435,7 +434,7 @@ public class BackrollClient {
|
|||
String xml = null;
|
||||
StringEntity params = null;
|
||||
if (json != null) {
|
||||
LOG.debug("JSON " + json.toString());
|
||||
LOG.debug("JSON {}", json.toString());
|
||||
params = new StringEntity(json.toString(), ContentType.APPLICATION_JSON);
|
||||
}
|
||||
|
||||
|
|
@ -451,19 +450,19 @@ public class BackrollClient {
|
|||
|
||||
final HttpResponse response = httpClient.execute(request);
|
||||
|
||||
LOG.info(String.format("Response received in POST request with body [%s] is: [%s] for URL [%s].", xml, response.toString(), url));
|
||||
LOG.debug("Response received in POST request with body {} is: {} for URL {}.", xml, response.toString(), url);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
protected HttpResponse get(final String path) throws IOException {
|
||||
String url = apiURI.toString() + path;
|
||||
LOG.debug(String.format("Backroll URL %s", url));
|
||||
LOG.debug("Backroll URL {}", url);
|
||||
final HttpGet request = new HttpGet(url);
|
||||
request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + backrollToken);
|
||||
request.setHeader("Content-type", "application/json");
|
||||
final HttpResponse response = httpClient.execute(request);
|
||||
LOG.debug(String.format("Response received in GET request is: [%s] for URL: [%s].", response.toString(), url));
|
||||
LOG.debug("Response received in GET request is: {} for URL: {}.", response.toString(), url);
|
||||
return response;
|
||||
}
|
||||
|
||||
|
|
@ -473,12 +472,12 @@ public class BackrollClient {
|
|||
request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + backrollToken);
|
||||
request.setHeader("Content-type", "application/json");
|
||||
final HttpResponse response = httpClient.execute(request);
|
||||
LOG.debug(String.format("Response received in GET request is: [%s] for URL: [%s].", response.toString(), url));
|
||||
LOG.debug("Response received in GET request is: {} for URL: {}.", response.toString(), url);
|
||||
return response;
|
||||
}
|
||||
|
||||
private boolean isResponseOk(final HttpResponse response) {
|
||||
if((response.getStatusLine().getStatusCode() == HttpStatus.SC_OK ||
|
||||
if ((response.getStatusLine().getStatusCode() == HttpStatus.SC_OK ||
|
||||
response.getStatusLine().getStatusCode() == HttpStatus.SC_ACCEPTED)) {
|
||||
return true;
|
||||
} else {
|
||||
|
|
@ -487,7 +486,7 @@ public class BackrollClient {
|
|||
}
|
||||
|
||||
private boolean isResponseAuthorized(final HttpResponse response) {
|
||||
return (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK || response.getStatusLine().getStatusCode() == HttpStatus.SC_ACCEPTED) ? true : false;
|
||||
return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK || response.getStatusLine().getStatusCode() == HttpStatus.SC_ACCEPTED;
|
||||
}
|
||||
|
||||
private String waitForGetRequestResponse(String urlToRequest) throws IOException, InterruptedException {
|
||||
|
|
@ -498,11 +497,11 @@ public class BackrollClient {
|
|||
int threshold = 12; // 2 minutes
|
||||
|
||||
do {
|
||||
if(cpt == threshold){
|
||||
if (cpt == threshold){
|
||||
break;
|
||||
} else {
|
||||
final HttpResponse response = get(urlToRequest);
|
||||
LOG.debug("Backroll!!!" + response);
|
||||
LOG.debug("Backroll!!! {}", response);
|
||||
if (isResponseOk(response)) {
|
||||
|
||||
body = response.getEntity();
|
||||
|
|
@ -518,33 +517,33 @@ public class BackrollClient {
|
|||
|
||||
} while (bodyStr.contains("PENDING"));
|
||||
|
||||
if(cpt == threshold){
|
||||
if (cpt == threshold){
|
||||
bodyStr = "ERROR";
|
||||
}
|
||||
|
||||
return bodyStr;
|
||||
}
|
||||
|
||||
private boolean isAuthentificated() {
|
||||
private boolean isAuthenticated() {
|
||||
boolean result = false;
|
||||
try {
|
||||
final HttpResponse response = post("/auth", null);
|
||||
result = isResponseAuthorized(response);
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
} catch (IOException e) {
|
||||
LOG.error(String.format("Failed to authentificate to Backroll due to: %s", e.getMessage()));
|
||||
LOG.error("Failed to authentificate to Backroll due to: {}", e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void loginIfAuthentificationFailed() {
|
||||
if(!isAuthentificated()) {
|
||||
private void loginIfAuthenticationFailed() {
|
||||
if (!isAuthenticated()) {
|
||||
login(appname, password);
|
||||
}
|
||||
}
|
||||
|
||||
private void login(final String appname, final String appsecret) {
|
||||
LOG.info("Backroll client - start login");
|
||||
LOG.debug("Backroll client - start login");
|
||||
final HttpPost request = new HttpPost(apiURI.toString() + "/login");
|
||||
|
||||
request.addHeader("content-type", "application/json");
|
||||
|
|
@ -566,7 +565,7 @@ public class BackrollClient {
|
|||
|
||||
LoginApiResponse loginResponse = new ObjectMapper().readValue(bodyStr, LoginApiResponse.class);
|
||||
backrollToken = loginResponse.accessToken;
|
||||
LOG.debug(String.format("Backroll client - Token : %s", backrollToken));
|
||||
LOG.debug("Backroll client - Token : {}", backrollToken);
|
||||
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
|
||||
|
|
@ -589,15 +588,15 @@ public class BackrollClient {
|
|||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
LOG.info("Backroll client - end login");
|
||||
LOG.debug("Backroll client - end login");
|
||||
}
|
||||
|
||||
private List<BackrollBackup> getBackrollBackups(final String vmId) {
|
||||
try {
|
||||
LOG.info("start to list Backroll backups for vm " + vmId);
|
||||
LOG.info("start to list Backroll backups for vm {}", vmId);
|
||||
final HttpResponse backupsAskingResponse = get("/virtualmachines/" + vmId + "/backups");
|
||||
|
||||
if(isResponseOk(backupsAskingResponse)) {
|
||||
if (isResponseOk(backupsAskingResponse)) {
|
||||
HttpEntity body = backupsAskingResponse.getEntity();
|
||||
String bodyStr = EntityUtils.toString(body);
|
||||
BackrollTaskRequestResponse backupTaskRequestResponse = new ObjectMapper().readValue(bodyStr, BackrollTaskRequestResponse.class);
|
||||
|
|
@ -615,13 +614,12 @@ public class BackrollClient {
|
|||
}
|
||||
return backups;
|
||||
} else {
|
||||
throw new CloudRuntimeException(
|
||||
"Failed to retrieve backups for this VM via Backroll");
|
||||
throw new CloudRuntimeException("Failed to retrieve backups for this VM via Backroll");
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
LOG.error("Failed to list backup form vm with Backroll due to:", e);
|
||||
LOG.error("Failed to list backup form vm with Backroll due to: {}", e);
|
||||
} catch (InterruptedException e) {
|
||||
LOG.error("Backroll Error:", e);
|
||||
LOG.error("Backroll Error: {}", e);
|
||||
}
|
||||
return new ArrayList<BackrollBackup>();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue