use of com.cloud.vm.SecondaryStorageVmVO in project cloudstack by apache.
the class SecondaryStorageManagerImpl method allocCapacity.
public void allocCapacity(long dataCenterId, SecondaryStorageVm.Role role) {
s_logger.debug(String.format("Allocate secondary storage VM standby capacity for zone [%s].", dataCenterId));
if (!isSecondaryStorageVmRequired(dataCenterId)) {
if (s_logger.isDebugEnabled()) {
s_logger.debug(String.format("Secondary storage VM not required in zone [%s] according to zone config.", dataCenterId));
}
return;
}
SecondaryStorageVmVO secStorageVm = null;
String errorString = null;
try {
boolean secStorageVmFromStoppedPool = false;
secStorageVm = assignSecStorageVmFromStoppedPool(dataCenterId, role);
if (secStorageVm == null) {
if (s_logger.isInfoEnabled()) {
s_logger.info("No stopped secondary storage VM is available, need to allocate a new secondary storage VM.");
}
if (_allocLock.lock(ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_SYNC_IN_SECONDS)) {
try {
secStorageVm = startNew(dataCenterId, role);
} finally {
_allocLock.unlock();
}
} else {
if (s_logger.isInfoEnabled()) {
s_logger.info("Unable to acquire synchronization lock for secondary storage VM allocation, wait for next scan.");
}
return;
}
} else {
if (s_logger.isInfoEnabled()) {
s_logger.info(String.format("Found a stopped secondary storage %s, starting it.", secStorageVm.toString()));
}
secStorageVmFromStoppedPool = true;
}
if (secStorageVm != null) {
long secStorageVmId = secStorageVm.getId();
GlobalLock secStorageVmLock = GlobalLock.getInternLock(getSecStorageVmLockName(secStorageVmId));
try {
if (secStorageVmLock.lock(ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_SYNC_IN_SECONDS)) {
try {
secStorageVm = startSecStorageVm(secStorageVmId);
} finally {
secStorageVmLock.unlock();
}
} else {
if (s_logger.isInfoEnabled()) {
s_logger.info(String.format("Unable to acquire synchronization lock for starting secondary storage %s.", secStorageVm.toString()));
}
return;
}
} finally {
secStorageVmLock.releaseRef();
}
if (secStorageVm == null) {
if (s_logger.isInfoEnabled()) {
s_logger.info(String.format("Unable to start secondary storage VM [%s] for standby capacity, it will be recycled and will start a new one.", secStorageVmId));
}
if (secStorageVmFromStoppedPool) {
destroySecStorageVm(secStorageVmId);
}
} else {
SubscriptionMgr.getInstance().notifySubscribers(ALERT_SUBJECT, this, new SecStorageVmAlertEventArgs(SecStorageVmAlertEventArgs.SSVM_UP, dataCenterId, secStorageVmId, secStorageVm, null));
if (s_logger.isInfoEnabled()) {
s_logger.info(String.format("Secondary storage %s was started.", secStorageVm.toString()));
}
}
}
} catch (Exception e) {
errorString = String.format("Unable to allocate capacity on zone [%s] due to [%s].", dataCenterId, errorString);
throw e;
} finally {
if (secStorageVm == null || secStorageVm.getState() != State.Running)
SubscriptionMgr.getInstance().notifySubscribers(ALERT_SUBJECT, this, new SecStorageVmAlertEventArgs(SecStorageVmAlertEventArgs.SSVM_CREATE_FAILURE, dataCenterId, 0l, null, errorString));
}
}
use of com.cloud.vm.SecondaryStorageVmVO in project cloudstack by apache.
the class SecondaryStorageManagerImpl method createSecStorageVmInstance.
protected Map<String, Object> createSecStorageVmInstance(long dataCenterId, SecondaryStorageVm.Role role) {
DataStore secStore = _dataStoreMgr.getImageStoreWithFreeCapacity(dataCenterId);
if (secStore == null) {
String msg = String.format("No secondary storage available in zone %s, cannot create secondary storage VM.", dataCenterId);
s_logger.warn(msg);
throw new CloudRuntimeException(msg);
}
long id = _secStorageVmDao.getNextInSequence(Long.class, "id");
String name = VirtualMachineName.getSystemVmName(id, _instance, "s").intern();
Account systemAcct = _accountMgr.getSystemAccount();
DataCenterDeployment plan = new DataCenterDeployment(dataCenterId);
DataCenter dc = _dcDao.findById(plan.getDataCenterId());
NetworkVO defaultNetwork = getDefaultNetworkForCreation(dc);
List<? extends NetworkOffering> offerings = null;
if (_sNwMgr.isStorageIpRangeAvailable(dataCenterId)) {
offerings = _networkModel.getSystemAccountNetworkOfferings(NetworkOffering.SystemControlNetwork, NetworkOffering.SystemManagementNetwork, NetworkOffering.SystemStorageNetwork);
} else {
offerings = _networkModel.getSystemAccountNetworkOfferings(NetworkOffering.SystemControlNetwork, NetworkOffering.SystemManagementNetwork);
}
LinkedHashMap<Network, List<? extends NicProfile>> networks = new LinkedHashMap<>(offerings.size() + 1);
NicProfile defaultNic = new NicProfile();
defaultNic.setDefaultNic(true);
defaultNic.setDeviceId(2);
try {
networks.put(_networkMgr.setupNetwork(systemAcct, _networkOfferingDao.findById(defaultNetwork.getNetworkOfferingId()), plan, null, null, false).get(0), new ArrayList<>(Arrays.asList(defaultNic)));
for (NetworkOffering offering : offerings) {
networks.put(_networkMgr.setupNetwork(systemAcct, offering, plan, null, null, false).get(0), new ArrayList<>());
}
} catch (ConcurrentOperationException e) {
s_logger.error(String.format("Unable to setup networks on %s due [%s].", dc.toString(), e.getMessage()), e);
return new HashMap<>();
}
HypervisorType availableHypervisor = _resourceMgr.getAvailableHypervisor(dataCenterId);
VMTemplateVO template = _templateDao.findSystemVMReadyTemplate(dataCenterId, availableHypervisor);
if (template == null) {
throw new CloudRuntimeException(String.format("Unable to find the system templates or it was not downloaded in %s.", dc.toString()));
}
ServiceOfferingVO serviceOffering = _serviceOffering;
if (serviceOffering == null) {
serviceOffering = _offeringDao.findDefaultSystemOffering(ServiceOffering.ssvmDefaultOffUniqueName, ConfigurationManagerImpl.SystemVMUseLocalStorage.valueIn(dataCenterId));
}
SecondaryStorageVmVO secStorageVm = new SecondaryStorageVmVO(id, serviceOffering.getId(), name, template.getId(), template.getHypervisorType(), template.getGuestOSId(), dataCenterId, systemAcct.getDomainId(), systemAcct.getId(), _accountMgr.getSystemUser().getId(), role, serviceOffering.isOfferHA());
secStorageVm.setDynamicallyScalable(template.isDynamicallyScalable());
secStorageVm = _secStorageVmDao.persist(secStorageVm);
try {
_itMgr.allocate(name, template, serviceOffering, networks, plan, null);
secStorageVm = _secStorageVmDao.findById(secStorageVm.getId());
} catch (InsufficientCapacityException e) {
String errorMessage = String.format("Unable to allocate secondary storage VM [%s] due to [%s].", name, e.getMessage());
s_logger.warn(errorMessage, e);
throw new CloudRuntimeException(errorMessage, e);
}
Map<String, Object> context = new HashMap<>();
context.put("secStorageVmId", secStorageVm.getId());
return context;
}
use of com.cloud.vm.SecondaryStorageVmVO in project cloudstack by apache.
the class SecondaryStorageManagerImpl method startSecStorageVm.
@Override
public SecondaryStorageVmVO startSecStorageVm(long secStorageVmId) {
try {
SecondaryStorageVmVO secStorageVm = _secStorageVmDao.findById(secStorageVmId);
_itMgr.advanceStart(secStorageVm.getUuid(), null, null);
return _secStorageVmDao.findById(secStorageVm.getId());
} catch (ConcurrentOperationException | InsufficientCapacityException | OperationTimedoutException | ResourceUnavailableException e) {
s_logger.warn(String.format("Unable to start secondary storage VM [%s] due to [%s].", secStorageVmId, e.getMessage()), e);
return null;
}
}
use of com.cloud.vm.SecondaryStorageVmVO in project cloudstack by apache.
the class SecondaryStorageManagerImpl method generateVMSetupCommand.
@Override
public boolean generateVMSetupCommand(Long ssAHostId) {
HostVO ssAHost = _hostDao.findById(ssAHostId);
if (ssAHost.getType() != Host.Type.SecondaryStorageVM) {
return false;
}
String ssvmName = ssAHost.getName();
SecondaryStorageVmVO secStorageVm = _secStorageVmDao.findByInstanceName(ssvmName);
if (secStorageVm == null) {
s_logger.warn(String.format("Secondary storage VM [%s] does not exist.", ssvmName));
return false;
}
SecStorageVMSetupCommand setupCmd = new SecStorageVMSetupCommand();
if (_allowedInternalSites != null) {
List<String> allowedCidrs = new ArrayList<>();
String[] cidrs = _allowedInternalSites.split(",");
for (String cidr : cidrs) {
if (NetUtils.isValidIp4Cidr(cidr) || NetUtils.isValidIp4(cidr) || !cidr.startsWith("0.0.0.0")) {
allowedCidrs.add(cidr);
}
}
setupCmd.setAllowedInternalSites(allowedCidrs.toArray(new String[allowedCidrs.size()]));
}
String copyPasswd = _configDao.getValue("secstorage.copy.password");
setupCmd.setCopyPassword(copyPasswd);
setupCmd.setCopyUserName(TemplateConstants.DEFAULT_HTTP_AUTH_USER);
Answer answer = _agentMgr.easySend(ssAHostId, setupCmd);
if (answer != null && answer.getResult()) {
if (s_logger.isDebugEnabled()) {
s_logger.debug(String.format("Successfully set HTTP auth into secondary storage VM [%s].", ssvmName));
}
return true;
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug(String.format("Failed to set HTTP auth into secondary storage VM [%s] due to [%s].", ssvmName, answer == null ? "answer null" : answer.getDetails()));
}
return false;
}
}
use of com.cloud.vm.SecondaryStorageVmVO in project cloudstack by apache.
the class SecondaryStorageManagerImpl method finalizeStart.
@Override
public boolean finalizeStart(VirtualMachineProfile profile, long hostId, Commands cmds, ReservationContext context) {
CheckSshAnswer answer = (CheckSshAnswer) cmds.getAnswer("checkSsh");
if (!answer.getResult()) {
s_logger.warn(String.format("Unable to connect via SSH to the VM [%s] due to [%s] ", profile.toString(), answer.getDetails()));
return false;
}
try {
_rulesMgr.getSystemIpAndEnableStaticNatForVm(profile.getVirtualMachine(), false);
IPAddressVO ipaddr = _ipAddressDao.findByAssociatedVmId(profile.getVirtualMachine().getId());
if (ipaddr != null && ipaddr.getSystem()) {
SecondaryStorageVmVO secVm = _secStorageVmDao.findById(profile.getId());
secVm.setPublicIpAddress(ipaddr.getAddress().addr());
_secStorageVmDao.update(secVm.getId(), secVm);
}
} catch (InsufficientAddressCapacityException ex) {
s_logger.error(String.format("Failed to get system IP and enable static NAT for the VM [%s] due to [%s].", profile.toString(), ex.getMessage()), ex);
return false;
}
return true;
}
Aggregations