// Module included in the following assemblies:
//
// * rosa_learning/deploying_application_workshop/learning-deploying-application-s2i-deployments.adoc
:_mod-docs-content-type: PROCEDURE
[id="learning-deploying-application-s2i-deployments-deploy-to-cluster_{context}"]
= Using S2i to deploy OSToy on your cluster

[role="_abstract"]
To simplify your deployment process, you can deploy OSToy on your cluster by using the Source-to-Image (S2I) builder. This tool saves you time by automatically building a reproducible container image directly from the source binaries.

.Procedure
. Add a secret to {ocp-short}.
+
This example emulates a `.env` file. Files are easily moved directly into an {ocp-short} environment and can even be renamed in the secret.
+
** Run the following command, replacing `<UserName>` with your GitHub username:
+
[source,terminal]
----
$ oc create -f https://raw.githubusercontent.com/<UserName>/ostoy/master/deployment/yaml/secret.yaml
----
+
. Add a ConfigMap to {ocp-short}.
+
This example emulates an HAProxy config file, which is typically used for overriding default configurations in an {ocp-short} application. Files can be renamed in the ConfigMap.
+
** Run the following command, replacing `<UserName>` with your GitHub username:
+
[source,terminal]
----
$ oc create -f https://raw.githubusercontent.com/<UserName>/ostoy/master/deployment/yaml/configmap.yaml
----
+
. Deploy the microservice.
+
You must deploy the microservice to ensure that the service environment variables are available from the UI application.
+
`--context-dir` builds the application defined in the `microservice` directory in the Git repository. The `app` label ensures the user interface (UI) application and microservice are both grouped in the {ocp-short} UI.
+
** Run the following command to create the microservice, replacing `<UserName>` with your GitHub username:
+
[source,terminal]
----
$ oc new-app https://github.com/<UserName>/ostoy \
    --context-dir=microservice \
    --name=ostoy-microservice \
    --labels=app=ostoy
---- 
+
*Example output*:
+
[source,terminal]
----
--> Creating resources with label app=ostoy ...
    imagestream.image.openshift.io "ostoy-microservice" created
    buildconfig.build.openshift.io "ostoy-microservice" created
    deployment.apps "ostoy-microservice" created
    service "ostoy-microservice" created
--> Success
    Build scheduled, use 'oc logs -f buildconfig/ostoy-microservice' to track its progress.
    Application is not exposed. You can expose services to the outside world by executing one or more of the commands below:
     'oc expose service/ostoy-microservice'
    Run 'oc status' to view your app.
----
+
. Check the status of the microservice.
** Check that the microservice was created and is running correctly by running the following command:
+
[source,terminal]
----
$ oc status
----
+
*For example*:
+
[source,terminal]
----
In project ostoy-s2i on server https://api.myrosacluster.g14t.p1.openshiftapps.com:6443

svc/ostoy-microservice - 172.30.47.74:8080
  dc/ostoy-microservice deploys istag/ostoy-microservice:latest <-
    bc/ostoy-microservice source builds https://github.com/UserName/ostoy on openshift/nodejs:14-ubi8
    deployment #1 deployed 34 seconds ago - 1 pod
----
+
Wait until you see that the microservice was successfully deployed. You can also check this through the web UI.
+
. Deploy the front end UI.
+
The application relies on several environment variables to define external settings.
+
** Attach the secret and ConfigMap and create a PersistentVolume by running the following command:
+
[source,terminal]
----
$ oc new-app https://github.com/<UserName>/ostoy \
    --env=MICROSERVICE_NAME=OSTOY_MICROSERVICE
----
+
*For example*:
+
[source,terminal]
----
--> Creating resources ...
    imagestream.image.openshift.io "ostoy" created
    buildconfig.build.openshift.io "ostoy" created
    deployment.apps "ostoy" created
    service "ostoy" created
--> Success
    Build scheduled, use 'oc logs -f buildconfig/ostoy' to track its progress.
    Application is not exposed. You can expose services to the outside world by executing one or more of the commands below:
     'oc expose service/ostoy'
    Run 'oc status' to view your app.
----
+
. Update the deployment by running the following command:
+
[source,terminal]
----
$ oc patch deployment ostoy --type=json -p \
    '[{"op": "replace", "path": "/spec/strategy/type", "value": "Recreate"}, {"op": "remove", "path": "/spec/strategy/rollingUpdate"}]'
----
+
. Set a liveness probe.
+
Create a liveness probe to ensure the pod restarts if something is wrong in the application.
+
** Run the following command:
+
[source,terminal]
----
$ oc set probe deployment ostoy --liveness --get-url=http://:8080/health
----
+
. Attach the secret, ConfigMap, and persistent volume to the deployment.
+
.. Run the following command to attach your secret:
+
[source,terminal]
----
$ oc set volume deployment ostoy --add \
    --secret-name=ostoy-secret \
    --mount-path=/var/secret
----
+
.. Run the following command to attach your ConfigMap:
+
[source,terminal]
----
$ oc set volume deployment ostoy --add \
    --configmap-name=ostoy-config \
    -m /var/config
----
+
.. Run the following command to create and attach your persistent volume:
+
[source,terminal]
----
$ oc set volume deployment ostoy --add \
    --type=pvc \
    --claim-size=1G \
    -m /var/demo_files
----
+
. Expose the UI application as an {ocp-short} Route.

** Run the following command to deploy the application as an HTTPS application that uses the included TLS wildcard certificates:
+
[source,terminal]
----
$ oc create route edge --service=ostoy --insecure-policy=Redirect
----
+
. Browse to your application with the following methods:
** Run the following command to open a web browser with your OSToy application:
+
[source,terminal]
----
$ python -m webbrowser "$(oc get route ostoy -o template --template='https://{{.spec.host}}')"
----
+
** Run the following command to get the route for the application and copy and paste the route into your browser:
+
[source,terminal]
----
$ oc get route
----