Skip to content

Commit

Permalink
fix(update): fixed permission issues when linking themes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
daniellockyer committed Oct 5, 2023
1 parent c766560 commit eb3f430
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/commands/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const symlinkSync = require('symlink-or-copy').sync;
const {GhostError} = require('../errors');
const Command = require('../command');
const DoctorCommand = require('./doctor');
const ghostUser = require('../utils/use-ghost-user');

class UpdateCommand extends Command {
static configureOptions(commandName, yargs, extensions) {
Expand Down Expand Up @@ -43,6 +44,7 @@ class UpdateCommand extends Command {
instance,
force,
activeVersion: instance.version,
ui: this.ui,
version,
zip,
v1
Expand Down Expand Up @@ -230,7 +232,7 @@ class UpdateCommand extends Command {
instance.nodeVersion = process.versions.node;
}

linkDefaultThemes({instance, rollback}) {
async linkDefaultThemes({instance, rollback, ui}) {
const currentThemesDir = path.join(process.cwd(), 'current', 'content', 'themes');
const contentThemesDir = path.join(instance.config.get('paths.contentPath'), 'themes');
// remove any broken symlinks caused by default themes no longer existing in previous version
Expand All @@ -239,7 +241,11 @@ class UpdateCommand extends Command {
const installedThemes = fs.readdirSync(contentThemesDir);
for (const theme of installedThemes) {
if (!fs.existsSync(path.join(contentThemesDir, theme))) {
fs.rmSync(path.join(contentThemesDir, theme));
if (ghostUser.shouldUseGhostUser(contentThemesDir)) {
await ui.sudo(`rm ${path.join(contentThemesDir, theme)}`, {sudoArgs: '-E -u ghost'});
} else {
fs.unlinkSync(path.join(contentThemesDir, theme));
}
}
}
}
Expand All @@ -250,10 +256,14 @@ class UpdateCommand extends Command {
const defaultThemes = fs.readdirSync(currentThemesDir);
for (const theme of defaultThemes) {
if (!fs.existsSync(path.join(contentThemesDir, theme))) {
symlinkSync(
path.join(currentThemesDir, theme),
path.join(contentThemesDir, theme)
);
if (ghostUser.shouldUseGhostUser(contentThemesDir)) {
await ui.sudo(`ln -s ${path.join(currentThemesDir, theme)} ${path.join(contentThemesDir, theme)}`, {sudoArgs: '-E -u ghost'});
} else {
symlinkSync(
path.join(currentThemesDir, theme),
path.join(contentThemesDir, theme)
);
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/unit/commands/update-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ describe('Unit: Commands > Update', function () {
force: false,
instance: fakeInstance,
activeVersion: '2.0.0',
ui,
zip: '',
v1: false
});
Expand Down Expand Up @@ -314,6 +315,7 @@ describe('Unit: Commands > Update', function () {
version: '2.0.0',
force: false,
instance: fakeInstance,
ui,
activeVersion: '1.25.0',
zip: '',
v1: false
Expand Down Expand Up @@ -357,6 +359,7 @@ describe('Unit: Commands > Update', function () {
force: false,
instance: fakeInstance,
activeVersion: '1.0.0',
ui: ui,
zip: '',
v1: false
});
Expand Down Expand Up @@ -424,6 +427,7 @@ describe('Unit: Commands > Update', function () {
instance: fakeInstance,
activeVersion: '1.1.0',
installPath: '/var/www/ghost/versions/1.0.0',
ui: ui,
rollback: true,
zip: '',
v1: false
Expand Down Expand Up @@ -546,6 +550,7 @@ describe('Unit: Commands > Update', function () {
instance: fakeInstance,
activeVersion: '1.1.0',
installPath: '/var/www/ghost/versions/1.0.0',
ui,
rollback: true,
zip: '',
v1: true
Expand Down Expand Up @@ -613,6 +618,7 @@ describe('Unit: Commands > Update', function () {
instance: fakeInstance,
activeVersion: '1.1.0',
installPath: '/var/www/ghost/versions/1.0.0',
ui,
rollback: true,
zip: '',
v1: false
Expand Down

0 comments on commit eb3f430

Please sign in to comment.