Skip to content
This repository has been archived by the owner on Jul 9, 2019. It is now read-only.

Commit

Permalink
Merge pull request #79 from poupotte/master
Browse files Browse the repository at this point in the history
improve error in status function, ref #78
  • Loading branch information
Frank Rousseau committed Jun 8, 2015
2 parents b7de4c1 + 00258db commit 9c875cd
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 38 deletions.
41 changes: 27 additions & 14 deletions lib/application.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ waitInstallComplete = (slug, callback) ->

msgHomeNotStarted = (app) ->
return """
Install home failed for #{app}. The Cozy Home looks not started.
Install home failed for #{app}. The Cozy Home looks not started.
Install operation cannot be performed.
"""

Expand All @@ -100,9 +100,9 @@ msgRepoGit = (app) ->

msgLongInstall = (app) ->
return """
#{app} installation is still running. You should check for
its status later. If the installation is too long, you should try
to stop it by uninstalling the application and running the
#{app} installation is still running. You should check for
its status later. If the installation is too long, you should try
to stop it by uninstalling the application and running the
installation again.
"""
msgInstallFailed = (app) ->
Expand All @@ -121,16 +121,28 @@ manifest =

# Callback all application stored in database
module.exports.getApps = (callback) ->
homeClient.get "api/applications/", (err, res, apps) ->
homeClient.get "api/applications/", (error, res, apps) ->
if apps? and apps.rows?
callback null, apps.rows
else
callback makeError(err, apps)

# Check if couch is available
helpers.clients['couch'].get '', (err, res, body) ->
if err or not res? or res.statusCode isnt 200
log.error "CouchDB looks not started"
# Check if data-system is available
helpers.clients['ds'].get '', (err, res, body) ->
if not res? or res.statusCode isnt 200
log.error "The Cozy Data System looks not started"
# Check if home is available
helpers.clients['home'].get '', (err, res, body) ->
if not res? or res.statusCode isnt 200
log.error "The Cozy Home looks not started"
# Other pbs: credentials, view, ...
callback makeError(error, apps)

# Install application <app>
install = module.exports.install = (app, options, callback) ->
recoverManifest = (callback) =>
recoverManifest = (callback) ->
# Create manifest
manifest.name = app
if options.displayName?
Expand Down Expand Up @@ -158,7 +170,7 @@ install = module.exports.install = (app, options, callback) ->
if err or body.error
if err?.code is 'ECONNREFUSED'
err = makeError msgHomeNotStarted(app), null
else if body and body.message and body.message.indexOf('Not Found') isnt -1
else if body?.message? and body.message.indexOf('Not Found') isnt -1
err = makeError msgRepoGit(app), null
else
err = makeError err, body
Expand Down Expand Up @@ -343,10 +355,11 @@ module.exports.getVersion = (app, callback) ->
module.exports.check = (app, url, callback=null) ->
statusClient = request.newClient url
statusClient.get "", (err, res) ->
if (res? and not res.statusCode in [200,403]) or (err? and
err.code is 'ECONNREFUSED')
log.raw "#{app}: " + "down".red
callback 'down' if callback?
badStatusCode = res? and not res.statusCode in [200,403]
econnRefused = err? and err.code is 'ECONNREFUSED'
if badStatusCode or econnRefused
log.raw "#{app}: " + "down".red
callback 'down' if callback?
else
log.raw "#{app}: " + "up".green
callback 'up' if callback?
Expand Down Expand Up @@ -507,7 +520,7 @@ stopStandalone = module.exports.stopStandalone = (callback, manifest=null) ->
log.info "Remove from database ..."
dsClient.setBasicAuth 'home', token
requestPath = "request/application/all/"
dsClient.post requestPath, {}, (err, response, apps) =>
dsClient.post requestPath, {}, (err, response, apps) ->
if err
return callback makeError("Data-system doesn't respond", null)
removeApp apps, manifest.name, () ->
Expand Down
14 changes: 7 additions & 7 deletions lib/database.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ configureCouchClient = (callback) ->
# Wait end of compaction
waitCompactComplete = (client, found, callback) ->
setTimeout ->
client.get '_active_tasks', (err, res, body) =>
client.get '_active_tasks', (err, res, body) ->
exist = false
for task in body
if task.type is "database_compaction"
Expand Down Expand Up @@ -92,15 +92,15 @@ module.exports.compact = (database, callback)->
if err or not body.ok
callback makeError(err, body)
else
waitCompactComplete couchClient, false, (success) =>
waitCompactComplete couchClient, false, (success) ->

# Comapct view <view> in database <database>
compactViews = module.exports.compactViews = (view, database, callback) ->
[username, password] = getAuthCouchdb()
couchClient.setBasicAuth username, password
path = "#{database}/_compact/#{view}"
couchClient.headers['content-type'] = 'application/json'
couchClient.post path, {}, (err, res, body) =>
couchClient.post path, {}, (err, res, body) ->
if err or not body.ok
callback makeError(err, body)
else
Expand All @@ -112,7 +112,7 @@ module.exports.compactAllViews = (database, callback) ->
path = "#{database}/_all_docs?startkey=\"_design/\"&endkey=" +
"\"_design0\"&include_docs=true"

couchClient.get path, (err, res, body) =>
couchClient.get path, (err, res, body) ->
if err or not body.rows
callback makeError(err, body)
else
Expand Down Expand Up @@ -145,7 +145,7 @@ module.exports.backup = (data, callback) ->
callback()

# Reverse backup
module.exports.reverseBackup = (backup, usernameBackup, passwordBackup, callback) ->
module.exports.reverseBackup = (backup, usernameBackup, passwordBackup, cb) ->
[username, password] = getAuthCouchdb()
prepareCozyDatabase username, password, ->
toBase64 = (str) ->
Expand Down Expand Up @@ -176,6 +176,6 @@ module.exports.reverseBackup = (backup, usernameBackup, passwordBackup, callbac
couchClient.headers['content-type'] = 'application/json'
couchClient.post "_replicate", data, (err, res, body) ->
if err or not body.ok
callback makeError(err, body)
cb makeError(err, body)
else
callback()
cb()
2 changes: 1 addition & 1 deletion lib/helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ getToken = module.exports.getToken = ->

module.exports.getAuthCouchdb = (exit=true) ->
try
data = fs.readFileSync '/etc/cozy/couchdb.login', 'utf8', (err, data) =>
data = fs.readFileSync '/etc/cozy/couchdb.login', 'utf8', (err, data) ->
username = data.split('\n')[0]
password = data.split('\n')[1]
return [username, password]
Expand Down
12 changes: 7 additions & 5 deletions lib/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,17 @@ program

program
.command("curlcouch <url> [method]")
.description("Send request curl -X <method> http://id:pwd@couchhost:couchport/cozy/<url> to couchdb ")
.description("""Send request curl -X <method>
http://id:pwd@couchhost:couchport/cozy/<url> to couchdb """)
.action (url, method) ->
if not method
method = 'GET'
[username, password] = helpers.getAuthCouchdb false
if username is '' and password is ''
request = "curl -X #{method} http://localhost:5984/cozy/#{url}"
[user, pwd] = helpers.getAuthCouchdb false
request = "curl -X #{method} "
if user is '' and pwd is ''
request += "http://localhost:5984/cozy/#{url}"
else
request = "curl -X #{method} http://#{username}:#{password}@localhost:5984/cozy/#{url}"
request += "http://#{user}:#{pwd}@localhost:5984/cozy/#{url}"
child = exec request, (err, stdout, stderr) ->
console.log stdout

Expand Down
7 changes: 4 additions & 3 deletions lib/monitoring.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ module.exports.status = (callback) ->
funcs = []
application.getApps (err, apps) ->
if err?
callback makeError("Cannot retrieve apps", null)
log.error "Cannot retrieve apps"
callback err
else
for app in apps
if app.state is 'stopped'
Expand All @@ -156,10 +157,10 @@ module.exports.log = (app, type, callback) ->
tail = spawn "tail", ["-f", path]

tail.stdout.setEncoding 'utf8'
tail.stdout.on 'data', (data) =>
tail.stdout.on 'data', (data) ->
log.raw data

tail.on 'close', (code) =>
tail.on 'close', (code) ->
log.info "ps process exited with code #{code}"

else
Expand Down
15 changes: 8 additions & 7 deletions lib/stack_application.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports.install = (app, options, callback) ->
if err or body.error
if err?.code is 'ECONNREFUSED'
err = makeError msgControllerNotStarted(app), null
else if body and body.message and body.message.indexOf('Not Found') isnt -1
else if body?.message? and body.message.indexOf('Not Found') isnt -1
err = makeError msgRepoGit(app), null
else
err = makeError err, body
Expand Down Expand Up @@ -123,15 +123,15 @@ module.exports.updateAll = (callback) ->
callback()

# Callback indexer version
getVersionIndexer = (callback) =>
indexClient.get '', (err, res, body) =>
getVersionIndexer = (callback) ->
indexClient.get '', (err, res, body) ->
if body? and body.split('v')[1]?
callback body.split('v')[1]
else
callback "unknown"

# Callback application version
module.exports.getVersion = (name, callback) =>
module.exports.getVersion = (name, callback) ->
if name is "indexer"
getVersionIndexer callback
else
Expand Down Expand Up @@ -159,9 +159,10 @@ module.exports.getVersion = (name, callback) =>
module.exports.check = (app, path="") ->
(callback) ->
helpers.clients[app].get path, (err, res) ->
if (res? and not res.statusCode in [200,403]) or (err? and
err.code is 'ECONNREFUSED')
log.raw "#{app}: " + "down".red
badStatusCode = res? and not res.statusCode in [200,403]
econnRefused = err? and err.code is 'ECONNREFUSED'
if badStatusCode or econnRefused
log.raw "#{app}: " + "down".red
else
log.raw "#{app}: " + "up".green
callback()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"main": "./lib/main",
"scripts": {
"lint": "coffeelint -f coffeelint.json lib/main.coffee",
"lint": "coffeelint -f coffeelint.json lib/",
"test": "mocha tests --reporter spec --compilers coffee:coffee-script/register --colors",
"build": "coffee -c lib/*.coffee",
"prepublish": "npm run build"
Expand Down

0 comments on commit 9c875cd

Please sign in to comment.