balena-supervisor/src/utils.coffee

66 lines
2.0 KiB
CoffeeScript
Raw Normal View History

2013-12-23 04:22:54 +00:00
Promise = require 'bluebird'
2014-06-18 16:54:36 +00:00
_ = require 'lodash'
2013-12-23 04:22:54 +00:00
fs = Promise.promisifyAll require 'fs'
2014-06-18 16:54:36 +00:00
config = require './config'
mixpanel = require 'mixpanel'
ping = require 'ping'
2014-04-28 10:56:17 +00:00
# Parses package.json and returns resin-supervisor's version
exports.getSupervisorVersion = ->
fs.readFileAsync(__dirname + '/../package.json', 'utf-8')
2014-04-28 10:56:17 +00:00
.then (data) ->
obj = JSON.parse data
2014-04-29 07:29:10 +00:00
return obj.version
2014-06-18 16:54:36 +00:00
mixpanelClient = mixpanel.init(config.mixpanelToken)
2014-06-18 16:54:36 +00:00
exports.mixpanelProperties = mixpanelProperties =
username: require('/boot/config.json').username
2014-09-15 11:31:14 +00:00
exports.mixpanelTrack = (event, properties = {}) ->
console.log('Event:', event, JSON.stringify(properties))
2014-06-18 16:54:36 +00:00
# Mutation is bad, and it should feel bad
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$/)
2014-09-15 11:31:14 +00:00
# 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)
# Helps in blinking the LED from the given end point.
exports.blink = (ms = 200) ->
fs.writeFileAsync(config.ledFile, 1)
.delay(ms)
.then -> fs.writeFileAsync(config.ledFile, 0)
# Helps in checking connectivity by pinging the given site.
exports.checkConnectivity = (host = '8.8.8.8') ->
2014-09-15 11:31:14 +00:00
ping.sys.promise_probe(host,
timeout: 1
extra: [ '-c 1' ]
).then (res) ->
return res.alive