Welcome, Guest
Username: Password: Remember me
Archived CloudStack Forums for reference purposes please direct all future inquiries to the Apache CloudStack mailing lists.


TOPIC: VM Allocation : Filter hosts according present VMs

VM Allocation : Filter hosts according present VMs 1 year 2 weeks ago #12809

  • nfo
  • nfo's Avatar
  • Fresh Boarder
  • Posts: 1
  • Karma: 0

First of all, sorry if my question is trivial, but this is my first day with cloudstak.

For my test, the goal is to install a new VM on a host if only a similar VM is not yet install on this host.
Consequently, at the end, I try to obtain severals hosts with differents (soft) VMs on them.

For that, I wrote the following code but, I don't know how to obtain from the host the VMs which are already installed and swited on on it. Is it possible or does it exist a workaround ?

I continue to look for ...
but feel free to give me a direction or documentation to solve this issue

Thanks in advance,
package fr.test.cloudstack;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ejb.Local;
import com.cloud.agent.manager.allocator.HostAllocator;
import com.cloud.deploy.DeploymentPlan;
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
import com.cloud.host.Host;
import com.cloud.host.Host.Type;
import com.cloud.host.dao.HostDao;
import com.cloud.offering.ServiceOffering;
import com.cloud.resource.ResourceManager;
import com.cloud.uservm.UserVm;
import com.cloud.utils.component.ComponentLocator;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachineProfile;
import com.cloud.host.HostVO;
@Local(value = { HostAllocator.class })
public class TestingAllocator implements HostAllocator {
	// Attributes
	private String _name;
	public List<Host> allocateTo(
			VirtualMachineProfile<? extends VirtualMachine> vmProfile,
			DeploymentPlan plan, Type type, ExcludeList avoid, int returnUpTo) {
		return allocateTo(vmProfile, plan, type, avoid, returnUpTo, true);
	public List<Host> allocateTo(
			VirtualMachineProfile<? extends VirtualMachine> vmProfile,
			DeploymentPlan plan, Type type, ExcludeList avoid, int returnUpTo,
			boolean considerReservedCapacity) {
		// Information
		long dcId = plan.getDataCenterId();
		long podId = plan.getPodId();
		long clusterId = plan.getClusterId();
		ServiceOffering serviceOffering = vmProfile.getServiceOffering();
		if (type == Host.Type.Storage || type == Host.Type.Routing)
			return new ArrayList<Host>();
		// 2) Retrieve available hosts
		ComponentLocator _locator = ComponentLocator.getCurrentLocator();
		//ResourceManager manager = _locator.getManager(ResourceManager.class) ;
		HostDao _hostDao = _locator.getDao(HostDao.class);
		List<HostVO> allHosts = _hostDao.listAll(); // _hostDao.listByHostTag(type, clusterId, podId, dcId, hostTag) ;
		// 3) Filtering hosts
		List<Host> availableHostsForCassandra = new ArrayList<Host>();
		for (HostVO hostVO : allHosts) {
			boolean isAvailable = hostVO.isAvailable();
			String hostname = hostVO.getName();
			// long hostId = hostVO.getId() ;
			boolean hasAlreadyACassandraVM = false;
			// See if a VM has already Cassandra otherwise not take it
			Map<String, String> details = hostVO.getDetails();
			List<String> tags = hostVO.getHostTags();
			// hasAlreadyACassandraVM = true ;
			if (isAvailable && !hasAlreadyACassandraVM && hostname != null
					&& hostname.startsWith("Cassandra")) {
		return availableHostsForCassandra;
	@Override // La méthode configure qui va être appelée en premier
	public boolean configure(String name, Map<String, Object> params) {
		this._name = name;
		return true;
	public String getName() {
		return this._name;
	public boolean start() {
		return true;
	public boolean stop() {
		return true;
	public boolean isVirtualMachineUpgradable(UserVm arg0, ServiceOffering arg1) {
		return true;
The administrator has disabled public write access.
Time to create page: 0.312 seconds


Citrix supports the open source community via developer support and evangeslism. We have a number of developers and evangelists that participate actively in the open source community in Apache Cloudstack, OpenDaylight, Xen Project and XenServer. We also conduct educational activities via the Build A Cloud events held all over the world.