Skip to content

Commit 26dc9fb

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 26dc9fb

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

extensions/linux/index.js

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