Automate ghost doctor to solve some frequent, straight forward issues

I find that some of the very often encountered issues with ghost cli commands (like update, in particular) are generated by incorrect file/folder permissions.

For example:

✖ Removing old Ghost versions
An error occurred.
Message: 'EACCES: permission denied, rmdir '/home/forge/''

Debug Information:
    OS: Ubuntu, v20.04.1 LTS
    Node Version: v12.19.0
    Ghost Version: 3.35.5
    Ghost-CLI Version: 1.14.1
    Environment: production
    Command: 'ghost update'

Additional log info available in: /home/forge/.ghost/logs/ghost-cli-debug-2020-10-08T18_36_48_313Z.log

Try running ghost doctor to check your system for known issues.

Since Ghost has all the info about what’s wrong and since Ghost already has sudo rights (it asks for sudo password anyways) it should be easy for the ghost doctor to run an additional command, such as:

sudo chown -R <user>.<group> /home/forge/

Also, as per: there are several other potential file permission issues…

To prevent accidental permissions changes, it’s advisable to execute tasks such as image upload or theme upload using Ghost admin.

This has happened to me often times to upload a new, locally edited, theme with another user rather than Ghost (as I’m sure most people almost never use this user to manage content, yet ghost owns the content folder).

In this case, ghost doctor could run on its own, as mentioned above (as I assume it’s pretty safe):

sudo chown -R ghost.ghost /path/to/content/

By the same token, when there are improper file/folder permissions identified by ghost doctor, it should inforce them at update and other times as it does at installation time.

sudo find /var/www/ghost/* -type d -exec chmod 775 {} \;
sudo find /var/www/ghost/* -type f -exec chmod 664 {} \;

The ghost-cli enforces default linux permissions (via ghost doctor hooks) for installations.

But probably the following needs to change, I assume:

The cli doesn’t support directory flags such as setuid and setguid ). If your commands keep failing because of file permissions, ensure your directories have no flags!