Skip to content

Commit

Permalink
Merge dfee787 into 217d113
Browse files Browse the repository at this point in the history
  • Loading branch information
acburdine committed Nov 6, 2019
2 parents 217d113 + dfee787 commit 3bddcf4
Show file tree
Hide file tree
Showing 8 changed files with 468 additions and 653 deletions.
83 changes: 28 additions & 55 deletions lib/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,67 +22,40 @@ class StartCommand extends Command {
return yargs;
}

run(argv) {
const ProcessManager = require('../process-manager');

async run(argv) {
const instance = this.system.getInstance();
const runOptions = {quiet: argv.quiet};

return instance.isRunning().then((isRunning) => {
if (isRunning) {
this.ui.log('Ghost is already running! For more information, run', 'ghost ls', 'green', 'cmd', true);
return Promise.resolve();
const isRunning = await instance.isRunning();
if (isRunning) {
this.ui.log('Ghost is already running! For more information, run', 'ghost ls', 'green', 'cmd', true);
return;
}

instance.checkEnvironment();
await this.runCommand(DoctorCommand, {categories: ['start'], ...argv, quiet: true});
await this.ui.run(() => instance.start(argv.enable), 'Starting Ghost', runOptions);

if (!argv.quiet) {
const isInstall = process.argv[2] === 'install';
let adminUrl = instance.config.get('admin.url') || instance.config.get('url');
// Strip the trailing slash and add the admin path
adminUrl = `${adminUrl.replace(/\/$/,'')}/ghost/`;

// Show a warning about direct mail - but in grey, it's not the most important message here
if (isInstall && instance.config.get('mail.transport') === 'Direct') {
this.ui.log('\nGhost uses direct mail by default. To set up an alternative email method read our docs at https://ghost.org/docs/concepts/config/#mail', 'gray');
}

instance.checkEnvironment();

return this.runCommand(DoctorCommand, Object.assign({
categories: ['start'],
quiet: true
}, argv)).then(() => {
const processInstance = instance.process;

const start = () => Promise.resolve(processInstance.start(process.cwd(), this.system.environment))
.then(() => {
instance.setRunningMode(this.system.environment);
});

return this.ui.run(start, 'Starting Ghost', runOptions).then(() => {
if (!argv.enable || !ProcessManager.supportsEnableBehavior(processInstance)) {
return Promise.resolve();
}

return Promise.resolve(processInstance.isEnabled()).then((isEnabled) => {
if (isEnabled) {
return Promise.resolve();
}
this.ui.log('\n------------------------------------------------------------------------------', 'white');

return this.ui.run(() => processInstance.enable(), 'Enabling Ghost instance startup on server boot', runOptions);
});
}).then(() => {
if (!argv.quiet) {
const isInstall = process.argv[2] === 'install';
let adminUrl = instance.config.get('admin.url') || instance.config.get('url');
// Strip the trailing slash and add the admin path
adminUrl = `${adminUrl.replace(/\/$/,'')}/ghost/`;

// Show a warning about direct mail - but in grey, it's not the most important message here
if (isInstall && instance.config.get('mail.transport') === 'Direct') {
this.ui.log('\nGhost uses direct mail by default. To set up an alternative email method read our docs at https://ghost.org/docs/concepts/config/#mail', 'gray');
}

this.ui.log('\n------------------------------------------------------------------------------', 'white');

if (isInstall) {
// Show a different message after a fresh install
this.ui.log('Ghost was installed successfully! To complete setup of your publication, visit', adminUrl, 'green', 'link', true);
} else {
this.ui.log('Your admin interface is located at', adminUrl, 'green', 'link', true);
}
}
});
});
});
if (isInstall) {
// Show a different message after a fresh install
this.ui.log('Ghost was installed successfully! To complete setup of your publication, visit', adminUrl, 'green', 'link', true);
} else {
this.ui.log('Your admin interface is located at', adminUrl, 'green', 'link', true);
}
}
}
}

Expand Down
64 changes: 17 additions & 47 deletions lib/commands/stop.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
const Command = require('../command');

class StopCommand extends Command {
Expand All @@ -20,9 +19,8 @@ class StopCommand extends Command {
return yargs;
}

run(argv) {
const errors = require('../errors');
const ProcessManager = require('../process-manager');
async run(argv) {
const {SystemError} = require('../errors');
const checkValidInstall = require('../utils/check-valid-install');

const runOptions = {quiet: argv.quiet};
Expand All @@ -31,59 +29,31 @@ class StopCommand extends Command {
return this.stopAll();
}

const instance = this.system.getInstance(argv.name);

if (argv.name) {
if (!instance) {
return Promise.reject(new errors.SystemError(`Ghost instance '${argv.name}' does not exist`));
}

process.chdir(instance.dir);
if (!argv.name) {
checkValidInstall('stop');
}

checkValidInstall('stop');

return instance.isRunning().then((isRunning) => {
if (!isRunning) {
this.ui.log('Ghost is already stopped! For more information, run', 'ghost ls', 'green', 'cmd', true);
return Promise.resolve();
}
const instance = this.system.getInstance(argv.name);

const stop = () => Promise.resolve(instance.process.stop(process.cwd())).then(() => {
instance.setRunningMode(null);
});
if (argv.name && !instance) {
throw new SystemError(`Ghost instance '${argv.name}' does not exist`);
}

return this.ui.run(stop, 'Stopping Ghost', runOptions);
}).then(() => {
if (!argv.disable || !ProcessManager.supportsEnableBehavior(instance.process)) {
return Promise.resolve();
}
const isRunning = await instance.isRunning();
if (!isRunning) {
this.ui.log('Ghost is already stopped! For more information, run', 'ghost ls', 'green', 'cmd', true);
return;
}

return Promise.resolve(instance.process.isEnabled()).then((isEnabled) => {
if (!isEnabled) {
return Promise.resolve();
}

return this.ui.run(
() => instance.process.disable(),
'Disabling Ghost instance startup on server boot',
runOptions
);
});
});
await this.ui.run(() => instance.stop(argv.disable), `Stopping Ghost: ${instance.name}`, runOptions);
}

stopAll() {
async stopAll() {
const Promise = require('bluebird');

const instances = this.system.getAllInstances(true);
const cwd = process.cwd();

return Promise.each(instances, (instance) => {
process.chdir(instance.dir);
return this.ui.run(() => this.run({quiet: true}), `Stopping Ghost: ${instance.name}`);
}).then(() => {
process.chdir(cwd);
await Promise.each(instances, async ({name}) => {
await this.ui.run(() => this.run({quiet: true, name}), `Stopping Ghost: ${name}`);
});
}
}
Expand Down
46 changes: 46 additions & 0 deletions lib/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,52 @@ class Instance {
this.system.setEnvironment(env === 'development', setNodeEnv);
}

/**
* Starts the Ghost instance
*
* @return {Promise<void>}
* @method start
* @public
*/
async start(enable = false) {
await this.process.start(this.dir, this.system.environment);
this.setRunningMode(this.system.environment);

if (!enable) {
return;
}

const isEnabled = await this.process.isEnabled();
if (isEnabled) {
return;
}

await this.process.enable();
}

/**
* Stops the ghost instance
*
* @return {Promise<void>}
* @method stop
* @public
*/
async stop(disable = false) {
await this.process.stop(this.dir);
this.setRunningMode(null);

if (!disable) {
return;
}

const isEnabled = await this.process.isEnabled();
if (!isEnabled) {
return;
}

await this.process.disable();
}

/**
* Gets a summary of this instance, comprised of various values from
* the cliConfig and the ghost config
Expand Down
18 changes: 8 additions & 10 deletions lib/process-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ const requiredMethods = [
'stop',
'isRunning'
];
const enableMethods = [
'isEnabled',
'enable',
'disable'
];

class ProcessManager {
/**
Expand Down Expand Up @@ -87,6 +82,14 @@ class ProcessManager {
}
}

// No-op base methods for enable/disable handling
async isEnabled() {
return false;
}

async enable() {}
async disable() {}

/**
* This function checks if this process manager can be used on this system
*
Expand All @@ -112,10 +115,5 @@ 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;
Loading

0 comments on commit 3bddcf4

Please sign in to comment.