summaryrefslogtreecommitdiffstats
path: root/modules-available/dnbd3/baseconfig/getconfig.inc.php
blob: fe1bef17d0e7038d2dd6c323e9bf173d5aca55e5 (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
<?php

if (!Dnbd3::isEnabled()) return;

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);
}

// 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)
		WHERE sxl.locationid IS NULL OR sxl.locationid IN (:lids)', array('lids' => $locationIds));
// Lookup of priority - first index (0) will be closest location in chain
// low value is higher priority
$locationsAssoc = array_flip($locationIds);
$servers = 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'];
	if ($defPrio === 1000 && is_null($row['locationid'])) {
		$serverLoc = Location::getFromIp($ip);
		if ($serverLoc !== false) {
			$row['locationid'] = $serverLoc;
		}
	}
	$old = isset($servers[$ip]) ? $servers[$ip] : $defPrio;
	if (is_null($row['locationid']) || !isset($locationsAssoc[$row['locationid']])) {
		$servers[$ip] = min($defPrio, $old) . '.' . mt_rand();
	} else {
		$servers[$ip] = min($locationsAssoc[$row['locationid']], $old) . '.' . mt_rand();
	}
}

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