Skip to content
Open
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
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,15 @@ test/loopback-example-app
coverage
.nyc_output
*.tap
intl/MSG.json
intl/de
intl/es
intl/fr
intl/it
intl/ja
intl/ko
intl/pt
intl/ru
intl/zh-Hans
intl/zh-Hant
intl/en/*.json
24 changes: 8 additions & 16 deletions bin/sl-svc-install.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env node

var g = require('strong-globalize');
var install = require('../');
var minimist = require('minimist');
var path = require('path');

var opts = minimist(process.argv.slice(2));

Expand All @@ -11,30 +13,20 @@ if (!opts.command && opts._.length > 0) {
delete opts._;

if (opts.help || opts.h) {
return usage(process.argv[1], console.log.bind(console));
g.log('sl-service-install.txt', process.argv[1]);
process.exit(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this introduces a bug, please return g.log(...)

}

if (opts.version || opts.v) {
return console.log(require('../package.json').version);
console.log(require('../package.json').version);
process.exit(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above

}

g.setRootDir(path.resolve(__dirname));

install(opts, function(err) {
if (err) {
console.error(err.stack);
process.exit(1);
}
});

function usage($0, p) {
p('usage: %s [options] -- <app and args>', $0);
p('');
p('Options:');
p(' -h,--help Print this message and exit.');
p(' --name NAME Name to use for service (default derived from app)');
p(' --user USER User to run service as.');
p(' --group GROUP Group to run service as.');
p(' --jobFile PATH File to create (default /etc/init/<name>.conf)');
p(' --cwd PATH Directory to run the service from.');
p(' --upstart [VER] Generate Upstart job for VER: 0.6 or 1.4 (default)');
p(' --systemd Generate systemd service');
}
79 changes: 42 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var chownr = require('chownr');
var debug = require('debug')('strong-service-installer');
var fmt = require('util').format;
var fs = require('fs');
var g = require('strong-globalize');
var mkdirp = require('mkdirp');
var passwd = require('./lib/passwd');
var path = require('path');
Expand Down Expand Up @@ -46,7 +47,7 @@ function install(opts, cb) {
async.applyEachSeries(steps, opts, function(err) {
if (err)
return cb(err);
install.log('Service %s installed (%s)', opts.name, opts.jobFile);
install.log(g.t('Service %s installed (%s)', opts.name, opts.jobFile));
cb(null, fmt('%s (%s)', opts.name, opts.jobFile));
});

Expand Down Expand Up @@ -86,29 +87,31 @@ function optionsPrecheck(opts, next) {
var errors = [];

if (!opts.command && !opts.name) {
errors.push('Missing command or name');
errors.push(g.t('Missing command or name'));
}

if (install.platform !== 'linux') {
install.error('%s: only Upstart on Linux is supported',
install.ignorePlatform ? 'Warning' : 'Error');
install.error(g.t('%s: only {{Upstart}} on Linux is supported',
install.ignorePlatform ? g.t('Warning') : g.t('Error')));
if (!install.ignorePlatform)
errors.push('Unsupported platform');
errors.push(g.t('Unsupported platform'));
}

if (!opts.systemd && !opts.upstart) {
opts.upstart = '1.4'; // default
} else if (opts.systemd && opts.upstart) {
install.error(
'Invalid usage (cannot specify both --systemd and --upstart)' +
', see `%s --help`', install.$0);
errors.push('Cannot specify both systemd and Upstart');
install.error(g.t(
'Invalid usage (cannot specify both {{--systemd}} and {{--upstart}})' +
', see `%s --help`', install.$0));
errors.push(g.t('Cannot specify both {{systemd}} and {{Upstart}}'));
}
opts.upstart = String(opts.upstart);
if (!opts.systemd && opts.upstart !== '0.6' && opts.upstart !== '1.4') {
install.error('Invalid usage (only upstart "0.6" and "1.4" supported)' +
', see `%s --help`', install.$0);
errors.push('Invalid upstart target (only 0.6 and 1.4 are supported)');
install.error(
g.t('Invalid usage (only {{upstart}} "0.6" and "1.4" supported)' +
', see `{{%s --help}}`', install.$0));
errors.push(
g.t('Invalid {{upstart}} target (only 0.6 and 1.4 are supported)'));
}

if (opts.systemd) {
Expand Down Expand Up @@ -187,7 +190,7 @@ function normalizeOptions(opts, next) {
} else if (opts.generator === upstartMaker) {
opts.jobFile = '/etc/init/' + opts.name + '.conf';
} else {
return next(new Error('Unknown init type, no path given'));
return next(g.Error('Unknown init type, no path given'));
}
}

Expand All @@ -213,13 +216,14 @@ function checkExistingJob(opts, next) {
fs.exists(opts.jobFile, function(exists) {
if (exists) {
if (opts.force) {
install.log('Warning: overwriting file "%s"', opts.jobFile);
install.log(g.t('Warning: overwriting file "%s"', opts.jobFile));
} else if (opts.dryRun) {
install.log('Warning: install would fail because %j already exists',
opts.jobFile);
install.log(g.t('Warning: install would fail because %j already exists',
opts.jobFile));
} else {
var message = fmt('File "%s" exists. Remove it or re-run with --force',
opts.jobFile);
var message = g.format(
'File "%s" exists. Remove it or re-run with --force',
opts.jobFile);
return next(Error(message));
}
}
Expand All @@ -239,7 +243,7 @@ function ensureJobFileDir(opts, cb) {
var dir = path.dirname(opts.jobFile);
opts.mkdirp(dir, {}, function(err, made) {
if (!err && made)
install.log('created %s...', made);
install.log(g.t('created %s...', made));
cb(err);
});
}
Expand All @@ -249,11 +253,11 @@ function ensureUser(opts, callback) {
if (err || exists)
return callback(err);
if (opts.dryRun) {
install.log('skipping user creation in dry-run');
install.log(g.t('skipping user creation in dry-run'));
return callback();
}
if (install.platform !== 'linux') {
install.log('skipping user creation on non-Linux platform');
install.log(g.t('skipping user creation on non-Linux platform'));
return callback();
}
opts.home = '/var/lib/' + opts.user;
Expand All @@ -276,8 +280,8 @@ function useradd(name, home, groups, callback) {
args.push(name);
child_process.execFile(cmd, args, function(err, stdout, stderr) {
if (err) {
install.error('Error adding user %s:\n%s\n%s',
name, stdout, stderr);
install.error(g.t('Error adding user %s:\n%s\n%s',
name, stdout, stderr));
}
callback(err);
});
Expand All @@ -299,8 +303,8 @@ function fillInGroup(opts, callback) {
}
child_process.execFile(cmd, args, function(err, stdout) {
if (err) {
install.error('Could not determine group for service user \'%s\': %s',
opts.user, err.message);
install.error(g.t('Could not determine group for service user \'%s\': %s',
opts.user, err.message));
} else {
opts.userGroup = stdout.trim();
opts.group = opts.group || opts.userGroup;
Expand All @@ -318,11 +322,11 @@ function ensureGroup(opts, callback) {
return callback(err);
}
if (opts.dryRun) {
install.log('skipping user modification in dry-run');
install.log(g.t('skipping user modification in dry-run'));
return callback();
}
if (install.platform !== 'linux') {
install.log('skipping user modification on non-Linux platform');
install.log(g.t('skipping user modification on non-Linux platform'));
return callback();
}
addUserToGroup(opts.user, opts.group, callback);
Expand All @@ -335,8 +339,9 @@ function userInRequiredGroup(user, group, callback) {
child_process.execFile(cmd, args, function(err, stdout) {
var groups = [];
if (err) {
install.error('Could not determine groups for service user \'%s\': %s',
user, err.message);
install.error(
g.t('Could not determine groups for service user \'%s\': %s',
user, err.message));
} else {
groups = stdout.trim().split(/\s+/);
}
Expand All @@ -353,8 +358,8 @@ function addUserToGroup(user, group, callback) {
];
child_process.execFile(cmd, args, function(err, stdout, stderr) {
if (err) {
install.error('Error adding user %s to group %s:\n%s\n%s',
user, group, stdout, stderr);
install.error(g.t('Error adding user %s to group %s:\n%s\n%s',
user, group, stdout, stderr));
}
callback(err);
});
Expand All @@ -363,8 +368,8 @@ function addUserToGroup(user, group, callback) {
function fillInHome(opts, callback) {
return opts.passwd(opts.user, function(err, user) {
if (err) {
install.error('Could not determine $HOME of \'%s\':',
opts.user, err.message);
install.error(g.t('Could not determine {{$HOME}} of \'%s\':',
opts.user, err.message));
}
if (!err) {
opts.env = opts.env || {};
Expand All @@ -381,13 +386,13 @@ function fillInHome(opts, callback) {

function resolveIds(opts, callback) {
if (opts.dryRun) {
install.log('skipping uid resolution in dry-run');
install.log(g.t('skipping uid resolution in dry-run'));
return setImmediate(callback);
}
uidNumber(opts.user, opts.userGroup, function(err, uid, gid) {
if (err) {
install.error('Error getting numeric uid/gid of %s/%s: %s',
opts.user, opts.userGroup, err.message);
install.error(g.t('Error getting numeric uid/gid of %s/%s: %s',
opts.user, opts.userGroup, err.message));
return callback(err);
}
opts._userId = uid;
Expand All @@ -405,7 +410,7 @@ function ensureDirectories(opts, cb) {
}
opts.mkdirp(dir, {}, function(err, made) {
if (!err && made) {
install.log('created %s...', made);
install.log(g.t('created %s...', made));
uidNumber(opts.user, opts.group, function(err, uid, gid) {
if (err)
return next(err);
Expand Down
11 changes: 11 additions & 0 deletions intl/en/sl-service-install.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
usage: {0} [options] {{-- <app and args>}}

Options:
{{-h,--help}} Print this message and exit.
{{--name NAME}} Name to use for service (default derived from app)
{{--user USER}} User to run service as.
{{--group GROUP}} Group to run service as.
{{--jobFile PATH}} File to create (default {{/etc/init/<name>.conf}})
{{--cwd PATH}} Directory to run the service from.
{{--upstart [VER]}} Generate {{Upstart}} job for VER: 0.6 or 1.4 (default)
{{--systemd}} Generate {{systemd}} service
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
"dependencies": {
"async": "^1.0.0",
"chownr": "0.0.2",
"cldr-data": "^28.0.3",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how come strong-globalize doesn't depend directly on cldr-data, if it needs it?

"debug": "^2.0.0",
"lodash": "^3.10.0",
"minimist": "^1.0.0",
"mkdirp": "^0.5.1",
"shell-quote": "^1.4.2",
"strong-globalize": "^1.0.0",
"strong-service-systemd": "^1.0.0",
"strong-service-upstart": "^1.0.0",
"uid-number": "^0.0.5",
Expand Down