[Cloud Foundry](EN) cf dev python basic example

Upload python application to Cloud Foundry’s CFAR(Cloud Foundry Application Runtime) by using cf dev


Environment and Prerequisite

  • Ubuntu 18.04
  • Baremetal Server


Goal

  • Upload python application to Cloud Foundry’s CFAR(Cloud Foundry Application Runtime) by using cf dev for practice.
  • This post is focus on practice and content is base on Reference


Cloud Foundry

Cloud Foundry

  • Cloud Foundry: Cloud Foundry is an open source, multi-cloud application platform as a service (PaaS) governed by the Cloud Foundry Foundation
  • It has many various projects like for deploying, managing and setting application environment.
  • Ex) BOSH and CFAR

CFAR(Cloud Foundry Application Runtime)

  • CFAR: Cloud Foundry Application Runtime (CFAR) is a code-centric platform that simplifies the lives of developers. It takes your code, written in any language or framework, and runs it on any cloud.
  • Just write codes and upload it to CFAR then application environment will be set.

CF Dev

  • CF Dev: CF Dev is a distribution of Cloud Foundry designed to run on a developer’s laptop or workstation using native hypervisors and a fully functional BOSH Director.
  • By using CF Dev, you can build app runtime environment easily on CFAR.
  • On this post, python flask application will be set up.


CF Dev

Install CF Dev

  • Install QEMU and libvirt
sudo apt install qemu-kvm libvirt-bin libvirt-doc -y
  • Install CF CLI
# ...first add the Cloud Foundry Foundation public key and package repository to your system
wget -q -O - https://packages.cloudfoundry.org/debian/cli.cloudfoundry.org.key | sudo apt-key add -
echo "deb https://packages.cloudfoundry.org/debian stable main" | sudo tee /etc/apt/sources.list.d/cloudfoundry-cli.list
# ...then, update your local package index, then finally install the cf CLI
sudo apt-get update -y
sudo apt-get install cf-cli -y
  • Install CF Dev plugin
cf install-plugin -r CF-Community cfdev


Make CF Dev environment

  • cf dev start
cf dev start
  • Result
Downloading Resources...
Progress: |====================>| 100.0%
Setting State...

CF Dev collects anonymous usage data to help us improve your user experience. We intend to share these anonymous usage analytics with user community by publishing quarterly reports at :

https://github.com/pivotal-cf/cfdev/wiki/Telemetry

Are you ok with CF Dev periodically capturing anonymized telemetry [y/N]?> y
WARNING: CF Dev requires 8192 MB of RAM to run. This machine may not have enough free RAM.
Creating the VM...
Starting the VM...
Fetching VM Address...
Waiting for the VM...
Deploying the BOSH Director...
Deploying CF...
  Done (11m24s)

 	  ██████╗███████╗██████╗ ███████╗██╗   ██╗
 	 ██╔════╝██╔════╝██╔══██╗██╔════╝██║   ██║
 	 ██║     █████╗  ██║  ██║█████╗  ██║   ██║
 	 ██║     ██╔══╝  ██║  ██║██╔══╝  ╚██╗ ██╔╝
 	 ╚██████╗██║     ██████╔╝███████╗ ╚████╔╝
 	  ╚═════╝╚═╝     ╚═════╝ ╚══════╝  ╚═══╝
 	             is now running!

 	To begin using CF Dev, please run:
 	    cf login -a https://api.dev.cfdev.sh --skip-ssl-validation

 	Admin user => Email: admin / Password: admin
 	Regular user => Email: user / Password: pass

 	To deploy a particular service, please run:
 	    cf dev deploy-service <service-name> [Available services: mysql]


Login

  • Email: user / Password: pass
cf login -a https://api.dev.cfdev.sh --skip-ssl-validation
  • Result
API endpoint: https://api.dev.cfdev.sh

Email: user

Password:
Authenticating...
OK

Targeted org cfdev-org

Targeted space cfdev-space



API endpoint:   https://api.dev.cfdev.sh (API version: 3.76.0)
User:           user
Org:            cfdev-org
Space:          cfdev-space


Make app files

  • Four files are needed.
  • Make directory
mkdir flask_test_cf
cd flask_test_cf

app.py

from flask import Flask
import os

app = Flask(__name__)

@app.route('/')
def hello_world():
    return "Hello World!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=int(os.getenv("PORT", 5001)))

Procfile

  • Command to run in CFAR
web: python app.py

requirements.txt

  • It can be generated by using pip freeze command
pip freeze >  requirements.txt
Click==7.0
Flask==1.1.1
itsdangerous==1.1.0
Jinja2==2.11.1
MarkupSafe==1.1.1
Werkzeug==1.0.0

runtime.txt

  • Write python version
python-3.6.9


Check files

$ ls
app.py  Procfile  requirements.txt  runtime.txt


Upload app using cf push

  • In project directory
# -m: memory size to use
# -b: buildpack option(we use python in this case)
# python-hello-world: app name, this will be subdomain name
cf push -m 128M -b python_buildpack python-hello-world
  • Result
Pushing app python-hello-world to org cfdev-org / space cfdev-space as user...
Getting app info...
Creating app with these attributes...
+ name:         python-hello-world
  path:         /home/twpower/flask_test_cf
  buildpacks:
+   python_buildpack
+ memory:       128M
  routes:
+   python-hello-world.dev.cfdev.sh

Creating app python-hello-world...
Mapping routes...
Comparing local files to remote cache...
Packaging files to upload...
Uploading files...
 963 B / 963 B [=========================================================================] 100.00% 1s

Waiting for API to complete processing files...

Staging app and tracing logs...
   Downloading python_buildpack...
   Downloaded python_buildpack (5M)
   Cell 19330b69-fa32-43e6-9884-9e89fcbbbc40 creating container for instance 6616abe5-490e-4854-9ee6-8485ad258dd6
   Cell 19330b69-fa32-43e6-9884-9e89fcbbbc40 successfully created container for instance 6616abe5-490e-4854-9ee6-8485ad258dd6
   Downloading app package...
   Downloaded app package (963B)
   -----> Python Buildpack version 1.6.36
   -----> Supplying Python
   -----> Installing python 3.6.9
          Download [https://buildpacks.cloudfoundry.org/dependencies/python/python-3.6.9-linux-x64-cflinuxfs3-f30bc832.tgz]
   -----> Installing pip-pop 0.1.3
          Download [https://buildpacks.cloudfoundry.org/dependencies/manual-binaries/pip-pop/pip-pop-0.1.3-fc106ef6.tar.gz]
   -----> Running Pip Install
          Collecting Click==7.0 (from -r /tmp/app/requirements.txt (line 1))
            Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
          Collecting Flask==1.1.1 (from -r /tmp/app/requirements.txt (line 2))
            Downloading https://files.pythonhosted.org/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
          Collecting itsdangerous==1.1.0 (from -r /tmp/app/requirements.txt (line 3))
            Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
          Collecting Jinja2==2.11.1 (from -r /tmp/app/requirements.txt (line 4))
            Downloading https://files.pythonhosted.org/packages/27/24/4f35961e5c669e96f6559760042a55b9bcfcdb82b9bdb3c8753dbe042e35/Jinja2-2.11.1-py2.py3-none-any.whl (126kB)
          Collecting MarkupSafe==1.1.1 (from -r /tmp/app/requirements.txt (line 5))
            Downloading https://files.pythonhosted.org/packages/b2/5f/23e0023be6bb885d00ffbefad2942bc51a620328ee910f64abe5a8d18dd1/MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl
          Collecting Werkzeug==1.0.0 (from -r /tmp/app/requirements.txt (line 6))
            Downloading https://files.pythonhosted.org/packages/ba/a5/d6f8a6e71f15364d35678a4ec8a0186f980b3bd2545f40ad51dd26a87fb1/Werkzeug-1.0.0-py2.py3-none-any.whl (298kB)
          Installing collected packages: Click, Werkzeug, itsdangerous, MarkupSafe, Jinja2, Flask
            The script flask is installed in '/tmp/contents020609285/deps/0/python/bin' which is not on PATH.
            Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
          Successfully installed Click-7.0 Flask-1.1.1 Jinja2-2.11.1 MarkupSafe-1.1.1 Werkzeug-1.0.0 itsdangerous-1.1.0
          You are using pip version 18.1, however version 20.0.2 is available.
          You should consider upgrading via the 'pip install --upgrade pip' command.
   Exit status 0
   Uploading droplet, build artifacts cache...
   Uploading droplet...
   Uploading build artifacts cache...
   Uploaded build artifacts cache (48.9M)
   Uploaded droplet (49.7M)
   Uploading complete
   Cell 19330b69-fa32-43e6-9884-9e89fcbbbc40 stopping instance 6616abe5-490e-4854-9ee6-8485ad258dd6
   Cell 19330b69-fa32-43e6-9884-9e89fcbbbc40 destroying container for instance 6616abe5-490e-4854-9ee6-8485ad258dd6

Waiting for app to start...
   Cell 19330b69-fa32-43e6-9884-9e89fcbbbc40 successfully destroyed container for instance 6616abe5-490e-4854-9ee6-8485ad258dd6

name:              python-hello-world
requested state:   started
routes:            python-hello-world.dev.cfdev.sh
last uploaded:     Mon 10 Feb 14:37:53 KST 2020
stack:             cflinuxfs3
buildpacks:        python

type:            web
instances:       1/1
memory usage:    128M
start command:   python app.py
     state     since                  cpu    memory           disk           details
#0   running   2020-02-10T05:35:17Z   0.0%   118.4K of 128M   177.7M of 1G


Check response

$ curl http://python-hello-world.dev.cfdev.sh
Hello World


Delete CF Dev

  • Stop cf dev
cf dev stop
  • Delete cfdev plugin
cf uninstall-plugin cfdev
  • Delete directory
rm -rf ~/.cfdev


Issue

  • virbr0 is not up in virtual machine environment.


Reference