Skip to content

Commit 5aaea31

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 5aaea31

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

extensions/linux/index.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
const os = require('os');
4+
const chownr = require('chownr');
5+
const execa = require('execa');
6+
const Promise = require('bluebird');
7+
8+
const cli = require('../../lib');
9+
10+
class LinuxExtension extends cli.Extension {
11+
setup(cmd, argv) {
12+
if (argv.local) {
13+
return;
14+
}
15+
16+
cmd.addStage('linux-user', this.addGhostUser.bind(this), null, 'a ghost system user');
17+
}
18+
19+
addGhostUser(argv, ctx, task) {
20+
if (os.platform() !== 'linux') {
21+
return task.skip('Platform is not linux');
22+
}
23+
24+
try {
25+
execa.shellSync('id ghost');
26+
return task.skip('Ghost user already exists')
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(`usermod --append --groups ghost ${ctx.currentuser}`);
45+
});
46+
}
47+
}, {
48+
title: 'Reloading permissions of current user',
49+
task: (ctx) => {
50+
this.ui.log('Reloading permissions of the current user. You will be prompted for your account password.', 'cyan');
51+
return execa.shell(`exec su -l ${ctx.currentuser}`);
52+
}
53+
}], false);
54+
}
55+
}
56+
57+
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

+16-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,30 @@ 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), null, '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+
return task.skip('Ghost user has not been set up.');
30+
}
31+
1932
let service = template(fs.readFileSync(path.join(__dirname, 'ghost.service.template'), 'utf8'));
2033
let serviceFilename = `ghost_${ctx.instance.name}.service`;
2134

2235
return ctx.instance.template(service({
2336
name: ctx.instance.name,
2437
dir: process.cwd(),
25-
user: process.getuid(),
38+
user: uid,
2639
environment: this.system.environment,
2740
ghost_exec_path: process.argv.slice(0,2).join(' ')
2841
}), 'systemd service file', serviceFilename, '/lib/systemd/system').then(() => {

0 commit comments

Comments
 (0)