April 21, 2017

Wouldn't it be great if???

Configuration management is one of the most useful site development features in Drupal 8. It makes a site's configuration exportable, importable and manageable in git. Whilst building the configuration management feature, a thought that often occurred was "Wouldn't it be great if you can take an existing set of configuration and install a new site from it?". Every Drupal developer has turned up to a new project and had to learn a different way to build a development site. Do you get the code from github? Download a database from production or some other special location? And is that database sanitised? I often wonder how many Drupal developers there are in the world wandering around with production databases on their laptops.

Install Drupal 8 from Configuration CMI

We started to explore the ability to install from configuration in https://www.drupal.org/node/1613424. Why hasn't this made it into Drupal 8 yet? It transpires that changing the installer is hard and it was not a top priority in the run up to the 8.0.0 release. To fix the feature gap, I created the contrib project Configuration Installer. This is a special installation profile that allows you to install from a configuration export.

So problem solved in contrib right? Well, no. Profiles are special weird magic modules. They can do everything a module can do and more. But things don't work the same. You can uninstall the Contact module after installing the Standard profile even though the contact module is listed as a dependency. This causes problems for the Configuration Installer because Standard's hook_install() implementation will error if Contact is not installed. The Configuration Installer can work around that with ugly code but its luck runs out if the install profile has custom or contrib modules included. In order to work, the Configuration Installer swaps the currently active profile during install so Drupal can't find modules or themes buried inside the install profile.

Your site's install profile could contain all the configuration

In an ideal world, I want to get a project's code and use the Drupal installer to select install profile for the site I'm working on and and install it. I want to make some configuration changes, export them, and commit them to git. I want to throw away that site and forget about it. I want to come back six months later and update the code, re-install a fresh site that includes any changes other developers made in the meantime, and continue to work on the site. All without having to think particularly hard. I want to use the standard Drupal tools like the installer, configuration admin UI, and Drush.

The good news is that we can have all this! This is precisely what the patch available at https://www.drupal.org/node/2788777 does. We can change the installer to read from its config/sync directory and run a configuration import instead of the regular install. It builds on the Configuration Installer project but does not require the magic of swapping around the profile, so is much more robust. The final piece of the puzzle is to change the configuration sync directory to the profile's config/sync in settings.php, and it just works. As a happy developer said trying out this for the first time:

I love this workflow :)

How to have a rebuildable and reinstallable Drupal today!

  1. Use https://github.com/drupal-composer/drupal-project to manage your codebase using composer.
  2. Use https://github.com/cweagans/composer-patches to apply the latest patch on https://www.drupal.org/node/2788777 as part of composer install.
  3. Create an install profile with a config/sync directory. The new Install Profile Generator module can do this for you with a simple Drush command.

Now I can update my install profile after making configuration changes by running drush config-export. I can share those changes by pushing the install profile. I can pull in changes from other developers by pulling and running drush config-import. I can rebuild the site from scratch by checking out the codebase and running drush site-install.

Afterthoughts

If you are thinking "Wouldn't it be nice if we could also create some content at the same time?" have a look at the Default Content project. You will need the patch from https://www.drupal.org/node/2833562.

If you are wondering whether applying a patch to core is safe - in this instance I would argue it really is. The only changes the patch makes are to the installer and makes no runtime changes.

Isn't the obfuscated config sync directory generated by core important? Well I'm not a big believer in security through obscurity. But also if you are using Apache then core checks for the presence of a .htaccess in your config sync directory that should prevent access. Other webservers should also be denying access to .yml files by default too.

Further reading

Thanks

Eli-T for helpful suggestions on this blogpost and working with me on the Install Profile Generator.

Catch for helpful suggestions on this blogpost.