Getting Started With Python

With OpenShift you can easily deploy and run Python applications. In this tutorial you will learn how to deploy a sample Python application, make modifications to it, and deploy your changes.

The tutorial assumes you have access to an OpenShift environment, installed the oc command line client, and have created a project to work in. You will also need an account on GitHub so you can fork the sample application used by this tutorial.

Step 1: The Sample Application

The sample application code repository used in this tutorial can be found on GitHub at:

To work through this tutorial you should first create a fork of this repository into your own GitHub account.

fork-sample-application

Once you have forked the sample code repository, take note of the HTTPS URL you would use if cloning your copy of the repository. You will need this URL when deploying the sample application.

url-for-cloning-repository

Step 2: Deploying the Application

To deploy the sample application with OpenShift you can use either the OpenShift web console, or the oc command line client.

If using the OpenShift web console, you should select Add to Project when in your project. From the Select Image or Template screen, enter python in the Filter by keyword field to list only builder images, templates or quick starts related to Python.

select-image-or-template

Select python:latest from the list of builder images displayed. Enter in myapp as the Name for your application, and set the Git Repository URL to be the HTTPS URL which would be used to clone your fork of the sample application code repository.

create-python-application

You can now select Create and the building and deployment of the sample application will start.

application-created

Select Continue to overview to view progress as the sample application is built and deployed. When complete, the overview page should show a single instance of your application as having being deployed.

application-overview

The SERVICE panel within the overview for the sample application will display the public URL at which the sample application can be accessed. You can click on the displayed link to go directly to it. The sample application should display ‘Hello World!’.

If you do not wish to use the OpenShift web console, but prefer to use the command line, you can deploy the sample application using the oc command line client. This is done using the oc new-app command, supplying it with the name of the builder to use, in this case python, along with the HTTPS URL which would be used to clone your fork of the sample application code repository. We also supply the --name option so we can give the application the name myapp.

$ oc new-app python~https://github.com/GrahamDumpleton/os-sample-python.git --name myapp
--> Found image 772dc19 (5 weeks old) in image stream "python" in project "openshift" under tag "3.4" for "python"

    Python 3.4
    ----------
    Platform for building and running Python 3.4 applications

    Tags: builder, python, python34, rh-python34

    * A source build using source code from https://github.com/GrahamDumpleton/os-sample-python.git will be created
      * The resulting image will be pushed to image stream "myapp:latest"
    * This image will be deployed in deployment config "myapp"
    * Port 8080/tcp will be load balanced by service "myapp"
      * Other containers can access this service through the hostname "myapp"

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

When oc new-app is used to deploy an application, you will need to explicitly expose the service so that it is given a public facing URL. This is done using the oc expose command.

$ oc expose svc/myapp
route "myapp" exposed

The results of running these commands will be reflected in the OpenShift web console and you can view the status of the application there. You can also use the oc status command to determine the current status of the application.

$ oc status
In project python-demos on server https://api.preview.openshift.com:443

http://myapp-python-demos.44fs.preview.openshiftapps.com to pod port 8080-tcp (svc/myapp)
  dc/myapp deploys istag/myapp:latest <-
    bc/myapp builds https://github.com/GrahamDumpleton/os-sample-python.git with openshift/python:3.4
    deployment #1 deployed 20 seconds ago - 1 pod

Step 3: Making Code Changes

To make changes to the sample application you can either make changes through the GitHub web interface, or you can make a clone of your fork of the sample application to your local machine.

$ git clone https://github.com/GrahamDumpleton/os-sample-python.git
Cloning into 'os-sample-python'...
remote: Counting objects: 43, done.
remote: Compressing objects: 100% (28/28), done.
remote: Total 43 (delta 11), reused 43 (delta 11), pack-reused 0
Unpacking objects: 100% (43/43), done.
Checking connectivity... done.

$ cd os-sample-python/

$ ls
README.md       config.py       requirements.txt    wsgi.py

Ensure you use the URL for your fork of the sample application, otherwise you will not be able to push back any changes you make.

The sample application is a simple Flask application, with everything contained in the wsgi.py file.

from flask import Flask
application = Flask(__name__)

@application.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    application.run()

Edit this file and change the message “Hello World!” to a greeting of your own.

If you are making the changes through the GitHub web interface the changes will be automatically pushed back into the code repository when you save the change. If working on your local machine, you will need to mark the file as changed, commit the change and push the changes back up to the code repository on GitHub.

$ git add wsgi.py

$ git commit -m 'Change greeting.'
[master 21bce2a] Change greeting.
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git push
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 302 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To https://github.com/GrahamDumpleton/os-sample-python.git
   da9328c..21bce2a  master -> master

The code changes have now been saved back into the code repository on GitHub. To deploy the changes to OpenShift start a build using the oc start-build command, with the name of the application as the argument.

$ oc start-build myapp
myapp-2

Run oc status and you can monitor the progress of the build and when complete it will show that a second deployment of your application has occurred.

$ oc status
In project python-demos on server https://api.preview.openshift.com:443

http://myapp-python-demos.44fs.preview.openshiftapps.com to pod port 8080-tcp (svc/myapp)
  dc/myapp deploys istag/myapp:latest <-
    bc/myapp builds https://github.com/GrahamDumpleton/os-sample-python.git with openshift/python:3.4
    deployment #2 deployed about a minute ago - 1 pod
    deployment #1 deployed about an hour ago

Builds can also be triggered through the OpenShift web console by clicking on Browse, Builds, and then selecting the name of your application, myapp in this case, and finally clicking on the Start Build button.

Visit the URL for your application and you should see the change you made.

Step 4: Deleting the Application

When you are done with the sample application, or you feel like starting over again, you can delete the application from OpenShift by running the oc delete all command, using a selector label to identify the application by name.

$ oc delete all --selector app=myapp
buildconfig "myapp" deleted
imagestream "myapp" deleted
deploymentconfig "myapp" deleted
route "myapp" deleted
service "myapp" deleted

The command will show a list of all the different definitions, as it deletes them, which were created for you when deploying the application.

Conclusion

In this quick tutorial we have shown how easy it is to deploy a Python application. You may have noted that you did not have to do anything yourself to install the required Python packages or start the WSGI server. This is because the OpenShift Python builder will install any packages for you which are listed in the requirements.txt file of the source code repository. So long as one of those packages is gunicorn and you have a wsgi.py file, the Gunicorn WSGI server will also be automatically started up to host your application when it is deployed.

Categories
News, OpenShift Online, Python
Tags
, ,
Comments are closed.