Search in sources :

Example 21 with State

use of com.cloud.vm.VirtualMachine.State in project cloudstack by apache.

the class VMSnapshotManagerImpl method listVMSnapshots.

@Override
public Pair<List<? extends VMSnapshot>, Integer> listVMSnapshots(ListVMSnapshotCmd cmd) {
    Account caller = getCaller();
    List<Long> permittedAccounts = new ArrayList<Long>();
    boolean listAll = cmd.listAll();
    Long id = cmd.getId();
    Long vmId = cmd.getVmId();
    String state = cmd.getState();
    String keyword = cmd.getKeyword();
    String name = cmd.getVmSnapshotName();
    String accountName = cmd.getAccountName();
    List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds());
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, listAll, false);
    Long domainId = domainIdRecursiveListProject.first();
    Boolean isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter searchFilter = new Filter(VMSnapshotVO.class, "created", false, cmd.getStartIndex(), cmd.getPageSizeVal());
    SearchBuilder<VMSnapshotVO> sb = _vmSnapshotDao.createSearchBuilder();
    _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("vm_id", sb.entity().getVmId(), SearchCriteria.Op.EQ);
    sb.and("domain_id", sb.entity().getDomainId(), SearchCriteria.Op.EQ);
    sb.and("status", sb.entity().getState(), SearchCriteria.Op.IN);
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("idIN", sb.entity().getId(), SearchCriteria.Op.IN);
    sb.and("display_name", sb.entity().getDisplayName(), SearchCriteria.Op.EQ);
    sb.and("account_id", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
    sb.done();
    SearchCriteria<VMSnapshotVO> sc = sb.create();
    _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (accountName != null && cmd.getDomainId() != null) {
        Account account = _accountMgr.getActiveAccountByName(accountName, cmd.getDomainId());
        sc.setParameters("account_id", account.getId());
    }
    if (vmId != null) {
        sc.setParameters("vm_id", vmId);
    }
    setIdsListToSearchCriteria(sc, ids);
    if (domainId != null) {
        sc.setParameters("domain_id", domainId);
    }
    if (state == null) {
        VMSnapshot.State[] status = { VMSnapshot.State.Ready, VMSnapshot.State.Creating, VMSnapshot.State.Allocated, VMSnapshot.State.Error, VMSnapshot.State.Expunging, VMSnapshot.State.Reverting };
        sc.setParameters("status", (Object[]) status);
    } else {
        sc.setParameters("state", state);
    }
    if (name != null) {
        sc.setParameters("display_name", name);
    }
    if (keyword != null) {
        SearchCriteria<VMSnapshotVO> ssc = _vmSnapshotDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("display_name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    Pair<List<VMSnapshotVO>, Integer> searchAndCount = _vmSnapshotDao.searchAndCount(sc, searchFilter);
    return new Pair<List<? extends VMSnapshot>, Integer>(searchAndCount.first(), searchAndCount.second());
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) State(com.cloud.vm.VirtualMachine.State) List(java.util.List) ArrayList(java.util.ArrayList) Pair(com.cloud.utils.Pair)

Example 22 with State

use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

private synchronized Answer execute(MigrateCommand cmd) {
    String vmName = cmd.getVmName();
    State state = null;
    String result = null;
    synchronized (_vms) {
        state = _vms.get(vmName);
        _vms.put(vmName, State.Stopping);
    }
    Domain dm = null;
    Connect dconn = null;
    Domain destDomain = null;
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
        dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        dconn = new Connect("qemu+tcp://" + cmd.getDestinationIp() + "/system");
        /*
			 * Hard code lm flags: VIR_MIGRATE_LIVE(1<<0) and
			 * VIR_MIGRATE_PERSIST_DEST(1<<3)
			 */
        destDomain = dm.migrate(dconn, (1 << 0) | (1 << 3), vmName, "tcp:" + cmd.getDestinationIp(), _migrateSpeed);
    } catch (LibvirtException e) {
        s_logger.debug("Can't migrate domain: " + e.getMessage());
        result = e.getMessage();
    } catch (Exception e) {
        s_logger.debug("Can't migrate domain: " + e.getMessage());
        result = e.getMessage();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
            if (dconn != null) {
                dconn.close();
            }
            if (destDomain != null) {
                destDomain.free();
            }
        } catch (final LibvirtException e) {
        }
    }
    if (result != null) {
        synchronized (_vms) {
            _vms.put(vmName, state);
        }
    } else {
        destroy_network_rules_for_vm(conn, vmName);
        cleanupVM(conn, vmName, getVnetId(VirtualMachineName.getVnet(vmName)));
    }
    return new MigrateAnswer(cmd, result == null, result, null);
}
Also used : MigrateAnswer(com.cloud.agent.api.MigrateAnswer) LibvirtException(org.libvirt.LibvirtException) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) Domain(org.libvirt.Domain) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException)

Example 23 with State

use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.

the class CloudZonesComputingResource method execute.

@Override
protected synchronized StartAnswer execute(StartCommand cmd) {
    VirtualMachineTO vmSpec = cmd.getVirtualMachine();
    String vmName = vmSpec.getName();
    LibvirtVMDef vm = null;
    State state = State.Stopped;
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
        synchronized (_vms) {
            _vms.put(vmName, State.Starting);
        }
        vm = createVMFromSpec(vmSpec);
        createVbd(conn, vmSpec, vmName, vm);
        createVifs(conn, vmSpec, vm);
        s_logger.debug("starting " + vmName + ": " + vm.toString());
        startDomain(conn, vmName, vm.toString());
        NicTO[] nics = vmSpec.getNics();
        for (NicTO nic : nics) {
            if (nic.isSecurityGroupEnabled()) {
                if (vmSpec.getType() != VirtualMachine.Type.User) {
                    default_network_rules_for_systemvm(conn, vmName);
                } else {
                    nic.setIp(null);
                    default_network_rules(conn, vmName, nic, vmSpec.getId());
                }
            }
        }
        // attached disk
        for (DiskDef disk : vm.getDevices().getDisks()) {
            if (disk.isAttachDeferred()) {
                attachOrDetachDevice(conn, true, vmName, disk.toString());
            }
        }
        if (vmSpec.getType() == VirtualMachine.Type.User) {
            for (NicTO nic : nics) {
                if (nic.getType() == TrafficType.Guest) {
                    InetAddress ipAddr = _dhcpSnooper.getIPAddr(nic.getMac(), vmName);
                    if (ipAddr == null) {
                        s_logger.debug("Failed to get guest DHCP ip, stop it");
                        StopCommand stpCmd = new StopCommand(vmName);
                        execute(stpCmd);
                        return new StartAnswer(cmd, "Failed to get guest DHCP ip, stop it");
                    }
                    s_logger.debug(ipAddr);
                    nic.setIp(ipAddr.getHostAddress());
                    post_default_network_rules(conn, vmName, nic, vmSpec.getId(), _dhcpSnooper.getDhcpServerIP(), _hostIp, _hostMacAddress);
                    _vmDataServer.handleVmStarted(cmd.getVirtualMachine());
                }
            }
        }
        state = State.Running;
        return new StartAnswer(cmd);
    } catch (Exception e) {
        s_logger.warn("Exception ", e);
        if (conn != null) {
            handleVmStartFailure(conn, vmName, vm);
        }
        return new StartAnswer(cmd, e.getMessage());
    } finally {
        synchronized (_vms) {
            if (state != State.Stopped) {
                _vms.put(vmName, state);
            } else {
                _vms.remove(vmName);
            }
        }
    }
}
Also used : LibvirtVMDef(com.cloud.agent.resource.computing.LibvirtVMDef) DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) StopCommand(com.cloud.agent.api.StopCommand) StartAnswer(com.cloud.agent.api.StartAnswer) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) InetAddress(java.net.InetAddress) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) ConfigurationException(javax.naming.ConfigurationException) LibvirtException(org.libvirt.LibvirtException) NicTO(com.cloud.agent.api.to.NicTO)

Example 24 with State

use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.

the class FakeComputingResource method sync.

protected HashMap<String, State> sync() {
    Map<String, State> newStates;
    Map<String, State> oldStates = null;
    HashMap<String, State> changes = new HashMap<String, State>();
    synchronized (_vms) {
        newStates = getVmManager().getVmStates();
        oldStates = new HashMap<String, State>(_vms.size());
        oldStates.putAll(_vms);
        for (Map.Entry<String, State> entry : newStates.entrySet()) {
            String vm = entry.getKey();
            State newState = entry.getValue();
            State oldState = oldStates.remove(vm);
            if (s_logger.isTraceEnabled()) {
                s_logger.trace("VM " + vm + ": xen has state " + newState + " and we have state " + (oldState != null ? oldState.toString() : "null"));
            }
            if (oldState == null) {
                _vms.put(vm, newState);
                changes.put(vm, newState);
            } else if (oldState == State.Starting) {
                if (newState == State.Running) {
                    _vms.put(vm, newState);
                } else if (newState == State.Stopped) {
                    s_logger.debug("Ignoring vm " + vm + " because of a lag in starting the vm.");
                }
            } else if (oldState == State.Stopping) {
                if (newState == State.Stopped) {
                    _vms.put(vm, newState);
                } else if (newState == State.Running) {
                    s_logger.debug("Ignoring vm " + vm + " because of a lag in stopping the vm. ");
                }
            } else if (oldState != newState) {
                _vms.put(vm, newState);
                changes.put(vm, newState);
            }
        }
        for (Map.Entry<String, State> entry : oldStates.entrySet()) {
            String vm = entry.getKey();
            State oldState = entry.getValue();
            if (s_logger.isTraceEnabled()) {
                s_logger.trace("VM " + vm + " is now missing from xen so reporting stopped");
            }
            if (oldState == State.Stopping) {
                s_logger.debug("Ignoring VM " + vm + " in transition state stopping.");
                _vms.remove(vm);
            } else if (oldState == State.Starting) {
                s_logger.debug("Ignoring VM " + vm + " in transition state starting.");
            } else if (oldState == State.Stopped) {
                _vms.remove(vm);
            } else {
                changes.put(entry.getKey(), State.Stopped);
            }
        }
    }
    return changes;
}
Also used : HashMap(java.util.HashMap) VmState(com.cloud.agent.api.StartupRoutingCommand.VmState) State(com.cloud.vm.VirtualMachine.State) Map(java.util.Map) HashMap(java.util.HashMap)

Example 25 with State

use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.

the class FakeComputingResource method execute.

protected synchronized StopAnswer execute(StopCommand cmd) {
    VmMgr vmMgr = getVmManager();
    StopAnswer answer = null;
    String vmName = cmd.getVmName();
    Integer port = vmMgr.getVncPort(vmName);
    Long bytesReceived = null;
    Long bytesSent = null;
    State state = null;
    synchronized (_vms) {
        state = _vms.get(vmName);
        _vms.put(vmName, State.Stopping);
    }
    try {
        String result = vmMgr.stopVM(vmName, false);
        if (result != null) {
            s_logger.info("Trying destroy on " + vmName);
            if (result == Script.ERR_TIMEOUT) {
                result = vmMgr.stopVM(vmName, true);
            }
            s_logger.warn("Couldn't stop " + vmName);
            if (result != null) {
                return new StopAnswer(cmd, result);
            }
        }
        answer = new StopAnswer(cmd, null, port, bytesSent, bytesReceived);
        String result2 = vmMgr.cleanupVnet(cmd.getVnet());
        if (result2 != null) {
            result = result2 + (result != null ? ("\n" + result) : "");
            answer = new StopAnswer(cmd, result, port, bytesSent, bytesReceived);
        }
        _dhcpSnooper.cleanup(vmName, null);
        return answer;
    } finally {
        if (answer == null || !answer.getResult()) {
            synchronized (_vms) {
                _vms.put(vmName, state);
            }
        }
    }
}
Also used : VmState(com.cloud.agent.api.StartupRoutingCommand.VmState) State(com.cloud.vm.VirtualMachine.State) StopAnswer(com.cloud.agent.api.StopAnswer) VmMgr(com.cloud.agent.mockvm.VmMgr) MockVmMgr(com.cloud.agent.mockvm.MockVmMgr)

Aggregations

State (com.cloud.vm.VirtualMachine.State)45 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)14 ConfigurationException (javax.naming.ConfigurationException)11 HashMap (java.util.HashMap)9 PowerState (com.cloud.vm.VirtualMachine.PowerState)8 LibvirtException (org.libvirt.LibvirtException)8 CheckVirtualMachineAnswer (com.cloud.agent.api.CheckVirtualMachineAnswer)7 StartAnswer (com.cloud.agent.api.StartAnswer)7 StopAnswer (com.cloud.agent.api.StopAnswer)7 Map (java.util.Map)7 Connect (org.libvirt.Connect)7 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 Ovm3ResourceException (com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException)5 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 VmState (com.cloud.agent.api.StartupRoutingCommand.VmState)4 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)4 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)4 Answer (com.cloud.agent.api.Answer)3 NicTO (com.cloud.agent.api.to.NicTO)3