|
| 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; |
0 commit comments