Skip to content

Commit

Permalink
Merge pull request #62 from prey/chmod-new-install
Browse files Browse the repository at this point in the history
1.3.6pre
  • Loading branch information
mauricioschneider committed Jan 13, 2015
2 parents 61857da + f83c503 commit 768f48b
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 46 deletions.
18 changes: 7 additions & 11 deletions lib/agent/plugins/control-panel/sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,26 @@ var make_request = function(what, data, opts, cb) {
// if there was an error, lets try connecting via a proxy if possible
if (err && !opts.proxy && (try_proxy || '').match('.')) {
opts.proxy = try_proxy;
return make_request(what, data, opts, callback);
return make_request(what, data, opts, cb);
}

bus.emit('response', what, err, resp);
cb && cb(err, resp);
});
};

var send = function(what, data, opts, callback) {
var send = function(what, data, opts, cb) {
var opts = opts || {};

if (!send_status_info || what == 'response')
return make_request(what, data, opts, callback);

var done = function(err, resp) {
if (err) {
var str = 'Got error: ' + err.message;
} else {
var str = 'Got status ' + resp.statusCode + ' from server: ' + resp.body.toString();
}

var str = err ? 'Got error: ' + err.message : 'Got ' + resp.statusCode + ' response: ' + resp.body.toString();
logger.info(str);
cb && cb(err, resp);
}

if (!send_status_info || what == 'response')
return make_request(what, data, opts, done);

get_status_info(function(err, status) {
opts.status = status;
make_request(what, data, opts, done);
Expand Down
1 change: 1 addition & 0 deletions lib/agent/triggers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var fs = require('fs'),
join = require('path').join,
actions = require('./actions'),
hooks = require('./hooks'),
logger = require('./common').logger.prefix('triggers');

var watchers = [],
Expand Down
27 changes: 21 additions & 6 deletions lib/conf/tasks/index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ var fs = require('fs'),
async = require('async'),
shared = require('./../shared'),
common = require('./../../common'),
join = require('path').join,
paths = common.system.paths,
os_name = process.platform.replace('win32', 'windows').replace('darwin', 'mac');
os_name = process.platform.replace('win32', 'windows').replace('darwin', 'mac'),
chela = require('chela');

/////////////////////////////////////////////////////////////////
// local requires
Expand Down Expand Up @@ -40,27 +42,40 @@ var set_up_config = function(cb) {
}

var set_up_version = function(version, cb) {

function finish() {
// first, ensure that /current path is readable by non-prey users
// so that impersonated commands within path work as expected.
log('Setting permissions on ' + paths.current);
chela.mod(paths.current, '0755', function(err) {
if (err) return cb(err);

// call post_activation hooks (e.g. firewall toggling in Windows)
log('Running post_activate hooks...');
os_hooks.post_activate(cb);
});
}

set_up_config(function(err) {
if (err) return cb(err);

if (!paths.versions) { // no version support, so cannot set version as current
log('No versions support.');
return os_hooks.post_activate(cb);
return finish();
}

log('Setting up ' + version + ' as current...');
shared.version_manager.set_current(version, function(err){
shared.version_manager.set_current(version, function(err) {
if (err) {
if (err.code == 'ALREADY_CURRENT')
log('Warning: This version is already set as current.');
else
return cb(err);
}

log('Version set. Running post_activate hooks.');
// call post_activation hooks (e.g. firewall toggling in Windows)
os_hooks.post_activate(cb);
finish();
});

})
}

Expand Down
2 changes: 1 addition & 1 deletion lib/conf/tasks/utils/create_user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ create_user() {
useradd -r -M -U -G ${ADMIN_GROUP} -s $SHELL $USER_NAME

for group in $groups; do
adduser $USER_NAME $group 2> /dev/null || true
usermod -a -G $group $USER_NAME 2> /dev/null || true
done

else
Expand Down
2 changes: 1 addition & 1 deletion lib/system/windows/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ exports.get_os_name = function(callback) {

exports.reconnect = function(cb) {
var cmd_path = path.join(__dirname, 'bin', 'autowc.exe');
exec(cmd_path + ' -connect', cb);
exec('"' + cmd_path + '" -connect', cb);
};
14 changes: 7 additions & 7 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"async": "",
"buckle": "0.0.3",
"campfire": "git://github.com/tomas/node-campfire",
"chela": "0.0.3",
"chela": "0.0.5",
"clean-exit": "0.0.3",
"colors": "^0.6.2",
"commander": "",
Expand Down
128 changes: 110 additions & 18 deletions test/lib/conf/tasks.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var fs = require('fs'),
getset = require('getset'),
rimraf = require('rimraf'),
helpers = require('./../../helpers'),
chela = require('chela'),
tmpdir = require('os').tmpdir();

var os_name = process.platform.replace('win32', 'windows').replace('darwin', 'mac');
Expand All @@ -23,7 +24,8 @@ var firewall = require('firewall');
describe('tasks', function() {

var old_config,
old_versions_path;
old_versions_path,
chmod_stub;

before(function() {
process.stdout.writable = false;
Expand All @@ -34,30 +36,40 @@ describe('tasks', function() {
// disable version paths for these tests
old_versions_path = common.system.paths.versions;
common.system.paths.versions = null;
})

stub_chmod();
});

after(function() {
process.stdout.writable = true;

common.config = old_config;
common.system.paths.versions = old_versions_path;

// release the chela.mod stub
chmod_stub.restore();
})

if (os_name == 'windows') {
function stub_chmod() {
// stub out chela.mod so we don't accidentally chmod the source dir
chmod_stub = sinon.stub(chela, 'mod', function(path, octal, cb) { cb() });
}

var firewall_stubs = {};
if (os_name == 'windows') {

before(function() {
firewall_stubs.add = sinon.stub(firewall, 'add_rule', function(obj, cb) { cb() })
firewall_stubs.del = sinon.stub(firewall, 'remove_rule', function(obj, cb) { cb() })
})
var firewall_stubs = {};

after(function() {
firewall_stubs.add.restore()
firewall_stubs.del.restore();
})
before(function() {
firewall_stubs.add = sinon.stub(firewall, 'add_rule', function(obj, cb) { cb() })
firewall_stubs.del = sinon.stub(firewall, 'remove_rule', function(obj, cb) { cb() })
})

after(function() {
firewall_stubs.add.restore()
firewall_stubs.del.restore();
})

}
}

describe('activate', function() {

Expand Down Expand Up @@ -303,11 +315,62 @@ describe('tasks', function() {
})
})

it('calls chela.mod', function(done) {
tasks.activate({}, function(err) {
chmod_stub.called.should.be.true;
done();
});
})

describe('with write access to paths.current', function() {

before(function(done) {
chmod_stub.restore();
fs.chmod(common.system.paths.current, '0755', done);
})

after(stub_chmod);

it('chmods files to 33261 (0755)', function(done) {
tasks.activate({}, function(err) {
should.not.exist(err);
fs.readdir(common.system.paths.current, function(err, list) {
var stat = fs.lstatSync(list[0]);
stat.mode.should.eql(33261);
done();
})
});
})

})

describe('with NO write access to paths.current', function() {

before(function(done) {
chmod_stub.restore();
fs.chmod(common.system.paths.current, '0200', done);
})

after(function(done) {
stub_chmod();
fs.chmod(common.system.paths.current, '0755', done);
})

it('returns a EPERM error', function(done) {
tasks.activate({}, function(err) {
should.exist(err);
err.code.should.eql('EACCES');
done();
});
})

})

})

describe('with versions support', function() {

var dir = tmpdir + '/versions';
var dir = join(tmpdir, 'versions');

before(function() {
common.system.paths.versions = dir;
Expand Down Expand Up @@ -386,10 +449,13 @@ describe('tasks', function() {

describe('and specific version dir is found', function() {

var version_dir = join(dir, '/2.3.4');
var install_dir = join(tmpdir, 'install');
var version_dir, install_dir;

before(function(done) {

version_dir = join(dir, '2.3.4');
install_dir = join(tmpdir, 'install');

fs.mkdir(version_dir, function(err) {
if (err) return done(err);

Expand Down Expand Up @@ -427,13 +493,23 @@ describe('tasks', function() {
})
})

it('does not chmod anything', function(done) {
chmod_stub.reset();
tasks.activate({}, function(err) {
chmod_stub.called.should.be.false;
done();
})
})

})

describe('with write access to install path', function() {

var current_dir = install_dir + '/current';
var current_dir;

before(function(done) {
current_dir = join(install_dir, 'current');

common.system.paths.current = current_dir;
fs.existsSync(current_dir).should.be.false;

Expand All @@ -456,6 +532,22 @@ describe('tasks', function() {

})

it('chmods files to 33261 (0755)', function(done) {

chmod_stub.restore();

tasks.activate({}, function(err) {
should.not.exist(err);
fs.readdir(common.system.paths.current, function(err, list) {
var stat = fs.lstatSync(list[0]);
stat.mode.should.eql(33261);
stub_chmod();
done();
})
});

})

})

})
Expand Down Expand Up @@ -484,7 +576,7 @@ describe('tasks', function() {
// let's assume we have a versions path and that config.sync works
// we're already testing the versions/no versions logic in .activate()
old_versions_path = common.system.paths.versions;
common.system.paths.versions = tmpdir + '/versions';
common.system.paths.versions = join(tmpdir, 'versions');

sync_stub = sinon.stub(common.config, 'sync', function(other_file, method, cb) { cb() } )
})
Expand Down
2 changes: 1 addition & 1 deletion tools/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ cleanup() {
run_specs(){
echo "Ensuring we have the latest packages..."
BUNDLE_ONLY=1 npm install
bin/prey test --recursive --bail --reporter dot
bin/prey test lib/agent/plugins --recursive --bail --reporter dot
}

create_release() {
Expand Down

0 comments on commit 768f48b

Please sign in to comment.