Skip to content

Commit a69ccc6

Browse files
aileenacburdine
authored andcommitted
fix(utils): differentiate root command error messages for DO
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.
1 parent 7066580 commit a69ccc6

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

lib/utils/check-root-user.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,19 @@ function checkRootUser(command) {
2222

2323
if (isOneClickInstall) {
2424
// We have a Digitalocean one click installation
25-
console.error(`${chalk.yellow('We discovered that you are using the Digitalocean One-Click install.')}
25+
if (!isRootInstall()) {
26+
// CASE: the user uses either the new DO image, where installations are following our setup guid (aka not-root),
27+
// or the user followed the fix root user guide already, but the user uses root to run the command
28+
console.error(`${chalk.yellow('Can\'t run command as \'root\' user.')}
29+
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}'.
30+
See ${chalk.underline.green('https://docs.ghost.org/docs/install#section-create-a-new-user')} for more information\n`);
31+
} else {
32+
// CASE: the ghost installation folder is owned by root. The user needs to migrate the installation
33+
// to a non-root and follow the instructions.
34+
console.error(`${chalk.yellow('We discovered that you are using the Digitalocean One-Click install.')}
2635
You need to create a user with regular account privileges and migrate your installation to use this user.
2736
Please follow the steps here: ${chalk.underline.green('https://docs.ghost.org/docs/troubleshooting#section-fix-root-user')} to fix your setup.\n`);
37+
}
2838

2939
// TODO: remove this 4 versions after 1.5.0
3040
if (includes(allowedCommands, command)) {

test/unit/utils/check-root-user-spec.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ describe('Unit: Utils > checkRootUser', function () {
4444
expect(exitStub.calledOnce).to.be.false;
4545
});
4646

47-
it('shows special message for DigitalOcean One-Click installs', function () {
47+
it('shows special message for DigitalOcean One-Click root installs', function () {
4848
const osStub = sandbox.stub(os, 'platform').returns('linux');
49+
const cwdStub = sandbox.stub(process, 'cwd').returns('/var/www/ghost');
4950
const fsStub = sandbox.stub(fs, 'existsSync');
51+
const fsStatStub = sandbox.stub(fs, 'statSync').returns({uid: 0});
5052
const processStub = sandbox.stub(process, 'getuid').returns(0);
5153
const exitStub = sandbox.stub(process, 'exit').throws();
5254
const errorStub = sandbox.stub(console, 'error');
@@ -59,7 +61,10 @@ describe('Unit: Utils > checkRootUser', function () {
5961
throw new Error('should not be thrown');
6062
} catch (e) {
6163
expect(e.message).to.not.equal('should not be thrown');
64+
expect(cwdStub.calledOnce).to.be.true;
6265
expect(fsStub.calledWithExactly('/root/.digitalocean_password')).to.be.true;
66+
expect(fsStub.calledWithExactly('/var/www/ghost/.ghost-cli')).to.be.true;
67+
expect(fsStatStub.calledWithExactly('/var/www/ghost/.ghost-cli')).to.be.true;
6368
expect(osStub.calledOnce).to.be.true;
6469
expect(processStub.calledOnce).to.be.true;
6570
expect(errorStub.calledOnce).to.be.true;
@@ -68,9 +73,40 @@ describe('Unit: Utils > checkRootUser', function () {
6873
}
6974
});
7075

71-
it('shows special message for DigitalOcean One-Click installs, but doesn\'t exit on `stop`', function () {
76+
it('throws error command run with root for non-root installs on a Digitalocean One-Click install', function () {
7277
const osStub = sandbox.stub(os, 'platform').returns('linux');
78+
const cwdStub = sandbox.stub(process, 'cwd').returns('/var/www/ghost');
7379
const fsStub = sandbox.stub(fs, 'existsSync');
80+
const fsStatStub = sandbox.stub(fs, 'statSync').returns({uid: 666});
81+
const processStub = sandbox.stub(process, 'getuid').returns(0);
82+
const exitStub = sandbox.stub(process, 'exit').throws();
83+
const errorStub = sandbox.stub(console, 'error');
84+
85+
fsStub.withArgs('/root/.digitalocean_password').returns(true);
86+
fsStub.withArgs('/var/www/ghost/.ghost-cli').returns(true);
87+
88+
try {
89+
checkRootUser('update');
90+
throw new Error('should not be thrown');
91+
} catch (e) {
92+
expect(e.message).to.not.equal('should not be thrown');
93+
expect(cwdStub.calledOnce).to.be.true;
94+
expect(fsStub.calledWithExactly('/root/.digitalocean_password')).to.be.true;
95+
expect(fsStub.calledWithExactly('/var/www/ghost/.ghost-cli')).to.be.true;
96+
expect(fsStatStub.calledWithExactly('/var/www/ghost/.ghost-cli')).to.be.true;
97+
expect(osStub.calledOnce).to.be.true;
98+
expect(processStub.calledOnce).to.be.true;
99+
expect(errorStub.calledOnce).to.be.true;
100+
expect(exitStub.calledOnce).to.be.true;
101+
expect(errorStub.args[0][0]).to.match(/Can't run command as 'root' user/);
102+
}
103+
});
104+
105+
it('shows special message for DigitalOcean One-Click root installs, but doesn\'t exit on `stop`', function () {
106+
const osStub = sandbox.stub(os, 'platform').returns('linux');
107+
const cwdStub = sandbox.stub(process, 'cwd').returns('/var/www/ghost');
108+
const fsStub = sandbox.stub(fs, 'existsSync');
109+
const fsStatStub = sandbox.stub(fs, 'statSync').returns({uid: 0});
74110
const processStub = sandbox.stub(process, 'getuid').returns(0);
75111
const exitStub = sandbox.stub(process, 'exit').throws();
76112
const errorStub = sandbox.stub(console, 'error');
@@ -79,7 +115,10 @@ describe('Unit: Utils > checkRootUser', function () {
79115
fsStub.withArgs('/var/www/ghost/.ghost-cli').returns(true);
80116

81117
checkRootUser('stop');
118+
expect(cwdStub.calledOnce).to.be.true;
82119
expect(fsStub.calledWithExactly('/root/.digitalocean_password')).to.be.true;
120+
expect(fsStub.calledWithExactly('/var/www/ghost/.ghost-cli')).to.be.true;
121+
expect(fsStatStub.calledWithExactly('/var/www/ghost/.ghost-cli')).to.be.true;
83122
expect(osStub.calledOnce).to.be.true;
84123
expect(processStub.calledOnce).to.be.true;
85124
expect(errorStub.calledOnce).to.be.true;

0 commit comments

Comments
 (0)