Skip to content

Commit

Permalink
feat(uninstall): add ghost uninstall command
Browse files Browse the repository at this point in the history
closes TryGhost#187
- adds `ghost uninstall` command that will delete the ghost instance and
any related configuration files
  • Loading branch information
acburdine committed May 6, 2017
1 parent e863aea commit b2f279c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ module.exports = {
},

update: {
description: 'update a Ghost instance',
description: 'Update a Ghost instance',

arguments: [
{name: 'version', optional: true}
Expand All @@ -136,5 +136,9 @@ module.exports = {
description: 'Force Ghost to update',
flag: true
}]
},

uninstall: {
description: 'Remove a Ghost instance & any related configuration files'
}
}
42 changes: 42 additions & 0 deletions lib/commands/uninstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';
const fs = require('fs-extra');

const stop = require('./stop');
const Config = require('../utils/config');
const checkValidInstall = require('../utils/check-valid-install');

module.exports.execute = function execute() {
checkValidInstall('uninstall');

this.ui.log('WARNING: Running this command will delete all of your themes, images, data, and any files related to this ghost instance!\n' +
'There is no going back!', 'yellow');

return this.ui.prompt({
type: 'confirm',
name: 'sure',
message: 'Are you sure you want to do this?',
default: false
}).then((answer) => {
if (!answer.sure) {
return Promise.reject(false);
}

let config = Config.load('.ghost-cli');

if (!config.get('running', false)) {
return Promise.resolve();
}

// If the instance is currently running we need to make
// sure it gets stopped
this.environment = config.get('running');
return stop.execute.apply(this);
}).then(() => {
let config = Config.load(fs.existsSync('config.production.json') ? 'production' : 'development');
this.service.setConfig(config);

return this.ui.run(this.service.callHook('uninstall'), 'Removing related configuration');
}).then(() => this.ui.run(() => {
return Promise.all(fs.readdirSync('.').map(file => fs.remove(file)));
}, 'Removing Ghost installation'));
}
2 changes: 1 addition & 1 deletion lib/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class ServiceManager {
}
};

ServiceManager.allowedHooks = ['setup', 'start', 'stop', 'run'];
ServiceManager.allowedHooks = ['setup', 'start', 'stop', 'run', 'uninstall'];
ServiceManager.knownServices = [
// TODO: we only know about the nginx & built in process manager services
// for now, in the future make this load globally installed services
Expand Down
11 changes: 11 additions & 0 deletions lib/services/nginx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const STAGING_URL = 'https://acme-staging.api.letsencrypt.org/directory';
class NginxService extends BaseService {
init() {
this.on('setup', 'setup');
this.on('uninstall', 'uninstall');
this.command('nginx-conf', 'setupConf');
this.command('nginx-ssl', 'setupSSL');
// TODO implement
Expand Down Expand Up @@ -188,6 +189,16 @@ class NginxService extends BaseService {
location._add('proxy_set_header', 'Host $http_host');
location._add('proxy_pass', `http://127.0.0.1:${this.config.get('server.port')}`);
}

uninstall() {
let confFile = `${this.parsedUrl.hostname}.conf`;

if (fs.existsSync(`/etc/nginx/sites-available/${confFile}`)) {
return this.ui.noSpin(() => execa.shell(`sudo rm /etc/nginx/sites-available/${confFile} && sudo rm /etc/nginx/sites-enabled/${confFile}`, {
stdio: 'inherit'
}).catch(() => Promise.reject(new errors.SystemError('Nginx config file could not be removed, you will need to do this manually.'))));
}
}
};

module.exports = {
Expand Down
11 changes: 11 additions & 0 deletions lib/services/process/systemd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class SystemdProcess extends BaseProcess {
init() {
// Register hook with service manager
this.on('setup', 'setup');
this.on('uninstall', 'uninstall');
}

get systemdName() {
Expand All @@ -34,6 +35,16 @@ class SystemdProcess extends BaseProcess {
}));
}

uninstall() {
let serviceFilename = `/lib/systemd/system/${this.systemdName}.service`;

if (fs.existsSync(serviceFilename)) {
return this.ui.noSpin(() => execa.shell(`sudo rm ${serviceFilename}`, {stdio: 'inherit'}).catch(() =>
Promise.reject(new errors.SystemError('Ghost systemd service file could not be removed, you will need to do it manually.'))
));
}
}

start() {
return this.ui.noSpin(() => execa.shell(`sudo systemctl start ${this.systemdName}`, {stdio: 'inherit'}))
.catch((error) => Promise.reject(new errors.ProcessError(error)));
Expand Down

0 comments on commit b2f279c

Please sign in to comment.