Skip to content

Latest commit

 

History

History
162 lines (117 loc) · 4.78 KB

18_Changing_code_on_the_fly.adoc

File metadata and controls

162 lines (117 loc) · 4.78 KB

Changing code on the fly

In this lab, we will learn how to make changes to the code on the local machine and immediately sync it with the code running in a pod.

Note
This model of programming is useful for interpreted languages.

Prerequisite: This lab assumes that you have git installed on your box.

Step 1: Create a new project
Create a new project on the OpenShift cluster with name synccode-UserName. Note Substitute the UserName with your userId

Step 2: Clone the git repository locally Clone the following git repository to your workstation. https://github.com/VeerMuchandi/nodejs-hello using the command:

$ git clone https://github.com/jimgarrett123/nodejs-hello.git

This is a very simple nodejs application. Feel free to understand the code.

Step 3: Create the application

Change to the nodejs-hello folder.

$ cd nodejs-hello

Now let us create an application using this code.

$ oc new-app .
--> Found image 448a51d (11 weeks old) in image stream "nodejs" in project "openshift" under tag "4" for "nodejs"

    Node.js 4
    ---------
    Platform for building and running Node.js 4 applications

    Tags: builder, nodejs, nodejs4

    * The source repository appears to match: nodejs
    * A source build using source code from https://github.com/VeerMuchandi/nodejs-hello.git#master will be created
      * The resulting image will be pushed to image stream "nodejs-hello:latest"
      * Use 'start-build' to trigger a new build
    * This image will be deployed in deployment config "nodejs-hello"
    * Port 8080/tcp will be load balanced by service "nodejs-hello"
      * Other containers can access this service through the hostname "nodejs-hello"

--> Creating resources with label app=nodejs-hello ...
    imagestream "nodejs-hello" created
    buildconfig "nodejs-hello" created
    deploymentconfig "nodejs-hello" created
    service "nodejs-hello" created
--> Success
    Build scheduled, use 'oc logs -f bc/nodejs-hello' to track its progress.
    Run 'oc status' to view your app.

Create a route:

$ oc expose service nodejs-hello
route "nodejs-hello" exposed

Wait for the build to complete and test the application:

$ oc get route
NAME           HOST/PORT                                                                       PATH      SERVICES       PORT       TERMINATION
nodejs-hello   http://nodejs-hello-jimgarrett.apps.rhpds.openshift.opentlc.com/             nodejs-hello   8080-tcp
$ curl http://nodejs-hello-jimgarrett.apps.rhpds.openshift.opentlc.com/
Welcome to OpenShift!! My HostName is: nodejs-hello-1-8qigc

Step 4: Make code changes and sync with rsync

Let us now make a small change to the application on your workstation.

Edit server.js file using your favorite text editor (I use vi). The code snippet below displays the welcome message:

app.get('/', function (req, res) {
  res.send('Welcome to OpenShift!! My HostName is: ' + os.hostname() + '\n');
});

Let us make a small change to insert a \n character after the welcome message so that the hostname is displayed on a different line. The edited code should look like this.

app.get('/', function (req, res) {
  res.send('Welcome to OpenShift!! \n My HostName is: ' + os.hostname() + '\n');
});

Run oc get pods to get your application pod name as shown below:

$ oc get pods
NAME                   READY     STATUS      RESTARTS   AGE
nodejs-hello-1-8qigc   1/1       Running     0          8m
nodejs-hello-1-build   0/1       Completed   0          9m

Use oc rsync to sync the code from the local workstation to the pod as shown below

$ oc rsync . nodejs-hello-1-8qigc:/opt/app-root/src --no-perms=true --exclude=.git
building file list ... done

sent 89 bytes  received 20 bytes  2.29 bytes/sec
total size is 805  speedup is 7.39

The command above will copy the code from the local machine to the pod. Note that:

  • /opt/app-root/src is the default home location on the pod

  • --exclude=.git will omit the hidden git folder

Now test the application again. This time it should show the output in two lines as below:

$ curl nodejs-hello-rsync-userxx.{{APPS_ADDRESS}}
Welcome to OpenShift!!
My HostName is: nodejs-hello-1-8qigc

Note that pod did not restart, we did not have to check into git repo and rebuild the image. The changes moved from your workstation directly to the pod.

Developers can use this mechanism to quickly test the changes on the fly. When ready they can commit the changes to the source control repository.

You can repeat a few more changes and enjoy the rsync!!!

Let’s clean up your project prior to continueing.

$ oc delete project synccode-UserName