Skip to content

Commit

Permalink
fix(utils): differentiate root command error messages for DO
Browse files Browse the repository at this point in the history
no issue

Fixes an issue for DigitalOcean One-Click installs, that already moved their installation (or are using the updated image) and in both cases have the ghost folder not owned by `root`, where they would be shown the message to follow the 'fix root install' guide, rather than the normal message to not use `root` and switch to the already existing user.
  • Loading branch information
aileen authored and acburdine committed Feb 21, 2018
1 parent 7066580 commit a69ccc6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
12 changes: 11 additions & 1 deletion lib/utils/check-root-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,19 @@ function checkRootUser(command) {

if (isOneClickInstall) {
// We have a Digitalocean one click installation
console.error(`${chalk.yellow('We discovered that you are using the Digitalocean One-Click install.')}
if (!isRootInstall()) {
// CASE: the user uses either the new DO image, where installations are following our setup guid (aka not-root),
// or the user followed the fix root user guide already, but the user uses root to run the command
console.error(`${chalk.yellow('Can\'t run command as \'root\' user.')}
Please use the user you set up in the installation process, or create a new user with regular account privileges and use this user to run 'ghost ${command}'.
See ${chalk.underline.green('https://docs.ghost.org/docs/install#section-create-a-new-user')} for more information\n`);
} else {
// CASE: the ghost installation folder is owned by root. The user needs to migrate the installation
// to a non-root and follow the instructions.
console.error(`${chalk.yellow('We discovered that you are using the Digitalocean One-Click install.')}
You need to create a user with regular account privileges and migrate your installation to use this user.
Please follow the steps here: ${chalk.underline.green('https://docs.ghost.org/docs/troubleshooting#section-fix-root-user')} to fix your setup.\n`);
}

// TODO: remove this 4 versions after 1.5.0
if (includes(allowedCommands, command)) {
Expand Down
43 changes: 41 additions & 2 deletions test/unit/utils/check-root-user-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ describe('Unit: Utils > checkRootUser', function () {
expect(exitStub.calledOnce).to.be.false;
});

it('shows special message for DigitalOcean One-Click installs', function () {
it('shows special message for DigitalOcean One-Click root installs', function () {
const osStub = sandbox.stub(os, 'platform').returns('linux');
const cwdStub = sandbox.stub(process, 'cwd').returns('/var/www/ghost');
const fsStub = sandbox.stub(fs, 'existsSync');
const fsStatStub = sandbox.stub(fs, 'statSync').returns({uid: 0});
const processStub = sandbox.stub(process, 'getuid').returns(0);
const exitStub = sandbox.stub(process, 'exit').throws();
const errorStub = sandbox.stub(console, 'error');
Expand All @@ -59,7 +61,10 @@ describe('Unit: Utils > checkRootUser', function () {
throw new Error('should not be thrown');
} catch (e) {
expect(e.message).to.not.equal('should not be thrown');
expect(cwdStub.calledOnce).to.be.true;
expect(fsStub.calledWithExactly('/root/.digitalocean_password')).to.be.true;
expect(fsStub.calledWithExactly('/var/www/ghost/.ghost-cli')).to.be.true;
expect(fsStatStub.calledWithExactly('/var/www/ghost/.ghost-cli')).to.be.true;
expect(osStub.calledOnce).to.be.true;
expect(processStub.calledOnce).to.be.true;
expect(errorStub.calledOnce).to.be.true;
Expand All @@ -68,9 +73,40 @@ describe('Unit: Utils > checkRootUser', function () {
}
});

it('shows special message for DigitalOcean One-Click installs, but doesn\'t exit on `stop`', function () {
it('throws error command run with root for non-root installs on a Digitalocean One-Click install', function () {
const osStub = sandbox.stub(os, 'platform').returns('linux');
const cwdStub = sandbox.stub(process, 'cwd').returns('/var/www/ghost');
const fsStub = sandbox.stub(fs, 'existsSync');
const fsStatStub = sandbox.stub(fs, 'statSync').returns({uid: 666});
const processStub = sandbox.stub(process, 'getuid').returns(0);
const exitStub = sandbox.stub(process, 'exit').throws();
const errorStub = sandbox.stub(console, 'error');

fsStub.withArgs('/root/.digitalocean_password').returns(true);
fsStub.withArgs('/var/www/ghost/.ghost-cli').returns(true);

try {
checkRootUser('update');
throw new Error('should not be thrown');
} catch (e) {
expect(e.message).to.not.equal('should not be thrown');
expect(cwdStub.calledOnce).to.be.true;
expect(fsStub.calledWithExactly('/root/.digitalocean_password')).to.be.true;
expect(fsStub.calledWithExactly('/var/www/ghost/.ghost-cli')).to.be.true;
expect(fsStatStub.calledWithExactly('/var/www/ghost/.ghost-cli')).to.be.true;
expect(osStub.calledOnce).to.be.true;
expect(processStub.calledOnce).to.be.true;
expect(errorStub.calledOnce).to.be.true;
expect(exitStub.calledOnce).to.be.true;
expect(errorStub.args[0][0]).to.match(/Can't run command as 'root' user/);
}
});

it('shows special message for DigitalOcean One-Click root installs, but doesn\'t exit on `stop`', function () {
const osStub = sandbox.stub(os, 'platform').returns('linux');
const cwdStub = sandbox.stub(process, 'cwd').returns('/var/www/ghost');
const fsStub = sandbox.stub(fs, 'existsSync');
const fsStatStub = sandbox.stub(fs, 'statSync').returns({uid: 0});
const processStub = sandbox.stub(process, 'getuid').returns(0);
const exitStub = sandbox.stub(process, 'exit').throws();
const errorStub = sandbox.stub(console, 'error');
Expand All @@ -79,7 +115,10 @@ describe('Unit: Utils > checkRootUser', function () {
fsStub.withArgs('/var/www/ghost/.ghost-cli').returns(true);

checkRootUser('stop');
expect(cwdStub.calledOnce).to.be.true;
expect(fsStub.calledWithExactly('/root/.digitalocean_password')).to.be.true;
expect(fsStub.calledWithExactly('/var/www/ghost/.ghost-cli')).to.be.true;
expect(fsStatStub.calledWithExactly('/var/www/ghost/.ghost-cli')).to.be.true;
expect(osStub.calledOnce).to.be.true;
expect(processStub.calledOnce).to.be.true;
expect(errorStub.calledOnce).to.be.true;
Expand Down

0 comments on commit a69ccc6

Please sign in to comment.