manman-sync: deal with multi node locations better
This commit is contained in:
		
							parent
							
								
									c3443b0cdd
								
							
						
					
					
						commit
						1fcf25dbb4
					
				| @ -2,7 +2,8 @@ package gluon-manman-sync | |||||||
| 
 | 
 | ||||||
| config sync 'sync' | config sync 'sync' | ||||||
| 	option enabled '0' | 	option enabled '0' | ||||||
|   option node_id '' |   option location_id '' | ||||||
|  | 	option node '' | ||||||
|   option last_data_hash '' |   option last_data_hash '' | ||||||
|   option last_data '' |   option last_data '' | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -21,6 +21,17 @@ local mappings = { | |||||||
|   eth = 'lan' |   eth = 'lan' | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | -- https://stackoverflow.com/a/1647577/3990041 | ||||||
|  | function string:split(pat) | ||||||
|  |   pat = pat or '%s+' | ||||||
|  |   local st, g = 1, self:gmatch("()("..pat..")") | ||||||
|  |   local function getter(segs, seps, sep, cap1, ...) | ||||||
|  |     st = sep and seps + #sep | ||||||
|  |     return self:sub(segs, (seps or 0) - 1), cap1 or sep, ... | ||||||
|  |   end | ||||||
|  |   return function() if st then return getter(st, g()) end end | ||||||
|  | end | ||||||
|  | 
 | ||||||
| -- https://gist.github.com/Uradamus/10323382 | -- https://gist.github.com/Uradamus/10323382 | ||||||
| local function shuffle(tbl) | local function shuffle(tbl) | ||||||
|   for i = #tbl, 2, -1 do |   for i = #tbl, 2, -1 do | ||||||
| @ -138,12 +149,21 @@ if uci:get_bool('gluon-manman-sync', 'sync', 'enabled') then | |||||||
|   uci:set('gluon-node-info', _location, 'latitude', location.location.lat) |   uci:set('gluon-node-info', _location, 'latitude', location.location.lat) | ||||||
|   uci:set('gluon-node-info', _location, 'longitude', location.location.long) |   uci:set('gluon-node-info', _location, 'longitude', location.location.long) | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  |   local local_router_id | ||||||
|  |   for id, _ in string.split(hostname, '-') do | ||||||
|  |     if id then | ||||||
|  |       local_router_id = id | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   local local_node = uci:get('gluon-manman-sync', 'sync', 'node') or local_router_id | ||||||
|   local node |   local node | ||||||
|   local should_hostname |   local should_hostname | ||||||
| 
 | 
 | ||||||
|   if #location.nodes > 1 then |   if #location.nodes > 1 then | ||||||
|     for _, potential_node in ipairs(location.nodes) do |     for _, potential_node in ipairs(location.nodes) do | ||||||
|       if potential_node.name == hostname then |       if potential_node.name == local_node then | ||||||
|         node = potential_node |         node = potential_node | ||||||
|         should_hostname = location.location.name .. '-' .. node.name |         should_hostname = location.location.name .. '-' .. node.name | ||||||
|       end |       end | ||||||
| @ -151,10 +171,12 @@ if uci:get_bool('gluon-manman-sync', 'sync', 'enabled') then | |||||||
|   else |   else | ||||||
|     node = location.nodes[1] |     node = location.nodes[1] | ||||||
|     should_hostname = location.location.name |     should_hostname = location.location.name | ||||||
|  |     -- save node in csse this location gets more nodes | ||||||
|  |     uci:set('gluon-manman-sync', 'sync', 'node', node.name) | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   if node == nil then |   if node == nil then | ||||||
|     print('E: unable to find matching node') |     print('E: unable to find matching node (selector "' .. node .. '")') | ||||||
|     return 2 |     return 2 | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user