craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.28k stars 635 forks source link

[FR] CLI commands to set isSystemLive to true and false #6729

Closed ademers closed 4 years ago

ademers commented 4 years ago

Description

It would be handy to take a Production site offline (maintenance mode) when pushing certain updates without having to edit the general.php config file or .env file on the Production server. And, then also being able to put it back online once updates are completed.

So deploy script on Production could look something like:

  1. ./craft isSystemLive/false
  2. composer install
  3. ./craft migrate/all
  4. ./craft project-config/apply
  5. ./craft clear-caches/all
  6. ./craft isSystemLive/true

Thanks for you consideration!

brandonkelly commented 4 years ago

Great idea! Just added new on and off commands, which will toggle the system.live project config value (bypassing allowAdminChanges restrictions, if disabled).

There’s also a new “Retry Duration” setting under Settings → General, which can be set to the number of seconds that the Retry-After HTTP header should be set to for 503 responses when the system is offline. (If empty, no Retry-After header will be sent.)

The off command has a --retry option that can set the Retry Duration setting as well:

$ php craft off --retry=60
The system is now offline.
The retry duration is now set to 60.
brandonkelly commented 4 years ago

Worth mentioning you can call these commands automatically with Composer scripts as well:

{
  "...": "...",
  "scripts": {
    "pre-install-cmd": [
      "@php craft off --retry=60"
    ],
    "post-install-cmd": [
      "@php craft clear-caches/all",
      "@php craft migrate/all",
      "@php craft project-config/apply",
      "@php craft on"
    ]
  }
}
bencroker commented 4 years ago

Seriously impressive turnaround on this @brandonkelly!!

mmikkel commented 4 years ago

@brandonkelly To be clear – will craft off override the isSystemLive config setting, if set in config/general.php (i.e. 'isSystemLive' => (bool)getenv('IS_SYSTEM_LIVE')?

brandonkelly commented 4 years ago

Sorry forgot to mention. If the isSystemLive config setting is set to true or false, these commands will just error out, as that takes precedence over the system.live project config value that these commands control.

ademers commented 4 years ago

@brandonkelly Thanks for this, and so fast!

I'm a bit confused re the above:

If the isSystemLive config setting is set to true or false, these commands will just error out

Is there anything I need to do/change in general.php to ensure that the commands work as expected?

brandonkelly commented 4 years ago

@ademers Only if you are setting the isSystemLive config setting there; then you would need to remove that line for these commands to work.

brandonkelly commented 4 years ago

Craft 3.5.8 is out now with the new on and off commands.

khalwat commented 4 years ago

Works beautifully in CI, thanks!

Resolving host devmode.fm...
Connected. Executing commands...
Executing command: chmod a+x craft
Executing command: php craft off --retry=60
The system is now offline.
The retry duration is now set to 60.
Executing command: echo ''
Executing command: sudo -S supervisorctl restart all
daemon-569990:daemon-569990_00: stopped
daemon-569990:daemon-569990_00: started
Executing command: php craft backup/db
Backing up the database ... done
Backup file: /home/forge/devmode.fm/releases/f1eb845ea807d7a04a97ebb6e3e9f2190369b708/storage/backups/devmode.fm--2020-09-02-205118--v3.5.8.sql (9.063 MB)
Executing command: php craft clear-caches/all
Clearing cache: Asset caches
Clearing cache: Asset indexing data
Clearing cache: Asset transform index
Clearing cache: Compiled templates
Clearing cache: Control panel resources
Error clearing cache Control panel resources: The dir argument must be a directory: /home/forge/devmode.fm/current/web/cpresources
Clearing cache: Data caches
Clearing cache: FastCGI Cache
Clearing cache: Retour redirect caches
Clearing cache: SEOmatic frontend template caches
Clearing cache: SEOmatic metadata caches
Clearing cache: SEOmatic sitemap caches
Clearing cache: Temp files
Clearing cache: Twigpack Manifest Cache
Executing command: php craft migrate/all
Yii Migration Tool (based on Yii v2.0.36)
Migrated up successfully.
Executing command: php craft project-config/sync
project-config/sync has been renamed to project-config/apply. Running that instead...
Applying changes from your project config files ... done
Executing command: php craft on
The system is now online.
Commands accomplished, great!
Saboteur777 commented 4 years ago

I have noticed one issue, though: dateModified field of config/project/project.yaml is bumped if system.live is toggled. This is not an issue if your deployment process discards the changes made on production, but it is if you do a git pull instead, as it will fail due to conflict.

khalwat commented 4 years ago

Ahhhhhh so that's what was modifying the date in the project.yaml!

I guess technically they have to, since the way it works is by changing the setting in Project Config.

Saboteur777 commented 4 years ago

Yepp! :D If am not mistaken, the change is not even applied until you sync the Project Config to the database right after the off/on command, similar to this:

php craft off --retry=60
php craft project-config/sync

... deployment process ...

php craft on
php craft project-config/sync

which could potentially disrupt a deployment if you do git pull instead of deploying an already compiled package.

brandonkelly commented 4 years ago

Hm, good points. I think we will refactor Maintenance Mode for Craft 4, to have it work more like Laravel, and have on/off toggle that instead of the system.live project config setting.

khalwat commented 4 years ago

Yeah @Saboteur777 seeing this after just doing php craft on then php craft off in local dev:

Screen Shot 2020-09-08 at 8 54 29 AM

So I've removed that from the composer.json scripts:

https://github.com/nystudio107/craft/commit/9f0ba054ed9c90b2ab17d8b6de6ad701372c643c

I'm keeping it for our CI pipeline tho, because it's done in a Docker container, and the results just deployed with rsync so there are no issues with git.

lassemt commented 4 years ago

Regarding the dates being modified when doing php craft off --retry=60. If I do a change that updates the project config locally, deploy, and run php craft off --retry=60 before php craft project-config/apply the change is removed.

One example. If I create a new section (single) locally it will create sections/mynewsingle--hash-or-whatever.yaml, this file is being deleted when deploying if I run php craft off --retry=60 before php craft project-config/apply.

This seems to be only happening after updating to 3.5.12.1, was previously running 3.5.11.1.

Edit: Created a separate issue for this #6984. @khalwat the Buddy "Prep Craft CMS" action in your scaffolding project will most likely not apply project config changes because of this in the latest version of the craft as I used it for the testing.

brandonkelly commented 4 years ago

As of the next release, the on and off commands will no longer update the dateModified value, or discard other pending project config changes (#6984).

There is still one semi-issue, which is that applying pending project config changes will overwrite the system.live value change performed by the off command. So running on after project-config/apply is redundant (unless system.live was set to false in project.yaml).

lassemt commented 4 years ago

As of the next release, the on and off commands will no longer update the dateModified value, or discard other pending project config changes (#6984).

There is still one semi-issue, which is that applying pending project config changes will overwrite the system.live value change performed by the off command. So running on after project-config/apply is redundant (unless system.live was set to false in project.yaml).

Noted! Thanks for the heads up and quick fix!

brandonkelly commented 4 years ago

Craft 3.5.13 is out now with that change ✨