Skip to content

Commit 210314f

Browse files
committed
feat(linux): add linux user extension
closes TryGhost#189 - add linux extension that creates a `ghost` user on the system, used by systemd (and potentially any other process manager)
1 parent 860cf18 commit 210314f

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

extensions/linux/index.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
const os = require('os');
4+
const execa = require('execa');
5+
const Promise = require('bluebird');
6+
7+
const cli = require('../../lib');
8+
9+
class LinuxExtension extends cli.Extension {
10+
setup(cmd, argv) {
11+
if (argv.local) {
12+
return;
13+
}
14+
15+
cmd.addStage('linux-user', this.addGhostUser.bind(this), null, 'a ghost system user');
16+
}
17+
18+
addGhostUser(argv, ctx, task) {
19+
if (os.platform() !== 'linux') {
20+
this.ui.log('Platform is not linux', 'yellow');
21+
return task.skip();
22+
}
23+
24+
try {
25+
execa.shellSync('id ghost');
26+
this.ui.log('Ghost user already exists', 'cyan');
27+
return task.skip();
28+
} catch (e) {
29+
if (!e.message.match(/no such user/)) {
30+
return Promise.reject(e);
31+
}
32+
}
33+
34+
return this.ui.listr([{
35+
title: 'Creating ghost system user',
36+
task: () => this.ui.sudo('useradd --system --user-group ghost')
37+
}, {
38+
title: 'Changing directory permissions',
39+
task: () => this.ui.sudo(`chown -R ghost:ghost ${ctx.instance.dir}`)
40+
}, {
41+
title: 'Adding current user to ghost group',
42+
task: (ctx) => {
43+
return execa.shell('id -un').then((result) => {
44+
ctx.currentuser = result.stdout;
45+
return this.ui.sudo(`gpasswd --add ${ctx.currentuser} ghost`);
46+
});
47+
}
48+
}], false);
49+
}
50+
}
51+
52+
module.exports = LinuxExtension;

extensions/linux/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "ghost-cli-linux",
33
"description": "general linux user configuration for Ghost",
44
"version": "0.0.0",
5+
"main": "index.js",
56
"keywords":[
67
"ghost-cli-extension"
78
]

extensions/systemd/index.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const fs = require('fs-extra');
44
const path = require('path');
5+
const execa = require('execa');
56
const template = require('lodash/template');
67

78
const cli = require('../../lib');
@@ -11,18 +12,31 @@ class SystemdExtension extends cli.Extension {
1112
let instance = this.system.getInstance();
1213

1314
if (!argv.local && instance.config.get('process') === 'systemd') {
14-
cmd.addStage('systemd', this._setup.bind(this));
15+
cmd.addStage('systemd', this._setup.bind(this), 'linux-user');
1516
}
1617
}
1718

18-
_setup(argv, ctx) {
19+
_setup(argv, ctx, task) {
20+
let uid;
21+
22+
try {
23+
uid = execa.shellSync('id -u ghost').stdout;
24+
} catch (e) {
25+
if (!e.message.match(/no such user/)) {
26+
return Promise.reject(e);
27+
}
28+
29+
this.ui.log('Ghost user has not been set up, please run `ghost setup linux-user` first', 'yellow');
30+
return task.skip();
31+
}
32+
1933
let service = template(fs.readFileSync(path.join(__dirname, 'ghost.service.template'), 'utf8'));
2034
let serviceFilename = `ghost_${ctx.instance.name}.service`;
2135

2236
return ctx.instance.template(service({
2337
name: ctx.instance.name,
2438
dir: process.cwd(),
25-
user: process.getuid(),
39+
user: uid,
2640
environment: this.system.environment,
2741
ghost_exec_path: process.argv.slice(0,2).join(' ')
2842
}), 'systemd service file', serviceFilename, '/lib/systemd/system').then(() => {

0 commit comments

Comments
 (0)