Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(linux): add linux user extension #271

Merged
merged 1 commit into from
Jul 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions extensions/linux/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

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

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') {
this.ui.log('Platform is not linux', 'yellow');
return task.skip();
}

try {
execa.shellSync('id ghost');
this.ui.log('Ghost user already exists', 'cyan');
return task.skip();
} 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
20 changes: 17 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,31 @@ 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);
}

this.ui.log('Ghost user has not been set up, please run `ghost setup linux-user` first', 'yellow');
return task.skip();
}

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