Skip to content

Commit

Permalink
feat(linux): add linux user extension
Browse files Browse the repository at this point in the history
closes #189
- add linux extension that creates a `ghost` user on the system, used by systemd (and potentially any other process manager)
  • Loading branch information
acburdine committed Jul 6, 2017
1 parent 860cf18 commit 8496b03
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
50 changes: 50 additions & 0 deletions extensions/linux/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

const os = require('os');
const execa = require('execa');
const Promise = require('bluebird');

const cli = require('../../lib');

class LinuxExtension extends cli.Extension {
setup(cmd, argv) {
if (argv.local) {
return;
}

cmd.addStage('linux-user', this.addGhostUser.bind(this), null, 'a ghost system user');
}

addGhostUser(argv, ctx, task) {
if (os.platform() !== 'linux') {
return task.skip('Platform is not linux');
}

try {
execa.shellSync('id ghost');
return task.skip('Ghost user already exists')
} catch (e) {
if (!e.message.match(/no such user/)) {
return Promise.reject(e);
}
}

return this.ui.listr([{
title: 'Creating ghost system user',
task: () => this.ui.sudo('useradd --system --user-group ghost')
}, {
title: 'Changing directory permissions',
task: () => this.ui.sudo(`chown -R ghost:ghost ${ctx.instance.dir}`)
}, {
title: 'Adding current user to ghost group',
task: (ctx) => {
return execa.shell('id -un').then((result) => {
ctx.currentuser = result.stdout;
return this.ui.sudo(`gpasswd --add ${ctx.currentuser} ghost`);
});
}
}], false);
}
}

module.exports = LinuxExtension;
1 change: 1 addition & 0 deletions extensions/linux/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "ghost-cli-linux",
"description": "general linux user configuration for Ghost",
"version": "0.0.0",
"main": "index.js",
"keywords":[
"ghost-cli-extension"
]
Expand Down
19 changes: 16 additions & 3 deletions extensions/systemd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const fs = require('fs-extra');
const path = require('path');
const execa = require('execa');
const template = require('lodash/template');

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

if (!argv.local && instance.config.get('process') === 'systemd') {
cmd.addStage('systemd', this._setup.bind(this));
cmd.addStage('systemd', this._setup.bind(this), 'linux-user');
}
}

_setup(argv, ctx) {
_setup(argv, ctx, task) {
let uid;

try {
uid = execa.shellSync('id -u ghost').stdout;
} catch (e) {
if (!e.message.match(/no such user/)) {
return Promise.reject(e);
}

return task.skip('Ghost user has not been set up.');
}

let service = template(fs.readFileSync(path.join(__dirname, 'ghost.service.template'), 'utf8'));
let serviceFilename = `ghost_${ctx.instance.name}.service`;

return ctx.instance.template(service({
name: ctx.instance.name,
dir: process.cwd(),
user: process.getuid(),
user: uid,
environment: this.system.environment,
ghost_exec_path: process.argv.slice(0,2).join(' ')
}), 'systemd service file', serviceFilename, '/lib/systemd/system').then(() => {
Expand Down

0 comments on commit 8496b03

Please sign in to comment.