Example #1
0
void Vm::handleBackupWorkflow(VirtTools* vt) {
	string newMode = "";
	string newState = "0";
	if (0 == activeBackupMode.compare("initialize")) {
		//20121002T010000Z
		string backupDn = "ou=backup,";
		backupDn.append(getDn());
		if (!lt->hasDn(backupDn)) {
			LDAPEntry* backupEntry = new LDAPEntry(backupDn);
			StringList values;
			values.add("top");
			values.add("organizationalUnit");

			backupEntry->addAttribute(LDAPAttribute("objectClass", values));
			backupEntry->addAttribute(LDAPAttribute("ou", "backup"));
			lt->addEntry(backupEntry);
			delete backupEntry;
		}
		char buffer[18];

		struct tm * timeinfo;
		time_t rawtime = backupConfiguration.getNextTime();
		timeinfo = localtime(&rawtime);

		strftime(buffer, 18, "%Y%m%dT%H%M00Z", timeinfo);

		activeBackupDn = "ou=";
		activeBackupDn.append(buffer).append(",").append(backupDn);
		if (!lt->hasDn(activeBackupDn)) {
			LDAPEntry* singelBackupEntry = new LDAPEntry(activeBackupDn);
			StringList values;
			values.add("top");
			values.add("organizationalUnit");
			values.add("sstProvisioning");

			time(&rawtime);
			timeinfo = localtime(&rawtime);
			strftime(buffer, 18, "%Y%m%dT%H%M00Z", timeinfo);

			singelBackupEntry->addAttribute(LDAPAttribute("objectClass", values));
			singelBackupEntry->addAttribute(LDAPAttribute("ou", buffer));
			singelBackupEntry->addAttribute(LDAPAttribute("sstProvisioningExecutionDate", "0"));
			singelBackupEntry->addAttribute(LDAPAttribute("sstProvisioningMode", "initialized"));
			singelBackupEntry->addAttribute(LDAPAttribute("sstProvisioningState", "0"));
			lt->addEntry(singelBackupEntry);
			delete singelBackupEntry;
		}
		SYSLOGLOGGER(logDEBUG) << (getDn()) << ": change sstProvisioningMode from initialize to initialized";

		newMode = "snapshot";
		newState = "0";
	}
	else if (0 == activeBackupMode.compare("initialized")) {
		// This attribute is written by the fc-brokerd and used internally by the fc-brokerd.

		newMode = "snapshot";
		newState = "0";
	}
	else if (0 == activeBackupMode.compare("snapshotted") && 0 == activeBackupReturnValue) {
		//  The attribute is changed by the Backup-Daemon from snapshotting to snapshotted when the snapshot process has finished.

		newMode = "merge";
		newState = "0";
	}
	else if (0 == activeBackupMode.compare("merged") && 0 == activeBackupReturnValue) {
		// The attribute is changed by the Backup-Daemon from merging to merged when the merge process has finished.

		newMode = "retain";
		newState = "0";
	}
	else if (0 == activeBackupMode.compare("retained") && 0 == activeBackupReturnValue) {
		// The attribute is changed by the Backup-Daemon from retaining to retained when the retain process has finished.

		newMode = "finished";
		time_t rawtime;
		struct tm * timeinfo;
		char buffer[18];

		time(&rawtime);
		timeinfo = localtime(&rawtime);

		strftime(buffer, 18, "%Y%m%dT%H%M00Z", timeinfo);
		newState = buffer;
	}
	else if (0 == activeBackupMode.compare("deleted") && 0 == activeBackupReturnValue) {
		lt->removeEntry(activeBackupDn, true);
	}
	else if (0 == activeBackupMode.compare("unretainedLargeFiles") && 0 == activeBackupReturnValue) {
		// The attribute is changed by the Backup-Daemon from unretaining to unretained when the unretain process has finished.

		newMode = "restore";
		newState = "0";

		vt->stopVmForRestore(this);
		// Merge ldif
	}
	else if (0 == activeBackupMode.compare("restored") && 0 == activeBackupReturnValue) {
		// The attribute is changed by the Backup-Daemon from restoring to restored when the restore process has finished.

		newMode = "finished";
		time_t rawtime;
		struct tm * timeinfo;
		char buffer[18];

		time(&rawtime);
		timeinfo = localtime(&rawtime);

		strftime(buffer, 18, "%Y%m%dT%H%M00Z", timeinfo);
		newState = buffer;
	}
	if (0 != newMode.length()) {
		LDAPModList* modlist = new LDAPModList();
		const string value = newMode;
		LDAPAttribute attr = LDAPAttribute("sstProvisioningMode", value);
		LDAPModification modification = LDAPModification(attr, LDAPModification::OP_REPLACE);
		modlist->addModification(modification);
		const string value2 = newState;
		attr = LDAPAttribute("sstProvisioningState", value2);
		modification = LDAPModification(attr, LDAPModification::OP_REPLACE);
		modlist->addModification(modification);
		SYSLOGLOGGER(logDEBUG) << (getDn()) << ": change sstProvisioningMode from " << activeBackupMode << " to " << value;
		lt->modifyEntry(activeBackupDn, modlist);
		delete modlist;
	}
	if (0 == newMode.compare("finished") && 0 == activeBackupMode.compare("retained") && singleBackupCount > backupConfiguration.getIterations()) {
		// check for deletion of an older backup
		if (0 < finishedBackups.size()) {
			string oldBackupDn = *(finishedBackups.begin());

			LDAPModList* modlist = new LDAPModList();
			LDAPAttribute attr = LDAPAttribute("sstProvisioningMode", "delete");
			LDAPModification modification = LDAPModification(attr, LDAPModification::OP_REPLACE);
			modlist->addModification(modification);
			attr = LDAPAttribute("sstProvisioningState", "0");
			modification = LDAPModification(attr, LDAPModification::OP_REPLACE);
			modlist->addModification(modification);
			SYSLOGLOGGER(logDEBUG) << (getDn()) << ": change sstProvisioningMode from finished to delete";
			lt->modifyEntry(oldBackupDn, modlist);
			delete modlist;
		}
	}
}
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;
}