OpenShift Cookbook
上QQ阅读APP看书,第一时间看更新

Creating an OpenShift application using the rhc command-line client

In this recipe, you will learn how to create an OpenShift application using rhc. We will create a PHP 5.4 application just for demonstration. This chapter will be language-agnostic and will only cover concepts that apply to all the application types. Different programming languages supported by OpenShift will be covered later in the book.

Getting ready

To step through this recipe, you will need the rhc command-line client installed on your machine. Please refer to the Installing the OpenShift rhc command-line client recipe in Chapter 1, Getting Started with OpenShift, for details. Also, you should set up your OpenShift account using rhc by following the Setting up an OpenShift account using rhc recipe in Chapter 1, Getting Started with OpenShift.

How to do it…

To create a PHP 5.4 OpenShift application named myapp, open a new command-line terminal and run the following command:

$ rhc create-app --app myapp --type php-5.4

You can also avoid typing the --app and --type options where OpenShift will automatically figure them out, as follows:

$ rhc create-app myapp php-5.4

You can also write the command as follows:

$ rhc app-create myapp php-5.4

All the rhc commands can take either the rhc <noun>-<verb> or rhc <verb>-<noun> form.

How it works…

Let's go through all the steps performed by the rhc create-app command:

  1. The rhc create-app command requires two mandatory options:--app and --type. You are not required to pass these options with the command, but you are required to provide their values as shown in the rhc create-app myapp php-5.4 command. These two options specify the application name and the web cartridge the application will use. The OpenShift server checks whether the application name and web cartridge name are correct. A valid application name must contain only alphanumeric characters and can be, at the most, 32 characters in length. You can view all the available web cartridges using the following command:
    $ rhc cartridges|grep web
    
  2. After making sure the application name and web cartridge name are correct, it will check whether sufficient gears are available in your domain to create an application. In the free tier, you only have access to three gears, so if you try to create an application after you have consumed all three, you will receive an error response. For example, if you have already created three applications and you try to create the fourth application, you will get the error response, user has already reached the gear limit of 3.
  3. If you have sufficient resources to create an application, rhc will make a HTTP POST request to create an application. The rhc command-line client is a wrapper around the OpenShift REST API. The OpenShift server will receive the POST request and allocate a gear for your application. The amount of RAM and disk space a gear is allocated depends on the gear size. In the free tier, you only have access to small gears, which have 512 MB of RAM and 1 GB of disk space. If you are in the paid tier, you can specify bigger gear sizes with the --gear option. The valid values for --gear at the time of writing are small, medium, and large.
  4. Next, OpenShift will install the web cartridge required by your application. In the application created previously, it will install the PHP 5.4 language runtime and Apache web server to serve your web requests and perform the required configuration.
  5. The OpenShift server will also create a private Git repository for your application. The Git repository will have a template application depending on the web cartridge type. You can specify your own template application using --from-code. This is covered in the next recipe.
  6. Once the application is created with all the required cartridges, the OpenShift server will create a public URL for your application and register it with the DNS. The public URL is a combination of the application name and the domain name. For the application created previously, the URL will be http://myapp-osbook.rhcloud.com. Here, myapp is the application name, and osbook is the domain name. You can also use your own custom domain name with OpenShift applications. This is covered in the Using your own custom domain name recipe.
  7. After the application DNS name is available, rhc will use the Git command-line to clone the application Git repository on your local machine.
  8. Finally, you will be shown the details of your application. You can view the running application at http://myapp-{domain-name}.rhcloud.com/. Please replace {domain-name}with your account domain name. An example is shown as follows:
    Your application 'myapp' is now available.
    
     URL: http://myapp-osbook.rhcloud.com/
     SSH to: 52ef686d4382ec39f500001a@myapp-osbook.rhcloud.com
     Git remote: ssh://52ef686d4382ec39f500001a@myapp-osbook.rhcloud.com/~/git/myapp.git/
     Cloned to: /home/vagrant/dev/apps/myapp
    

Let's look at the myapp application directory on your local machine. After the application is created, a directory with a name that is identical to the application name is created on your local machine. It houses the source code of the template application created by OpenShift, as follows:

$ ls -a
.git .openshift index.php

Let's look at each of these components one by one as follows:

  1. The .git directory stores the Git repository of the myapp application. This directory contains the complete history of the repository. The .git/config file contains the configuration for the repository. The rhc command-line tool also adds the application-specific metadata to the .git/config file. The application-specific metadata is under the rhc section:
    [rhc]
    app-id = 52ef686d4382ec39f500001a
    app-name = myapp
    domain-name = osbook
    
  2. The .openshift directory stores OpenShift-specific files. The .openshift directory has three subdirectories—action_hooks, cron, and markers:
    • The action_hooks directory stores the executable scripts, which gives application developers an entry point into various applications and platform life cycle operations. An example of using an action hook would be to send an e-mail after the application is deployed.
    • The cron directory stores the executable scripts, which can be scheduled to run periodically. We will cover this in detail in the Adding a cron cartridge to an application recipe later in this chapter.
    • The markers directory allows a user to specify settings such as hot deployment, debugging, and the version of Java to be used. As these settings are specific to web cartridges, we will cover them in detail in web-cartridge-specific chapters.
  3. The index.php file contains a simple PHP application that you see when you visit the application URL.

    Note

    At the time of writing this book, applications in the free tier will idle out after 24 hours of inactivity. Inactivity means no HTTP request has been made to your application URL from outside the gear. When idling, it takes a few seconds for the gear to wake up and start processing web requests.

There's more

The rhc command-line tool will raise an exception if the application creation takes more than 120 seconds. To overcome errors related to timeout, you can specify the --timeout option as shown in the following code. The timeout value is in seconds:

$ rhc app-create myapp php-5.4 --timeout 300

You can also configure the timeout in the ~/.openshift/express.conf file, as shown in the following code. This will apply to all the commands:

# The default timeout for network operations
timeout=300

See also

  • The Creating a WordPress application using the web console recipe in Chapter 1, Getting Started with OpenShift
  • The Specifying your own template Git repository URL recipe
  • The Adding a cron cartridge to an application recipe
  • The Viewing application details recipe
  • The Using your own custom domain name recipe