<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    enable-openshift-ci: full example using openshift-java-client
</h3>
<span style="margin-bottom: 10px;">
    modified by <a href="https://community.jboss.org/people/adietish">Andre Dietisheim</a> in <i>JBoss Tools</i> - <a href="https://community.jboss.org/docs/DOC-19828">View the full document</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>This wiki entry will show you a complete example using the <a class="jive-link-external-small" href="https://github.com/openshift/openshift-java-client">openshift-java-client</a>. The openshift-java-client is a java library that allows you talk to the OpenShift PaaS programmatically. </p><p>This article will develop a command line tool in java, that takes a project on your disk and sets up a <a class="jive-link-external-small" href="http://jenkins-ci.org/">jenkins</a> build for it on OpenShift. </p><p>The example is hosted on github at <a class="jive-link-external-small" href="https://github.com/adietish/enable-openshift-ci">https://github.com/adietish/enable-openshift-ci</a> and you may freely reuse, redistribute or modify the code since it's licensed under the <a class="jive-link-external-small" href="http://www.eclipse.org/legal/epl-v10.html">Eclipse Public License</a>.</p><h1>Requirements</h1><ul><li>your project is required to build with maven and has to be committed to a git repository</li><li>you need an account on <a class="jive-link-external-small" href="https://openshift.redhat.com/app/">OpenShift</a>. (If you have no account yet, you would have to <a class="jive-link-external-small" href="https://openshift.redhat.com/app/account/new">signup</a> first)</li><li>you need 3 free application slots (given that you have a default quota of 3 applications, you shall have no application in your account).</li><li>you need to have git available on your command-line (enable-openshift-ci is using it to deploy)</li></ul><h1>Usage</h1><pre class="jive-pre"><code class="jive-code">-p&#160;&#160;&#160;&#160; the project (folder) that we will enable CI for
-pw&#160;&#160; the OpenShift password
-u&#160;&#160;&#160;&#160; the OpenShift user
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>You have to tell enable-openshift-ci where it will find your project (<strong>-p</strong>), what's your OpenShift user (<strong>-u</strong>) and what password you are using (-<strong>pw</strong>). </p><p>An exemplary call using the jar would look the following:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code">java -jar target/enable-openshift-ci-0.0.1-SNAPSHOT-jar-with-dependencies.jar -p &lt;PATH_TO_PROJECT&gt; -a test -u &lt;USER&gt; -pw &lt;PASSWORT&gt;</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Calling it using maven would look like this:</p><pre class="jive-pre"><code class="jive-code">mvn test -Du=&lt;USER&gt; -Dpw=&lt;PASSWORD&gt; -Dp=&lt;PATH_TO_PROJECT&gt;</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Enable-openshift-ci will log into your OpenShift account, create a domain (if needed), an application for your project and a jenkins application. It will then add the remote application git repo to your local project. You will then be ready to merge the remote content to your local project, add the openshift profile to your pom and push your project upstream. This will trigger the jenkins build.</p><h1>Implementation</h1><h2>Build setup</h2><p>First of all enable-openshift-ci requires the <a class="jive-link-external-small" href="https://github.com/openshift/openshift-java-client">openshift-java-client</a>. The client library is available as artifact from maven central. Enable-openshift-ci is a maven project, we can therefore add the openshift-java-client to its classpath by simply using the following snippet in the it's pom:</p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag">&lt;dependency&gt;</span>
&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;groupId&gt;</span>com.openshift<span class="jive-xml-tag">&lt;/groupId&gt;</span>
&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;artifactId&gt;</span>openshift-java-client<span class="jive-xml-tag">&lt;/artifactId&gt;</span>
&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;version&gt;</span>2.0.0<span class="jive-xml-tag">&lt;/version&gt;</span>
<span class="jive-xml-tag">&lt;/dependency&gt;</span>
</code></pre><h2>A few classes</h2><ul><li><a class="jive-link-external-small" href="https://github.com/adietish/enable-openshift-ci/blob/master/src/main/java/com/openshift/client/example/enableci/Main.java">Main</a>: Bootstrapping</li><li><a class="jive-link-external-small" href="https://github.com/adietish/enable-openshift-ci/blob/master/src/main/java/com/openshift/client/example/enableci/Parameters.java">Parameters</a>: Parses, validates and holds the command line parameters</li><li><a class="jive-link-external-small" href="https://github.com/adietish/enable-openshift-ci/blob/master/src/main/java/com/openshift/client/example/enableci/OpenShiftCI.java">OpenShiftCI</a>: Creates the OpenShift CI, pushes your project to it</li></ul><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h2>Parameters: Talk to me via command line parameters</h2><p>To keep the example simple, we will stick to a GUI-less programm, that you control by command-line arguments. The <a class="jive-link-external-small" href="https://github.com/adietish/enable-openshift-ci/blob/master/src/main/java/com/openshift/client/example/enableci/Parameters.java">Parameters</a> class holds all parameters that were given at the command line. It will check them for validity and offer them for further processing.</p><p>What's important in this context is that the Parameters class will ensure that your project exists, that it is a <a class="jive-link-external-small" href="http://maven.apache.org/">maven</a> project and that it is shared with a <a class="jive-link-external-small" href="http://git-scm.com/">git</a> repository.</p><h2>OpenShiftCI: I want my CI instance!</h2><p>The <a class="jive-link-external-small" href="https://github.com/adietish/enable-openshift-ci/blob/master/src/main/java/com/openshift/client/example/enableci/OpenShiftCI.java">OpenShiftCI</a> class is where it all happens. It will connect to your <a class="jive-link-external-small" href="https://openshift.redhat.com/app/">OpenShift</a> account, create your Jenkins instance and push your project to it. The <a class="jive-link-external-small" href="https://github.com/adietish/enable-openshift-ci/blob/master/src/main/java/com/openshift/client/example/enableci/OpenShiftCI.java#L57">create()</a> method looks like this:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">IUser user = createUser();
IDomain domain = getOrCreateDomain(user);
IApplication application = getOrCreateApplication(project.getName(), domain);
IApplication jenkinsApplication = getOrCreateJenkins(domain);
waitForApplication(jenkinsApplication);
waitForApplication(application);
embedJenkinsClient(application);
deployToOpenShift(application);
System.out.println(<font color="red">"Done."</font>);
</code></pre><h2>Let's get in touch with OpenShift</h2><p>Before enable-openshift-ci can manipulate resources on OpenShift, it has to connect to it. It asks the <a class="jive-link-external-small" href="https://github.com/openshift/openshift-java-client/blob/master/src/main/java/com/openshift/client/OpenShiftConnectionFactory.java">OpenShiftConnectionFactory</a> for a new connection. </p><p>The first required parameter is the url of the OpenShift PaaS. You may either hard code it or ask the OpenShift configuration for it:</p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>new</b></font> OpenShiftConfiguration().getLibraServer()
</code></pre><p>The connection factory also asks for a meaningful client id. We will use <span style="color: #dd1144; font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; background-color: #ffffff;">"enable-openshift-ci"</span>. </p><p>Last but not least, you also have to give it your OpenShift credentials.</p><pre class="jive-pre"><code class="jive-code jive-java">String openshiftServer = <font color="navy"><b>new</b></font> OpenShiftConfiguration().getLibraServer();
IOpenShiftConnection connection = <font color="navy"><b>new</b></font> OpenShiftConnectionFactory().getConnection(<font color="red">"enable-openshift-ci"</font>, <font color="red">"&lt;user&gt;"</font>, <font color="red">"&lt;password&gt;"</font>, openshiftServer);
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Once you have your connection you can get your user instance which will allow you to create your the domain and the applications:</p><pre class="jive-pre"><code class="jive-code jive-java">IUser user = connection.getUser();
</code></pre><h2>Now I need a domain</h2><p>All resources on OpenShift are bound to a domain. We therefore have to make sure we have a domain in a first step, we either get the existing one or create a new one in <a class="jive-link-external-small" href="https://github.com/adietish/enable-openshift-ci/blob/master/src/main/java/com/openshift/client/example/enableci/OpenShiftCI.java#L81">getOrCreateDomain()</a>:</p><pre class="jive-pre"><code class="jive-code jive-java">IDomain domain = user.getDefaultDomain();
<font color="navy"><b>if</b></font> (domain == <font color="navy"><b>null</b></font>) <font color="navy">{</font>
&#160; domain = user.createDomain(<font color="red">"openshiftci"</font>);
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>In case we create a new domain, we have to make sure that OpenShift has our ssh-key. The ssh-key is required as soon as you deal with an OpenShift git-repository, the application logs etc (not when manipulating resources). To keep things simple, we will stick to the default ssh-key id_rsa.pub and add it to OpenShift in case it's not present yet. When adding the key, we will use a unique name which is the current time in millisecons in this simplified example:</p><pre class="jive-pre"><code class="jive-code jive-java">ISSHPublicKey key = <font color="navy"><b>new</b></font> SSHPublicKey(SSH_PUBLIC_KEY);
IOpenShiftSSHKey addedKey = user.getSSHKeyByPublicKey(key.getPublicKey());
<font color="navy"><b>if</b></font> (addedKey == <font color="navy"><b>null</b></font>) <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; user.putSSHKey(String.valueOf(System.currentTimeMillis()), key);
<font color="navy">}</font>
</code></pre><h2>Let's get an application for my project</h2><p>Now that we have a domain, we are ready to create an application for our project. </p><p>Our tool uses an constant name for the new project. It therefore checks if this application already exists:</p><pre class="jive-pre"><code class="jive-code jive-java">IApplication application = domain.getApplicationByName(<font color="red">"openshiftci"</font>);
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>If there's no such application, it will create a new one:</p><p><span class="n" style="font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff;">IApplication application</span><span style="font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff;"> </span><span class="o" style="font-weight: bold;">=</span><span style="font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff;"> </span><span class="n" style="font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff;">domain</span><span class="o" style="font-weight: bold;">.</span><span class="na" style="color: #008080;">createApplication</span><span class="o" style="font-weight: bold;">(</span><span class="n" style="font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff;">name</span><span class="o" style="font-weight: bold;">,</span><span style="font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff;"> </span><span class="n" style="font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; background-color: #ffffff;">ICartridge</span><span class="o" style="font-weight: bold;">.</span><span class="na" style="color: #008080;">JBOSSAS_7</span><span class="o" style="font-weight: bold;">);</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>If it already exists, <a class="jive-link-external-small" href="https://github.com/adietish/enable-openshift-ci/blob/master/src/main/java/com/openshift/client/example/enableci/OpenShiftCI.java#L106">getOrCreateApplication()</a> will check if it's a jbossas-7 application, since we are dealing with a maven aka java project. </p><pre class="jive-pre"><code class="jive-code jive-java">ICartridge.JBOSSAS_7.equals(application.getCartridge())
</code></pre><h2>OpenShift, gimme my CI instance!</h2><p>We now created an application that OpenShift builds whenever we push to it's git repository. The build is executed within the git push. To have a jenkins CI instance doing that work, we have to add a jenkins application and connect both. </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a class="jive-link-external-small" href="https://github.com/adietish/enable-openshift-ci/blob/master/src/main/java/com/openshift/client/example/enableci/OpenShiftCI.java#L123">getOrCreateJenkins()</a> first checks if there are any jenkins applications within your domain:</p><pre class="jive-pre"><code class="jive-code jive-java">List&lt;IApplication&gt; jenkinsApplications = domain.getApplicationsByCartridge(ICartridge.JENKINS_14);
<font color="navy"><b>if</b></font>(jenkinsApplications.isEmpty()) <font color="navy">{</font>
&#160;
&#160;&#160; ...
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>If it spots an jenkins instance, it will leave this step alone, it wont create another jenkins application. If there's none yet, it will create a new one and print it's url and credentials:</p><pre class="jive-pre"><code class="jive-code jive-java">IApplication jenkins = domain.createApplication(DEFAULT_JENKINS_NAME, ICartridge.JENKINS_14);
System.out.println(jenkins.getCreationLog());
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>We now have to make sure both applications are reachable. We will eventually <a class="jive-link-external-small" href="https://github.com/adietish/enable-openshift-ci/blob/master/src/main/java/com/openshift/client/example/enableci/OpenShiftCI.java#L116">wait</a> for them to become ready:</p><pre class="jive-pre"><code class="jive-code jive-java">Future&lt;Boolean&gt; applicationAccessible = application.waitForAccessibleAsync(WAIT_TIMEOUT);
<font color="navy"><b>if</b></font> (!applicationAccessible.get()) <font color="navy">{</font>
&#160;&#160; ...
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Once they are both ready, we can connect both and add the jenkins cartridge to our project application. Doing this tells OpenShift to build the project application within in the jenkins application. </p><p>The <a class="jive-link-external-small" href="https://github.com/openshift/openshift-java-client/blob/master/src/main/java/com/openshift/client/IEmbeddedCartridge.java">embedded cartridge</a> offers a creation log, that shows at what url the jenkins jobs may be reached at.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">IEmbeddedCartridge jenkinsClient = application.addEmbeddableCartridge(IEmbeddableCartridge.JENKINS_14);
System.out.println(jenkinsClient.getCreationLog());
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h2>We're almost there</h2><p>Enable-openshift-ci will now last but not least add the git repository of your application as remote to your local project repository. It will use the git command line to "git add remote":</p><pre class="jive-pre"><code class="jive-code jive-java">exec(<font color="red">"git remote add openshift -f "</font> + application.getGitUrl());
</code></pre><p><span class="o" style="font-weight: bold; font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; background-color: #ffffff;"><br/></span></p><p>To now have your project built on OpenShift, you'll have to make sure your pom has an openshift profile:</p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag">&lt;profile&gt;</span>
&#160;&#160;&#160; <span class="jive-xml-comment"><span>&lt;!-- When built in OpenShift the 'openshift' profile will be used when invoking mvn. --&gt;
&#160;&#160;&#160; &lt;!-- Use this profile for any OpenShift specific customization your app will need. --&gt;
&#160;&#160;&#160; &lt;!-- By default that is to put the resulting archive into the 'deployments' folder. --&gt;
&#160;&#160;&#160; &lt;!-- </span><a class="jive-link-external-small" href="http://maven.apache.org/guides/mini/guide-building-for-different-environments.html" target="_blank">http://maven.apache.org/guides/mini/guide-building-for-different-environments.html</a><span> --&gt;</span></span>
&#160;&#160;&#160; <span class="jive-xml-tag">&lt;id&gt;</span>openshift<span class="jive-xml-tag">&lt;/id&gt;</span>
&#160;&#160;&#160; <span class="jive-xml-tag">&lt;build&gt;</span>
&#160;&#160;&#160; &#160;&#160;&#160; <span class="jive-xml-tag">&lt;plugins&gt;</span>
&#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; <span class="jive-xml-tag">&lt;plugin&gt;</span>
&#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; <span class="jive-xml-tag">&lt;artifactId&gt;</span>maven-war-plugin<span class="jive-xml-tag">&lt;/artifactId&gt;</span>
&#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; <span class="jive-xml-tag">&lt;version&gt;</span>2.1.1<span class="jive-xml-tag">&lt;/version&gt;</span>
&#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; <span class="jive-xml-tag">&lt;configuration&gt;</span>
&#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; <span class="jive-xml-tag">&lt;outputDirectory&gt;</span>deployments<span class="jive-xml-tag">&lt;/outputDirectory&gt;</span>
&#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; <span class="jive-xml-tag">&lt;warName&gt;</span>ROOT<span class="jive-xml-tag">&lt;/warName&gt;</span>
&#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; <span class="jive-xml-tag">&lt;/configuration&gt;</span>
&#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; <span class="jive-xml-tag">&lt;/plugin&gt;</span>
&#160;&#160;&#160; &#160;&#160;&#160; <span class="jive-xml-tag">&lt;/plugins&gt;</span>
&#160;&#160;&#160; <span class="jive-xml-tag">&lt;/build&gt;</span>
<span class="jive-xml-tag">&lt;/profile&gt;</span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>You'll then simply merge the remote repo into you local repo and push it upstream. You'll do something like this:</p><pre class="jive-pre"><code class="jive-code jive-java">git add .
git commit -a -m <font color="navy">'deploying to openshift'</font>
git merge openshift/master -s recursive -X ours
git push openshift HEAD
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Pushing your project upstream will now trigger a jenkins build. </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>We're there, you now have a utility that will setup a jenkins for your project. We discussed all steps involved. </p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Comment by <a href="https://community.jboss.org/docs/DOC-19828">going to Community</a></p>

        <p style="margin: 0;">Create a new document in JBoss Tools at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2128">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>