Skip to content

Instantly share code, notes, and snippets.

@othiym23
Created September 14, 2011 19:16
Show Gist options
  • Save othiym23/1217498 to your computer and use it in GitHub Desktop.
Save othiym23/1217498 to your computer and use it in GitHub Desktop.
lockerd.coffee
checkKeys = ->
lcrypto.generateSymKey (hasKey) ->
unless hasKey
shutdown 1
return
lcrypto.generatePKKeys (hasKeys) ->
unless hasKeys
shutdown 1
return
finishStartup()
finishStartup = ->
gitHead = spawn("git", [ "rev-parse", "--verify", "HEAD" ])
gitHead.stdout.on_ "data", (data) ->
fs.writeFileSync path.join(lconfig.lockerDir, lconfig.me, "gitrev.json"), JSON.stringify(data.toString())
lconfig.scannedDirs.forEach (dirToScan) ->
console.log dirToScan
installable = true
installable = false if dirToScan == "Collections"
serviceManager.scanDirectory dirToScan, installable
syncManager.scanDirectory "Connectors"
serviceManager.findInstalled()
webservice.startService lconfig.lockerPort, runMigrations
lockerPortNext = "1" + lconfig.lockerPort
lockerPortNext++
runMigrations = ->
migrations = []
metaData = version: 1
try
migrations = fs.readdirSync(path.join(lconfig.lockerDir, "/migrations"))
console.error migrations
metaData = JSON.parse(fs.readFileSync(path.join(lconfig.lockerDir, lconfig.me, "state.json")))
console.error metaData
migrations = migrations.sort() if migrations.length > 0
i = 0
while i < migrations.length
if migrations[i].substring(0, 13) > metaData.version
try
console.log "running global migration : " + migrations[i]
migrate = require(path.join(lconfig.lockerDir, "migrations", migrations[i]))
ret = migrate(lconfig)
if ret
curMe = JSON.parse(fs.readFileSync(path.join(lconfig.lockerDir, lconfig.me, "state.json"), "utf8"))
metaData.version = migrations[i].substring(0, 13)
curMe.version = metaData.version
fs.writeFileSync path.join(lconfig.lockerDir, lconfig.me, "state.json"), JSON.stringify(curMe, null, 4)
else
console.error "failed to run global migration!"
process.exit 1
serviceMap.migrations.push lconfig.lockerBase + "/Me/" + metaData.id + "/" + ret if typeof ret == "string"
catch E
console.log "error running global migration : " + migrations[i] + " ---- " + E
i++
if serviceManager.serviceMap().migrations.length > 0
syncManager.synclets().executable = false
syncManager.findInstalled()
async.forEachSeries serviceManager.serviceMap().migrations, ((call, cb) ->
console.log "running migration followup for " + call
request.get uri: call, (err, res, body) ->
if err or not res or res.statusCode != 200
console.error "failed to run migration, should be bailing hard! " + util.inspect(err) + ":" + util.inspect(res) + " trying to hit " + call
else
console.log "migration success: " + JSON.stringify(body)
cb()
), ->
serviceManager.serviceMap().migrations = []
postStartup()
else
syncManager.findInstalled()
postStartup()
postStartup = ->
thservice.start()
lscheduler.masterScheduler.loadAndStart()
console.log "locker is running, use your browser and visit " + lconfig.lockerBase
exports.alive = true
shutdown = (returnCode) ->
process.stdout.write "\n"
shuttingDown_ = true
serviceManager.shutdown ->
mongoProcess.kill()
console.log "Shutdown complete."
process.exit returnCode
conf = {}
conf._exit = false
exports.alive = false
npm = require("npm")
require.paths.push __dirname + "/Common/node"
spawn = require("child_process").spawn
fs = require("fs")
path = require("path")
request = require("request")
async = require("async")
util = require("util")
lconfig = require("lconfig")
lconfig.load (if process.argv[2] == "--config" then process.argv[3] else "Config/config.json")
console.log process.pid
fs.writeFileSync __dirname + "/Logs/locker.pid", "" + process.pid
logger = require("logger")
lconsole = require("lconsole")
lscheduler = require("lscheduler")
syncManager = require("lsyncmanager")
serviceManager = require("lservicemanager")
mongodb = require("mongodb")
webservice = require(__dirname + "/Ops/webservice.js")
lcrypto = require("lcrypto")
thservice = require(__dirname + "/Ops/thservice.js")
lmongo = require("lmongo")
console.warn "if I'm running on a public IP I needs to have password protection," + "which if so inclined can be hacked into lockerd.js and added since" + " it's apparently still not implemented :)\n\n" if lconfig.lockerHost != "localhost" and lconfig.lockerHost != "127.0.0.1"
shuttingDown_ = false
if lconfig.airbrakeKey
airbrake = require("airbrake").createClient(lconfig.airbrakeKey)
airbrake.handleExceptions()
path.exists lconfig.me + "/" + lconfig.mongo.dataDir, (exists) ->
unless exists
try
fs.mkdirSync lconfig.me, 0755
catch err
console.error "err", err if err.code != "EEXIST"
fs.mkdirSync lconfig.me + "/" + lconfig.mongo.dataDir, 0755
mongoProcess = spawn("mongod", [ "--dbpath", lconfig.lockerDir + "/" + lconfig.me + "/" + lconfig.mongo.dataDir, "--port", lconfig.mongo.port ])
mongoProcess.stderr.on_ "data", (data) ->
console.error "mongod err: " + data
mongoOutput = ""
mongodExit = (errorCode) ->
return if shuttingDown_
if errorCode != 0
db = new mongodb.Db("locker", new mongodb.Server(lconfig.mongo.host, lconfig.mongo.port, {}), {})
db.open (error, client) ->
if error
console.error "mongod did not start successfully and was not already running (" + errorCode + "), here was the stdout: " + mongoOutput
shutdown 1
else
console.error "found a previously running mongodb running on port " + lconfig.mongo.port + " so we will use that"
db.close()
checkKeys()
mongoProcess.on_ "exit", mongodExit
callback = (data) ->
mongoOutput += data
if mongoOutput.match(RegExp(" waiting for connections on port", "g"))
mongoProcess.stdout.removeListener "data", callback
lmongo.connect checkKeys
mongoProcess.stdout.on_ "data", callback
process.on_ "SIGINT", ->
shutdown 0
process.on_ "SIGTERM", ->
shutdown 0
exports.shutdown = shutdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment