summaryrefslogblamecommitdiffstats
path: root/modules-available/dnbd3/baseconfig/getconfig.inc.php
blob: eff821fca4ff83ac5596a871b40954a6ab9aadad (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11

     


                         





                                                                    
 
                                                 

 

                                                
                          









                                                                                              
                                                                         
                                                                         
                               

                                           
                    
                        

                                                          
                                                             


                                
                                                  





                                                                                          
                                                               
                                                                                             




                                                        
                                            
                                                                                         
                                                                      
                
                                                                                                 


         



                                

                                                                           
                                                                            
<?php

/** @var ?string $uuid */
/** @var ?string $ip */

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 === null) {
	$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();
foreach ($res as $row) {
	if ($row['fixedip'] === '<self>') {
		$row['fixedip'] = Property::getServerIp();
		$defPrio = Dnbd3::preferLocal() ? 500 : 2000;
	} else {
		$defPrio = 1000;
	}
	$ip = $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 = $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)));