Mindmajix

Create an OpenShift Express domain

Before you can create an application, you need to create a domain. OpenShift Express uses non-strict domains (that is, there is no preceding period), and the domain name forms part of the application name. The syntax for the application name is applicationName–domainName.rhcloud.com.

Each username can only support a single domain, but you can create multiple applications within this domain. If you need multiple domains, you need to create multiple accounts using different usernames.

To create a new domain, run the rhc-create-domain command. For example, supposing you were to create the as7sample domain for the user fmarchioni@fmailbox.com:

$ rhc-create-domain -n as7sample -l fmarchioni@fmailbox.com -p password

In this command line, the –n option specifies the name of the domain that you want to create. The –l option is the e-mail address that you used to apply for your OpenShift Express account and –p as its password.

What if I need to develop my application from different hosts?

As explained before, communication between your computer and OpenShift Express happens over SSH using secure keys. So, in order to use your domain from different machines you need to copy SSH keys and the SSH configuration file from one machine to another so that you can use OpenShift Express on multiple computers. You should
also copy the express configuration file which is located at $HOME/.openshift/express.conf.

Create your first OpenShift Express application

Having registered a domain, we will now use it to create an application. OpenShift is able to deliver a set of different server applications: you can mix and match Java, PHP, Perl, Python, and Ruby to your heart’s content. What really matters is that you deploy applications on a JBoss AS 7.0 platform using the Java EE 6 web profile.

Actually, the configuration of the JBossAS7 server used by the OpenShift Express JBoss cartridge is a simple modification of the JBoss 7.0.2. Final release (upgraded to 7.1.0 Alpha just before this book was going to be printed). The OpenShift Express release does contain some limitations concerning the amount of memory and the number of
processes which can be spawned by the application server.

From a conceptual point of view, an application can be seen as a container for one or more server-side components. The command used to create an application is rhccreate- app, which accepts multiple options. For a complete list of options run: $ rhc-create-app –help

Supposing we want to create an application named example using our credentials, we will issue:

$ rhc-create-app -a example
-l fmarchioni@fmailbox
.com –p password -t jbossas-7.0 -r /home/francesco/example

Here, the –a option points to the application name, the -l flag stands for the user login name, -p is the password, -t is the application type (which, in our case, will be a JBoss 7.0 application), finally the -r option is used to point to a folder on your local filesystem where OpenShift will store all the data-related to your application. Part of
that data will be a local copy of the Git-versioning system. In the next section, we will return more in detail on the local repository.

When you issue the rhc-create-app, the SSL handshake will take place:
Creating remote application space: example
Contacting https://openshift.redhat.com

RESULT:
Successfully created application: example Adding rhcloud.com to ~/.ssh/config
Warning: Permanently added ‘exampleas7sample.
rhcloud.com,50.17.130.104′ (RSA) to the
list of known hosts.
Receiving objects: 100% (19/19), done. Confirming application example is available

Attempt # 1
Success! Your application is now published here:
http://example-as7sample.rhcloud.com/
Okay. Now, your domain contains an application namespace which can be verified by pointing to the URL where it has been published: http://exampleas7sample. rhcloud.com/.
In order to deploy artifacts in your application namespace, we need to use the Git local repository which has been created for us.

If you look beneath the application’s example folder, you will notice something familiar in it:

example
|
|— deployments
|
|— pom.xml
|
|— src
|
|— .openshift

As you can imagine, the deployments folder performs the same task of the equivalent JBOSS_HOME/standalone/deployments directory. Applications placed in it will be candidate for uploading in the OpenShift repository.

So, now we need to create an application and deploy it there. As our first example, we will deploy a simple cloud service which renders a text file as a PDF—something that you might have searched for on the Internet at least once in your life!

This application consists just of a Servlet which translated the request into a PDF response, using the iText library (available at: http://itextpdf.com/download.php)
package sample;
import java.io.IOException;
import javax.servlet.*;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
@WebServlet(“/TexttoPdf”)
public class TexttoPdf extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{
doPost(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{
String text = request.getParameter(“text”);
response.setContentType(“application/pdf”);
Document document = new Document();
try{
PdfWriter.getInstance(document,
response.getOutputStream());
document.open();
document.add(new Paragraph(text));
document.close();
}catch(DocumentException e){
e.printStackTrace();
}
}
}
The application obviously needs an HTML/JSP page which carries the text to parse in a text area:
<form action=”TexttoPdf” method=”post”>
<textarea cols=”80″ rows=”5″ name=”text”>
This text will be converted in PDF.
</textarea>
<input type=”submit” value=”Convert to PDF”>
</form>

Next, we will package the application into a file named createpdf.war and we will bring it to the cloud. For this purpose, let’s copy the createpdf.war file into the deployments folder using the cp command: $ cp createpdf.war /home/francesco/example/deployments.

We need to add and commit our application to the Git repository using the git add and git commit command:
$ git add deployments/createpdf.war
$ git commit -m “First application on the cloud” deployments/ createpdf.war
[master 1637c21] Deploy createpdf application 1 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 deployments/createpdf.war

In practice, with the git add command, we tell Git to take a snapshot of the file createpdf.war. You can permanently store the contents of the index in the repository with git commit.

Your application has now been moved on the remote Git repository. The command git push will actual deploy the application on JBoss AS 7: git push

Finally, we are able to access our application using the OpenShift application name and domain, in addition to the web context: http://exampleas7sample. rhcloud.com/createpdf

Screenshot_38

Launching our application generates as result a PDF file: your first cloud application!

Screenshot_39

Having completed the first application in the next sections, we will now show you how to manage your Openshift Express applications and introduce some advanced features.

Watching what’s happening on the server

When you deploy or run your applications you might be interested to know what is happening on the server side. The most basic form of control on your application server is the console which produces some information, such as when an application is deployed or if it encountered any error.

Tailing for the application server logs is quite simple and you just need to use the rhctail- files for this. For instance, our example application can be watched using this shell:

$ rhc-tail-files -a example –p password
Contacting https://openshift.redhat.com
Attempting to tail files: example/logs/*
Use ctl + c to stop
==> example/logs/boot.log <==
14:41:17,500 INFO [org.jboss.modules] JBoss Modules version 1.0.1.GA
14:41:17,785 INFO [org.jboss.msc] JBoss MSC version 1.0.0.GA
14:41:17,834 INFO [org.jboss.as] JBoss AS 7.0.0.Final “Lightning”
starting
14:41:17,836 DEBUG [org.jboss.as.config] Configured system
properties:
. . . . . . . . . . . .
==> example/logs/server.log <==
14:41:18,706 INFO [org.jboss.as.connector.subsystems.datasources]
(Controller Boot Thread) Deploying JDBC-compliant driver class
org.h2.Driver (version 1.2)
14:41:18,712 INFO [org.jboss.as.connector.subsystems.datasources]
(Controller Boot Thread) Deploying non-JDBC-compliant driver class
com.mysql.jdbc.Driver (version 5.1)
14:41:18,732 INFO [org.jboss.as.clustering.infinispan.subsystem]
(Controller Boot Thread) Activating Infinispan subsystem.
14:41:18,860 INFO [org.jboss.as.naming] (Controller Boot Thread)
Activating Naming Subsystem
14:41:18,877 INFO [org.jboss.as.naming] (MSC service thread 1-1) Starting Naming Service

Managing AS 7 applications

Working in the cloud may be perceived a bit difficult, at first, because you don’t have your standard instruments to manage your applications. At the time of writing, the only available instrument to manage your applications is a command line, however a new web interface has been announced which will further simplify your application’s
administration.

In order to control your applications, you can use the rhc-ctl-app command, which accepts besides user credentials and info, the –c command, which allows basic application control.

The following is the list of available commands which can be used to manage your applications (you can get, at any time, the list of available options using the –help flag):

openshift express

For example, supposing you want to delete your just created application example, you could use the following shell:

$ rhc-ctl-app -l fmarchioni@fmailbox.com -p mypassword -a example -c
destroy
Contacting https://openshift.redhat.com
!!!! WARNING !!!! WARNING !!!! WARNING !!!!
You are about to destroy the example application. This is NOT reversible, all remote data for this application will be
removed.
Do you want to destroy this application (y/n): y
Contacting https://openshift.redhat.com
API version: 1.1.1
Broker version: 1.1.1
RESULT:
Successfully destroyed application: example

0 Responses on Create an OpenShift Express domain"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.