Report the host ip address to the API on startup and every 5mins after.

This commit is contained in:
Pagan Gazzard 2014-07-11 15:57:49 +01:00 committed by Pablo Carranza Vélez
parent be5a32187a
commit a47022a0b4
3 changed files with 59 additions and 0 deletions

View File

@ -58,3 +58,12 @@ knex('config').select('value').where(key: 'uuid').then ([uuid]) ->
, 5 * 60 * 1000) # Every 5 mins
supervisor.update()
updateIpAddr = ->
utils.findIpAddrs().then (ipAddrs) ->
application.updateDeviceInfo(
ip_address: ipAddrs.join(' ')
)
console.log('Starting periodic check for IP addresses..')
setInterval(updateIpAddr, 5 * 60 * 1000) # Every 5 mins
updateIpAddr()

View File

@ -222,3 +222,29 @@ exports.update = ->
setTimeout(exports.update)
# Set the updating as finished
currentlyUpdating = 0
exports.updateDeviceInfo = (body) ->
Promise.all([
knex('config').select('value').where(key: 'apiKey')
knex('config').select('value').where(key: 'uuid')
])
.spread ([{value: apiKey}], [{value: uuid}]) ->
resinAPI.get(
resource: 'device'
options:
filter:
uuid: uuid
customOptions:
apikey: apiKey
)
.then (devices) ->
if devices.length is 0
throw new Error('Could not find this device?!')
deviceID = devices[0].id
resinAPI.patch(
resource: 'device'
id: deviceID
body: body
customOptions:
apikey: apiKey
)

View File

@ -22,3 +22,27 @@ exports.mixpanelTrack = (event, properties={}) ->
properties = _.assign(_.cloneDeep(properties), mixpanelProperties)
mixpanelClient.track(event, properties)
# Returns an array of the host's ip address(es) by parsing the host /proc/net/fib_trie
exports.findIpAddrs = ->
fs.readFileAsync('/mnt/fib_trie', 'utf8')
.then (fibtrie) ->
prevLine = ''
fibtrie.split('\n')
.map (line) ->
line = line.trim()
# We only care about LOCAL routes (not UNICAST or BROADCAST)
if line.match(/LOCAL$/)
# Then we make sure the previous line was an ending branch (and hence contains an IP - 127.0.0.0 has BROADCAST and LOCAL entries)
if prevLine.match(/^\|--/)
# Then we remove the ending branch bit
maybeAddr = prevLine.replace(/^\|--/, '').trim()
# And ignore loopback/docker interfaces.
# TODO: Docker interface can technically be on another address range if 172.17
if !maybeAddr.match(/^(127.0.0.1|172.17.)/)
ipAddr = maybeAddr
prevLine = line
return ipAddr
.filter(Boolean)