Skip to content

Commit

Permalink
feat(process): add enable/disable support
Browse files Browse the repository at this point in the history
closes TryGhost#248
- add isEnabled, enable, disable methods to process manager
- add support for enable/disable to start and stop commands
  • Loading branch information
acburdine committed Jul 3, 2017
1 parent f5f6e05 commit dfeed36
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
23 changes: 23 additions & 0 deletions extensions/systemd/systemd.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ class SystemdProcessManager extends cli.ProcessManager {
.catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
}

isEnabled() {
try {
execa.shellSync(`systemctl is-enabled ${this.systemdName}`);
return true;
} catch (e) {
if (!e.message.match(/disabled/)) {
throw e;
}

return false;
}
}

enable() {
return this.ui.sudo(`systemctl enable ${this.systemdName} --quiet`)
.catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
}

disable() {
return this.ui.sudo(`systemctl disable ${this.systemdName} --quiet`)
.catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
}

isRunning() {
try {
execa.shellSync(`systemctl is-active ${this.systemdName}`);
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class SetupCommand extends Command {
});

yargs = super.configureOptions(commandName, yargs, extensions);
return ConfigCommand.configureOptions('config', yargs, extensions);
yargs = ConfigCommand.configureOptions('config', yargs, extensions);
yargs = StartCommand.configureOptions('start', yargs, extensions);
}

constructor(ui, system) {
Expand Down Expand Up @@ -157,7 +158,6 @@ SetupCommand.options = {
default: true
},
start: {
name: 'start',
description: 'Automatically start Ghost without prompting',
type: 'boolean',
default: false
Expand Down
30 changes: 29 additions & 1 deletion lib/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const omit = require('lodash/omit');

const Command = require('../command');
const startupChecks = require('./doctor/checks/startup');
const ProcessManager = require('../process-manager');

class StartCommand extends Command {
static configureOptions(commandName, yargs, extensions) {
Expand All @@ -29,7 +30,8 @@ class StartCommand extends Command {
instance.checkEnvironment();

return this.ui.listr(startupChecks, {environment: this.system.environment, config: instance.config}).then(() => {
let start = () => Promise.resolve(instance.process.start(process.cwd(), this.system.environment)).then(() => {
let processInstance = instance.process;
let start = () => Promise.resolve(processInstance.start(process.cwd(), this.system.environment)).then(() => {
instance.running = this.system.environment;
});

Expand All @@ -38,12 +40,38 @@ class StartCommand extends Command {
}

return this.ui.run(start, 'Starting Ghost').then(() => {
// If process manager doesn't support enable behavior OR
// it's already enabled, then skip prompt
if (!ProcessManager.supportsEnableBehavior(processInstance) || processInstance.isEnabled()) {
return Promise.resolve({yes: false});
}

// If prompts are disbabled or enable is passed,
// skip prompt
if (argv.enable || !argv.prompt) {
return Promise.resolve({yes: argv.enable});
}

return this.ui.confirm('Do you wish to enable the Ghost instance to start on reboot?')
}).then((answer) => {
if (!answer.yes) {
return Promise.resolve();
}

return this.ui.run(processInstance.enable(), 'Enabling instance startup on server boot');
}).then(() => {
this.ui.log(`You can access your blog at ${instance.config.get('url')}`, 'cyan');
});
});
}
}

StartCommand.description = 'Start an instance of Ghost';
StartCommand.options = {
enable: {
description: 'Enable the instance to restart on server reboot (if the process manager supports it)',
type: 'boolean'
}
}

module.exports = StartCommand;
11 changes: 10 additions & 1 deletion lib/commands/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Promise = require('bluebird');

const errors = require('../errors');
const Command = require('../command');
const ProcessManager = require('../process-manager');

class StopCommand extends Command {
static configureOptions(commandName, yargs, extensions) {
Expand Down Expand Up @@ -51,7 +52,11 @@ class StopCommand extends Command {
return stop();
}

return this.ui.run(stop, 'Stopping Ghost');
return this.ui.run(stop, 'Stopping Ghost').then(() => {
if (argv.disable && ProcessManager.supportsEnableBehavior(instance.process) && instance.process.isEnabled()) {
return this.ui.run(instance.process.disable(), 'Disabling instance startup on server boot');
}
});
}

stopAll() {
Expand All @@ -74,6 +79,10 @@ StopCommand.options = {
alias: 'a',
description: 'option to stop all running Ghost blogs',
type: 'boolean'
},
disable: {
description: 'Disable restarting Ghost on server reboot (if the process manager supports it)',
type: 'boolean'
}
};
StopCommand.global = true;
Expand Down
11 changes: 11 additions & 0 deletions lib/process-manager.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
'use strict';
const every = require('lodash/every');
const requiredMethods = [
'start',
'stop',
'isRunning'
];
const enableMethods = [
'isEnabled',
'enable',
'disable'
];

class ProcessManager {
/**
Expand Down Expand Up @@ -79,5 +85,10 @@ function isValid(SubClass) {
return missing;
}

function supportsEnableBehavior(processInstance) {
return every(enableMethods, (method) => processInstance[method]);
}

module.exports = ProcessManager
module.exports.isValid = isValid;
module.exports.supportsEnableBehavior = supportsEnableBehavior;

0 comments on commit dfeed36

Please sign in to comment.