fix simulator

This commit is contained in:
Edison Su 2013-07-08 18:43:00 -07:00
parent 94aa67eae0
commit 5e13327339
4 changed files with 94 additions and 105 deletions

View File

@ -29,6 +29,8 @@ import com.cloud.agent.api.PingTestCommand;
import com.cloud.api.commands.SimulatorAddSecondaryAgent;
import com.cloud.dc.dao.HostPodDao;
import com.cloud.exception.DiscoveryException;
import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.resource.*;
import com.cloud.simulator.MockHost;
import com.cloud.simulator.MockHostVO;
@ -81,6 +83,8 @@ public class MockAgentManagerImpl extends ManagerBase implements MockAgentManage
ResourceManager _resourceMgr;
@Inject
SimulatorSecondaryDiscoverer discoverer;
@Inject
HostDao hostDao;
private SecureRandom random;
private final Map<String, AgentResourceBase> _resources = new ConcurrentHashMap<String, AgentResourceBase>();
private ThreadPoolExecutor _executor;
@ -250,10 +254,8 @@ public class MockAgentManagerImpl extends ManagerBase implements MockAgentManage
this.mode = "Stop";
}
@Override
@DB
public void run() {
private void handleSystemVMStop() {
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
try {
if (this.mode.equalsIgnoreCase("Stop")) {
@ -281,6 +283,25 @@ public class MockAgentManagerImpl extends ManagerBase implements MockAgentManage
txn.close();
}
//stop ssvm agent
HostVO host = hostDao.findByGuid(this.guid);
if (host != null) {
try {
_resourceMgr.deleteHost(host.getId(), true, true);
} catch (Exception e) {
s_logger.debug("Failed to delete host: ", e);
}
}
}
@Override
@DB
public void run() {
if (this.mode.equalsIgnoreCase("Stop")) {
handleSystemVMStop();
return;
}
String resource = null;
if (vmType.equalsIgnoreCase("secstorage")) {
resource = "com.cloud.agent.AgentStorageResource";

View File

@ -71,7 +71,10 @@ import com.cloud.storage.Storage.ImageFormat;
import com.cloud.storage.Storage.StoragePoolType;
import com.cloud.storage.VMTemplateStorageResourceAssoc;
import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.dao.VMTemplateDao;
import com.cloud.storage.template.TemplateProp;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.component.ManagerBase;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.exception.CloudRuntimeException;
@ -113,6 +116,8 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa
MockVMDao _mockVMDao = null;
@Inject
MockHostDao _mockHostDao = null;
@Inject
VMTemplateDao templateDao;
private MockVolumeVO findVolumeFromSecondary(String path, String ssUrl, MockVolumeType type) {
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
@ -474,26 +479,12 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa
if ( !(store instanceof NfsTO )){
return new Answer(cmd, false, "Unsupported image data store: " + store);
}
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
MockSecStorageVO storage = null;
String nfsUrl = ((NfsTO) store).getUrl();
try {
txn.start();
storage = _mockSecStorageDao.findByUrl(nfsUrl);
if (storage == null) {
return new Answer(cmd, false, "Failed to get secondary storage");
}
txn.commit();
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Error when finding sec storage " + nfsUrl, ex);
} finally {
txn.close();
txn = Transaction.open(Transaction.CLOUD_DB);
txn.close();
}
txn = Transaction.open(Transaction.SIMULATOR_DB);
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
storage = _mockSecStorageDao.findByUrl(nfsUrl);
try {
txn.start();
List<MockVolumeVO> templates = _mockVolumeDao.findByStorageIdAndType(storage.getId(),
@ -504,7 +495,6 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa
templateInfos.put(template.getName(), new TemplateProp(template.getName(), template.getPath()
.replaceAll(storage.getMountPoint(), ""), template.getSize(), template.getSize(), true, false));
}
txn.commit();
return new ListTemplateAnswer(nfsUrl, templateInfos);
} catch (Exception ex) {
txn.rollback();
@ -889,90 +879,63 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa
@Override
public void preinstallTemplates(String url, long zoneId) {
MockSecStorageVO storage = null;
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
try {
txn.start();
storage = _mockSecStorageDao.findByUrl(url);
if (storage == null) {
storage = new MockSecStorageVO();
URI uri;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
return;
}
String nfsHost = uri.getHost();
String nfsPath = uri.getPath();
String path = nfsHost + ":" + nfsPath;
String dir = "/mnt/" + UUID.nameUUIDFromBytes(path.getBytes()).toString() + File.separator;
storage.setUrl(url);
storage.setCapacity(DEFAULT_HOST_STORAGE_SIZE);
storage.setMountPoint(dir);
storage = _mockSecStorageDao.persist(storage);
// preinstall default templates into secondary storage
long defaultTemplateSize = 2 * 1024 * 1024 * 1024L;
MockVolumeVO template = new MockVolumeVO();
template.setName("simulator-domR");
template.setPath(storage.getMountPoint() + "template/tmpl/1/100/" + UUID.randomUUID().toString());
template.setPoolId(storage.getId());
template.setSize(defaultTemplateSize);
template.setType(MockVolumeType.TEMPLATE);
template.setStatus(Status.DOWNLOADED);
template = _mockVolumeDao.persist(template);
template = new MockVolumeVO();
template.setName("simulator-Centos");
template.setPath(storage.getMountPoint() + "template/tmpl/1/111/" + UUID.randomUUID().toString());
template.setPoolId(storage.getId());
template.setSize(defaultTemplateSize);
template.setType(MockVolumeType.TEMPLATE);
template.setStatus(Status.DOWNLOADED);
template = _mockVolumeDao.persist(template);
txn.commit();
}
} catch (Exception ex) {
throw new CloudRuntimeException("Unable to find sec storage at " + url, ex);
} finally {
Transaction txn = Transaction.open(Transaction.CLOUD_DB);
txn = Transaction.open(Transaction.CLOUD_DB);
txn.close();
}
if (storage == null) {
storage = new MockSecStorageVO();
URI uri;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
return;
}
String nfsHost = uri.getHost();
String nfsPath = uri.getPath();
String path = nfsHost + ":" + nfsPath;
String dir = "/mnt/" + UUID.nameUUIDFromBytes(path.getBytes()).toString() + File.separator;
storage.setUrl(url);
storage.setCapacity(DEFAULT_HOST_STORAGE_SIZE);
storage.setMountPoint(dir);
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
try {
txn.start();
storage = _mockSecStorageDao.persist(storage);
txn.commit();
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Error when saving storage " + storage, ex);
} finally {
txn.close();
txn = Transaction.open(Transaction.CLOUD_DB);
txn.close();
}
// preinstall default templates into secondary storage
long defaultTemplateSize = 2 * 1024 * 1024 * 1024L;
MockVolumeVO template = new MockVolumeVO();
template.setName("simulator-domR");
template.setPath(storage.getMountPoint() + "template/tmpl/1/100/" + UUID.randomUUID().toString());
template.setPoolId(storage.getId());
template.setSize(defaultTemplateSize);
template.setType(MockVolumeType.TEMPLATE);
template.setStatus(Status.DOWNLOADED);
txn = Transaction.open(Transaction.SIMULATOR_DB);
try {
txn.start();
template = _mockVolumeDao.persist(template);
txn.commit();
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Error when saving template " + template, ex);
} finally {
txn.close();
txn = Transaction.open(Transaction.CLOUD_DB);
txn.close();
}
template = new MockVolumeVO();
template.setName("simulator-Centos");
template.setPath(storage.getMountPoint() + "template/tmpl/1/111/" + UUID.randomUUID().toString());
template.setPoolId(storage.getId());
template.setSize(defaultTemplateSize);
template.setType(MockVolumeType.TEMPLATE);
template.setStatus(Status.DOWNLOADED);
txn = Transaction.open(Transaction.SIMULATOR_DB);
try {
txn.start();
template = _mockVolumeDao.persist(template);
txn.commit();
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Error when saving template " + template, ex);
} finally {
txn.close();
txn = Transaction.open(Transaction.CLOUD_DB);
txn.close();
}
}
}
@Override

View File

@ -50,7 +50,7 @@ public class AgentStorageResource extends AgentResourceBase implements Secondary
}
public AgentStorageResource() {
setType(Host.Type.SecondaryStorage);
setType(Type.SecondaryStorageVM);
}
@Override

View File

@ -25,6 +25,10 @@ import javax.ejb.Local;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.dao.VMTemplateDao;
import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
import org.apache.cloudstack.storage.datastore.db.ImageStoreVO;
import org.apache.cloudstack.storage.resource.SecondaryStorageDiscoverer;
import org.apache.cloudstack.storage.resource.SecondaryStorageResource;
import org.apache.log4j.Logger;
@ -54,6 +58,8 @@ public class SimulatorSecondaryDiscoverer extends SecondaryStorageDiscoverer imp
@Inject AgentManager _agentMgr;
@Inject ResourceManager _resourceMgr;
@Inject SnapshotDao _snapshotDao;
@Inject
ImageStoreDao imageStoreDao;
protected SecondaryStorageResource resource;
public void setResource(SecondaryStorageResource resource) {
@ -73,6 +79,10 @@ public class SimulatorSecondaryDiscoverer extends SecondaryStorageDiscoverer imp
s_logger.debug("It's not NFS or file or ISO, so not a secondary storage server: " + uri.toString());
return null;
}
List<ImageStoreVO> stores = imageStoreDao.listImageStores();
for (ImageStoreVO store : stores) {
_mockStorageMgr.preinstallTemplates(store.getUrl(), dcId);
}
Map<SecondaryStorageResource, Map<String, String>> resources = new HashMap<SecondaryStorageResource, Map<String, String>>();
resources.put(this.resource, new HashMap<String, String>());
return resources;
@ -81,12 +91,7 @@ public class SimulatorSecondaryDiscoverer extends SecondaryStorageDiscoverer imp
@Override
public void postDiscovery(List<HostVO> hosts, long msId) {
for (HostVO host: hosts) {
if(s_logger.isDebugEnabled()) {
s_logger.debug("Preinstalling simulator templates");
}
_mockStorageMgr.preinstallTemplates(host.getStorageUrl(), host.getDataCenterId());
}
}
@Override