Drupal Installation Profile and Pre-configuration

Share: Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInShare on RedditEmail this to someonePrint this page

This blog article describes an installation profile and pre-configuration dialog screens that were created in order to accomplish easy integration between the Bluemix database and the Drupal image that’s modified for Bluemix. The changes to the installation profile and pre-configuration screens allow database credentials passed from VCAP_SERVICES environment variable to be automatically incorporated into the Drupal setup process.

Note:  If you just want to know how to run a Drupal container within Bluemix and have that instance of Drupal use a database created within Bluemix, then follow the steps within the tutorial here. That tutorial accesses the Drupal profile and configuration files that are described within this blog article. 

Before we begin, know that all the necessary code can be found in this GitHub repository. Bluemix passes database credentials through the VCAP_SERVICES environment variable, a string that can be parsed into JSON, so we need to incorporate that into the code of our Drupal setup in order for Drupal to automatically parse VCAP_SERVICES and set the database credentials. An example VCAP_SERVICES can be seen below for a ClearDB database.
example_vcap_servicesBefore doing anything, we need to base our Docker image off of a pre-existing image (obviously, you don’t want to start from scratch) so we pull from the official Drupal 7 image on Docker Hub. Thus, the beginning of our .Dockerfile looks like so:

Then we want to add two of our own profiles that mimic the Drupal standard installation and the minimal installation. In order to do that, we need two additional files per profile (a .profile file and .info file per Drupal 7 specifications) for a total of four files plus the Dockerfile.

Let’s take a look at various code sections within the minimal install .profile file which is named bluemixminimal.profile.

First we need to implement hook_install() like so:

We want to mimic the typical minimal install which is why our hook_install includes the standard Drupal minimal.install. However, we also want a hook_install_tasks_alter() function because we want to change the database configuration page to preload our database values from the VCAP_SERVICES environment variable. Thus the following code segment has been added to the bluemixminimal.profile file:

This means we will point to our own install settings function when we hit that part of the installation. Essentially, we will modify the form valid pre-populate with our values.  The code segment below contains the modified function within the bluemixminimal.profile file.  The three vertical dots represent unchanged portions of the function (to see the modified profile in its entirety go to the Github repository). I made the following changes:

  1. On line 10, we set declare and initialize default values for the form variables that will show up on the installation screen. Notice I’ve given the database name a default message in case the user tries to use our installation profile without a Bluemix service.
  2. On line 16, we check for VCAP_SERVICES, which tells us the user has connected to a Bluemix service, and then on lines 17 to 38, I essentially parse the JSON object for the necessary credentials. Lines 40 to 48 are where I set the parsed values to the form variable so that the necessary credentials will show on the screen.
  3. For this code block, the last thing to do is change line 61 to use our own hook_install_settings_form_submit function because we need to provide a form submit and form validate functions. The form submit function is just the normal Drupal code (no different than here).

Notice that we didn’t pass the database password, Drupal prevents us from doing this, but it’s also fine because it’s probably best we don’t expose the password during the Drupal installation for security purposes.

We now need to include a hook_install_settings_form_submit function. This function is the same as the standard profile installation except, of course, our profile name is used in the function name (line 1):

However, our hook_install_settings_form_validate function needs to be modified to supply our password (lines 8 to 24):

With that, we just need the .info (bluemixminimal.info in our case) file for the installation profile. This is simply:

Now copy both files but replace every “bluemixminimal” with “bluemixstandard” and change the bluemixstandard.info file to:

With that done put the bluemixminimal files in a folder titled–you guessed it–bluemixminimal, and put the bluemixstandard files in another folder called bluemixstandard. Like so:
profile file structure
Now we can modify the Dockerfile to add our profiles to the Drupal installation:

Congratulations, you can build the docker image and run the Drupal installation without having to type in any database values. However, for my purposes, I also didn’t want the user attempting to change the database fields. Furthermore, I wanted the page to dynamically select the MySQL or PostgreSQL option without user input. The solution to this is somewhat “hacky” as Drupal isn’t really built for this sort of thing, but here’s a solution that worked.

There’s a template php file within the “seven” theme that is used during the installation phase to serve up the web pages. I’ve modified it so that if the user is using the Bluemix installation profile, some javascript will execute that performs the desired behavior. The file is called maintenance-page.tpl.php and is found in the /themes/seven directory. The added code is below (you can see the entire file here), and this file should be put in the same directory as the Dockerfile.

The Dockerfile should be updated as follows:

Phew! Finally, that’s it. This can be built and run as expected.

Share: Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInShare on RedditEmail this to someonePrint this page