Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.3.10-rc #130

Merged
merged 18 commits into from
May 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/agent/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ actions.stop = function(name) {
} else {
action.stop();
}

}

actions.stop_all = function() {
Expand Down
Binary file modified lib/agent/actions/alarm/lib/alarm.mp3
Binary file not shown.
165 changes: 88 additions & 77 deletions lib/agent/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,19 @@ var common = require('./common'),
logger = common.logger,
pidfile; // null unless we do set one.

var write = function(str) {
function warn(str, code) {
if (process.stdout.writable)
process.stdout.write(str + '\n');
}

if (program.nodeVersion) {
return logger.write(process.version);
} else if (!common.config.present()) {
write('\nLooks like there\'s no config file yet. Glad to see you\'re getting started. :)');
write('To finish setting up Prey, please run `prey config hooks post_install` as root.\n');
return process.exit(1);
if (typeof code != 'undefined')
process.exit(code);
}

/////////////////////////////////////////////////////////////
// event, signal handlers
/////////////////////////////////////////////////////////////

var shutdown = function(code, wait) {
function shutdown(code, wait) {
var wait = wait || 10000;

var die = function(graceful) {
Expand All @@ -79,91 +74,107 @@ var shutdown = function(code, wait) {
}
}

process.on('exit', function(code) {
shutdown(code);
});

// sent by other instance when updating config
// SIGHUP is the default signal sent by Upstart when reloading a job.
process.on('SIGHUP', function() {
logger.warn('Got SIGHUP signal!');
agent.reload();
})

// sent by Upstart
process.on('SIGQUIT', function() {
logger.warn('Got QUIT signal.');
shutdown(0, 10000);
});

// sent by LaunchDaemon
// we cannot exit with code 0 as LaunchDaemon
// will assume the process exited normally.
process.on('SIGTERM', function() {
logger.warn('Got TERM signal.');
shutdown(11, 10000);
});

// sent when developing. :)
// 130 is the 'official' exit code in Bash for SIGINTs
process.on('SIGINT', function() {
if (!agent.running()) {
logger.warn('Ok, ok, whatever you say.');
return process.exit(2);
}
function trap_signals() {

process.on('exit', function(code) {
shutdown(code);
});

// sent by other instance when updating config
// SIGHUP is the default signal sent by Upstart when reloading a job.
process.on('SIGHUP', function() {
logger.warn('Got SIGHUP signal!');
agent.reload();
})

// sent by Upstart
process.on('SIGQUIT', function() {
logger.warn('Got QUIT signal.');
shutdown(0, 10000);
});

// sent by LaunchDaemon
// we cannot exit with code 0 as LaunchDaemon
// will assume the process exited normally.
process.on('SIGTERM', function() {
logger.warn('Got TERM signal.');
shutdown(11, 10000);
});

logger.warn('Got INT signal.');
shutdown(130, 5000);
});
// sent when developing. :)
// 130 is the 'official' exit code in Bash for SIGINTs
process.on('SIGINT', function() {
if (!agent.running()) {
logger.warn('Ok, ok, whatever you say.');
return process.exit(2);
}

logger.warn('Got INT signal.');
shutdown(130, 5000);
});

process.on('uncaughtException', function (err) {
logger.critical('UNCAUGHT EXCEPTION: ' + (err.message || err));
logger.debug(err.stack);
process.on('uncaughtException', function (err) {
logger.critical('UNCAUGHT EXCEPTION: ' + (err.message || err));
logger.debug(err.stack);

if (!common.config.get('send_crash_reports'))
return shutdown(1, 5000);
if (!common.config.get('send_crash_reports'))
return shutdown(1, 5000);

common.exceptions.send(err, function() {
shutdown(1, 5000);
common.exceptions.send(err, function() {
shutdown(1, 5000);
});
});
});

}

////////////////////////////////////////////////////////////
// launcher
/////////////////////////////////////////////////////////////

if (process.argv[2] == 'console')
program.mode = 'console';
(function() {

if (program.allowOther || program.run || program.mode == 'console') {
return agent.run();
}
if (program.nodeVersion) {
return warn(process.version, 0);
} else if (!common.config.present()) {
warn('\nLooks like there\'s no config file yet. Glad to see you\'re getting started. :)');
return warn('To finish setting up Prey, please run `prey config hooks post_install` as root.\n', 1);
}

trap_signals();

// if running in console mode, or using -a or -r, pidfile won't
// be available for them to remove on process.exit().
pidfile = common.pid_file;
if (process.argv[2] == 'console')
program.mode = 'console';

pid.store(pidfile, function(err, running) {
if (err) {
var msg = err.code == 'EPERM' ? 'No write access to pidfile: ' + pidfile : err.message;
write('Cannot continue: ' + msg);
return process.exit(1);
if (program.allowOther || program.run || program.mode == 'console') {
return agent.run();
}

if (!running) return agent.run();
// if running in console mode, or using -a or -r, pidfile won't
// be available for them to remove on process.exit().
pidfile = common.pid_file;

if (process.stdout.writable) {
write('\n The Prey agent is running. Good job!');
pid.store(pidfile, function(err, running) {
if (err) {
var msg = err.code == 'EPERM' ? 'No write access to pidfile: ' + pidfile : err.message;
return warn('Cannot continue: ' + msg, 1);
}

if (!running) return agent.run();

if (process.stdout.writable) {
warn('\n The Prey agent is running. Good job!');

if (running.stat && running.stat.ctime) {
var run_time = (new Date() - running.stat.ctime)/(60 * 1000);
var run_time_str = run_time.toString().substring(0,5);
write(' It has been live for ' + run_time_str + ' minutes, under process ID ' + running.pid + '.');
if (running.stat && running.stat.ctime) {
var run_time = (new Date() - running.stat.ctime)/(60 * 1000);
var run_time_str = run_time.toString().substring(0,5);
warn(' It has been live for ' + run_time_str + ' minutes, under process ID ' + running.pid + '.');
}

warn(' To trigger actions or retrieve information, log in to your Prey account at https://preyproject.com');
warn(' For additional configuration options, please run `prey config`.\n');
}

write(' To trigger actions or retrieve information, log in to your Prey account at https://preyproject.com');
write(' For additional configuration options, please run `prey config`.\n');
}
process.exit(10);
});

process.exit(10);
});
})();
1 change: 0 additions & 1 deletion lib/agent/hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var logger = require('./common').logger.prefix('hooks'),
Emitter = require('events').EventEmitter,
hooks = {},
emitter = new Emitter();

var trigger = function(event) {
Expand Down
4 changes: 2 additions & 2 deletions lib/agent/plugins/control-panel/api/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ exports.authorize = function(opts, cb) {
if (resp.statusCode == 401) {
cb(errors.get('INVALID_CREDENTIALS'))

} else if (body.user && parseInt(body.user.available_slots) <= 0) {
} else if (body && body.user && parseInt(body.user.available_slots) <= 0) {
cb(errors.get('NO_AVAILABLE_SLOTS'));

} else if (body && body.key || (body.user && body.user.key)) {
} else if (body && body.key || (body && body.user && body.user.key)) {
cb(null, set(body.key || body.user.key));

} else {
Expand Down
12 changes: 7 additions & 5 deletions lib/agent/plugins/control-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,20 @@ var wait_for_config = function() {
var attempts = 0;

var timer = setInterval(function() {

logger.info('Reloading config...');
config.reload();

if (config.get('api_key') && config.get('device_key')) {

clearInterval(timer);
// set the new keys in the api before booting
init_api(config.all(), function() {
boot();
})
init_api(config.all(), function() { boot() });

} else if (++attempts > 30) { // five mins total
throw new Error('Not configured. Stopping.');
}

}, 10000); // 10 seconds
}

Expand Down Expand Up @@ -250,7 +252,7 @@ exports.unload = function(cb) {
}

// export API for conf module
exports.load_api = function(opts) {
init_api(opts);
exports.load_api = function(opts, cb) {
init_api(opts, cb);
return api;
};
6 changes: 2 additions & 4 deletions lib/agent/plugins/control-panel/sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ var make_request = function(what, data, opts, cb) {
what = what.replace(/s$/, '');

opts.multipart = what == 'report';

var msg = 'Posting ' + what;
logger.info(msg);
logger.info('Posting ' + what);

api.push[what](data, opts, function(err, resp) {
bus.emit('response', what, err, resp);
Expand All @@ -28,7 +26,7 @@ var send = function(what, data, opts, cb) {
var opts = opts || {};

var done = function(err, resp) {
var str = err ? 'Got error: ' + err.message : 'Got ' + resp.statusCode + ' response: ' + resp.body.toString();
var str = err ? 'Got error: ' + err.message : 'Got ' + resp.statusCode + ' response: ' + resp.body;
logger.info(str);
cb && cb(err, resp);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/agent/plugins/control-panel/test/main_load.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('main.load', function() {
// insert default values to config
config.set('host', 'destination.com');
config.set('protocol', 'https');
config.global = { get: function() {} }

var background = false;

Expand Down Expand Up @@ -391,4 +392,4 @@ describe('main.load', function() {

})

})
})
13 changes: 8 additions & 5 deletions lib/agent/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,34 @@ var get = exports.get = function(name, cb) {
callback = cb || function(){ /* noop */ };

var fire_callbacks = function(name, err, result) {
callbacks[name].forEach(function(fn){
var list = callbacks[name];
callbacks[name] = [];

list.forEach(function(fn){
fn(err, result, name);
});
callbacks[name] = [];
}

if (name == 'report')
name = 'stolen';

map(function(err, getters) {

if (err) return callback(err);

if (getters[name]) {

callbacks[name] = callbacks[name] || [];
callbacks[name].push(callback);

if (callbacks[name].length > 1)
if (callbacks[name].length > 1) {
return logger.info(name + ' already in progress.');
}

logger.debug('Fetching ' + name);
getters[name](function(err, result) {

fire_callbacks(name, err, result);

if (!cb) { // only emit when no callback passed
if (err)
hooks.trigger('error', err);
Expand All @@ -106,7 +110,6 @@ var get = exports.get = function(name, cb) {
files.push(result.file); // keep a record so we remove it afterwards
}

fire_callbacks(name, err, result);
});

} else {
Expand Down
8 changes: 4 additions & 4 deletions lib/agent/transports/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ exports.defaults = function(opts) {
}

exports.get = function(url, opts, cb) {
return exports.request('GET', url, null, opts, cb)
return request('GET', url, null, opts, cb)
}

exports.post = function(url, data, opts, cb) {
return exports.request('POST', url, data, opts, cb)
return request('POST', url, data, opts, cb)
}

exports.put = function(url, data, opts, cb) {
return exports.request('PUT', url, data, opts, cb)
return request('PUT', url, data, opts, cb)
}

exports.del = function(url, data, opts, cb) {
return exports.request('DELETE', url, data, opts, cb)
return request('DELETE', url, data, opts, cb)
}
4 changes: 2 additions & 2 deletions lib/agent/triggers/power/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var emitter,

var check_battery_status = function() {

providers.get('battery_status', function(err, current){
providers.get('battery_status', function(err, current) {
if (err || !emitter) return;

// console.log('Current: ' + current.state);
Expand All @@ -31,7 +31,7 @@ exports.start = function(opts, cb){
if (err) return cb(err);

power.on('state_changed', function(info){
check_battery_status(info);
setTimeout(check_battery_status, 1500);
});

power.on('low_power', function(info){
Expand Down
Loading