# Lab: Openshift S2I and Templates

## Overview

S2I is a tool deployed in OpenShift that provides a repeatable method to generate application images from source/binary code. Templates provide a parameterized set of objects that can be processed by OpenShift.

In this lab you'll use these capabilities to deploy a small legacy Java EE app to OpenShift in a multi-user OpenShift environment

### Step 1: Logon into the OpenShift Web Console and to the OpenShift CLI

1.1 Login into the OpenShift web console using the user credentials provided to you

* Access the OpenShift console via the url link provided by the workshop instructors
* Select `clouddragons-ldap` login option

  ![Login options](/files/-M8MbmivOHzxW8Y_1_a5)
* Login with the username and password provided by the workshop instructors

1.2 From the OpenShift web console click on your username in the upper right and select **Copy Login Command**

![Copy Login Command](/files/-M8Mbmj15gwbSKptYuiB)

1.3 You are prompted to login to the OpenShift console again. Repeat the same login procedure above to login.

1.4 Click **Display Token** link.

1.5 Copy the contents in the field **Log in with this token**. It provides a valid login command with an alive token.

1.6 Paste the login command in a terminal window and run it (Note: leave the web console browser tab open as you'll need it later on in the lab)

1.7 If prompted with `Use insecure connections? (y/n):`, enter **y**.

1.8 Set an environment variable for your *studentid* based on your user identifier from the instructor (e.g. **user001**)

```bash
    export STUDENTID=userNNN
```

1.9 Create a new OpenShift project for this lab

```bash
   oc new-project pbw-$STUDENTID
```

### Step 2: Clone the WebSphere Liberty S2I image source, create a Docker image,  and push it to the OpenShift internal registry

2.1 Clone the the WebSphere Liberty S2I image source by issuing the following commands in the terminal window you just used to login via the CLI

```
   git clone https://github.com/IBMAppModernization/s2i-liberty-javaee7.git
   cd s2i-liberty-javaee7
```

2.2 Get the hostname of your OpenShift internal registry so you can push images to it

```
   export INTERNAL_REG_HOST=`oc get route default-route --template='{{ .spec.host }}' -n openshift-image-registry`
```

2.3 Create a new OpenShift project for this lab (**Note:** your project name must be unique. Combine your lab STUDENT ID with the prefix `pbw-` to create a unique project name like `pbw-user???` where `user???` is your username e.g. `user012`)

```
   oc new-project pbw-user???
```

2.4 Build the S2I Liberty image and tag it appropriately for the internal registry

```
   podman build -t $INTERNAL_REG_HOST/`oc project -q`/s2i-liberty-javaee7:1.0 .
```

2.5 Login to the internal registry

```
   podman login -u `oc whoami` -p `oc whoami -t` $INTERNAL_REG_HOST
```

2.6 Push the S2I Liberty image to the internal registry

```
    podman push $INTERNAL_REG_HOST/`oc project -q`/s2i-liberty-javaee7:1.0
```

### Step 3: Install MariaDB from the OpenShift template catalog

3.1 In your Web console browser tab select the **Developer** role at the top left and make sure your project (ie *pbw-user???*) is selected.

![View All](/files/-M8MKG4CTJ8eDbLTclCY)

3.2 Click on the **Database** tile

![Database tile](/files/-M8MbmjCybvlsHxeU0N1)

3.4 Click on **MariaDB (Ephemeral)**

![Create MariaDB](/files/-M8MKG4Dio2d-BKwkKZ2)

3.5 Click **Instantiate Template**

3.6 Enter the following values for the fields indicated below (leave remaining values at their default values)

| Field name                  | Value          |
| --------------------------- | -------------- |
| MariaDB Connection Username | `pbwadmin`     |
| MariaDB Connection Password | `l1bertyR0cks` |
| MariaDB Database Name       | `plantsdb`     |

When you're done the dialog should look like the following:

![DB values](/files/-M8MKG4Ew_67SjNJnkfh)

3.7 Click **Create**

3.8 Wait until the **Status** changes to **Ready** before continuing

![Status](/files/-M8MKG4Gk47Do4s7APvH)

### Step 4: Clone the Github repo that contains the code for the Plants by WebSphere app

4.1 From your terminal go back to your home directory

```
   cd ~
```

4.2 From the client terminal window clone the Git repo with the following commands

```
   git clone https://github.com/IBMAppModernization/app-modernization-plants-by-websphere-jee6.git
   cd app-modernization-plants-by-websphere-jee6
```

### Step 5: Install the Plants by WebSphere Liberty app using a template that utilizes S2I to build the app image

5.1 Add the Plants by WebSphere Liberty app template to your OpenShift cluster

```
   oc create -f openshift/templates/s2i/pbw-liberty-template.yaml
```

5.2 In your Web console browser click on **+ Add** (top left)

![Add](/files/-M8MKG4HfWIt8YDcABWn)

5.3 Click the **From Catalog** tile

5.4 Select the **Other** category and then click on the **Plants by WebSphere on Liberty** tile

![PBW Tile](/files/-M8MbmjLmA_DU9PCKKf5)

5.5 Click **Instantiate Template**

5.6 Click **Create**

![Create](/files/-M8MbmjMSoqp_MVNa9hB)

5.7 To see the progress of the build and subsequent deployment select the **Administrator** role, then select **Workloads** and then **Pods**. The builder pod should be running (Note: that name of the pod will have *build* as a suffix)

![Pods](/files/-M8MbmjPYMz0TNF3N_Vg)

5.8 After a few minutes the build pod will terminate and the pod with the Plants by Websphere app will appear. Wait for that pod to show **Ready** before continuing. (Note: the name of the pod will not have *build* as a suffix).

![Pods](/files/-M8MbmjRCr7zCHxYn_gg)

5.9 In the navigation area on the left click **Networking** and then **Routes**. Click on the link in the **Location** column to launch the Plants By WebSphere app.

![Launch app](/files/-M8MKG4IRwQGh7FVXnFH)

### Step 6: Test the Plants by WebSphere app

6.1 From the Plants by WebSphere app UI, click on the **HELP** link

![Running app](/files/-M8MKG4JzDI1OxmawgDb)

6.2. Click on **Reset database** to populate the MariaDB database with data

6.3. Verify that browsing different sections of the online catalog shows product descriptions and images.

![Online catalog](/files/-M8MKG4K-yR3PNmF0sNY)

## Summary

With even small simple apps requiring multiple OpenShift objects, templates greatly simplify the process of distributing OpenShift apps. S2I allows you to reuse the same builder image for apps on the same app server, avoiding the effort of having to create unique images for each app.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ibm-developer.gitbook.io/cloudpakforapplications-appmod/modern-development-with-openshift/app-modernization-openshift-s2i-templates-lab-shared.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
