Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from TryGhost/systemd
Add systemd process manager
- Loading branch information
Showing
7 changed files
with
129 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[Unit] | ||
Description=Ghost systemd service for blog: <%= name %> | ||
# Documentation=add documentation link here | ||
|
||
[Service] | ||
Type=notify | ||
NotifyAccess=all | ||
WorkingDirectory=<%= dir %> | ||
User=<%= user %> | ||
Environment="NODE_ENV=<%= environment %>" | ||
ExecStart=<%= ghost_exec_path %> run | ||
Restart=on-failure | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var BaseProcess = require('../index'), | ||
execSync = require('child_process').execSync; | ||
|
||
module.exports = BaseProcess.extend({ | ||
name: 'systemd', | ||
|
||
init: function init(config) { | ||
this._super(config); | ||
this.service = 'ghost_' + config.get('pname'); | ||
}, | ||
|
||
setup: function setup() { | ||
var Promise = require('bluebird'), | ||
template = require('lodash/template'), | ||
path = require('path'), | ||
fs = require('fs'), | ||
service = template(fs.readFileSync(path.join(__dirname, 'ghost.service.template'), 'utf8')), | ||
serviceFileName = this.service + '.service'; | ||
|
||
fs.writeFileSync(path.join(process.cwd(), serviceFileName), service({ | ||
name: this.config.get('pname'), | ||
dir: process.cwd(), | ||
user: process.getuid(), | ||
// TODO: once setup supports passing in node_env, replace | ||
// this line with the environment. Until now, it's safe to | ||
// hardcode 'production' in. | ||
environment: 'production', | ||
ghost_exec_path: process.argv.slice(0,2).join(' ') | ||
}), 'utf8'); | ||
|
||
try { | ||
// Because of the loading spinner, we must run this using execSync in the case | ||
// that the sudo command prompts for a password. Running a promisified version of exec | ||
// does not work in this case. | ||
execSync('sudo mv ' + serviceFileName + ' /lib/systemd/system', { | ||
cwd: process.cwd(), | ||
stdio: ['inherit', 'inherit', 'inherit'] | ||
}); | ||
} catch (e) { | ||
return Promise.reject('Ghost service file could not be put in place, ensure you have proper sudo permissions and systemd is installed.'); | ||
} | ||
}, | ||
|
||
start: function start() { | ||
execSync('systemctl start ' + this.service, { | ||
stdio: ['inherit', 'inherit', 'inherit'] | ||
}); | ||
}, | ||
|
||
stop: function stop() { | ||
execSync('systemctl stop ' + this.service, { | ||
stdio: ['inherit', 'inherit', 'inherit'] | ||
}); | ||
}, | ||
|
||
success: function success() { | ||
// This seems to work better than execSync, probably because it's | ||
// asynchronous and thus doesn't interfere with the event loop | ||
require('child_process').exec('systemd-notify --ready'); | ||
} | ||
}); |