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

Added username option to systemd #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This module is installed using [node package manager (npm)][npm]:
# during installation on Windows platforms using node-gyp. A
# suitable build chain must be configured on Windows platforms
# before installation.

npm install os-service

It is loaded using the `require()` function:
Expand All @@ -20,9 +20,9 @@ It is loaded using the `require()` function:
A program can then be added, removed and run as a service:

service.add ("my-service");

service.remove ("my-service");

service.run (function () {
// Stop request received (i.e. a kill signal on Linux or from the
// Service Control Manager on Windows), so let's stop!
Expand All @@ -46,20 +46,20 @@ with a `--add` parameter, and removes the created service when called with a
`--remove` parameter:

if (process.argv[2] == "--add") {
service.add ("my-service", {programArgs: ["--run"]}, function(error){
service.add ("my-service", {programArgs: ["--run"]}, function(error){
if (error)
console.trace(error);
});
} else if (process.argv[2] == "--remove") {
service.remove ("my-service", function(error){
service.remove ("my-service", function(error){
if (error)
console.trace(error);
});
} else if (process.argv[2] == "--run") {
service.run (function () {
service.stop (0);
});

// Run service program code...
} else {
// Show usage...
Expand All @@ -85,24 +85,24 @@ The following example adds or removes number of services:

if (program.argv[2] == "--add") {
service.add ("service1", {programPath: "c:\example\svc1.js",
function(error) {
function(error) {
if (error) {
console.trace(error);
} else {
service.add ("service2", {programPath: "c:\example\svc2.js",
function(error) {
function(error) {
if (error) {
console.trace(error);
}
});
}
});
} else {
service.remove ("service2", function(error) {
service.remove ("service2", function(error) {
if (error) {
console.trace(error);
} else {
service.remove ("service1", function(error) {
service.remove ("service1", function(error) {
if (error) {
console.trace(error);
}
Expand All @@ -127,7 +127,7 @@ following code:
service.run (function () {
service.stop (0);
});

// Run service program code...

# Running Service Programs
Expand Down Expand Up @@ -203,7 +203,7 @@ The `name` parameter specifies the name of the created service. The optional
the service will be run using these credentials when started, see the
`CreateService()` functions [win32 API documentation][createservice] for
details on the format of the username, on all other platforms this parameter
is ignored
is ignored. This is also used on Linux with systemd
* `password` - See the `username` parameter
* `systemdWantedBy` - For when systemd will be used a target can be specified
for the `WantedBy` attribute under the `[Install]` section in the generated
Expand Down Expand Up @@ -232,7 +232,7 @@ the program:
username: ".\Stephen Vickers",
password: "MyPassword :)"
};

service.add ("my-service", options, function(error) {
if (error)
console.trace(error);
Expand Down Expand Up @@ -280,7 +280,7 @@ The program should perform cleanup tasks and then call the `service.stop()`
function.

The following example starts a program as a service:

service.run (function () {
service.stop ();
});
Expand Down Expand Up @@ -317,7 +317,7 @@ Example programs are included under the modules `example` directory.
## Version 1.0.1 - 03/03/2015

* Support Linux platforms which don't have the start-stop-daemon program

## Version 1.0.2 - 30/03/2015

* Linux start/stop link under `/etc/rcN.d` directories are not removed
Expand Down
34 changes: 18 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ var linuxSystemUnit = [
'StandardOutput=null',
'StandardError=null',
'UMask=0007',
'User=##USERNAME##',
'ExecStart=##NODE_PATH## ##NODE_ARGS## ##PROGRAM_PATH## ##PROGRAM_ARGS##',
'',
'[Install]',
Expand Down Expand Up @@ -200,14 +201,14 @@ function add (name, options, cb) {
? options.programPath
: process.argv[1];

var username = options ? (options.username || null) : null;
var username = (options ? (options.username || null) : null) || process.env.SUDO_USER || os.userInfo().username;
var password = options ? (options.password || null) : null;

if (os.platform() == "win32") {
var displayName = (options && options.displayName)
? options.displayName
: name;

var serviceArgs = [];

serviceArgs.push (nodePath);
Expand All @@ -217,14 +218,14 @@ function add (name, options, cb) {
serviceArgs.push (options.nodeArgs[i]);

serviceArgs.push (programPath);

if (options && options.programArgs)
for (var i = 0; i < options.programArgs.length; i++)
serviceArgs.push (options.programArgs[i]);

for (var i = 0; i < serviceArgs.length; i++)
serviceArgs[i] = "\"" + serviceArgs[i] + "\"";

var servicePath = serviceArgs.join (" ");

deps = options.dependencies
Expand All @@ -248,7 +249,7 @@ function add (name, options, cb) {
if (options && options.programArgs)
for (var i = 0; i < options.programArgs.length; i++)
programArgs.push ("\"" + options.programArgs[i] + "\"");

var runLevels = [2, 3, 4, 5];
if (options && options.runLevels)
runLevels = options.runLevels;
Expand All @@ -265,15 +266,15 @@ function add (name, options, cb) {
var ctlOptions = {
mode: 493 // rwxr-xr-x
};

fs.stat("/usr/lib/systemd/system", function(error, stats) {
if (error) {
if (error.code == "ENOENT") {
var startStopScript = [];

for (var i = 0; i < linuxStartStopScript.length; i++) {
var line = linuxStartStopScript[i];

line = line.replace("##NAME##", name);
line = line.replace("##NODE_PATH##", nodePath);
line = line.replace("##NODE_ARGS##", nodeArgsStr);
Expand All @@ -282,10 +283,10 @@ function add (name, options, cb) {
line = line.replace("##RUN_LEVELS_ARR##", runLevels.join(" "));
line = line.replace("##RUN_LEVELS_STR##", runLevels.join(""));
line = line.replace("##DEPENDENCIES##", deps);

startStopScript.push(line);
}

var startStopScriptStr = startStopScript.join("\n");

fs.writeFile(initPath, startStopScriptStr, ctlOptions, function(error) {
Expand Down Expand Up @@ -323,18 +324,19 @@ function add (name, options, cb) {

for (var i = 0; i < linuxSystemUnit.length; i++) {
var line = linuxSystemUnit[i];

line = line.replace("##NAME##", name);
line = line.replace("##NODE_PATH##", nodePath);
line = line.replace("##NODE_ARGS##", nodeArgsStr);
line = line.replace("##PROGRAM_PATH##", programPath);
line = line.replace("##PROGRAM_ARGS##", programArgsStr);
line = line.replace("##SYSTEMD_WANTED_BY##", systemdWantedBy);
line = line.replace("##DEPENDENCIES##", deps);

line = line.replace("##USERNAME##", username);

systemUnit.push(line);
}

var systemUnitStr = systemUnit.join("\n");

fs.writeFile(systemPath, systemUnitStr, ctlOptions, function(error) {
Expand All @@ -353,7 +355,7 @@ function add (name, options, cb) {
}
})
}

return this;
}

Expand Down Expand Up @@ -447,10 +449,10 @@ function run (stopCallback) {
stopCallback ();
});
}

runInitialised = true;
}

if (os.platform() == "win32") {
getServiceWrap ().run ();
}
Expand Down