Transcript
Page 1: Dev ops with liberty, maven and chef

Lab AAI-2901 DevOps with Liberty, Maven & Chef

Lab Instructions

February 2015

Page 2: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 2 of 35

Please Note IBM’s statements regarding its plans, directions, and intent are subject to change or withdrawal without notice at IBM’s sole discretion.

Information regarding potential future products is intended to outline our general product direction and it should not be relied on in making a purchasing decision.

The information mentioned regarding potential future products is not a commitment, promise, or legal obligation to deliver any material, code or functionality. Information about potential future products may not be incorporated into any contract. The development, release, and timing of any future features or functionality described for our products remains at our sole discretion.

Performance is based on measurements and projections using standard IBM benchmarks in a controlled environment. The actual throughput or performance that any user will experience will vary depending upon many factors, including considerations such as the amount of multipro-gramming in the user’s job stream, the I/O configuration, the storage configuration, and the workload processed. Therefore, no assurance can be given that an individual user will achieve results similar to those stated here.

Acknowledgements and Disclaimers

Availability. References in this presentation to IBM products, programs, or services do not imply that they will be available in all countries in which IBM operates.

The workshops, sessions and materials have been prepared by IBM or the session speakers and reflect their own views. They are provided for informational purposes only, and are neither intended to, nor shall have the effect of being, legal or other guidance or advice to any participant. While efforts were made to verify the completeness and accuracy of the information contained in this presentation, it is provided AS-IS without warranty of any kind, express or implied. IBM shall not be responsible for any damages arising out of the use of, or otherwise related to, this presentation or any other materials. Nothing contained in this presentation is intended to, nor shall have the effect of, creating any warranties or representations from IBM or its suppliers or licensors, or altering the terms and conditions of the applicable license agreement governing the use of IBM software.

All customer examples described are presented as illustrations of how those customers have used IBM products and the results they may have achieved. Actual environmental costs and performance characteristics may vary by customer. Nothing contained in these materials is intended to, nor shall have the effect of, stating or implying that any activities undertaken by you will result in any specific sales, revenue growth or other results.

© Copyright IBM Corporation 2015. All rights reserved.

U.S. Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

IBM, the IBM logo, ibm.com, Interconnect, WebSphere, and WebSphere Application Server are trademarks or registered trademarks of Interna-tional Business Machines Corporation in the United States, other countries, or both. If these and other IBM trademarked terms are marked on their first occurrence in this information with a trademark symbol (® or ™), these symbols indicate U.S. registered or common law trademarks owned by IBM at the time this information was published. Such trademarks may also be registered or common law trademarks in other coun-tries. A current list of IBM trademarks is available on the Web at “Copyright and trademark information” at www.ibm.com/legal/copytrade.shtml

The Chef™ Mark and Chef Logo are either registered trademarks/service marks or trademarks/ service marks of Chef, in the United States and other countries and are used with Chef Inc's permission. IBM is not affiliated with, endorsed or sponsored by Chef Inc.

Other company, product, or service names may be trademarks or service marks of others.

Page 3: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 3 of 35

Contents 1 OBJECTIVES ................................................................................................................................................................... 5

2 PREREQUISITE KNOWLEDGE ......................................................................................................................................... 5

3 CASE STUDY USED IN THIS LAB ..................................................................................................................................... 5

3.1 DEMONSTRATE CONTINUOUS DELIVERY USING WEBSPHERE LIBERTY AND OPEN SOURCE TOOLS ......................................................... 5

4 STEP-BY-STEP INSTRUCTIONS ........................................................................................................................................ 5

4.1 USING WEBSPHERE APPLICATION SERVER DEVELOPER TOOLS WITH MAVEN PROJECTS .................................................................... 5 4.1.1 Start Eclipse .......................................................................................................................................................... 5 4.1.2 Set the workspace preferences for use with Maven ............................................................................................. 5 4.1.3 Clone the Acme Air Git repository ......................................................................................................................... 6 4.1.4 Import the Maven based AcmeAir projects .......................................................................................................... 7 4.1.5 Add Liberty runtime and server to the workspace ................................................................................................ 8 4.1.6 Complete the server configuration for the AcmeAir application ......................................................................... 13 4.1.7 Start the WebSphere Liberty Profile server and AcmeAir application ................................................................. 14 4.1.8 Initialize the application by loading the flight data-set ...................................................................................... 14 4.1.9 Explore AcmeAir .................................................................................................................................................. 15 4.1.10 Update AcmeAir .................................................................................................................................................. 15

4.2 USING LIBERTY MAVEN PLUG-IN FOR INTEGRATION TESTS ........................................................................................................ 16 4.2.1 Enable activation of integration tests ................................................................................................................. 16 4.2.2 Enable Build Helper Maven plug-in ..................................................................................................................... 16 4.2.3 Enable and configure Liberty Maven plug-in ...................................................................................................... 17 4.2.4 Enable and configure Maven Failsafe plug-in ..................................................................................................... 18 4.2.5 Create a Run Configuration for AcmeAir integration tests .................................................................................. 18 4.2.6 View the test reports .......................................................................................................................................... 20 4.2.7 Push the pom.xml change to Git ......................................................................................................................... 20

4.3 CONTINUOUS INTEGRATION WITH JENKINS ............................................................................................................................ 20 4.3.1 Create a build job for AcmeAir ............................................................................................................................ 20 4.3.2 Examine the build job results .............................................................................................................................. 23 4.3.3 Triggering a build on commit .............................................................................................................................. 24

4.4 CONTINUOUS DEPLOYMENT WITH CHEF................................................................................................................................ 26 4.4.1 Chef ..................................................................................................................................................................... 26 4.4.2 Liberty Profile Collectives .................................................................................................................................... 27 4.4.3 Web Admin Center .............................................................................................................................................. 27 4.4.4 Chef cookbook for Acme Air ................................................................................................................................ 27 4.4.5 Create the Acme Air cookbook to deploy to a single server ................................................................................ 28 4.4.6 Bootstrap Chef node ........................................................................................................................................... 31 4.4.7 Configure the Chef node ..................................................................................................................................... 31 4.4.8 Populate the Chef node ....................................................................................................................................... 32 4.4.9 Populate the Chef node automatically with Jenkins ........................................................................................... 33 4.4.10 Provision three Acme Air application servers ...................................................................................................... 34 4.4.11 Provision a collective containing three Acme Air application servers ................................................................. 35

4.5 YOU CAN NOW SEE CONTINUOUS DELIVERY DEMONSTRATED ..................................................................................................... 35

Page 4: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 4 of 35

Key Reference Notes for Lab AAI-2901

Passwords and shortcuts List key id/password information for your lab (examples below). Also include any shortcuts, naming conventions, installation locations, locations of code snippets, and any other information about the lab environment that will help the user to have a suc-cessful experience and to complete the lab on time. If the category does not apply to your lab, simply remove it.

Password information

VMware images called workstation, server and template

User: liberty Password: liberty

Code Snippets

/home/liberty/snippets

Naming conventions

File system shortcuts

Page 5: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 5 of 35

1 Objectives In this lab, you will learn:

How to make a change to a web application running in the WebSphere Liberty Profile and push

that change all the way from a developer's Eclipse IDE to a production environment.

You will use Apache Maven with the WebSphere Liberty Maven plugin as the build tool.

You will use Git for source control.

You will use Jenkins for continuous integration.

You will use Chef™ to push a new build to the production environment.

2 Prerequisite Knowledge Basic Linux knowledge

Familiarity with the Eclipse IDE

Basic Apache Maven understanding

Basic understanding of continuous integration and deployment concepts

3 Case Study used in this Lab

3.1 Demonstrate continuous delivery using WebSphere Liberty and open source tools

Show continuous delivery using the WebSphere Liberty Profile in an open source tool environment.

4 Step-by-Step Instructions

This lab will make use of three VMware images:

Workstation This is where you will use Eclipse and Apache Maven to compile and test the Acme

Air application.

Server This holds a Git repository, a Jenkins server and a Chef Server which we will use in this

lab to demonstrate continuous delivery.

Template This is used as the 'production' server where the Acme Air application will be de-

ployed.

4.1 Using WebSphere Application Server Developer Tools with Maven projects

You will use WebSphere Application Server Developer Tools (WDT) to clone a Git repository contain-

ing the AcmeAir application and import it as a set of Maven-based projects. We will use WDT to de-

ploy the application to the Liberty server and perform incremental updates. The Eclipse version

used in this lab has WDT pre-installed.

4.1.1 Start Eclipse

In VMware workstation, switch to the tab marked 'workstation'. This has a wallpaper of pond

lilies.

Open Eclipse by clicking on the purple Eclipse icon. The Eclipse splash screen will appear and the

Workspace Launcher dialog will appear displaying a default workspace of

/home/liberty/workspace.

Click OK.

4.1.2 Set the workspace preferences for use with Maven

The Maven integration in Eclipse defaults to settings familiar to Eclipse users developing Java EE

applications. We're going to use settings more familiar to Maven users.

Select Window > Preferences

Expand Java EE then expand Maven then select Maven Project Settings.

Page 6: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 6 of 35

Click on the “Set all Maven values” button (at the top right side of the window).

The values have now changed to reflect a project structure familiar to users of Maven.

Click OK.

Select Window > Preferences > Java EE > Project

Uncheck the “Add project to an EAR” checkbox at the top of the window.

Click OK.

Eclipse is now ready to work with Maven projects and WebSphere Liberty Profile.

4.1.3 Clone the Acme Air Git repository

The application you're going to use is hosted in a Git repository on the server VM. You need to

clone this repository into Eclipse.

Open the Git Repositories view. Select Window > Show View > Other …

Expand Git then select Git Repositories and click OK.

The Git Repositories view appears in the lower right of the window.

In the Git Repositories view, click on “Clone a Git Repository”.

In the URI field, type in ssh://server/home/liberty/acmeair.git

Page 7: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 7 of 35

Click Next. If you see a pop-up window asking about the authenticity of host server click Yes.

Leave all the branches selected and click Next.

Leave the default value for the “Directory” field and click Finish.

The Git repository is now cloned. In the Git Repositories view expand “acmeair” and you'll

see the following:

4.1.4 Import the Maven based AcmeAir projects

The acmeair repository contains five Maven based projects. You will now import them into Eclipse.

Select File > Import > Maven > Existing Maven Projects and click Next.

In the “Root Directory” field, type in /home/liberty/git/acmeair then click on the Refresh

button.

Page 8: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 8 of 35

Press Finish to import the projects. The Enterprise Explorer view will look like this:

The acmeair project represents the root of the Git repository. A project has been created for

each of directories beneath this.

Now run Maven to build the projects and create the application artifacts: right click on the pro-

ject acmeair, and select Run As > Maven install.

4.1.5 Add Liberty runtime and server to the workspace

You're now going to create a WebSphere Liberty Profile server to run the application from Eclipse:

Select Window > Preferences

Expand Server then select Runtime Environments.

Click Add > WebSphere Application Server V8.5 Liberty Profile and click Next.

Page 9: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 9 of 35

In the “Path” field under “Installation folder”, type in or select the /home/liberty/wlp di-

rectory. Notice how notice Eclipse looks ahead for you as you type. As soon as you type the 'l' of

liberty you will be given /home/liberty/wlp as a choice for auto-completion. You should now

see:

Click Finish. You now have a new Server runtime environment called “WebSphere Application

Server V8.5 Liberty Profile” listed in the Server Runtime Environments window.

Click OK to close the Server Runtime Environment preference page.

You are now ready to create a Liberty Profile server from the runtime environment you have just

created.

Select File > New > Other...

In the wizard expand Server and select Server and click Next.

Page 10: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 10 of 35

Scroll down the list of server types and select “WebSphere Application Server V8.5 Liberty Pro-

file”.

Click Next.

Page 11: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 11 of 35

In “Server name”, type in AcmeAirDemo. Leave the server configuration. Click Next.

Page 12: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 12 of 35

You will see one application is available to use with the server. Select the acmeair-webapp appli-

cation, then click Add >

Click Finish.

Page 13: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 13 of 35

Verify in the server has been created by selecting the “Servers” view, then expand the “Web-

Sphere Application Server V8.5 Liberty Profile at localhost” entry and the “Server Configuration”

entry after that. You should see the following.

Double click the “Server Configuration [server.xml]” to open the server.xml configuration file.

By default it will open in the design view. Go to the “Source” tab of the editor to see the underlying

XML.

The AcmeAir application makes use of the Java Persistence API (JPA) and the JAX-RS API. These

features of the WebSphere Liberty Profile have been added to the Feature Manager configuration

automatically.

The localConnector-1.0 feature enables the server to be accessed through Java Management Exten-

sions (JMX). This feature allows Eclipse to control the server.

4.1.6 Complete the server configuration for the AcmeAir application

You will now replace the <httpEndpoint> element and add further configuration. The XML snippets

below are provided in

/home/liberty/snippets/server.xml-snippet

so you can copy-paste them into the server.xml.

Replace the <httpEndpoint> element with:

<httpEndpoint id="defaultHttpEndpoint"

host="*"

httpPort="9080" >

<tcpOptions soReuseAddr="true"/>

</httpEndpoint>

host=”*” means the server will listen on all adapters, rather than just localhost which is the default.

The soReuseAddr flag allows for the reuse of the HTTP Port if the underlying socket is in the

TIME_WAIT when the server restarts. This allows the server to restart immediately rather than wait

for the socket to timeout.

Add the database configuration: the JDBC driver for the Derby database which refers to a library

containing the Derby databse files themselves.

<jdbcDriver id="DerbyEmbedded" libraryRef="DerbyLib"/>

<library filesetRef="DerbyFileset" id="DerbyLib"/>

<fileset id="DerbyFileset"

dir="/home/liberty/.m2/repository/org/apache/derby/derby/10.10.1.1"

includes="derby*.jar"/>

Add a transactional and non-transactional Data Source configuration to the server.xml to config-

ure how database connections are made.

<dataSource id="jdbc/acmeairdatasource"

jdbcDriverRef="DerbyEmbedded"

Page 14: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 14 of 35

jndiName="jdbc/acmeairdatasource">

<properties createDatabase="create" databaseName="acmeairdatabase"/>

</dataSource>

<dataSource id="jdbc/acmeairdatasourcenonjta"

jdbcDriverRef="DerbyEmbedded"

jndiName="jdbc/acmeairdatasourcenonjta" transactional="false">

<properties createDatabase="create" databaseName="acmeairdatabase"/>

</dataSource>

The server is now configured to run the AcmeAir application.

4.1.7 Start the WebSphere Liberty Profile server and AcmeAir application

In the “Servers” view select the “WebSphere Application Server V8.5 Liberty Profile at lo-

calhost” (this is the server).

Click on the “Start the Server” button

After several seconds the Console view will automatically come to the front and display that the

Application acmeair-webapp has started. Also included is a link to the application's URL. Click

the URL to verify the application is active.

Note: if there is no internet connection, there will be a minor formatting problem with the Ac-

tions bar of the application.

4.1.8 Initialize the application by loading the flight data-set

Go to http://localhost:9080/acmeair-webapp/rest/api/loader/loadSmall address. This will cause

the application database to be populated with sample data. This request will take several sec-

onds to complete. Wait until you see “Sample data loaded” message in the browser.

Page 15: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 15 of 35

4.1.9 Explore AcmeAir

Login to the application by clicking on the “Login” action.

A login dialog will come up with the user name and password already in the dialog. Press OK.

Next, click on the “Flights” action and type in New York under “Leave From” and Paris under

“Arrive At”.

Click on “Find Flights” or “Browse Flights” button.

Pick any outbound flight and select it by clicking on it.

Similarly, pick any return flight and select it by clicking on it.

Click on “Book Select Flights” button.

4.1.10 Update AcmeAir

The AcmeAir application has one small problem. It does not return to the welcome page after press-

ing on the “Logout” action. This bug, therefore, can reveal private customer data or flight infor-

mation to others. You will use the WDT incremental update feature to fix this bug without restarting

the application.

To replicate the problem click on the “Account” action and then click on the “Logout” action.

The screen will remain displaying the customer profile information.

The problem lies in the acmeair-common.js JavaScript file. In Eclipse, while the application is

still running, open the acmeair-webapp/src/main/webapp/js/acmeair-common.js file.

Click on the logout function in the Outline view.

Add window.location='index.html'; after line 89. The new function should look like the fol-

lowing:

function logout() {

updateLoggedInUserWelcome();

var loggedinuser = dojo.cookie("loggedinuser");

if (loggedinuser == null) {

return;

}

dojo.xhrGet({

content : {

login: loggedinuser

},

url: 'rest/api/login/logout',

load: function(response, ioArgs) {

if (response != 'logged out') {

// TODO: why isn't error function being called in this case

alert('error logging out, response: ' + response);

return;

}

dojo.cookie("loggedinuser", null, {expires: -1});

Page 16: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 16 of 35

updateLoggedInUserWelcome();

window.location='index.html';

},

error: function(response, ioArgs) {

alert('error logging out, response: ' + response);

}

});

}

Save the file.

To verify this fix, return to the application in Firefox and refresh the page.

Login to the application again and click on the “Account” action.

Click on the “Logout” action. This time the application should return to the welcome page.

4.2 Using Liberty Maven plug-in for integration tests

The AcmeAir application has acmeair-itests project that contains integration tests. The integration

tests exercise the REST API of the application. Since the tests require an application to be running,

we will configure the project to automatically start the Liberty server running the application before

the tests and stop the server afterwards.

The acmeair-itests project is pre-configured to create a Liberty user directory with the AcmeAir ap-

plication, server configuration, and its dependencies such as the Derby database. To start and stop

the Liberty server we will use the Liberty Maven plug-in. To prevent port conflicts, we will combine it

with the Build Helper Maven plug-in to find and assign a free port to the Liberty server. We will use

the Maven profile feature to activate the execution of the integration tests.

4.2.1 Enable activation of integration tests

In section 4.1.4 you ran Maven, specifically the Maven 'install' goal. This isn't yet configured to run

the integration tests. You're going make a change so the integration tests will run if the Liberty in-

stallation directory is passed to Maven in the wlpInstallDir property.

Open the acmeair-itests/pom.xml file.

Click on the “pom.xml” tab.

Find and remove the XML comment marks (<!-- and -->) around the following block in

pom.xml:

Save the changes.

When the wlpInstallDir property is specified, the run-its Maven profile will be activated. The

profile will enable a few Maven plug-in that allow the integration tests to run after the following

steps.

4.2.2 Enable Build Helper Maven plug-in

The Build Helper Maven plug-in is used to find an available network port. This port will be used

for the HTTP endpoint of the Liberty server. The available port is stored under the wlp.http.port

Maven property.

Page 17: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 17 of 35

In the same editor as the previous step, find and remove the XML comment marks (<!-- and --

>) around the following block in pom.xml:

Save the changes.

4.2.3 Enable and configure Liberty Maven plug-in

The Liberty Maven plug-in is used to start the Liberty server before integration tests are executed

and stop the server after the tests are done. The plug-in will pass the wlp.http.port property as-

signed by the Build Helper plug-in to the Liberty server as a bootstrap property. The plug-in is also

configured to use a Liberty user directory created under the project's target/ directory.

In the same editor as in the previous step, find and remove the XML comment marks (<!-- and

-->) around the following block in pom.xml:

Save the changes.

This change causes the Liberty server to be started prior to the integration tests being run and

stopped just afterwards.

Page 18: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 18 of 35

4.2.4 Enable and configure Maven Failsafe plug-in

The Maven Failsafe plug-in is used to execute the integration tests. The tests will exercise the REST

API of the application. To do so, the tests must be passed the port of the Liberty server. The value

of the wlp.http.port property assigned by the Build Helper plug-in is passed as the httpPort sys-

tem property to the tests.

In the same editor as before, find and remove the XML comment marks (<!-- and -->) around

the following block in pom.xml:

Save the changes.

4.2.5 Create a Run Configuration for AcmeAir integration tests

In order to run the AcmeAir integration tests from Eclipse we need to create a new Eclipse run con-

figuration. We will also need to pass the location of the Liberty profile installation to the Liberty Ma-

ven plug-in so it can start and stop the server.

Select “Run configurations...” option from Run menu.

Locate the “Maven Build” entry in the left hand list: Right-click it and select

New.

Type run-itests in the “Name” field.

Set the base directory by clicking on the “Browse Workspace...” button and select the acmeair

project.

Type install in the “Goals” field.

Create a new parameter by clicking on the “Add..” button. Type in wlpInstallDir as the pa-

rameter name and /home/liberty/wlp as the parameter value.

Page 19: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 19 of 35

Click Apply button.

Click Run button to run the integration tests. The tests should complete successfully.

Examine the console output to see the Liberty Maven plug-in starting the server before the tests

run:

Page 20: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 20 of 35

Examine the console output for to see the Liberty Maven plug-in stopping the server after the

tests:

4.2.6 View the test reports

The integration tests are located in the acmeair-itests/src/test/java/ directory. After the tests run,

the test reports are generated under the acmeair-itests/target/failsafe-reports/ directory. To view

the test reports:

Expand the acmeair-itests project. Then expand the target directory within that.

Right client the target directory and select Refresh. The failsafe-reports directory will appear.

Double click the TEST-com.acmeair.test.AcmeAirTest.xml file to see the report.

4.2.7 Push the pom.xml change to Git

In preparation for the 'Continuous Integration with Jenkins' section we will need to push the chang-

es you've just made to the acmeair-itests pom.xml to Git so the itests can be run in a Jenkins build.

In Eclipse, right click on the acmeair project and select Team > Commit...

Type in Configure acmeair-itests as the commit message.

Under “Files” ensure only the acmeair-itests/pom.xml file is selected by unchecking the acmeair-

webapp/src/main/webapp/js/acmeair-common.js file.

Press on the “Commit and Push” button.

The change is pushed to the Git repository and a results window is displayed. Click on OK.

4.3 Continuous integration with Jenkins

We will use Jenkins to create and run a build job for the AcmeAir application. The job will checkout

the application code from Git, compile the application, execute the tests, and publish the build arti-

facts. The job will poll the Git repository every two minutes and automatically start a build if any

changes are detected.

4.3.1 Create a build job for AcmeAir

Open the Firefox web browser.

Page 21: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 21 of 35

Go to http://server:9080/jenkins/ address. This may take a few seconds the first time.

Click on “New item”

Type in AcmeAir tests as the “Item name”.

Select “Build a free-style software project” option.

Click OK.

Under “Source Code Management” choose Git.

In the Repository URL field, enter ssh://liberty@server/home/liberty/acmeair.git

Scroll down until you find “Build Triggers”. Select “Poll SCM”.

Page 22: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 22 of 35

Under “Schedule” enter H/2 * * * *. That will cause the Git repository to be polled every two

minutes.

Under “Build” click on “Add build step” button and choose “Invoke top-level Maven targets”.

Under “Maven Version” choose apache_maven.

Under “Goals” type in clean install.

Click on Advanced button

Under “Properties” enter wlpInstallDir=/opt/liberty/wlp

Under “Post-build Actions” click on “Add post-build actions” button and choose “Archive the arti-

facts”.

Under “Files to archive” enter acmeair-webapp/target/*war, acmeair-

itests/src/main/resources/servers/acmeair/*, acmeair-itests/target/usr/shared/**

Click on “Add post-build actions” button again and choose “Publish Junit test result report”.

Under “Test report XMLs” enter acmeair-itests/target/failsafe-reports/*.xml

Click Save.

Page 23: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 23 of 35

After a minute or two a build should automatically start. It will appear in the Build History table.

If not, click on the Build Now link.

4.3.2 Examine the build job results

Click on the build under “Build History” table.

Page 24: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 24 of 35

Once the build completes and is successful you should see the screen below. The acmeair-

webapp-1.0-SNAPSHOT.war file should appear under “Build Artifacts” and “Test Result” should

indicate no failures.

If the build is still running you can click on “Console Output” to see the live output of the build

job.

Click on “Test Result” to examine the results of the integration tests.

4.3.3 Triggering a build on commit

We configured Jenkins to poll the Git repository for any changes every two minutes. If any

changes are detected, Jenkins will start a new build job. We will commit and push the change

we made in the Update AcmeAir step to trigger a new build.

Switch back to Eclipse.

In Eclipse, right click on the acmeair project and select Team > Commit...

Type in Fix welcome page refresh on logout as the commit message.

Under “Files” ensure only the acmeair-webapp/src/main/webapp/js/acmeair-common.js file is

selected.

Page 25: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 25 of 35

Press on the “Commit and Push” button.

On the “Push Results” window press OK button.

Switch to Firefox and go to http://server:9080/jenkins/job/AcmeAir tests/ address.

Page 26: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 26 of 35

Within two minutes a new build should appear under the “Build History” table.

Click on the new build and verify that the right commit message appears under “Changes”.

4.4 Continuous deployment with Chef

4.4.1 Chef

Chef is an open source configuration management tool by Chef Inc. (formerly Opscode). It enables

you to describe your IT infrastructure ‘as code’. You write ‘Recipes’ which themselves manipulate

‘Resources’, using the Ruby language and a Ruby Domain Specific Language, specific to Chef. These

artifacts are brought together into a ‘Cookbooks’.

The impact of describing your IT infrastructure as code, is you can treat it like any other code – i.e.

store it in a version control system such as Git, Subversion, Rational Team Concert etc. Being code,

it can be tested, which implies standing up an instance of your infrastructure as a test case. This

leads to your infrastructure being as “versionable, testable and repeatable as application code.” See

https://www.chef.io/chef/ for more information.

Page 27: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 27 of 35

Chef cookbooks are developed on your ‘Chef Workstation’. You upload them to a ‘Chef Server’ which

also holds metadata about each of the machines (called ‘nodes’ in Chef). The ‘Chef Client’ is an

agent that runs on each node and periodically connects to the Chef Server. It retrieves updates to

its metadata then runs the list of recipes described in the metatdata. The result is for the configura-

tion of the various software components managed by Chef on the node, to converge to a desired

state.

4.4.2 Liberty Profile Collectives

A collective is a set of Liberty servers in a single administrative domain. It consists of at least one

server designated a ‘collective controller’ and many servers as ‘collective members’. A client con-

nects to the collective controller to send administrative commands to any single server or group of

servers in the collective. Server status and other monitored characteristics can be retrieved. Having

a single point to connect to simplifies administrating large numbers of servers.

4.4.3 Web Admin Center

The Web Admin Center feature is a web front end for managing Liberty profile servers and applica-

tions. We will be using it to give us a view of the Liberty profile servers we manage in the ‘template’

VM.

4.4.4 Chef cookbook for Acme Air

In this section you will create a Chef Cookbook for the Acme Air application. You will make use of

the WebSphere Liberty Profile cookbook and others to create:

1) a single Liberty profile server on the ‘template’ VM, and have it run the Acme Air application,

2) a set of single servers independently running the application,

3) A Liberty profile ‘collective’ consisting of a cluster of servers all of them running the applica-

tion except for one acting as the collective controller which will run the Admin Center.

A Chef cookbook encapsulates all the configuration artifacts related to a particular piece of software

– for example ‘apache2’ for the Apache HTTP server or ‘mysql’. These can be found on the Chef Su-

permarket at https://supermarket.chef.io/dashboard

When you develop your own Chef cookbook your development environment will consist of a ‘chef-

repo’ which contains other cookbooks, ones that your cookbook depends on, for example, and other

artifacts such as certificates, data bags and environments. You can read more about the setting up

Chef on your ‘workstation’ on the Chef website: https://docs.chef.io/workstation.html

Page 28: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 28 of 35

The ‘workstation’ VM you have been using already contains a ‘chef-repo’ with the required struc-

ture:

It is located in /home/liberty/chef-repo

4.4.5 Create the Acme Air cookbook to deploy to a single server

We’ll now add a cookbook to the chef-repo to install the ‘acmeair’ application.

Open a terminal window.

Execute “cd /home/liberty/chef-repo” command to change directory to chef-repo directory.

Execute “knife cookbook create acmeair” command to create an empty cookbook.

Execute “gedit /home/liberty/chef-repo/cookbooks/acmeair/metadata.rb” command to edit the

metadata file of the cookbook. We will add a dependency on the Liberty cookbook. Append the

following line to the end of file:

depends "wlp"

Page 29: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 29 of 35

Save the changes and close the editor.

We’re going to represent a server as a Chef ‘resource’ and ‘provider’. The resource defines the inter-

face and the provider is the implementation.

Execute “gedit /home/liberty/chef-repo/cookbooks/acmeair/resources/server.rb”

Add the following code (code is in /home/liberty/snippets/acmeair_single-

server/acmeair/resources/server.rb if you wish to copy-paste)

attribute :server_name, :kind_of => String, :default => nil

attribute :http_port_num, :kind_of => Integer, :default => 9081

attribute :https_port_num, :kind_of => Integer, :default => 9444

actions :install

default_action :install

Save the changes and close the editor.

Now for the provider:

Execute “gedit /home/liberty/chef-repo/cookbooks/acmeair/providers/server.rb”

Insert the following code (code is in /home/liberty/snippets/acmeair_single-

server/acmeair/providers/server.rb)

First: code to ensure the ‘template’ node has the necessary directories created for the server:

action :install do

liberty_dir = "#{node[:wlp][:base_dir]}/wlp"

# create directories for shared resources and server definition

["#{liberty_dir}/usr/shared/resources/derby/",

"#{liberty_dir}/usr/servers/#{new_resource.server_name}/",

"#{liberty_dir}/usr/servers/#{new_resource.server_name}/apps/"].each do | name |

directory name do

mode 00775

owner node[:wlp][:user]

group node[:wlp][:group]

action :create

recursive true

end

end

Ensure the files published from the Jenkins build are copied to the server:

jenkins_base_url =

"http://server:9080/jenkins/job/AcmeAir%20tests/lastSuccessfulBuild/artifact"

# mapping of remote resources to local files

url_map = {

"#{liberty_dir}/usr/shared/resources/derby/derby.jar" => "#{jen-

kins_base_url}/acmeair-itests/target/usr/shared/resources/derby/derby.jar",

"#{liberty_dir}/usr/servers/#{new_resource.server_name}/server.xml" => "#{jen-

kins_base_url}/acmeair-itests/src/main/resources/servers/acmeair/server.xml",

"#{liberty_dir}/usr/servers/#{new_resource.server_name}/bootstrap.properties"

=> "#{jenkins_base_url}/acmeair-

itests/src/main/resources/servers/acmeair/bootstrap.properties",

"#{liberty_dir}/usr/servers/#{new_resource.server_name}/apps/acmeair.war" =>

"#{jenkins_base_url}/acmeair-webapp/target/acmeair-webapp-1.0-SNAPSHOT.war"

}

Page 30: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 30 of 35

# download each file and place it in right directory

url_map.each do | file, url |

remote_file file do

source url

user node[:wlp][:user]

group node[:wlp][:group]

action :create_if_missing

end

end

Create a bootstrap.properties file specific to the server we’re creating:

wlp_bootstrap_properties "set bootstrap.properties" do

server_name "#{new_resource.server_name}"

properties "httpPort" => "#{new_resource.http_port_num}", "httpsPort" =>

"#{new_resource.https_port_num}"

action :set

end

Start the server:

# start server if it is not running already

wlp_server "#{new_resource.server_name}" do

action :start

end

The database of flight information needs to be initialized on creation. The Acme Air application

does this when you hit the rest/api/loader/loadSmall URL. This code will automate that:

# populate the database

bash "populate db" do

user node[:wlp][:user]

group node[:wlp][:group]

code <<-EOH

for retry in {1..10}

do

wget

http://localhost:#{new_resource.http_port_num}/acmeair/rest/api/loader/loadSmall -O

-

if [ "$?" = 0 ]; then

break

else

echo "Retrying ... attempt $retry"

sleep 1

fi

done

EOH

end

end

Save the changes and close the editor.

We’ve created a resource and a provider, now we need a recipe for the Chef Client to run to ensure

the server resource is created.

Execute “gedit /home/liberty/chef-repo/cookbooks/acmeair/recipes/default.rb” and add this

code to create the server resource:

Page 31: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 31 of 35

include_recipe "wlp::default"

acmeair_server "create server acmeair1" do

server_name "acmeair1"

action :install

end

We need to upload the cookbook to the Chef Server. Execute “knife cookbook upload acmeair”

on the command line to add the AcmeAir cookbook to the Chef server.

4.4.6 Bootstrap Chef node

The ‘template’ VM is accessible from the ‘workstation’ VM via ssh. But there is no Chef Client in-

stalled there yet. To bootstrap the ‘template’ VM as a Chef Node, which will install the chef-client on

the VM and add the VM as a node in the Chef Server:

Execute “knife bootstrap template --sudo -x liberty” command to register the “template” node

with the Chef server.

Verify that the “template” node is properly registered with the Chef server by executing “knife

node list” command. This command should return one node named “template”.

4.4.7 Configure the Chef node

Now that we have created the AcmeAir cookbook and registered the “template” node, we have to

instruct Chef how to configure the node in order to run the application. For example, we need to

specify the location of the Liberty profile and the Java installation.

To configure the “template” node configuration on the Chef server run the following command

“knife node edit template -e gedit”. This will bring up an editor window. Replace the contents

with the following (the text is in the /home/liberty/snippets/node-conf file):

Page 32: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 32 of 35

{

"name": "template",

"chef_environment": "_default",

"normal": {

"wlp": {

"archive": {

"accept_license": "true",

"version_yaml": "http://server:9111/files/index.yml"

}

},

"java": {

"install_flavor": "ibm",

"ibm": {

"url": "http://server:9111/files/ibm-java-x86_64-sdk-7.1-0.0.bin",

"accept_ibm_download_terms": "true"

}

},

"tags": [

]

},

"run_list": [

"recipe[apt]",

"recipe[acmeair]"

]

}

Save the changes and close the editor.

You should see “Saving updated normal, run_list on node template” message in the terminal

window if the update was successful.

4.4.8 Populate the Chef node

Execute “knife ssh name:template sudo chef-client” on the command line. This will cause the

chef-client command to run on the “template” node. Chef-client will execute a set of cookbooks

on the node. It will use the Java community cookbook to install IBM JRE, use the Liberty cook-

book to install Liberty server, and execute the AcmeAir cookbook to setup Liberty server defini-

tion for the application. Since chef-client will be running for the first time, it will take a while to

complete. The command should complete successfully and look like the following.

Page 33: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 33 of 35

At this point, the AcmeAir application should be fully deployed and running on the “template”

node. To verify, switch to Firefox and open the http://template:9081/acmeair/ address.

4.4.9 Populate the Chef node automatically with Jenkins

We will create a new Jenkins build job that will invoke chef-client on the “template” node to update

it, only after all the integration tests pass.

Switch to Firefox and open the http://server:9080/jenkins address.

Click on “New item”.

Type in AcmeAir-Chef as the “Item name”.

Select “Build a free-style software project” option.

Click OK.

Under “Build” click on “Add build step” button and choose “Send files or execute commands over

SSH”.

Choose template in Name field under “SSH Server”.

Set the 'Exec command” to: sudo chef-client. You will probably see a warning saying 'Either

Source files, Exec command or both must be supplied', you can safely ignore this.

Click Save.

Next, we will need to update the existing “AcmeAir tests” job to trigger the “AcmeAir-Chef” job.

Page 34: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 34 of 35

In Firefox open the http://server:9080/jenkins/job/AcmeAir tests/configure address to edit the

“AcmeAir tests” job.

Under “Post-build Actions” click on “Add post-build action” and choose “Build other projects”.

Under “Projects to build” type in AcmeAir-Chef.

Click Save.

Verify that the “AcmeAir tests” jobs triggers the “AcmeAir-Chef” job by requesting a new “AcmeAir

tests” build.

Click on “Build Now”.

Next, click on “Back to Dashboard” to view the build queue and status of the jobs.

4.4.10 Provision three Acme Air application servers

To reconfigure the ‘template’ VM to run multiple Liberty profile servers that are unconnected by

any Collective administrative mechanism, a change to the recipe is required:

Execute “gedit /home/liberty/chef-repo/cookbooks/acmeair/recipes/default.rb” and modify it to

loop around the acmeair_server resource block:

include_recipe "wlp::default"

servers = node[:acmeair][:servers][:application]

for server_num in 1..servers

acmeair_server "create server acmeair#{server_num}" do

server_name "acmeair#{server_num}"

http_port_num 9080+server_num

https_port_num 9443+server_num

action :install

end

end

Alternatively, a patch file is available in the /home/liberty/snippets directory. Change directory

to /home/liberty/chef-repo/cookbooks

Then execute “patch –p1 < /home/liberty/snippets/acmeair_single-to-multiple.patch”

Add a property to the node metadata giving the number of servers to create.

Execute “knife node edit template -e gedit”.

Add the “acmeair” section directly below the line that reads “normal”

"acmeair": {

"servers": {

"application": 3,

}

},

Re-run the chef client by executing: “knife ssh name:template sudo chef-client” on the com-

mand line. Now try the Acme Air web page for each of the Liberty profile servers running Acme

Air:

http://template:9081/acmeair/

http://template:9082/acmeair/

http://template:9083/acmeair/

Page 35: Dev ops with liberty, maven and chef

© 2014, 2015 Copyright IBM Corporation Page 35 of 35

4.4.11 Provision a collective containing three Acme Air application servers

You’re now going to create a server to host the Web Admin Center application and act as a Collec-

tive Controller to three Liberty profile servers running Acme Air.

Change directory to /home/liberty/chef-repo/cookbooks

Apply the patch to move from running multiple single servers to running a collective:

“patch –p1 < /home/liberty/snippets/acmeair_multiple-to-collective.patch”

Note: if you haven’t quite followed all the instructions exactly then you can instead replace the

/home/liberty/chef-repo/cookbooks/acmeair directory with

/home/liberty/snippets/acmeair_collective/acmeair

This update does the following:

Adds libraries/serverconfig.rb which assists in creating a library to help with adding <include>

and <feature> elements to the server.xml configuration file.

Adds the controller resource and provider to represent the Liberty profile server acting as a

Collective Controller and Web Admin Center.

Makes a change to the server resource and provider so that if a collective_port_num is provided

then the server is joined to a collective by connecting to the collective controller listening on

that port.

Modifies recipes/default.rb so a controller is created

Next, edit the node metadata to set the controller property.

Execute “knife node edit template –e gedit”

Modify the acmeair section to read:

"acmeair": {

"servers": {

"application": 3,

"collective": 1

}

},

Re-run the chef client by executing: “knife ssh name:template sudo chef-client” on the command

line. Now try the Admin Center at http://template:9080/adminCenter using the username/password

of admin/adminpwd. Click on the Explore tool to view the running servers, the cluster they are a

part of and the application (AcmeAir) running on them.

4.5 You can now see continuous delivery demonstrated

In this Lab session you’ve created a continuous delivery pipeline. You’ve taken the source of a web

application, configured it to compile and run tests of the WebSphere Liberty Profile, built it using

Jenkins, and automated the deployment of successful builds into a server running on a ‘production’

Virtual Machine. The next change you make to the application will be delivered through the pipeline

from Eclipse into the ‘production’ system. Go ahead:

Make a change to the application in Eclipse, run tests locally.

Commit and push the change to the Git repository.

Watch Jenkins build and test the application.

Watch Chef take the changes and deploy them to the “template” node acting as your production

system.


Recommended