summaryrefslogtreecommitdiffstats
path: root/modules-available/dnbd3/baseconfig/getconfig.inc.php
blob: e4f84d817faaf7c003e6d95116960065cd387a00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php

if (Dnbd3::isEnabled()) {
	if (!Dnbd3::hasNfsFallback()) {
		ConfigHolder::add("SLX_VM_NFS", false, 1000);
		ConfigHolder::add("SLX_VM_NFS_USER", false, 1000);
		ConfigHolder::add("SLX_VM_NFS_PASSWD", false, 1000);
	}

	ConfigHolder::add('SLX_VM_DNBD3', 'yes');
}

// Locations from closest to furthest (order)
$locations = ConfigHolder::get('SLX_LOCATIONS');
if ($locations === false) {
	$locationIds = [0];
} else {
	$locationIds = explode(' ', $locations);
	if (empty($locationIds)) {
		$locationIds[] = 0;
	}
}

$res = Database::simpleQuery('SELECT s.fixedip, m.clientip, sxl.locationid FROM dnbd3_server s
		LEFT JOIN machine m USING (machineuuid)
		LEFT JOIN dnbd3_server_x_location sxl USING (serverid)');
// Lookup of priority - first index (0) will be closest location in chain
// low value is higher priority
$locationsAssoc = array_flip($locationIds);
$servers = array();
$fallback = array();
while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
	if ($row['fixedip'] === '<self>') {
		$row['fixedip'] = Property::getServerIp();
		$defPrio = 2000;
	} else {
		$defPrio = 1000;
	}
	$ip = $row['fixedip'] ? $row['fixedip'] : $row['clientip'];
	// See if this server is meant for the client at all
	if (!is_null($row['locationid']) && !isset($locationsAssoc[$row['locationid']])) {
		$fallback[$ip] = true;
		continue;
	}
	// Server allowed for client
	if ($defPrio === 1000 && is_null($row['locationid'])) {
		// Server is not assigned to this location, try to guess it by its IP address
		$serverLoc = Location::getFromIp($ip);
		if ($serverLoc !== false) {
			$row['locationid'] = $serverLoc;
		}
	}
	$old = isset($servers[$ip]) ? $servers[$ip] : PHP_INT_MAX;
	if (is_null($row['locationid']) || !isset($locationsAssoc[$row['locationid']])) {
		$servers[$ip] = min($defPrio . '.' . mt_rand(), $old);
	} else {
		$servers[$ip] = min($locationsAssoc[$row['locationid']] . '.' . mt_rand(), $old);
	}
}

foreach ($servers as $k => $v) {
	unset($fallback[$k]);
}

asort($servers, SORT_NUMERIC | SORT_ASC);
ConfigHolder::add('SLX_DNBD3_SERVERS', implode(' ', array_keys($servers)));
ConfigHolder::add('SLX_DNBD3_FALLBACK', implode(' ', array_keys($fallback)));