Skip to content

Commit

Permalink
Fix for Env Variables in Ubuntu
Browse files Browse the repository at this point in the history
  • Loading branch information
arvind-agarwal committed Jan 29, 2015
1 parent 1170290 commit a44cc5d
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 6 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (C) 2014 Zapty Inc, Arvind Agarwal & the Contributors
Copyright (C) 2014 Zapty Inc, Arvind Agarwal

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ This command will stop service if running, clean up all provisioned files and se
$ sudo forever list
```

Run Non nodejs Scripts as Service
Run non nodejs scripts as service
---------------------------------

forever allows to use -c command line parameter to point to alternate command line for execution, using that one can easily launch non-node apps also as service
Expand Down
4 changes: 4 additions & 0 deletions bin/forever-service
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ platforms.get(function(err, platform){


if(options.envVars) ctx.envVars = options.envVars;
if(ctx.envVars){
//Split at space, but ignore space inside quotes..
ctx.envVarsArray = ctx.envVars.match(/(?:[^\s"']+|["'][^"']*["'])+/g);
}
if(options.scriptOptions) ctx.scriptOptions = options.scriptOptions;
if(options.minUptime) ctx.minUptime = options.minUptime;
if(options.spinSleepTime) ctx.spinSleepTime = options.spinSleepTime;
Expand Down
6 changes: 6 additions & 0 deletions lib/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ exports.delete = function(ctx, callback){
ctx.installer.delete(ctx, scripts, callback);
});
}


exports.splitEnvVariables=function(envVars){
//Split at space, but ignore space inside quotes..
return envVars.match(/(?:[^\s"']+|["'][^"']*["'])+/g);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "forever-service",
"version": "0.4.3",
"version": "0.4.4",
"preferGlobal": "true",
"description": "Provision node script as a service via forever, allowing it to automatically start on boot, working across various Linux distros and OS",
"main": "lib/api.js",
Expand Down
4 changes: 2 additions & 2 deletions templates/upstart/upstart.template
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ env KILLWAITTIME={{forceKillWaitTime|default('5000')}}

chdir {{cwd}}

{%- if envVars|default(false) %}
{%- for v in envVars.split(" ") %}
{%- if envVarsArray|default(false) %}
{%- for v in envVarsArray %}
env {{ v }}
{%- endfor %}
{%- endif %}
Expand Down
45 changes: 45 additions & 0 deletions test/scriptBuilderTest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var should = require('should');
var scriptBuilder = require('../lib/scriptBuilder');
var installer = require('../lib/installer');
var fs = require('fs-extra');

describe('Generate sysvinit script', function(){
Expand Down Expand Up @@ -51,3 +52,47 @@ describe('Generate sysvinit script', function(){

});
});

describe("Check Environment Variable splitting", function(){

it("Should work with single env variable", function(){
var e = installer.splitEnvVariables('HOME=/xyz/');
e.should.be.an.Array;
e.should.have.length(1);
e.should.containEql('HOME=/xyz/');
});


it("Should split env variables by space", function(){
var e = installer.splitEnvVariables('z=10 x=test HOME=/xyz/');
e.should.be.an.Array;
e.should.have.length(3);
e.should.containEql('z=10');
e.should.containEql('x=test');
e.should.containEql('HOME=/xyz/');
});


it("Should split env variables by space but avoid space splitting inside quote", function(){
var e = installer.splitEnvVariables('z=10 x="test testing" HOME=/xyz/');
e.should.be.an.Array;
e.should.have.length(3);
e.should.containEql('z=10');
e.should.containEql('x="test testing"');
e.should.containEql('HOME=/xyz/');
});


it("Should split env variables by space but avoid space splitting inside single quote", function(){
var e = installer.splitEnvVariables("z=10 x='test testing' HOME=/xyz/");
e.should.be.an.Array;
e.should.have.length(3);
e.should.containEql('z=10');
e.should.containEql("x='test testing'");
e.should.containEql('HOME=/xyz/');
});


});


4 changes: 3 additions & 1 deletion test/server/normalserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ process.once('SIGTERM', function(){

server.listen(8088);
console.log("Server is listening");

if(process.env.TEST){
console.log("Env variable TEST "+process.env.TEST);
}

0 comments on commit a44cc5d

Please sign in to comment.