use of com.cloud.exception.DiscoveryException in project cloudstack by apache.
the class StorageManagerImpl method discoverImageStore.
@Override
public ImageStore discoverImageStore(String name, String url, String providerName, Long zoneId, Map details) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException {
DataStoreProvider storeProvider = _dataStoreProviderMgr.getDataStoreProvider(providerName);
if (storeProvider == null) {
storeProvider = _dataStoreProviderMgr.getDefaultImageDataStoreProvider();
if (storeProvider == null) {
throw new InvalidParameterValueException("can't find image store provider: " + providerName);
}
// ignored passed provider name and use default image store provider name
providerName = storeProvider.getName();
}
ScopeType scopeType = ScopeType.ZONE;
if (zoneId == null) {
scopeType = ScopeType.REGION;
}
if (name == null) {
name = url;
}
ImageStoreVO imageStore = _imageStoreDao.findByName(name);
if (imageStore != null) {
throw new InvalidParameterValueException("The image store with name " + name + " already exists, try creating with another name");
}
// check if scope is supported by store provider
if (!((ImageStoreProvider) storeProvider).isScopeSupported(scopeType)) {
throw new InvalidParameterValueException("Image store provider " + providerName + " does not support scope " + scopeType);
}
// check if we have already image stores from other different providers,
// we currently are not supporting image stores from different
// providers co-existing
List<ImageStoreVO> imageStores = _imageStoreDao.listImageStores();
for (ImageStoreVO store : imageStores) {
if (!store.getProviderName().equalsIgnoreCase(providerName)) {
throw new InvalidParameterValueException("You can only add new image stores from the same provider " + store.getProviderName() + " already added");
}
}
if (zoneId != null) {
// Check if the zone exists in the system
DataCenterVO zone = _dcDao.findById(zoneId);
if (zone == null) {
throw new InvalidParameterValueException("Can't find zone by id " + zoneId);
}
Account account = CallContext.current().getCallingAccount();
if (Grouping.AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(account.getId())) {
PermissionDeniedException ex = new PermissionDeniedException("Cannot perform this operation, Zone with specified id is currently disabled");
ex.addProxyObject(zone.getUuid(), "dcId");
throw ex;
}
}
Map<String, Object> params = new HashMap();
params.put("zoneId", zoneId);
params.put("url", url);
params.put("name", name);
params.put("details", details);
params.put("scope", scopeType);
params.put("providerName", storeProvider.getName());
params.put("role", DataStoreRole.Image);
DataStoreLifeCycle lifeCycle = storeProvider.getDataStoreLifeCycle();
DataStore store;
try {
store = lifeCycle.initialize(params);
} catch (Exception e) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Failed to add data store: " + e.getMessage(), e);
}
throw new CloudRuntimeException("Failed to add data store: " + e.getMessage(), e);
}
if (((ImageStoreProvider) storeProvider).needDownloadSysTemplate()) {
// trigger system vm template download
_imageSrv.downloadBootstrapSysTemplate(store);
} else {
// populate template_store_ref table
_imageSrv.addSystemVMTemplatesToSecondary(store);
}
// associate builtin template with zones associated with this image store
associateCrosszoneTemplatesToZone(zoneId);
// duplicate cache store records to region wide storage
if (scopeType == ScopeType.REGION) {
duplicateCacheStoreRecordsToRegionStore(store.getId());
}
return (ImageStore) _dataStoreMgr.getDataStore(store.getId(), DataStoreRole.Image);
}
use of com.cloud.exception.DiscoveryException in project cloudstack by apache.
the class LibvirtServerDiscoverer method find.
@Override
public Map<? extends ServerResource, Map<String, String>> find(long dcId, Long podId, Long clusterId, URI uri, String username, String password, List<String> hostTags) throws DiscoveryException {
ClusterVO cluster = _clusterDao.findById(clusterId);
if (cluster == null || cluster.getHypervisorType() != getHypervisorType()) {
if (s_logger.isInfoEnabled())
s_logger.info("invalid cluster id or cluster is not for " + getHypervisorType() + " hypervisors");
return null;
}
Map<KvmDummyResourceBase, Map<String, String>> resources = new HashMap<KvmDummyResourceBase, Map<String, String>>();
Map<String, String> details = new HashMap<String, String>();
if (!uri.getScheme().equals("http")) {
String msg = "urlString is not http so we're not taking care of the discovery for this: " + uri;
s_logger.debug(msg);
return null;
}
com.trilead.ssh2.Connection sshConnection = null;
String agentIp = null;
try {
String hostname = uri.getHost();
InetAddress ia = InetAddress.getByName(hostname);
agentIp = ia.getHostAddress();
String guid = UUID.nameUUIDFromBytes(agentIp.getBytes()).toString();
List<HostVO> existingHosts = _resourceMgr.listAllHostsInOneZoneByType(Host.Type.Routing, dcId);
if (existingHosts != null) {
for (HostVO existingHost : existingHosts) {
if (existingHost.getGuid().toLowerCase().startsWith(guid.toLowerCase())) {
s_logger.debug("Skipping " + agentIp + " because " + guid + " is already in the database for resource " + existingHost.getGuid());
return null;
}
}
}
sshConnection = new com.trilead.ssh2.Connection(agentIp, 22);
sshConnection.connect(null, 60000, 60000);
if (!sshConnection.authenticateWithPassword(username, password)) {
s_logger.debug("Failed to authenticate");
throw new DiscoveredWithErrorException("Authentication error");
}
if (!SSHCmdHelper.sshExecuteCmd(sshConnection, "lsmod|grep kvm", 3)) {
s_logger.debug("It's not a KVM enabled machine");
return null;
}
List<PhysicalNetworkSetupInfo> netInfos = _networkMgr.getPhysicalNetworkInfo(dcId, getHypervisorType());
String kvmPrivateNic = null;
String kvmPublicNic = null;
String kvmGuestNic = null;
for (PhysicalNetworkSetupInfo info : netInfos) {
if (info.getPrivateNetworkName() != null) {
kvmPrivateNic = info.getPrivateNetworkName();
}
if (info.getPublicNetworkName() != null) {
kvmPublicNic = info.getPublicNetworkName();
}
if (info.getGuestNetworkName() != null) {
kvmGuestNic = info.getGuestNetworkName();
}
}
if (kvmPrivateNic == null && kvmPublicNic == null && kvmGuestNic == null) {
kvmPrivateNic = _kvmPrivateNic;
kvmPublicNic = _kvmPublicNic;
kvmGuestNic = _kvmGuestNic;
}
if (kvmPublicNic == null) {
kvmPublicNic = (kvmGuestNic != null) ? kvmGuestNic : kvmPrivateNic;
}
if (kvmPrivateNic == null) {
kvmPrivateNic = (kvmPublicNic != null) ? kvmPublicNic : kvmGuestNic;
}
if (kvmGuestNic == null) {
kvmGuestNic = (kvmPublicNic != null) ? kvmPublicNic : kvmPrivateNic;
}
String parameters = " -m " + _hostIp + " -z " + dcId + " -p " + podId + " -c " + clusterId + " -g " + guid + " -a";
parameters += " --pubNic=" + kvmPublicNic;
parameters += " --prvNic=" + kvmPrivateNic;
parameters += " --guestNic=" + kvmGuestNic;
parameters += " --hypervisor=" + cluster.getHypervisorType().toString().toLowerCase();
String setupAgentCommand = "cloudstack-setup-agent ";
if (!username.equals("root")) {
setupAgentCommand = "sudo cloudstack-setup-agent ";
}
if (!SSHCmdHelper.sshExecuteCmd(sshConnection, setupAgentCommand + parameters, 3)) {
s_logger.info("cloudstack agent setup command failed: " + setupAgentCommand + parameters);
return null;
}
KvmDummyResourceBase kvmResource = new KvmDummyResourceBase();
Map<String, Object> params = new HashMap<String, Object>();
params.put("router.aggregation.command.each.timeout", _configDao.getValue(Config.RouterAggregationCommandEachTimeout.toString()));
params.put("zone", Long.toString(dcId));
params.put("pod", Long.toString(podId));
params.put("cluster", Long.toString(clusterId));
params.put("guid", guid);
params.put("agentIp", agentIp);
kvmResource.configure("kvm agent", params);
resources.put(kvmResource, details);
HostVO connectedHost = waitForHostConnect(dcId, podId, clusterId, guid);
if (connectedHost == null)
return null;
details.put("guid", connectedHost.getGuid());
// place a place holder guid derived from cluster ID
if (cluster.getGuid() == null) {
cluster.setGuid(UUID.nameUUIDFromBytes(String.valueOf(clusterId).getBytes()).toString());
_clusterDao.update(clusterId, cluster);
}
// save user name and password
_hostDao.loadDetails(connectedHost);
Map<String, String> hostDetails = connectedHost.getDetails();
hostDetails.put("password", password);
hostDetails.put("username", username);
_hostDao.saveDetails(connectedHost);
return resources;
} catch (DiscoveredWithErrorException e) {
throw e;
} catch (Exception e) {
String msg = " can't setup agent, due to " + e.toString() + " - " + e.getMessage();
s_logger.warn(msg);
} finally {
if (sshConnection != null)
sshConnection.close();
}
return null;
}
use of com.cloud.exception.DiscoveryException in project cloudstack by apache.
the class AddImageStoreS3CMD method execute.
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
Map<String, String> dm = new HashMap();
dm.put(ApiConstants.S3_ACCESS_KEY, getAccessKey());
dm.put(ApiConstants.S3_SECRET_KEY, getSecretKey());
dm.put(ApiConstants.S3_END_POINT, getEndPoint());
dm.put(ApiConstants.S3_BUCKET_NAME, getBucketName());
if (getSigner() != null && (getSigner().equals(ApiConstants.S3_V3_SIGNER) || getSigner().equals(ApiConstants.S3_V4_SIGNER))) {
dm.put(ApiConstants.S3_SIGNER, getSigner());
}
if (isHttps() != null) {
dm.put(ApiConstants.S3_HTTPS_FLAG, isHttps().toString());
}
if (getConnectionTimeout() != null) {
dm.put(ApiConstants.S3_CONNECTION_TIMEOUT, getConnectionTimeout().toString());
}
if (getMaxErrorRetry() != null) {
dm.put(ApiConstants.S3_MAX_ERROR_RETRY, getMaxErrorRetry().toString());
}
if (getSocketTimeout() != null) {
dm.put(ApiConstants.S3_SOCKET_TIMEOUT, getSocketTimeout().toString());
}
if (getConnectionTtl() != null) {
dm.put(ApiConstants.S3_CONNECTION_TTL, getConnectionTtl().toString());
}
if (getUseTCPKeepAlive() != null) {
dm.put(ApiConstants.S3_USE_TCP_KEEPALIVE, getUseTCPKeepAlive().toString());
}
try {
ImageStore result = _storageService.discoverImageStore(null, null, "S3", null, dm);
ImageStoreResponse storeResponse;
if (result != null) {
storeResponse = _responseGenerator.createImageStoreResponse(result);
storeResponse.setResponseName(getCommandName());
storeResponse.setObjectName("imagestore");
setResponseObject(storeResponse);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add S3 Image Store.");
}
} catch (DiscoveryException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
}
}
use of com.cloud.exception.DiscoveryException in project cloudstack by apache.
the class DirectAgentManagerSimpleImpl method loadResource.
protected void loadResource(Long hostId) {
HostVO host = hostDao.findById(hostId);
Map<String, Object> params = new HashMap<String, Object>();
params.put("guid", host.getGuid());
params.put("ipaddress", host.getPrivateIpAddress());
params.put("username", "root");
params.put("password", "password");
params.put("zone", String.valueOf(host.getDataCenterId()));
params.put("pod", String.valueOf(host.getPodId()));
ServerResource resource = null;
if (host.getHypervisorType() == HypervisorType.XenServer) {
resource = new XcpOssResource();
try {
resource.configure(host.getName(), params);
} catch (ConfigurationException e) {
logger.debug("Failed to load resource:" + e.toString());
}
} else if (host.getHypervisorType() == HypervisorType.KVM) {
resource = new LibvirtComputingResource();
try {
params.put("public.network.device", "cloudbr0");
params.put("private.network.device", "cloudbr0");
resource.configure(host.getName(), params);
} catch (ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (host.getHypervisorType() == HypervisorType.VMware) {
ClusterVO cluster = clusterDao.findById(host.getClusterId());
String url = clusterDetailsDao.findDetail(cluster.getId(), "url").getValue();
URI uri;
try {
uri = new URI(url);
String userName = clusterDetailsDao.findDetail(cluster.getId(), "username").getValue();
String password = clusterDetailsDao.findDetail(cluster.getId(), "password").getValue();
VmwareServerDiscoverer discover = new VmwareServerDiscoverer();
Map<? extends ServerResource, Map<String, String>> resources = discover.find(host.getDataCenterId(), host.getPodId(), host.getClusterId(), uri, userName, password, null);
for (Map.Entry<? extends ServerResource, Map<String, String>> entry : resources.entrySet()) {
resource = entry.getKey();
}
if (resource == null) {
throw new CloudRuntimeException("can't find resource");
}
} catch (DiscoveryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
hostResourcesMap.put(hostId, resource);
HostEnvironment env = new HostEnvironment();
SetupCommand cmd = new SetupCommand(env);
cmd.setNeedSetup(true);
resource.executeRequest(cmd);
}
Aggregations