Skip to content

Commit eb3f430

Browse files
committed
fix(update): fixed permission issues when linking themes
refs https://forum.ghost.org/t/permission-denied-when-updating-source-theme-linking/41651/5 - if we're in an environment that uses the `ghost` user, `ghost-mgr` doesn't have permissions to do these steps after we've already chown'd the folder - in that case, we should sudo to the `ghost` user to run these commands
1 parent c766560 commit eb3f430

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

lib/commands/update.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const symlinkSync = require('symlink-or-copy').sync;
66
const {GhostError} = require('../errors');
77
const Command = require('../command');
88
const DoctorCommand = require('./doctor');
9+
const ghostUser = require('../utils/use-ghost-user');
910

1011
class UpdateCommand extends Command {
1112
static configureOptions(commandName, yargs, extensions) {
@@ -43,6 +44,7 @@ class UpdateCommand extends Command {
4344
instance,
4445
force,
4546
activeVersion: instance.version,
47+
ui: this.ui,
4648
version,
4749
zip,
4850
v1
@@ -230,7 +232,7 @@ class UpdateCommand extends Command {
230232
instance.nodeVersion = process.versions.node;
231233
}
232234

233-
linkDefaultThemes({instance, rollback}) {
235+
async linkDefaultThemes({instance, rollback, ui}) {
234236
const currentThemesDir = path.join(process.cwd(), 'current', 'content', 'themes');
235237
const contentThemesDir = path.join(instance.config.get('paths.contentPath'), 'themes');
236238
// remove any broken symlinks caused by default themes no longer existing in previous version
@@ -239,7 +241,11 @@ class UpdateCommand extends Command {
239241
const installedThemes = fs.readdirSync(contentThemesDir);
240242
for (const theme of installedThemes) {
241243
if (!fs.existsSync(path.join(contentThemesDir, theme))) {
242-
fs.rmSync(path.join(contentThemesDir, theme));
244+
if (ghostUser.shouldUseGhostUser(contentThemesDir)) {
245+
await ui.sudo(`rm ${path.join(contentThemesDir, theme)}`, {sudoArgs: '-E -u ghost'});
246+
} else {
247+
fs.unlinkSync(path.join(contentThemesDir, theme));
248+
}
243249
}
244250
}
245251
}
@@ -250,10 +256,14 @@ class UpdateCommand extends Command {
250256
const defaultThemes = fs.readdirSync(currentThemesDir);
251257
for (const theme of defaultThemes) {
252258
if (!fs.existsSync(path.join(contentThemesDir, theme))) {
253-
symlinkSync(
254-
path.join(currentThemesDir, theme),
255-
path.join(contentThemesDir, theme)
256-
);
259+
if (ghostUser.shouldUseGhostUser(contentThemesDir)) {
260+
await ui.sudo(`ln -s ${path.join(currentThemesDir, theme)} ${path.join(contentThemesDir, theme)}`, {sudoArgs: '-E -u ghost'});
261+
} else {
262+
symlinkSync(
263+
path.join(currentThemesDir, theme),
264+
path.join(contentThemesDir, theme)
265+
);
266+
}
257267
}
258268
}
259269
}

test/unit/commands/update-spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ describe('Unit: Commands > Update', function () {
240240
force: false,
241241
instance: fakeInstance,
242242
activeVersion: '2.0.0',
243+
ui,
243244
zip: '',
244245
v1: false
245246
});
@@ -314,6 +315,7 @@ describe('Unit: Commands > Update', function () {
314315
version: '2.0.0',
315316
force: false,
316317
instance: fakeInstance,
318+
ui,
317319
activeVersion: '1.25.0',
318320
zip: '',
319321
v1: false
@@ -357,6 +359,7 @@ describe('Unit: Commands > Update', function () {
357359
force: false,
358360
instance: fakeInstance,
359361
activeVersion: '1.0.0',
362+
ui: ui,
360363
zip: '',
361364
v1: false
362365
});
@@ -424,6 +427,7 @@ describe('Unit: Commands > Update', function () {
424427
instance: fakeInstance,
425428
activeVersion: '1.1.0',
426429
installPath: '/var/www/ghost/versions/1.0.0',
430+
ui: ui,
427431
rollback: true,
428432
zip: '',
429433
v1: false
@@ -546,6 +550,7 @@ describe('Unit: Commands > Update', function () {
546550
instance: fakeInstance,
547551
activeVersion: '1.1.0',
548552
installPath: '/var/www/ghost/versions/1.0.0',
553+
ui,
549554
rollback: true,
550555
zip: '',
551556
v1: true
@@ -613,6 +618,7 @@ describe('Unit: Commands > Update', function () {
613618
instance: fakeInstance,
614619
activeVersion: '1.1.0',
615620
installPath: '/var/www/ghost/versions/1.0.0',
621+
ui,
616622
rollback: true,
617623
zip: '',
618624
v1: false

0 commit comments

Comments
 (0)