The examples in this chapter are designed to put some context around the previous chapters. The examples are all given using Red Hat’s OpenShift platform, but those interested in other platforms can think of these as thought experiments. The general process and concepts apply to most PaaS providers and half the fun of PaaS is trying out different options and picking the best one for the job.
Everyone starts at the hello world stage. With OpenShift, it’s as simple as a single command to create a publicly accessible web application. This example uses the OpenShift client tools. Many PaaS providers offer command line tools and a well documented API to allow use via existing tools. This make it easy to create and destroy entire environments via a script. As with most offerings there is also a web interface.
Installing the OpenShift client tools is easy but requires users to have Rubygems installed as well as git. Users can also use the Java client tools or eclipse plugins.
The rest of this example assumes users have already signed up for a free account at http://openshift.redhat.com/. After that, users need to create a unique namespace to work in under the rhcloud.com domain.
$ rhc-create-domain -n example1 -l email@example.com Generating OpenShift ssh key to /home/example/.ssh/libra_id_rsa Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/example/.ssh/libra_id_rsa. Your public key has been saved in /home/example/.ssh/libra_id_rsa.pub. The key fingerprint is: e8:4d:58:73:5e:55:89:78:69:86:e3:c6:cc:13:f9:2e example@myworkstation The key's randomart image is: +--[ RSA 2048]----+ | + +.o| | * B . | | o * O | | + + O . | | o S o o | | . o E . | | . . . | | | | | +-----------------+ Contacting https://openshift.redhat.com/ Creation successful You may now create an application. Please make note of your local config file in /home//.openshift/express.conf which has been created and populated for you.
This step does two things, first it registers the new DNS name example1. Next it sets up SSH pubkey authentication. When using git later to push code, this ssh key will be used. All applications created with this account will be $SOMETHING-example1.rhcloud.com.
Next use the tools to create a new PHP application.
$ rhc-create-app -a myapp -t php-5.3 Password: Found a bug? Post to the forum and we'll get right on it. IRC: #openshift on freenode Forums: https://www.redhat.com/openshift/forums Attempting to create remote application space: myapp Contacting https://openshift.redhat.com/ API version: 1.1.1 Broker version: 1.1.1 RESULT: Successfully created application: myapp Checking ~/.ssh/config Contacting https://openshift.redhat.com/ Found rhcloud.com in ~/.ssh/config... No need to adjust Now your new domain name is being propagated worldwide (this might take a minute)... Pulling new repo down Warning: Permanently added 'myapp-example1.rhcloud.com' (RSA) to the list of known hosts. Confirming application myapp is available Attempt # 1 Success! Your application is now published here: http://myapp-example1.rhcloud.com/ The remote repository is located here: ssh://firstname.lastname@example.org/~/git/myapp.git/ To make changes to your application, commit to myapp/. Then run 'git push' to update your OpenShift space
This command, which takes roughly 15-20 seconds to complete, creates a public application and clones a git repository. The repository is now located on the users workstation in their current working path under ./myapp/ and the actual application can be seen in a browser via http://myapp-example1.rhcloud.com/. The sample application is just a basic hello world application.
Making a simple change to the application involves editing the file, committing to git and then issuing a push. By doing a push, the local code changes are copied or “pushed” to the remote repo and then deployed automatically. This example adds a simple phpinfo to the end of our index.php file.
$ cd myapp/php $ ls health_check.php index.php $ echo '<?php phpinfo(); ?>' >> index.php $ git commit -a -m "Added phpinfo to end of index" [master bf7edf1] Added phpinfo to end of index 1 files changed, 1 insertions(+), 1 deletions(-) $ git push Counting objects: 7, done. Delta compression using up to 4 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 401 bytes, done. Total 4 (delta 2), reused 0 (delta 0) remote: Stopping application... remote: Waiting for stop to finish remote: Done remote: Running .openshift/action_hooks/build remote: Starting application... remote: Done To ssh://email@example.com/~/git/myapp.git/ ddf2b85..bf7edf1 master -> master
To summarize: this example has three elements. First, alter the landing page for http://myapp-example1.rhcloud.com which is an index.php file. Second, commit that change. Third push the change to the PaaS provider using git which automatically publishes.
In its most basic form, this simplicity is the power of PaaS. The examples above show the basic workflow of many PaaS providers. Create an application or compute power on some remote server. Link that remote application to development source code. Then publish changes.
Deploying Drupal in OpenShift. Creating a more advanced example in OpenShift can be done via a pre-populated Drupal repository. This particular example can be found at github: https://github.com/openshift/drupal-example. There are four steps to take in order to download, configure and deploy Drupal.
The first step is to create a PHP application much like we did in the first example. In addition to PHP, a MySQL database will be added.
$ rhc-create-app -a drupal -t php-5.3 Password: Found a bug? Post to the forum and we'll get right on it. IRC: #openshift on freenode Forums: https://www.redhat.com/openshift/forums Attempting to create remote application space: drupal Contacting https://openshift.redhat.com API version: 1.1.1 Broker version: 1.1.1 RESULT: Successfully created application: drupal Checking ~/.ssh/config Contacting https://openshift.redhat.com Found rhcloud.com in ~/.ssh/config... No need to adjust Now your new domain name is being propagated worldwide (this might take a minute)... Pulling new repo down Confirming application drupal is available Attempt # 1 Success! Your application is now published here: http://drupal-example1.rhcloud.com/ The remote repository is located here: ssh://firstname.lastname@example.org/~/git/drupal.git/ To make changes to your application, commit to drupal/. Then run 'git push' to update your OpenShift space $ rhc-ctl-app -a drupal -e add-mysql-5.1 Password: Contacting https://openshift.redhat.com Contacting https://openshift.redhat.com API version: 1.1.1 Broker version: 1.1.1 RESULT: Mysql 5.1 database added. Please make note of these credentials: Root User: admin Root Password: M9_b8mEUdgVK Database Name: drupal Connection URL: mysql://127.1.40.1:3306/
From this point on, the “drupal” application has MySQL access. The only thing left is to actually put Drupal in the repo (to replace the hello world app) and push it. To do this, we use git to add an upstream repo so we can keep things in sync if updates come out.
# This directory was created when we ran rhc-create-app -a drupal $ cd drupal $ git remote add upstream -m master git://github.com/openshift/drupal-example.git $ git pull -s recursive -X theirs upstream master $ git push Counting objects: 1135, done. Delta compression using up to 8 threads. Compressing objects: 100% (990/990), done. Writing objects: 100% (1127/1127), 2.90 MiB | 563 KiB/s, done. Total 1127 (delta 121), reused 1122 (delta 120) remote: Stopping application... remote: Waiting for stop to finish remote: Done remote: Running .openshift/action_hooks/build remote: remote: Database schema not found, importing 'drupal.sql' schema. remote: remote: remote: done. remote: ================================================== remote: Drupal-Admin login: admin remote: Drupal-Admin password: OpenShiftAdmin remote: Don't forget to change your drupal admin password! remote: ================================================== remote: mode of `/var/lib/libra/0d7e452672394ef38856c2ba264c2045/drupal/repo//php/sites/default/settings.php' changed to 0440 (r--r-----) remote: mode of `/var/lib/libra/0d7e452672394ef38856c2ba264c2045/drupal/repo//php/sites/default/default.settings.php' changed to 0440 (r--r-----) remote: Starting application... remote: Done To ssh://email@example.com/~/git/drupal.git/ 0f9bf26..04ceafb master -> master
That’s it. Drupal is now up and running at http://drupal-example1.rhcloud.com/. We can confirm this via the command line using wget or a browser:
$ wget -qO- http://drupal-mcgrath.rhcloud.com/ | grep '<title>' <title>Welcome to OpenShift Drupal | OpenShift Drupal</title>
As it turns out, not many changes were required for Drupal to run in OpenShift. Mostly there was a .openshift/action_hooks/build script that needed to import the Drupal database. Then in the Drupal configs, instead of storing the database user-name and password in the repo, environment variables were provided by OpenShift and used. For those that know Drupal, this is in the sites/default/settings.php file:
$databases = array ( 'default' => array ( 'default' => array ( 'database' => 'drupal', 'username' => $_ENV['OPENSHIFT_DB_USERNAME'], 'password' => $_ENV['OPENSHIFT_DB_PASSWORD'], 'host' => $_ENV['OPENSHIFT_DB_HOST'], 'port' => $_ENV['OPENSHIFT_DB_PORT'], 'driver' => 'mysql', 'prefix' => '', ), ), );
These two examples are fairly rudimentary, but demonstrate how simple it is to get things up and running in a PaaS environment like OpenShift. In the second example, Drupal required a database which was provided and configured by the platform. Additionally, access to the database was provided via an automated mechanism (in this case environment variables). What’s not demonstrated in these examples is what is required of maintenance.
In the Drupal example, some libraries were provided by the platform. By committing and pushing the Drupal code via a git repo, the user must maintain that code when updates to Drupal come out. As discussed in Chapter 3, binaries and libraries provided by the platform are typically maintained by the platform. This means OpenShift will ensure Apache, PHP and several common dependencies are properly maintained. The Drupal code, however, is the customer’s responsibility, which includes watching any Drupal CVE’s that may be published.