diff --git a/agent/src/com/cloud/agent/resource/computing/LibvirtComputingResource.java b/agent/src/com/cloud/agent/resource/computing/LibvirtComputingResource.java index 574b3669113..6ae752fd200 100644 --- a/agent/src/com/cloud/agent/resource/computing/LibvirtComputingResource.java +++ b/agent/src/com/cloud/agent/resource/computing/LibvirtComputingResource.java @@ -1189,10 +1189,9 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv s_logger.debug(result); return new CreateAnswer(cmd, result); } + + vol = createVolume(primaryPool, tmplVol); - LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(UUID.randomUUID().toString(), tmplVol.getInfo().capacity, volFormat.QCOW2, tmplVol.getPath(), volFormat.QCOW2); - s_logger.debug(volDef.toString()); - vol = primaryPool.storageVolCreateXML(volDef.toString(), 0); if (vol == null) { return new Answer(cmd, false, " Can't create storage volume on storage pool"); } @@ -1229,21 +1228,46 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv protected ManageSnapshotAnswer execute(final ManageSnapshotCommand cmd) { String snapshotName = cmd.getSnapshotName(); String VolPath = cmd.getVolumePath(); + String snapshotPath = cmd.getSnapshotPath(); + String vmName = cmd.getVmName(); try { - StorageVol vol = getVolume(VolPath); - if (vol == null) { - return new ManageSnapshotAnswer(cmd, false, null); + DomainInfo.DomainState state = null; + Domain vm = null; + if (vmName != null) { + try { + vm = getDomain(cmd.getVmName()); + state = vm.getInfo().state; + } catch (LibvirtException e) { + + } } - Domain vm = getDomain(cmd.getVmName()); - String vmUuid = vm.getUUIDString(); - Object[] args = new Object[] {snapshotName, vmUuid}; - String snapshot = SnapshotXML.format(args); - s_logger.debug(snapshot); - if (cmd.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) { - vm.snapshotCreateXML(snapshot); + + if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING) { + String vmUuid = vm.getUUIDString(); + Object[] args = new Object[] {snapshotName, vmUuid}; + String snapshot = SnapshotXML.format(args); + s_logger.debug(snapshot); + if (cmd.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) { + vm.snapshotCreateXML(snapshot); + } else { + DomainSnapshot snap = vm.snapshotLookupByName(snapshotName); + snap.delete(0); + } } else { - DomainSnapshot snap = vm.snapshotLookupByName(snapshotName); - snap.delete(0); + /*VM is not running, create a snapshot by ourself*/ + final Script command = new Script(_manageSnapshotPath, _timeout, s_logger); + if (cmd.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) { + command.add("-c", VolPath); + } else { + command.add("-d", snapshotPath); + } + + command.add("-n", snapshotName); + String result = command.execute(); + if (result != null) { + s_logger.debug("Failed to manage snapshot: " + result); + return new ManageSnapshotAnswer(cmd, false, "Failed to manage snapshot: " + result); + } } } catch (LibvirtException e) { s_logger.debug("Failed to manage snapshot: " + e.toString()); @@ -1260,28 +1284,52 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv String snapshotName = cmd.getSnapshotName(); String snapshotPath = cmd.getSnapshotUuid(); String snapshotDestPath = null; + String vmName = cmd.getVmName(); try { StoragePool secondaryStoragePool = getNfsSPbyURI(_conn, new URI(secondaryStoragePoolURL)); String ssPmountPath = _mountPoint + File.separator + secondaryStoragePool.getUUIDString(); snapshotDestPath = ssPmountPath + File.separator + dcId + File.separator + "snapshots" + File.separator + accountId + File.separator + volumeId; - final Script command = new Script(_manageSnapshotPath, _timeout, s_logger); + Script command = new Script(_manageSnapshotPath, _timeout, s_logger); command.add("-b", snapshotPath); command.add("-n", snapshotName); command.add("-p", snapshotDestPath); + command.add("-t", snapshotName); String result = command.execute(); if (result != null) { s_logger.debug("Failed to backup snaptshot: " + result); return new BackupSnapshotAnswer(cmd, false, result, null); } /*Delete the snapshot on primary*/ - Domain vm = getDomain(cmd.getVmName()); - String vmUuid = vm.getUUIDString(); - Object[] args = new Object[] {snapshotName, vmUuid}; - String snapshot = SnapshotXML.format(args); - s_logger.debug(snapshot); - DomainSnapshot snap = vm.snapshotLookupByName(snapshotName); - snap.delete(0); + + DomainInfo.DomainState state = null; + Domain vm = null; + if (vmName != null) { + try { + vm = getDomain(cmd.getVmName()); + state = vm.getInfo().state; + } catch (LibvirtException e) { + + } + } + + if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING) { + String vmUuid = vm.getUUIDString(); + Object[] args = new Object[] {snapshotName, vmUuid}; + String snapshot = SnapshotXML.format(args); + s_logger.debug(snapshot); + DomainSnapshot snap = vm.snapshotLookupByName(snapshotName); + snap.delete(0); + } else { + command = new Script(_manageSnapshotPath, _timeout, s_logger); + command.add("-d", snapshotPath); + command.add("-n", snapshotName); + result = command.execute(); + if (result != null) { + s_logger.debug("Failed to backup snapshot: " + result); + return new BackupSnapshotAnswer(cmd, false, "Failed to backup snapshot: " + result, null); + } + } } catch (LibvirtException e) { return new BackupSnapshotAnswer(cmd, false, e.toString(), null); } catch (URISyntaxException e) { @@ -1357,7 +1405,11 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv try { secondaryPool = getNfsSPbyURI(_conn, new URI(cmd.getSecondaryStoragePoolURL())); /*TODO: assuming all the storage pools mounted under _mountPoint, the mount point should be got from pool.dumpxml*/ - String templatePath = _mountPoint + File.separator + secondaryPool.getUUIDString() + File.separator + templateInstallFolder; + String templatePath = _mountPoint + File.separator + secondaryPool.getUUIDString() + File.separator + templateInstallFolder; + File f = new File(templatePath); + if (!f.exists()) { + f.mkdir(); + } String tmplPath = templateInstallFolder + File.separator + tmplFileName; Script command = new Script(_createTmplPath, _timeout, s_logger); command.add("-t", templatePath); @@ -1404,38 +1456,58 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv } protected CreatePrivateTemplateAnswer execute(CreatePrivateTemplateCommand cmd) { String secondaryStorageURL = cmd.getSecondaryStorageURL(); - String snapshotUUID = cmd.getSnapshotPath(); StoragePool secondaryStorage = null; - StoragePool privateTemplStorage = null; - StorageVol privateTemplateVol = null; - StorageVol snapshotVol = null; try { String templateFolder = cmd.getAccountId() + File.separator + cmd.getTemplateId() + File.separator; String templateInstallFolder = "/template/tmpl/" + templateFolder; - + secondaryStorage = getNfsSPbyURI(_conn, new URI(secondaryStorageURL)); /*TODO: assuming all the storage pools mounted under _mountPoint, the mount point should be got from pool.dumpxml*/ - String mountPath = _mountPoint + File.separator + secondaryStorage.getUUIDString() + templateInstallFolder; - File mpfile = new File(mountPath); + String tmpltPath = _mountPoint + File.separator + secondaryStorage.getUUIDString() + templateInstallFolder; + File mpfile = new File(tmpltPath); if (!mpfile.exists()) { mpfile.mkdir(); } + + Script command = new Script(_createTmplPath, _timeout, s_logger); + command.add("-f", cmd.getSnapshotPath()); + command.add("-c", cmd.getSnapshotName()); + command.add("-t", tmpltPath); + command.add("-n", cmd.getUniqueName() + ".qcow2"); + command.add("-s"); + String result = command.execute(); - // Create a SR for the secondary storage installation folder - privateTemplStorage = getNfsSPbyURI(_conn, new URI(secondaryStorageURL + templateInstallFolder)); - snapshotVol = getVolume(snapshotUUID); - - LibvirtStorageVolumeDef vol = new LibvirtStorageVolumeDef(UUID.randomUUID().toString(), snapshotVol.getInfo().capacity, volFormat.QCOW2, null, null); - s_logger.debug(vol.toString()); - privateTemplateVol = copyVolume(privateTemplStorage, vol, snapshotVol); + if (result != null) { + s_logger.debug("failed to create template: " + result); + return new CreatePrivateTemplateAnswer(cmd, + false, + result, + null, + 0, + null, + null); + } + + Map params = new HashMap(); + params.put(StorageLayer.InstanceConfigKey, _storage); + Processor qcow2Processor = new QCOW2Processor(); + + qcow2Processor.configure("QCOW2 Processor", params); + + FormatInfo info = qcow2Processor.process(tmpltPath, null, cmd.getUniqueName()); + + TemplateLocation loc = new TemplateLocation(_storage, tmpltPath); + loc.create(1, true, cmd.getUniqueName()); + loc.addFormat(info); + loc.save(); return new CreatePrivateTemplateAnswer(cmd, true, null, - templateInstallFolder + privateTemplateVol.getName(), - privateTemplateVol.getInfo().capacity/1024*1024, /*in Mega unit*/ - privateTemplateVol.getName(), + templateInstallFolder + cmd.getUniqueName() + ".qcow2", + info.virtualSize, + cmd.getUniqueName(), ImageFormat.QCOW2); } catch (URISyntaxException e) { return new CreatePrivateTemplateAnswer(cmd, @@ -1454,7 +1526,31 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv 0, null, null); - } + } catch (InternalErrorException e) { + return new CreatePrivateTemplateAnswer(cmd, + false, + e.toString(), + null, + 0, + null, + null); + } catch (IOException e) { + return new CreatePrivateTemplateAnswer(cmd, + false, + e.toString(), + null, + 0, + null, + null); + } catch (ConfigurationException e) { + return new CreatePrivateTemplateAnswer(cmd, + false, + e.toString(), + null, + 0, + null, + null); + } } private StoragePool getNfsSPbyURI(Connect conn, URI uri) throws LibvirtException { @@ -2361,7 +2457,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv Iterator> itr = entrySet.iterator(); while (itr.hasNext()) { Map.Entry entry = itr.next(); - if (entry.getValue().equalsIgnoreCase(sourceFile)) { + if ((entry.getValue() != null) && (entry.getValue().equalsIgnoreCase(sourceFile))) { diskDev = entry.getKey(); break; } @@ -2942,9 +3038,9 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv } private String getHypervisorPath() { - File f =new File("/usr/bin/cloud-qemu-kvm"); + File f =new File("/usr/bin/cloud-qemu-system-x86_64"); if (f.exists()) { - return "/usr/bin/cloud-qemu-kvm"; + return "/usr/bin/cloud-qemu-system-x86_64"; } else { if (_conn == null) return null; @@ -3098,7 +3194,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv brName = setVnetBrName(vnetId); String vnetDev = "vtap" + vnetId; createVnet(vnetId, _pifs.first()); - vnetNic.defBridgeNet(brName, vnetDev, guestMac, interfaceDef.nicModel.VIRTIO); + vnetNic.defBridgeNet(brName, null, guestMac, interfaceDef.nicModel.VIRTIO); } nics.add(vnetNic); @@ -3114,7 +3210,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv brName = setVnetBrName(vnetId); String vnetDev = "vtap" + vnetId; createVnet(vnetId, _pifs.second()); - pubNic.defBridgeNet(brName, vnetDev, pubMac, interfaceDef.nicModel.VIRTIO); + pubNic.defBridgeNet(brName, null, pubMac, interfaceDef.nicModel.VIRTIO); } nics.add(pubNic); return nics; @@ -3166,7 +3262,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv String datadiskPath = tmplVol.getKey(); diskDef hda = new diskDef(); - hda.defFileBasedDisk(rootkPath, "vda", diskDef.diskBus.IDE, diskDef.diskFmtType.QCOW2); + hda.defFileBasedDisk(rootkPath, "hda", diskDef.diskBus.IDE, diskDef.diskFmtType.QCOW2); disks.add(hda); diskDef hdb = new diskDef(); @@ -3502,6 +3598,22 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv } } + private StorageVol createVolume(StoragePool destPool, StorageVol tmplVol) throws LibvirtException { + if (isCentosHost()) { + LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(UUID.randomUUID().toString(), tmplVol.getInfo().capacity, volFormat.QCOW2, null, null); + s_logger.debug(volDef.toString()); + StorageVol vol = destPool.storageVolCreateXML(volDef.toString(), 0); + + /*create qcow2 image based on the name*/ + Script.runSimpleBashScript("qemu-img create -f qcow2 -b " + tmplVol.getPath() + " " + vol.getPath() ); + return vol; + } else { + LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(UUID.randomUUID().toString(), tmplVol.getInfo().capacity, volFormat.QCOW2, tmplVol.getPath(), volFormat.QCOW2); + s_logger.debug(volDef.toString()); + return destPool.storageVolCreateXML(volDef.toString(), 0); + } + } + private StorageVol getVolume(StoragePool pool, String volKey) { StorageVol vol = null; try { diff --git a/agent/src/com/cloud/agent/resource/computing/LibvirtDomainXMLParser.java b/agent/src/com/cloud/agent/resource/computing/LibvirtDomainXMLParser.java index 779962af0a4..b5271c8e62c 100644 --- a/agent/src/com/cloud/agent/resource/computing/LibvirtDomainXMLParser.java +++ b/agent/src/com/cloud/agent/resource/computing/LibvirtDomainXMLParser.java @@ -94,6 +94,8 @@ public class LibvirtDomainXMLParser extends LibvirtXMLParser { } else if (qName.equalsIgnoreCase("disk")) { diskMaps.put(diskDev, diskFile); _disk = false; + diskFile = null; + diskDev = null; } else if (qName.equalsIgnoreCase("description")) { _desc = false; } diff --git a/client/tomcatconf/commands.properties.in b/client/tomcatconf/commands.properties.in index fb62d18615e..47f934cabd4 100644 --- a/client/tomcatconf/commands.properties.in +++ b/client/tomcatconf/commands.properties.in @@ -139,6 +139,7 @@ listSystemVms=com.cloud.api.commands.ListSystemVMsCmd;1 updateConfiguration=com.cloud.api.commands.UpdateCfgCmd;1 listConfigurations=com.cloud.api.commands.ListCfgsByCmd;1 addConfig=com.cloud.api.commands.AddConfigCmd;15 +listCapabilities=com.cloud.api.commands.ListCapabilitiesCmd;15 #### pod commands createPod=com.cloud.api.commands.CreatePodCmd;1 @@ -207,4 +208,4 @@ listNetworkGroups=com.cloud.api.commands.ListNetworkGroupsCmd;11 registerPreallocatedLun=com.cloud.server.api.commands.RegisterPreallocatedLunCmd;1 deletePreallocatedLun=com.cloud.server.api.commands.DeletePreallocatedLunCmd;1 -listPreallocatedLuns=com.cloud.api.commands.ListPreallocatedLunsCmd;1 \ No newline at end of file +listPreallocatedLuns=com.cloud.api.commands.ListPreallocatedLunsCmd;1 diff --git a/client/tomcatconf/components.xml.in b/client/tomcatconf/components.xml.in index ce698854bbe..9365e78ead9 100755 --- a/client/tomcatconf/components.xml.in +++ b/client/tomcatconf/components.xml.in @@ -215,8 +215,9 @@ - - + + + diff --git a/core/src/com/cloud/server/ManagementServer.java b/core/src/com/cloud/server/ManagementServer.java index 840d9343ca6..85a3115ec07 100644 --- a/core/src/com/cloud/server/ManagementServer.java +++ b/core/src/com/cloud/server/ManagementServer.java @@ -2187,4 +2187,5 @@ public interface ManagementServer { boolean checkIfMaintenable(long hostId); + Map listCapabilities(); } diff --git a/core/src/com/cloud/vm/dao/VMInstanceDao.java b/core/src/com/cloud/vm/dao/VMInstanceDao.java index 653445ad331..d496a80f4f3 100644 --- a/core/src/com/cloud/vm/dao/VMInstanceDao.java +++ b/core/src/com/cloud/vm/dao/VMInstanceDao.java @@ -80,4 +80,5 @@ public interface VMInstanceDao extends GenericDao { List listByHostIdTypes(long hostid, VirtualMachine.Type... types); List listUpByHostIdTypes(long hostid, VirtualMachine.Type... types); + List listByZoneIdAndType(long zoneId, VirtualMachine.Type type); } diff --git a/core/src/com/cloud/vm/dao/VMInstanceDaoImpl.java b/core/src/com/cloud/vm/dao/VMInstanceDaoImpl.java index 3ec6b2a12dc..d625db08bf7 100644 --- a/core/src/com/cloud/vm/dao/VMInstanceDaoImpl.java +++ b/core/src/com/cloud/vm/dao/VMInstanceDaoImpl.java @@ -51,6 +51,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase implem protected final SearchBuilder HostSearch; protected final SearchBuilder LastHostSearch; protected final SearchBuilder ZoneSearch; + protected final SearchBuilder ZoneVmTypeSearch; protected final SearchBuilder ZoneTemplateNonExpungedSearch; protected final SearchBuilder NameLikeSearch; protected final SearchBuilder StateChangeSearch; @@ -79,6 +80,11 @@ public class VMInstanceDaoImpl extends GenericDaoBase implem ZoneSearch = createSearchBuilder(); ZoneSearch.and("zone", ZoneSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); ZoneSearch.done(); + + ZoneVmTypeSearch = createSearchBuilder(); + ZoneVmTypeSearch.and("zone", ZoneVmTypeSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); + ZoneVmTypeSearch.and("type", ZoneVmTypeSearch.entity().getType(), SearchCriteria.Op.EQ); + ZoneVmTypeSearch.done(); ZoneTemplateNonExpungedSearch = createSearchBuilder(); ZoneTemplateNonExpungedSearch.and("zone", ZoneTemplateNonExpungedSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ); @@ -193,6 +199,15 @@ public class VMInstanceDaoImpl extends GenericDaoBase implem return listActiveBy(sc); } + @Override + public List listByZoneIdAndType(long zoneId, VirtualMachine.Type type) { + SearchCriteria sc = ZoneVmTypeSearch.create(); + sc.setParameters("zone", zoneId); + sc.setParameters("type", type.toString()); + return listActiveBy(sc); + } + + @Override public List listNonExpungedByZoneAndTemplate(long zoneId, long templateId) { SearchCriteria sc = ZoneTemplateNonExpungedSearch.create(); diff --git a/python/bindir/cloud-web-ipallocator.in b/python/bindir/cloud-web-ipallocator.in new file mode 100755 index 00000000000..3a278d29e56 --- /dev/null +++ b/python/bindir/cloud-web-ipallocator.in @@ -0,0 +1,136 @@ +#! /usr/bin/python +import web +import socket, struct +import cloud_utils +from cloud_utils import Command +urls = ("/ipallocator", "ipallocator") +app = web.application(urls, globals()) + +augtool = Command("augtool") +service = Command("service") +class dhcp: + _instance = None + def __init__(self): + self.availIP=[] + self.router=None + self.netmask=None + self.initialized=False + + options = augtool.match("/files/etc/dnsmasq.conf/dhcp-option").stdout.strip() + for option in options.splitlines(): + if option.find("option:router") != -1: + self.router = option.split("=")[1].strip().split(",")[1] + print self.router + + dhcp_range = augtool.get("/files/etc/dnsmasq.conf/dhcp-range").stdout.strip() + dhcp_start = dhcp_range.split("=")[1].strip().split(",")[0] + dhcp_end = dhcp_range.split("=")[1].strip().split(",")[1] + self.netmask = dhcp_range.split("=")[1].strip().split(",")[2] + print dhcp_start, dhcp_end, self.netmask + + start_ip_num = self.ipToNum(dhcp_start); + end_ip_num = self.ipToNum(dhcp_end) + print start_ip_num, end_ip_num + + for ip in range(start_ip_num, end_ip_num + 1): + self.availIP.append(ip) + print self.availIP[0], self.availIP[len(self.availIP) - 1] + + #load the ip already allocated + self.reloadAllocatedIP() + + def ipToNum(self, ip): + return struct.unpack("!I", socket.inet_aton(ip))[0] + + def numToIp(self, num): + return socket.inet_ntoa(struct.pack('!I', num)) + + def getFreeIP(self): + if len(self.availIP) > 0: + ip = self.numToIp(self.availIP[0]) + self.availIP.remove(self.availIP[0]) + return ip + else: + return None + + def getNetmask(self): + return self.netmask + + def getRouter(self): + return self.router + + def getInstance(): + if not dhcp._instance: + dhcp._instance = dhcp() + return dhcp._instance + getInstance = staticmethod(getInstance) + + def reloadAllocatedIP(self): + dhcp_hosts = augtool.match("/files/etc/dnsmasq.conf/dhcp-host").stdout.strip().splitlines() + + for host in dhcp_hosts: + if host.find("dhcp-host") != -1: + allocatedIP = self.ipToNum(host.split("=")[1].strip().split(",")[1]) + if allocatedIP in self.availIP: + self.availIP.remove(allocatedIP) + + def allocateIP(self, mac): + newIP = self.getFreeIP() + dhcp_host = augtool.match("/files/etc/dnsmasq.conf/dhcp-host").stdout.strip() + cnt = len(dhcp_host.splitlines()) + 1 + script = """set %s %s + save"""%("/files/etc/dnsmasq.conf/dhcp-host[" + str(cnt) + "]", str(mac) + "," + newIP) + augtool < script + #reset dnsmasq + service("dnsmasq", "restart", stdout=None, stderr=None) + return newIP + + def releaseIP(self, ip): + dhcp_host = augtool.match("/files/etc/dnsmasq.conf/dhcp-host").stdout.strip() + path = None + for host in dhcp_host.splitlines(): + if host.find(ip) != -1: + path = host.split("=")[0].strip() + + if path == None: + print "Can't find " + str(ip) + " in conf file" + return None + + print path + script = """rm %s + save"""%(path) + augtool < script + + #reset dnsmasq + service("dnsmasq", "restart", stdout=None, stderr=None) + +class ipallocator: + def GET(self): + try: + user_data = web.input() + command = user_data.command + print "Processing: " + command + + dhcpInit = dhcp.getInstance() + + if command == "getIpAddr": + mac = user_data.mac + zone_id = user_data.dc + pod_id = user_data.pod + print mac, zone_id, pod_id + freeIP = dhcpInit.allocateIP(mac) + if not freeIP: + return "0,0,0" + print "Find an available IP: " + freeIP + + return freeIP + "," + dhcpInit.getNetmask() + "," + dhcpInit.getRouter() + elif command == "releaseIpAddr": + ip = user_data.ip + zone_id = user_data.dc + pod_id = user_data.pod + dhcpInit.releaseIP(ip) + except: + return None + +if __name__ == "__main__": + app.run() diff --git a/scripts/storage/qcow2/createtmplt.sh b/scripts/storage/qcow2/createtmplt.sh index 55efe952af5..ef75c6270e9 100755 --- a/scripts/storage/qcow2/createtmplt.sh +++ b/scripts/storage/qcow2/createtmplt.sh @@ -80,6 +80,20 @@ create_from_file() { fi } +create_from_snapshot() { + local tmpltImg=$1 + local snapshotName=$2 + local tmpltfs=$3 + local tmpltname=$4 + + cloud-qemu-img convert -f qcow2 -O qcow2 -s $snapshotName $tmpltImg /$tmpltfs/$tmpltname >& /dev/null + if [ $? -gt 0 ] + then + printf "Failed to create template /$tmplfs/$tmpltname from snapshot $snapshotName on disk $tmpltImg " + exit 2 + fi +} + tflag= nflag= fflag= @@ -89,8 +103,9 @@ hvm=false cleanup=false dflag= cflag= +snapshotName= -while getopts 'uht:n:f:s:c:d:' OPTION +while getopts 'uht:n:f:sc:d:' OPTION do case $OPTION in t) tflag=1 @@ -103,10 +118,10 @@ do tmpltimg="$OPTARG" ;; s) sflag=1 - volsize="$OPTARG" + sflag=1 ;; c) cflag=1 - cksum="$OPTARG" + snapshotName="$OPTARG" ;; d) dflag=1 descr="$OPTARG" @@ -119,12 +134,6 @@ do esac done -if [ "$tflag$nflag$fflag" != "111" ] -then - usage - exit 2 -fi - if [ ! -d /$tmpltfs ] then @@ -148,7 +157,12 @@ then printf "failed to uncompress $tmpltimg\n" fi -create_from_file $tmpltfs $tmpltimg $tmpltname +if [ "$sflag" == "1" ] +then + create_from_snapshot $tmpltimg $snapshotName $tmpltfs $tmpltname +else + create_from_file $tmpltfs $tmpltimg $tmpltname +fi touch /$tmpltfs/template.properties echo -n "" > /$tmpltfs/template.properties diff --git a/scripts/storage/qcow2/managesnapshot.sh b/scripts/storage/qcow2/managesnapshot.sh index 3c7692161d6..d9b339267a0 100755 --- a/scripts/storage/qcow2/managesnapshot.sh +++ b/scripts/storage/qcow2/managesnapshot.sh @@ -16,13 +16,20 @@ create_snapshot() { local snapshotname=$2 local failed=0 - qemu-img snapshot -c $snapshotname $disk + if [ ! -f $disk ] + then + failed=1 + printf "No disk $disk exist\n" >&2 + return $failed + fi + + cloud-qemu-img snapshot -c $snapshotname $disk if [ $? -gt 0 ] then - failed=1 + failed=2 printf "***Failed to create snapshot $snapshotname for path $disk\n" >&2 - qemu-img snapshot -d $snapshotname $disk + cloud-qemu-img snapshot -d $snapshotname $disk if [ $? -gt 0 ] then @@ -34,21 +41,24 @@ create_snapshot() { } destroy_snapshot() { - local backupSnapDir=$1 + local disk=$1 local snapshotname=$2 local failed=0 - if [ -f $backupSnapDir/$snapshotname ] + if [ ! -f $disk ] then - rm -f $backupSnapDir/$snapshotname - - if [ $? -gt 0 ] - then - printf "***Failed to delete snapshot $snapshotname for path $backupSnapDir\n" >&2 - failed=1 - fi + failed=1 + printf "No disk $disk exist\n" >&2 + return $failed fi + cloud-qemu-img snapshot -d $snapshotname $disk + if [ $? -gt 0 ] + then + failed=2 + printf "Failed to delete snapshot $snapshotname for path $disk\n" >&2 + fi + return $failed } @@ -71,6 +81,7 @@ backup_snapshot() { local disk=$1 local snapshotname=$2 local destPath=$3 + local destName=$4 if [ ! -d $destPath ] then @@ -90,7 +101,7 @@ backup_snapshot() { return 1 fi - cloud-qemu-img convert -f qcow2 -O qcow2 -s $snapshotname $disk $destPath/$snapshotname >& /dev/null + cloud-qemu-img convert -f qcow2 -O qcow2 -s $snapshotname $disk $destPath/$destName >& /dev/null if [ $? -gt 0 ] then printf "Failed to backup $snapshotname for disk $disk to $destPath" >&2 @@ -107,8 +118,9 @@ bflag= nflag= pathval= snapshot= +tmplName= -while getopts 'c:d:r:n:b:p:' OPTION +while getopts 'c:d:r:n:b:p:t:' OPTION do case $OPTION in c) cflag=1 @@ -128,6 +140,8 @@ do ;; p) destPath="$OPTARG" ;; + t) tmplName="$OPTARG" + ;; ?) usage ;; esac @@ -144,7 +158,7 @@ then exit $? elif [ "$bflag" == "1" ] then - backup_snapshot $pathval $snapshot $destPath + backup_snapshot $pathval $snapshot $destPath $tmplName exit $? elif [ "$rflag" == "1" ] then diff --git a/scripts/vm/hypervisor/kvm/setup_agent.sh b/scripts/vm/hypervisor/kvm/setup_agent.sh index 7697aaa0d0b..1c0a18112df 100755 --- a/scripts/vm/hypervisor/kvm/setup_agent.sh +++ b/scripts/vm/hypervisor/kvm/setup_agent.sh @@ -174,4 +174,4 @@ done #install_cloud_agent $dflag #install_cloud_consoleP $dflag cloud_agent_setup $host $zone $pod $guid -cloud_consoleP_setup $host $zone $pod +#cloud_consoleP_setup $host $zone $pod diff --git a/server/src/com/cloud/api/commands/ListAccountsCmd.java b/server/src/com/cloud/api/commands/ListAccountsCmd.java index 294248fabd0..f2f8561c23f 100644 --- a/server/src/com/cloud/api/commands/ListAccountsCmd.java +++ b/server/src/com/cloud/api/commands/ListAccountsCmd.java @@ -16,8 +16,8 @@ * */ -package com.cloud.api.commands; - +package com.cloud.api.commands; + import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -36,206 +36,204 @@ import com.cloud.user.UserStatisticsVO; import com.cloud.uservm.UserVm; import com.cloud.utils.Pair; import com.cloud.vm.State; - -public class ListAccountsCmd extends BaseCmd{ - public static final Logger s_logger = Logger.getLogger(ListAccountsCmd.class.getName()); - private static final String s_name = "listaccountsresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_TYPE, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.STATE, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.IS_CLEANUP_REQUIRED, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.KEYWORD, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.PAGE, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.PAGESIZE, Boolean.FALSE)); - } - - public String getName() { - return s_name; - } - public List> getProperties() { - return s_properties; - } - - @Override - public List> execute(Map params) { - Long id = (Long)params.get(BaseCmd.Properties.ID.getName()); - Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); - Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName()); - Long type = (Long)params.get(BaseCmd.Properties.ACCOUNT_TYPE.getName()); - String state = (String)params.get(BaseCmd.Properties.STATE.getName()); - Boolean needCleanup = (Boolean)params.get(BaseCmd.Properties.IS_CLEANUP_REQUIRED.getName()); - Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName()); - Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName()); - String keyword = (String)params.get(BaseCmd.Properties.KEYWORD.getName()); - boolean isAdmin = false; - Long accountId = null; - - String accountName = null; - - if ((account == null) || isAdmin(account.getType())) { - accountName = (String)params.get(BaseCmd.Properties.NAME.getName()); - isAdmin = true; - if (domainId == null) { - // default domainId to the admin's domain - domainId = ((account == null) ? Domain.ROOT_DOMAIN : account.getDomainId()); - } else if (account != null) { - if (!getManagementServer().isChildDomain(account.getDomainId(), domainId)) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid domain id (" + domainId + ") given, unable to list accounts"); - } - } - } else { - accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName()); - accountId = account.getId(); - } - - Long startIndex = Long.valueOf(0); - int pageSizeNum = 50; - if (pageSize != null) { - pageSizeNum = pageSize.intValue(); - } - if (page != null) { - int pageNum = page.intValue(); - if (pageNum > 0) { - startIndex = Long.valueOf(pageSizeNum * (pageNum-1)); - } - } - Criteria c = new Criteria("id", Boolean.TRUE, startIndex, Long.valueOf(pageSizeNum)); - if (isAdmin == true) { - c.addCriteria(Criteria.ID, id); - if (keyword == null) { - c.addCriteria(Criteria.ACCOUNTNAME, accountName); - c.addCriteria(Criteria.DOMAINID, domainId); - c.addCriteria(Criteria.TYPE, type); - c.addCriteria(Criteria.STATE, state); - c.addCriteria(Criteria.ISCLEANUPREQUIRED, needCleanup); - } else { - c.addCriteria(Criteria.KEYWORD, keyword); - } - } else { - c.addCriteria(Criteria.ID, accountId); - } - - List accounts = getManagementServer().searchForAccounts(c); - - List> accountTags = new ArrayList>(); - Object[] aTag = new Object[accounts.size()]; - int i = 0; - for (AccountVO accountO : accounts) { - boolean accountIsAdmin = (accountO.getType() == Account.ACCOUNT_TYPE_ADMIN); - - if ((accountO.getRemoved() == null)&&(accountO.getId() != 1)) { - List> accountData = new ArrayList>(); - accountData.add(new Pair(BaseCmd.Properties.ID.getName(), Long.valueOf(accountO.getId()).toString())); - accountData.add(new Pair(BaseCmd.Properties.NAME.getName(), accountO.getAccountName())); - accountData.add(new Pair(BaseCmd.Properties.ACCOUNT_TYPE.getName(), Short.valueOf(accountO.getType()).toString())); - Domain domain = getManagementServer().findDomainIdById(accountO.getDomainId()); - accountData.add(new Pair(BaseCmd.Properties.DOMAIN_ID.getName(), Long.toString(domain.getId()))); - accountData.add(new Pair(BaseCmd.Properties.DOMAIN.getName(), domain.getName())); - - //get network stat - List stats = getManagementServer().listUserStatsBy(accountO.getId()); - if (stats == null) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error searching for user stats"); - } - - long bytesSent = 0; - long bytesReceived = 0; - for (UserStatisticsVO stat : stats) { - long rx = stat.getNetBytesReceived() + stat.getCurrentBytesReceived(); - long tx = stat.getNetBytesSent() + stat.getCurrentBytesSent(); - bytesReceived = bytesReceived + Long.valueOf(rx); - bytesSent = bytesSent + Long.valueOf(tx); - } - accountData.add(new Pair(BaseCmd.Properties.BYTES_RECEIVED.getName(), Long.valueOf(bytesReceived).toString())); - accountData.add(new Pair(BaseCmd.Properties.BYTES_SENT.getName(), Long.valueOf(bytesSent).toString())); - - // Get resource limits and counts - - long vmLimit = getManagementServer().findCorrectResourceLimit(ResourceType.user_vm, accountO.getId()); - String vmLimitDisplay = (accountIsAdmin || vmLimit == -1) ? "Unlimited" : String.valueOf(vmLimit); - long vmTotal = getManagementServer().getResourceCount(ResourceType.user_vm, accountO.getId()); - String vmAvail = (accountIsAdmin || vmLimit == -1) ? "Unlimited" : String.valueOf(vmLimit - vmTotal); - accountData.add(new Pair(BaseCmd.Properties.VM_LIMIT.getName(), vmLimitDisplay)); - accountData.add(new Pair(BaseCmd.Properties.VM_TOTAL.getName(), vmTotal)); - accountData.add(new Pair(BaseCmd.Properties.VM_AVAIL.getName(), vmAvail)); - - long ipLimit = getManagementServer().findCorrectResourceLimit(ResourceType.public_ip, accountO.getId()); - String ipLimitDisplay = (accountIsAdmin || ipLimit == -1) ? "Unlimited" : String.valueOf(ipLimit); - long ipTotal = getManagementServer().getResourceCount(ResourceType.public_ip, accountO.getId()); - String ipAvail = (accountIsAdmin || ipLimit == -1) ? "Unlimited" : String.valueOf(ipLimit - ipTotal); - accountData.add(new Pair(BaseCmd.Properties.IP_LIMIT.getName(), ipLimitDisplay)); - accountData.add(new Pair(BaseCmd.Properties.IP_TOTAL.getName(), ipTotal)); - accountData.add(new Pair(BaseCmd.Properties.IP_AVAIL.getName(), ipAvail)); - - long volumeLimit = getManagementServer().findCorrectResourceLimit(ResourceType.volume, accountO.getId()); - String volumeLimitDisplay = (accountIsAdmin || volumeLimit == -1) ? "Unlimited" : String.valueOf(volumeLimit); - long volumeTotal = getManagementServer().getResourceCount(ResourceType.volume, accountO.getId()); - String volumeAvail = (accountIsAdmin || volumeLimit == -1) ? "Unlimited" : String.valueOf(volumeLimit - volumeTotal); - accountData.add(new Pair(BaseCmd.Properties.VOLUME_LIMIT.getName(), volumeLimitDisplay)); - accountData.add(new Pair(BaseCmd.Properties.VOLUME_TOTAL.getName(), volumeTotal)); - accountData.add(new Pair(BaseCmd.Properties.VOLUME_AVAIL.getName(), volumeAvail)); - - long snapshotLimit = getManagementServer().findCorrectResourceLimit(ResourceType.snapshot, accountO.getId()); - String snapshotLimitDisplay = (accountIsAdmin || snapshotLimit == -1) ? "Unlimited" : String.valueOf(snapshotLimit); - long snapshotTotal = getManagementServer().getResourceCount(ResourceType.snapshot, accountO.getId()); - String snapshotAvail = (accountIsAdmin || snapshotLimit == -1) ? "Unlimited" : String.valueOf(snapshotLimit - snapshotTotal); - accountData.add(new Pair(BaseCmd.Properties.SNAPSHOT_LIMIT.getName(), snapshotLimitDisplay)); - accountData.add(new Pair(BaseCmd.Properties.SNAPSHOT_TOTAL.getName(), snapshotTotal)); - accountData.add(new Pair(BaseCmd.Properties.SNAPSHOT_AVAIL.getName(), snapshotAvail)); - - long templateLimit = getManagementServer().findCorrectResourceLimit(ResourceType.template, accountO.getId()); - String templateLimitDisplay = (accountIsAdmin || templateLimit == -1) ? "Unlimited" : String.valueOf(templateLimit); - long templateTotal = getManagementServer().getResourceCount(ResourceType.template, accountO.getId()); - String templateAvail = (accountIsAdmin || templateLimit == -1) ? "Unlimited" : String.valueOf(templateLimit - templateTotal); - accountData.add(new Pair(BaseCmd.Properties.TEMPLATE_LIMIT.getName(), templateLimitDisplay)); - accountData.add(new Pair(BaseCmd.Properties.TEMPLATE_TOTAL.getName(), templateTotal)); - accountData.add(new Pair(BaseCmd.Properties.TEMPLATE_AVAIL.getName(), templateAvail)); - - // Get stopped and running VMs - - int vmStopped = 0; - int vmRunning = 0; - - Long[] accountIds = new Long[1]; - accountIds[0] = accountO.getId(); - - Criteria c1 = new Criteria(); - c1.addCriteria(Criteria.ACCOUNTID, accountIds); - List virtualMachines = getManagementServer().searchForUserVMs(c1); - - //get Running/Stopped VMs - for (Iterator iter = virtualMachines.iterator(); iter.hasNext();) { - // count how many stopped/running vms we have - UserVm vm = iter.next(); - - if (vm.getState() == State.Stopped) { - vmStopped++; - } else if (vm.getState() == State.Running) { - vmRunning++; - } - } - - accountData.add(new Pair(BaseCmd.Properties.VM_STOPPED.getName(), vmStopped)); - accountData.add(new Pair(BaseCmd.Properties.VM_RUNNING.getName(), vmRunning)); - - //show this info to admins only - if (isAdmin == true) { - accountData.add(new Pair(BaseCmd.Properties.STATE.getName(), accountO.getState())); - accountData.add(new Pair(BaseCmd.Properties.IS_CLEANUP_REQUIRED.getName(), Boolean.valueOf(accountO.getNeedsCleanup()).toString())); - } - - aTag[i++] = accountData; - } - } - Pair accountTag = new Pair("account", aTag); - accountTags.add(accountTag); - return accountTags; - } -} + +public class ListAccountsCmd extends BaseCmd{ + public static final Logger s_logger = Logger.getLogger(ListAccountsCmd.class.getName()); + private static final String s_name = "listaccountsresponse"; + private static final List> s_properties = new ArrayList>(); + + static { + s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_TYPE, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.STATE, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.IS_CLEANUP_REQUIRED, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.KEYWORD, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.PAGE, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.PAGESIZE, Boolean.FALSE)); + } + + public String getName() { + return s_name; + } + public List> getProperties() { + return s_properties; + } + + @Override + public List> execute(Map params) { + Long id = (Long)params.get(BaseCmd.Properties.ID.getName()); + Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); + Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName()); + Long type = (Long)params.get(BaseCmd.Properties.ACCOUNT_TYPE.getName()); + String state = (String)params.get(BaseCmd.Properties.STATE.getName()); + Boolean needCleanup = (Boolean)params.get(BaseCmd.Properties.IS_CLEANUP_REQUIRED.getName()); + Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName()); + Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName()); + String keyword = (String)params.get(BaseCmd.Properties.KEYWORD.getName()); + boolean isAdmin = false; + Long accountId = null; + + String accountName = null; + + if ((account == null) || isAdmin(account.getType())) { + accountName = (String)params.get(BaseCmd.Properties.NAME.getName()); + isAdmin = true; + if (domainId == null) { + // default domainId to the admin's domain + domainId = ((account == null) ? Domain.ROOT_DOMAIN : account.getDomainId()); + } else if (account != null) { + if (!getManagementServer().isChildDomain(account.getDomainId(), domainId)) { + throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid domain id (" + domainId + ") given, unable to list accounts"); + } + } + } else { + accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName()); + accountId = account.getId(); + } + + Long startIndex = Long.valueOf(0); + int pageSizeNum = 50; + if (pageSize != null) { + pageSizeNum = pageSize.intValue(); + } + if (page != null) { + int pageNum = page.intValue(); + if (pageNum > 0) { + startIndex = Long.valueOf(pageSizeNum * (pageNum-1)); + } + } + Criteria c = new Criteria("id", Boolean.TRUE, startIndex, Long.valueOf(pageSizeNum)); + if (isAdmin == true) { + c.addCriteria(Criteria.ID, id); + if (keyword == null) { + c.addCriteria(Criteria.ACCOUNTNAME, accountName); + c.addCriteria(Criteria.DOMAINID, domainId); + c.addCriteria(Criteria.TYPE, type); + c.addCriteria(Criteria.STATE, state); + c.addCriteria(Criteria.ISCLEANUPREQUIRED, needCleanup); + } else { + c.addCriteria(Criteria.KEYWORD, keyword); + } + } else { + c.addCriteria(Criteria.ID, accountId); + } + + List accounts = getManagementServer().searchForAccounts(c); + + List> accountTags = new ArrayList>(); + Object[] aTag = new Object[accounts.size()]; + int i = 0; + for (AccountVO accountO : accounts) { + boolean accountIsAdmin = (accountO.getType() == Account.ACCOUNT_TYPE_ADMIN); + + List> accountData = new ArrayList>(); + accountData.add(new Pair(BaseCmd.Properties.ID.getName(), Long.valueOf(accountO.getId()).toString())); + accountData.add(new Pair(BaseCmd.Properties.NAME.getName(), accountO.getAccountName())); + accountData.add(new Pair(BaseCmd.Properties.ACCOUNT_TYPE.getName(), Short.valueOf(accountO.getType()).toString())); + Domain domain = getManagementServer().findDomainIdById(accountO.getDomainId()); + accountData.add(new Pair(BaseCmd.Properties.DOMAIN_ID.getName(), Long.toString(domain.getId()))); + accountData.add(new Pair(BaseCmd.Properties.DOMAIN.getName(), domain.getName())); + + //get network stat + List stats = getManagementServer().listUserStatsBy(accountO.getId()); + if (stats == null) { + throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error searching for user stats"); + } + + long bytesSent = 0; + long bytesReceived = 0; + for (UserStatisticsVO stat : stats) { + long rx = stat.getNetBytesReceived() + stat.getCurrentBytesReceived(); + long tx = stat.getNetBytesSent() + stat.getCurrentBytesSent(); + bytesReceived = bytesReceived + Long.valueOf(rx); + bytesSent = bytesSent + Long.valueOf(tx); + } + accountData.add(new Pair(BaseCmd.Properties.BYTES_RECEIVED.getName(), Long.valueOf(bytesReceived).toString())); + accountData.add(new Pair(BaseCmd.Properties.BYTES_SENT.getName(), Long.valueOf(bytesSent).toString())); + + // Get resource limits and counts + + long vmLimit = getManagementServer().findCorrectResourceLimit(ResourceType.user_vm, accountO.getId()); + String vmLimitDisplay = (accountIsAdmin || vmLimit == -1) ? "Unlimited" : String.valueOf(vmLimit); + long vmTotal = getManagementServer().getResourceCount(ResourceType.user_vm, accountO.getId()); + String vmAvail = (accountIsAdmin || vmLimit == -1) ? "Unlimited" : String.valueOf(vmLimit - vmTotal); + accountData.add(new Pair(BaseCmd.Properties.VM_LIMIT.getName(), vmLimitDisplay)); + accountData.add(new Pair(BaseCmd.Properties.VM_TOTAL.getName(), vmTotal)); + accountData.add(new Pair(BaseCmd.Properties.VM_AVAIL.getName(), vmAvail)); + + long ipLimit = getManagementServer().findCorrectResourceLimit(ResourceType.public_ip, accountO.getId()); + String ipLimitDisplay = (accountIsAdmin || ipLimit == -1) ? "Unlimited" : String.valueOf(ipLimit); + long ipTotal = getManagementServer().getResourceCount(ResourceType.public_ip, accountO.getId()); + String ipAvail = (accountIsAdmin || ipLimit == -1) ? "Unlimited" : String.valueOf(ipLimit - ipTotal); + accountData.add(new Pair(BaseCmd.Properties.IP_LIMIT.getName(), ipLimitDisplay)); + accountData.add(new Pair(BaseCmd.Properties.IP_TOTAL.getName(), ipTotal)); + accountData.add(new Pair(BaseCmd.Properties.IP_AVAIL.getName(), ipAvail)); + + long volumeLimit = getManagementServer().findCorrectResourceLimit(ResourceType.volume, accountO.getId()); + String volumeLimitDisplay = (accountIsAdmin || volumeLimit == -1) ? "Unlimited" : String.valueOf(volumeLimit); + long volumeTotal = getManagementServer().getResourceCount(ResourceType.volume, accountO.getId()); + String volumeAvail = (accountIsAdmin || volumeLimit == -1) ? "Unlimited" : String.valueOf(volumeLimit - volumeTotal); + accountData.add(new Pair(BaseCmd.Properties.VOLUME_LIMIT.getName(), volumeLimitDisplay)); + accountData.add(new Pair(BaseCmd.Properties.VOLUME_TOTAL.getName(), volumeTotal)); + accountData.add(new Pair(BaseCmd.Properties.VOLUME_AVAIL.getName(), volumeAvail)); + + long snapshotLimit = getManagementServer().findCorrectResourceLimit(ResourceType.snapshot, accountO.getId()); + String snapshotLimitDisplay = (accountIsAdmin || snapshotLimit == -1) ? "Unlimited" : String.valueOf(snapshotLimit); + long snapshotTotal = getManagementServer().getResourceCount(ResourceType.snapshot, accountO.getId()); + String snapshotAvail = (accountIsAdmin || snapshotLimit == -1) ? "Unlimited" : String.valueOf(snapshotLimit - snapshotTotal); + accountData.add(new Pair(BaseCmd.Properties.SNAPSHOT_LIMIT.getName(), snapshotLimitDisplay)); + accountData.add(new Pair(BaseCmd.Properties.SNAPSHOT_TOTAL.getName(), snapshotTotal)); + accountData.add(new Pair(BaseCmd.Properties.SNAPSHOT_AVAIL.getName(), snapshotAvail)); + + long templateLimit = getManagementServer().findCorrectResourceLimit(ResourceType.template, accountO.getId()); + String templateLimitDisplay = (accountIsAdmin || templateLimit == -1) ? "Unlimited" : String.valueOf(templateLimit); + long templateTotal = getManagementServer().getResourceCount(ResourceType.template, accountO.getId()); + String templateAvail = (accountIsAdmin || templateLimit == -1) ? "Unlimited" : String.valueOf(templateLimit - templateTotal); + accountData.add(new Pair(BaseCmd.Properties.TEMPLATE_LIMIT.getName(), templateLimitDisplay)); + accountData.add(new Pair(BaseCmd.Properties.TEMPLATE_TOTAL.getName(), templateTotal)); + accountData.add(new Pair(BaseCmd.Properties.TEMPLATE_AVAIL.getName(), templateAvail)); + + // Get stopped and running VMs + + int vmStopped = 0; + int vmRunning = 0; + + Long[] accountIds = new Long[1]; + accountIds[0] = accountO.getId(); + + Criteria c1 = new Criteria(); + c1.addCriteria(Criteria.ACCOUNTID, accountIds); + List virtualMachines = getManagementServer().searchForUserVMs(c1); + + //get Running/Stopped VMs + for (Iterator iter = virtualMachines.iterator(); iter.hasNext();) { + // count how many stopped/running vms we have + UserVm vm = iter.next(); + + if (vm.getState() == State.Stopped) { + vmStopped++; + } else if (vm.getState() == State.Running) { + vmRunning++; + } + } + + accountData.add(new Pair(BaseCmd.Properties.VM_STOPPED.getName(), vmStopped)); + accountData.add(new Pair(BaseCmd.Properties.VM_RUNNING.getName(), vmRunning)); + + //show this info to admins only + if (isAdmin == true) { + accountData.add(new Pair(BaseCmd.Properties.STATE.getName(), accountO.getState())); + accountData.add(new Pair(BaseCmd.Properties.IS_CLEANUP_REQUIRED.getName(), Boolean.valueOf(accountO.getNeedsCleanup()).toString())); + } + + aTag[i++] = accountData; + } + Pair accountTag = new Pair("account", aTag); + accountTags.add(accountTag); + return accountTags; + } +} diff --git a/server/src/com/cloud/api/commands/ListCapabilitiesCmd.java b/server/src/com/cloud/api/commands/ListCapabilitiesCmd.java new file mode 100644 index 00000000000..2532f3673d5 --- /dev/null +++ b/server/src/com/cloud/api/commands/ListCapabilitiesCmd.java @@ -0,0 +1,58 @@ +/** + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package com.cloud.api.commands; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.apache.log4j.Logger; + +import com.cloud.api.BaseCmd; +import com.cloud.utils.Pair; + +public class ListCapabilitiesCmd extends BaseCmd { + public static final Logger s_logger = Logger.getLogger(ListCapabilitiesCmd.class.getName()); + + private static final String s_name = "listcapabilitiesresponse"; + private static final List> s_properties = new ArrayList>(); + + public String getName() { + return s_name; + } + public List> getProperties() { + return s_properties; + } + + @Override + public List> execute(Map params) { + + Map capabilities = null; + capabilities = getManagementServer().listCapabilities(); + + Iterator iterator = capabilities.keySet().iterator(); + List> capabilityPair = new ArrayList>(); + while (iterator.hasNext()) { + String capability = iterator.next(); + capabilityPair.add(new Pair(capability, capabilities.get(capability))); + } + return capabilityPair; + } +} diff --git a/server/src/com/cloud/configuration/Config.java b/server/src/com/cloud/configuration/Config.java index d46e8f28211..c97749a6bc8 100644 --- a/server/src/com/cloud/configuration/Config.java +++ b/server/src/com/cloud/configuration/Config.java @@ -146,6 +146,7 @@ public enum Config { DirectAttachNetworkExternalAPIURL("Advanced", ManagementServer.class, String.class, "direct.attach.network.externalIpAllocator.url", null, "Direct-attach VMs using external DHCP server (API url)", null), DirectAttachUntaggedVlanEnabled("Advanced", ManagementServer.class, String.class, "direct.attach.untagged.vlan.enabled", "false", "Indicate whether the system supports direct-attached untagged vlan", "true,false"), CheckPodCIDRs("Advanced", ManagementServer.class, String.class, "check.pod.cidrs", "true", "If true, different pods must belong to different CIDR subnets.", "true,false"), + MD5Hashed("Advanced", ManagementServer.class, Boolean.class, "security.password.md5hashed", "true", "If set to false password is sent in clear text or else md5hashed", null), // XenServer VmAllocationAlgorithm("Advanced", ManagementServer.class, String.class, "vm.allocation.algorithm", "random", "If 'random', hosts within a pod will be randomly considered for VM/volume allocation. If 'firstfit', they will be considered on a first-fit basis.", null), diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java index 047da0d1bef..9978cee29b1 100644 --- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java +++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java @@ -57,6 +57,7 @@ import com.cloud.offering.NetworkOffering.GuestIpType; import com.cloud.service.ServiceOfferingVO; import com.cloud.service.dao.ServiceOfferingDao; import com.cloud.storage.DiskOfferingVO; +import com.cloud.storage.SecondaryStorage; import com.cloud.storage.dao.DiskOfferingDao; import com.cloud.user.AccountVO; import com.cloud.user.UserVO; @@ -66,9 +67,14 @@ import com.cloud.utils.component.Inject; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; import com.cloud.utils.net.NetUtils; +import com.cloud.vm.ConsoleProxyVO; import com.cloud.vm.DomainRouterVO; +import com.cloud.vm.SecondaryStorageVmVO; import com.cloud.vm.VMInstanceVO; +import com.cloud.vm.VirtualMachine; +import com.cloud.vm.dao.ConsoleProxyDao; import com.cloud.vm.dao.DomainRouterDao; +import com.cloud.vm.dao.SecondaryStorageVmDao; import com.cloud.vm.dao.VMInstanceDao; @Local(value={ConfigurationManager.class}) @@ -91,6 +97,8 @@ public class ConfigurationManagerImpl implements ConfigurationManager { @Inject AccountDao _accountDao; @Inject EventDao _eventDao; @Inject UserDao _userDao; + @Inject ConsoleProxyDao _consoleDao; + @Inject SecondaryStorageVmDao _secStorageDao; public boolean _premium; @Override @@ -665,13 +673,6 @@ public class ConfigurationManagerImpl implements ConfigurationManager { throw new InvalidParameterValueException("A zone with ID: " + zoneId + " does not exist."); } - // If DNS values are being changed, make sure there are no VMs in this zone - if (dns1 != null || dns2 != null || internalDns1 != null || internalDns2 != null) { - if (zoneHasVMs(zoneId)) { - throw new InternalErrorException("The zone is not editable because there are VMs in the zone."); - } - } - // If the Vnet range is being changed, make sure there are no allocated VNets if (vnetRange != null) { if (zoneHasAllocatedVnets(zoneId)) { @@ -679,22 +680,6 @@ public class ConfigurationManagerImpl implements ConfigurationManager { } } - //To modify a zone, we need to make sure there are no domr's associated with it - //1. List all the domain router objs - //2. Check if any of these has the current data center associated - //3. If yes, throw exception - //4, If no, edit - List allDomainRoutersAvailable = _domrDao.listAll(); - - for(DomainRouterVO domR : allDomainRoutersAvailable) - { - if(domR.getDataCenterId() == zoneId) - { - throw new InternalErrorException("The zone is not editable because there are domR's associated with the zone."); - } - } - - //5. Reached here, hence editable DataCenterVO zone = _zoneDao.findById(zoneId); String oldZoneName = zone.getName(); @@ -703,6 +688,12 @@ public class ConfigurationManagerImpl implements ConfigurationManager { newZoneName = oldZoneName; } + boolean dnsUpdate = false; + + if(dns1 != null || dns2 != null){ + dnsUpdate = true; + } + if (dns1 == null) { dns1 = zone.getDns1(); } @@ -742,6 +733,48 @@ public class ConfigurationManagerImpl implements ConfigurationManager { _zoneDao.addVnet(zone.getId(), begin, end); } + if(dnsUpdate){ + + //Update dns for domRs in zone + + List DomainRouters = _domrDao.listByDataCenter(zoneId); + + for(DomainRouterVO domR : DomainRouters) + { + domR.setDns1(dns1); + domR.setDns2(dns2); + _domrDao.update(domR.getId(), domR); + } + + //Update dns for console proxies in zone + List ConsoleProxies = _vmInstanceDao.listByZoneIdAndType(zoneId, VirtualMachine.Type.ConsoleProxy); + + for(VMInstanceVO consoleVm : ConsoleProxies) + { + ConsoleProxyVO proxy = _consoleDao.findById(consoleVm.getId()); + if( proxy!= null ){ + proxy.setDns1(dns1); + proxy.setDns2(dns2); + _consoleDao.update(proxy.getId(), proxy); + } + } + + //Update dns for secondary storage Vms in zone + List storageVms = _vmInstanceDao.listByZoneIdAndType(zoneId, VirtualMachine.Type.SecondaryStorageVm); + + for(VMInstanceVO storageVm : storageVms) + { + SecondaryStorageVmVO secStorageVm = _secStorageDao.findById(storageVm.getId()); + if( secStorageVm!= null ){ + secStorageVm.setDns1(dns1); + secStorageVm.setDns2(dns2); + _secStorageDao.update(secStorageVm.getId(), secStorageVm); + } + } + + } + + saveConfigurationEvent(userId, null, EventTypes.EVENT_ZONE_EDIT, "Successfully edited zone with name: " + zone.getName() + ".", "dcId=" + zone.getId(), "dns1=" + dns1, "dns2=" + dns2, "internalDns1=" + internalDns1, "internalDns2=" + internalDns2, "vnetRange=" + vnetRange, "guestCidr=" + guestCidr); return zone; diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index c477a6d226e..f83a482ac78 100644 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -4402,6 +4402,7 @@ public class ManagementServerImpl implements ManagementServer { SearchBuilder sb = _accountDao.createSearchBuilder(); sb.and("accountName", sb.entity().getAccountName(), SearchCriteria.Op.LIKE); sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ); + sb.and("nid", sb.entity().getId(), SearchCriteria.Op.NEQ); sb.and("type", sb.entity().getType(), SearchCriteria.Op.EQ); sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ); sb.and("needsCleanup", sb.entity().getNeedsCleanup(), SearchCriteria.Op.EQ); @@ -4433,6 +4434,9 @@ public class ManagementServerImpl implements ManagementServer { // I want to join on user_vm.domain_id = domain.id where domain.path like 'foo%' sc.setJoinParameters("domainSearch", "path", domain.getPath() + "%"); + sc.setParameters("nid", 1L); + } else { + sc.setParameters("nid", 1L); } if (type != null) { @@ -8590,6 +8594,26 @@ public class ManagementServerImpl implements ManagementServer { return false; } + @Override + public Map listCapabilities() { + Map capabilities = new HashMap(); + + String networkGroupsEnabled = _configs.get("direct.attach.network.groups.enabled"); + if(networkGroupsEnabled == null) + networkGroupsEnabled = "false"; + + capabilities.put("networkGroupsEnabled", networkGroupsEnabled); + + final Class c = this.getClass(); + String fullVersion = c.getPackage().getImplementationVersion(); + String version = "unknown"; + if(fullVersion.length() > 0){ + version = fullVersion.substring(0,fullVersion.lastIndexOf(".")); + } + capabilities.put("cloudStackVersion", version); + return capabilities; + } + } diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java b/server/src/com/cloud/storage/StorageManagerImpl.java index d3f2156f79d..53395a5a130 100644 --- a/server/src/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/com/cloud/storage/StorageManagerImpl.java @@ -939,10 +939,12 @@ public class StorageManagerImpl implements StorageManager { if (vmId != null) { VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId); if (vmInstance != null) { - return vmInstance.getHostId(); + Long hostId = vmInstance.getHostId(); + if (hostId != null && !avoidHosts.contains(vmInstance.getHostId())) + return hostId; } } - return null; + /*Can't find the vm where host resides on(vm is destroyed? or volume is detached from vm), randomly choose a host to send the cmd */ } List poolHosts = _poolHostDao.listByHostStatus(poolVO.getId(), Status.Up); Collections.shuffle(poolHosts); diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java index a372e17b6e8..c6f82d2f567 100644 --- a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java +++ b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java @@ -287,7 +287,7 @@ public class SnapshotManagerImpl implements SnapshotManager { } txn.commit(); - VolumeVO volume = _volsDao.findById(volumeId); + VolumeVO volume = _volsDao.lock(volumeId, true); if (!shouldRunSnapshot(userId, volume, policyIds)) { // A null snapshot is interpreted as snapshot creation failed which is what we want to indicate @@ -477,7 +477,7 @@ public class SnapshotManagerImpl implements SnapshotManager { _snapshotDao.update(snapshot.getId(), snapshot); long volumeId = snapshot.getVolumeId(); - VolumeVO volume = _volsDao.findById(volumeId); + VolumeVO volume = _volsDao.lock(volumeId, true); String primaryStoragePoolNameLabel = _storageMgr.getPrimaryStorageNameLabel(volume); Long dcId = volume.getDataCenterId(); diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 7a26cc7d100..721f73e3eb4 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -686,6 +686,7 @@ public class UserVmManagerImpl implements UserVmManager { } boolean started = false; + Transaction txn = Transaction.currentTxn(); try { @@ -736,6 +737,11 @@ public class UserVmManagerImpl implements UserVmManager { VolumeVO vol = rootVols.get(0); List vols = _volsDao.findCreatedByInstance(vm.getId()); + List vos = new ArrayList(); + /*compete with take snapshot*/ + for (VolumeVO userVmVol : vols) { + vos.add(_volsDao.lock(userVmVol.getId(), true)); + } Answer answer = null; int retry = _retry; @@ -2215,7 +2221,7 @@ public class UserVmManagerImpl implements UserVmManager { @Override @DB public SnapshotVO createTemplateSnapshot(long userId, long volumeId) { SnapshotVO createdSnapshot = null; - VolumeVO volume = _volsDao.findById(volumeId); + VolumeVO volume = _volsDao.lock(volumeId, true); Long id = null; diff --git a/ui/index.html b/ui/index.jsp old mode 100644 new mode 100755 similarity index 93% rename from ui/index.html rename to ui/index.jsp index cd1ae182aa8..8be1f714663 --- a/ui/index.html +++ b/ui/index.jsp @@ -1,3 +1,9 @@ +<%@ page import="java.util.Date" %> + +<% +long milliseconds = new Date().getTime(); +%> + @@ -5,11 +11,10 @@ - + cloud.com - User Console - @@ -38,12 +43,12 @@ - + - - - + + + diff --git a/ui/content/tab_accounts.html b/ui/jsp/tab_accounts.jsp old mode 100644 new mode 100755 similarity index 98% rename from ui/content/tab_accounts.html rename to ui/jsp/tab_accounts.jsp index 5a63bb419c5..df4da1544c5 --- a/ui/content/tab_accounts.html +++ b/ui/jsp/tab_accounts.jsp @@ -1,4 +1,8 @@ - +<%@ page import="java.util.Date" %> +<% +long milliseconds = new Date().getTime(); +%> +