vector<AttributeMap> Ldap::search(string base, int scope, string filter, const StringList& attribs) { if(bound == false) { return vector<AttributeMap>(); } LDAPSearchResults* lr = lc->search(base, scope, filter,attribs, false); LDAPEntry* le; const LDAPAttribute* la; StringList s; vector<AttributeMap> result; AttributeMap temp; int i = 0; while( (le = lr->getNext()) ) { for(StringList::const_iterator it =attribs.begin(); it!=attribs.end(); it++) { //cout << endl << "Name: " << *it << " |"; la = le->getAttributeByName(*it); if(la == NULL) continue; s = la->getValues(); for(StringList::const_iterator st = s.begin(); st != s.end(); st ++) { // concatenates multivalues with | temp[*it] += (i>0?"|"+*st:*st); i++; } i=0; } //cout << endl; if(temp.size() > 0) { result.push_back(temp); temp.clear(); } } return result; }
const string LdapTools::nextSpicePort(const Node* node) { int port = 0; int portMin = Config::getInstance()->getSpicePortMin(); int portMax = Config::getInstance()->getSpicePortMax(); int size = portMax - portMin + 1; //bool* portsUsed = new bool[size]; bool* portsUsed = (bool *) malloc(size * sizeof(bool)); for (int i = 0; i < size; i++) { portsUsed[i] = false; } string base("ou=virtual machines,ou=virtualization,ou=services,"); base.append(Config::getInstance()->getLdapBaseDn()); // string filter = "(&(objectClass=sstSpice))"; string filter = "(&(objectClass=sstSpice)(sstNode="; filter.append(node->getName()).append("))"); StringList attrs = StringList(); attrs.add("sstVirtualMachine"); attrs.add("sstSpicePort"); LDAPSearchResults* entries = lc->search(base, LDAPConnection::SEARCH_SUB, filter, attrs); LDAPEntry* entry = entries->getNext(); while (entry != 0) { string vmName = ""; const LDAPAttribute* attribute = entry->getAttributeByName("sstVirtualMachine"); const StringList values = attribute->getValues(); StringList::const_iterator it = values.begin(); if (it != values.end()) { vmName = it->c_str(); } const LDAPAttribute* attribute2 = entry->getAttributeByName("sstSpicePort"); const StringList values2 = attribute2->getValues(); StringList::const_iterator it2 = values2.begin(); if (it2 != values2.end()) { port = atoi(it2->c_str()); SYSLOGLOGGER(logDEBUG) << " " << port << " in use " << port - portMin << " (" << vmName << ")"; portsUsed[port - portMin] = true; } delete entry; entry = entries->getNext(); } filter = "(&(objectClass=sstSpice)(sstMigrationNode="; filter.append(node->getName()).append("))"); attrs = StringList(); attrs.add("sstMigrationSpicePort"); entries = lc->search(base, LDAPConnection::SEARCH_SUB, filter, attrs); entry = entries->getNext(); while (entry != 0) { const LDAPAttribute* attribute = entry->getAttributeByName("sstMigrationSpicePort"); const StringList values = attribute->getValues(); StringList::const_iterator it = values.begin(); if (it != values.end()) { port = atoi(it->c_str()); SYSLOGLOGGER(logDEBUG) << "M " << port << " in use " << port - portMin; portsUsed[port - portMin] = true; } delete entry; entry = entries->getNext(); } port = 0; for (int i = 0; i < size; i++) { if (!portsUsed[i]) { port = portMin + i; break; } } //delete[] portsUsed; free(portsUsed); SYSLOGLOGGER(logDEBUG) << "nextSpicePort: " << base << "; " << filter << "; port: " << port; char buffer[10]; sprintf(buffer, "%d", port); return string(buffer); }
Vm* LdapTools::cloneVm(const Vm* vm, const Node* targetNode, VirtTools* vt, string newUuid) { Vm* retval = NULL; size_t pos; const VmPool* vmPool = vm->getVmPool(); SYSLOGLOGGER(logDEBUG) << "cloneVm DN: " << (vm->getDn()); LDAPSearchResults* entries = lc->search(vm->getDn(), LDAPConnection::SEARCH_SUB); if (entries != 0) { string newVmDn; LDAPEntry* entry = entries->getNext(); const string oldVmDn = entry->getDN(); string uuid; vm->getDnPart(oldVmDn, "sstVirtualMachine", uuid); SYSLOGLOGGER(logDEBUG) << "old DN: " << oldVmDn << "; uuid: " << uuid; string oldVm = "sstVirtualMachine="; oldVm.append(uuid); string newVm = "sstVirtualMachine=" + newUuid; pos = oldVmDn.find(oldVm); newVmDn = newVm; newVmDn.append(oldVmDn.substr(oldVm.length())); SYSLOGLOGGER(logDEBUG) << "new DN: " << newVmDn; string firstMac = ""; bool diskSet = false; while (entry != 0) { string dn = entry->getDN(); pos = dn.find(oldVm); string newDn = dn.substr(0, pos); newDn.append(newVm).append(dn.substr(pos + oldVm.length())); //SYSLOGLOGGER(logDEBUG) << "oldDn: " << dn; //SYSLOGLOGGER(logDEBUG) << "newDn: " << newDn; LDAPEntry* newEntry = new LDAPEntry(newDn, entry->getAttributes()); if (0 == newDn.find("sstVirtualMachine")) { // DN starts with sstVirtualMachine newEntry->delAttribute("sstVirtualMachine"); const LDAPAttribute* attribute = entry->getAttributeByName("sstDisplayName"); StringList values = attribute->getValues(); StringList::const_iterator it = values.begin(); string displayName = *it; displayName.append(" clone"); newEntry->replaceAttribute(LDAPAttribute("sstDisplayName", displayName)); newEntry->replaceAttribute(LDAPAttribute("sstNode", targetNode->getName())); newEntry->replaceAttribute(LDAPAttribute("sstVirtualMachineType", "dynamic")); newEntry->replaceAttribute(LDAPAttribute("sstVirtualMachineSubType", "Desktop")); newEntry->replaceAttribute(LDAPAttribute("sstOsBootDevice", "hd")); newEntry->replaceAttribute(LDAPAttribute("sstSpicePort", nextSpicePort(targetNode))); newEntry->replaceAttribute(LDAPAttribute("sstSpicePassword", newUuid)); } else if (0 == newDn.find("sstDisk") && !diskSet) { // DN starts with sstDisk const LDAPAttribute* attribute = entry->getAttributeByName("sstDevice"); StringList values = attribute->getValues(); StringList::const_iterator it = values.begin(); string value = *it; if (0 == value.compare("disk")) { const string volumeName = vt->generateUUID(); string sourceFile = vmPool->getStoragePoolDir(); sourceFile.append("/").append(volumeName).append(".qcow2"); SYSLOGLOGGER(logINFO) << "volumeName: " << volumeName; SYSLOGLOGGER(logINFO) << "sourceFile: " << sourceFile; try { vt->createBackingStoreVolumeFile(vm, vmPool->getStoragePoolName(), volumeName); } catch (VirtException& e) { SYSLOGLOGGER(logINFO) << "-------------- caught Exception ---------"; SYSLOGLOGGER(logINFO) << e; lc->del(newDn); delete entry; return NULL; } newEntry->replaceAttribute(LDAPAttribute("sstVolumeName", volumeName)); newEntry->replaceAttribute(LDAPAttribute("sstSourceFile", sourceFile)); diskSet = true; } } else if (0 == newDn.find("sstInterface") && 0 == firstMac.size()) { // DN start with sstInterface firstMac = vt->generateMacAddress(); newEntry->replaceAttribute(LDAPAttribute("sstMacAddress", firstMac)); } lc->add(newEntry); delete entry; entry = entries->getNext(); } string peopleDn = "ou=people,"; peopleDn.append(newVmDn); LDAPEntry* peopleEntry = new LDAPEntry(peopleDn); StringList values; values.add("top"); values.add("organizationalUnit"); values.add("sstRelationship"); peopleEntry->addAttribute(LDAPAttribute("objectClass", values)); peopleEntry->addAttribute(LDAPAttribute("ou", "people")); peopleEntry->addAttribute(LDAPAttribute("description", "This is the assigned people subtree.")); peopleEntry->addAttribute(LDAPAttribute("sstBelongsToCustomerUID", vm->getCustomerUID())); peopleEntry->addAttribute(LDAPAttribute("sstBelongsToResellerUID", vm->getResellerUID())); lc->add(peopleEntry); delete peopleEntry; const NetworkRange* range = vmPool->getRange(); string base = "ou=dhcp,ou=networks,ou=virtualization,ou=services,"; base.append(Config::getInstance()->getLdapBaseDn()); string filter = "(&(objectClass=sstVirtualizationNetworkRange)(cn="; filter.append(range->getRange()).append("))"); SYSLOGLOGGER(logDEBUG) << "dhcp base: " << base << "; filter " << filter; StringList attrs = StringList(); attrs.add("cn"); LDAPSearchResults* entries2 = lc->search(base, LDAPConnection::SEARCH_SUB, filter, attrs); if (NULL != entries2) { LDAPEntry* entry2 = entries2->getNext(); if (NULL != entry2) { string dn = "cn="; dn.append(newUuid).append(",ou=virtual machines,"); string entryDn = entry2->getDN(); delete entry2; SYSLOGLOGGER(logDEBUG) << "rangeDN: " << entryDn; pos = entryDn.find("ou=ranges,"); dn.append(entryDn.substr(pos + 10)); SYSLOGLOGGER(logDEBUG) << "dhcp dn:" << dn; LDAPEntry* dhcpEntry = new LDAPEntry(dn); StringList vals; vals.add("top"); vals.add("dhcpHost"); vals.add("sstVirtualizationNetwork"); dhcpEntry->addAttribute(LDAPAttribute("objectClass", vals)); dhcpEntry->addAttribute(LDAPAttribute("cn", newUuid)); dhcpEntry->addAttribute(LDAPAttribute("sstBelongsToCustomerUID", vm->getCustomerUID())); dhcpEntry->addAttribute(LDAPAttribute("sstBelongsToResellerUID", vm->getResellerUID())); dhcpEntry->addAttribute(LDAPAttribute("dhcpHWAddress", "ethernet " + firstMac)); dhcpEntry->addAttribute(LDAPAttribute("dhcpStatements", "fixed-address " + getFreeIp(range))); lc->add(dhcpEntry); delete dhcpEntry; } } retval = readVm(newVmDn, true); } return retval; }