manman-sync finiush
This commit is contained in:
		
							parent
							
								
									46f58256e9
								
							
						
					
					
						commit
						a1f9015fbd
					
				| @ -7,7 +7,7 @@ include ../gluon.mk | |||||||
| 
 | 
 | ||||||
| define Package/gluon-config-mode-manman-sync | define Package/gluon-config-mode-manman-sync | ||||||
|   TITLE:=Sync location data from manman |   TITLE:=Sync location data from manman | ||||||
|   DEPENDS:=+gluon-config-mode-core +luci-lib-ip +gluon-manman-sync |   DEPENDS:=+gluon-config-mode-core +gluon-manman-sync | ||||||
| endef | endef | ||||||
| 
 | 
 | ||||||
| $(eval $(call BuildPackageGluon,gluon-config-mode-manman-sync)) | $(eval $(call BuildPackageGluon,gluon-config-mode-manman-sync)) | ||||||
|  | |||||||
| @ -27,6 +27,6 @@ return function(form, uci) | |||||||
| 	end | 	end | ||||||
| 
 | 
 | ||||||
| 	function s:write() | 	function s:write() | ||||||
| 		uci:save('gluon') | 		uci:save('gluon-manman-sync') | ||||||
| 	end | 	end | ||||||
| end | end | ||||||
|  | |||||||
| @ -6,7 +6,7 @@ PKG_VERSION:=1 | |||||||
| include ../gluon.mk | include ../gluon.mk | ||||||
| 
 | 
 | ||||||
| define Package/gluon-manman-sync | define Package/gluon-manman-sync | ||||||
|   DEPENDS:=+gluon-core +micrond |   DEPENDS:=+gluon-core +micrond +luci-lib-ip +luci-lib-httpclient | ||||||
|   TITLE:=Sync configuration with data from manman |   TITLE:=Sync configuration with data from manman | ||||||
| endef | endef | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										2
									
								
								package/gluon-manman-sync/check_site.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								package/gluon-manman-sync/check_site.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,2 @@ | |||||||
|  | need_string(in_site({'manman', 'api'}), false) | ||||||
|  | need_string(in_site({'manman', 'key'}), false) | ||||||
| @ -1,9 +1,17 @@ | |||||||
| #!/usr/bin/lua | #!/usr/bin/lua | ||||||
| 
 | 
 | ||||||
| local uci = require('simple-uci').cursor() | local uci = require('simple-uci').cursor() | ||||||
| local ip = require "luci.ip" -- luci-lib-ip | local ip = require 'luci.ip' -- luci-lib-ip | ||||||
|  | local fetch = require 'luci.httpclient' | ||||||
|  | local json = require 'luci.jsonc' | ||||||
|  | local site = require 'gluon.site' | ||||||
| 
 | 
 | ||||||
| local manman = 'https://manman.graz.funkfeuer.at' | local pretty_hostname = require 'pretty_hostname' | ||||||
|  | local hostname = pretty_hostname.get(uci) | ||||||
|  | 
 | ||||||
|  | local manapi = site.manman.api() | ||||||
|  | local mankey = site.manman.key() | ||||||
|  | -- TODO: use manman ecdsa key to verify response | ||||||
| 
 | 
 | ||||||
| -- NOTE: these will have mesh_ appended for static-ip | -- NOTE: these will have mesh_ appended for static-ip | ||||||
| local mappings = { | local mappings = { | ||||||
| @ -21,50 +29,99 @@ if uci:get_bool('gluon-manman-sync', 'sync', 'enabled') then | |||||||
| 
 | 
 | ||||||
|   -- check manman reachability, abort if not reachable |   -- check manman reachability, abort if not reachable | ||||||
| 
 | 
 | ||||||
|   -- get location from ip |   local success, a, b, c = pcall(function() return fetch.request_raw(manapi .. '/') end) | ||||||
| 
 |   if not success then | ||||||
|   location = getLocation() |     print('E: couldnt reach manman: ' .. a) | ||||||
|   print('Syncing with location ' .. location['name']) |  | ||||||
| 
 |  | ||||||
|   local device |  | ||||||
| 
 |  | ||||||
|   if #location['devices'] > 1 then |  | ||||||
|     print('E: multiple devices, not impl yet') |  | ||||||
|     -- TODO: iterate devices, check if any with matching hostname, then use that |  | ||||||
|     return 2 |     return 2 | ||||||
|   else |   else | ||||||
|     device = location['devices'][0] |     if a ~= 200 then | ||||||
|  |       print('E: couldnt reach manman - unexpected fetch result', a, b, c) | ||||||
|  |       return 2 | ||||||
|  |     end | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   if device == nil then |   -- try to fetch data | ||||||
|     print('E: unable to find matching device') |   print('Fetching manman data...') | ||||||
|  |   local code, _, result = fetch.request_raw(manapi .. '/location/show/' .. location_id) | ||||||
|  | 
 | ||||||
|  |   if code == 404 then | ||||||
|  |     print('E: location does not exist') | ||||||
|     return 2 |     return 2 | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   print('Syncing with device ' .. device['name']) |   if code < 1 then | ||||||
|  |     print('E: failed to fetch') | ||||||
|  |     return 2 | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   -- cloudflare's reverse proxies send http chunked responses with chunk sizes | ||||||
|  |   -- for whatever reasons the chunk size gets smashed into the result | ||||||
|  |   -- this is a hack to fish it out, it is irrelevant on unaffected reverse proxies | ||||||
|  |   j_start = string.find(result, '{') | ||||||
|  |   result = string.sub(result, j_start) | ||||||
|  | 
 | ||||||
|  |   local location = json.parse(result) | ||||||
|  |   print('Syncing with location ' .. location.location.name) | ||||||
|  | 
 | ||||||
|  |   uci:set('gluon-node-info', 'owner', 'contact', location.administrator.email) | ||||||
|  |   uci:set('gluon-node-info', 'location', 'share_location', '1') | ||||||
|  |   uci:set('gluon-node-info', 'location', 'latitude', location.location.lat) | ||||||
|  |   uci:set('gluon-node-info', 'location', 'longitutde', location.location.long) | ||||||
|  | 
 | ||||||
|  |   local node | ||||||
|  | 
 | ||||||
|  |   local should_hostname | ||||||
|  | 
 | ||||||
|  |   if #location.nodes > 1 then | ||||||
|  |     for i, potential_node in ipairs(location.nodes) do | ||||||
|  |       if potential_node.name == hostname then | ||||||
|  |         node = potential_node | ||||||
|  |         should_hostname = location.location.name .. '-' .. node.name | ||||||
|  |       end | ||||||
|  |     end | ||||||
|  |   else | ||||||
|  |     node = location.nodes[1] | ||||||
|  |     should_hostname = location.location.name | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   if node == nil then | ||||||
|  |     print('E: unable to find matching node') | ||||||
|  |     return 2 | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   if hostname ~= should_hostname then | ||||||
|  |     print('Renaming node to ' .. should_hostname) | ||||||
|  |     pretty_hostname.set(uci, should_hostname) | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   print('Syncing data for node ' .. node.name) | ||||||
| 
 | 
 | ||||||
|   -- TODO: compare device |   -- TODO: compare device | ||||||
| 
 | 
 | ||||||
|   -- try to fetch data |  | ||||||
|   -- check if anything changed since last time |   -- check if anything changed since last time | ||||||
|   -- if yes, apply changes and do gluon-reload |   -- if yes, apply changes and do gluon-reload | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|   for index, net in ipairs(device['networks']) do |   for i, net in ipairs(node.interfaces) do | ||||||
|     net_name = net['name'] |     net_name = net.name | ||||||
|     net_mapped = mappings[net_name] or net_name |     net_mapped = mappings[net_name] or net_name | ||||||
|     if not string.find(net_mapped, '_') then |     if not string.find(net_mapped, '_') then | ||||||
|       net_mapped = 'mesh_' .. net_mapped |       net_mapped = 'mesh_' .. net_mapped | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     cidr = ip.new(net['ip'], net['netmask']):string() |     cidr = ip.new(net.ip, net.netmask):string() | ||||||
| 
 | 
 | ||||||
|     print('Syncing ' .. net_name .. ' as ' .. net_mapped .. ' to ' .. cidr) |     print('Syncing ' .. net_name .. ' as ' .. net_mapped .. ' to ' .. cidr) | ||||||
|     uci:set('gluon-static-ip', net_mapped, 'ip4', cidr) |     uci:set('gluon-static-ip', net_mapped, 'ip4', cidr) | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|  |   uci:save('gluon-manman-sync') | ||||||
|  |   uci:save('gluon-static-ip') | ||||||
|  |   uci:save('gluon-node-info') | ||||||
|  | 
 | ||||||
|   -- TODO: exec gluon-reload |   -- TODO: exec gluon-reload | ||||||
|   print('Reloading...') |   print('Reloading...') | ||||||
|  |   os.execute('exec gluon-reload') | ||||||
| else | else | ||||||
|   print('manman-sync not enabled, skipping') |   print('manman-sync not enabled, skipping') | ||||||
| end | end | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user