Search This Blog

Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Monday, 20 April 2015

Deploying a Python application to IBM Bluemix

The following simple example shows how to push the basic hello world Python application to IBM Bluemix. We use the buildpack below.

https://github.com/heroku/heroku-buildpack-python.git

1. Create a file called requirements.txt

pas@pass-mbp:~/bluemix-apps/python-demo$ cat requirements.txt
Flask==0.10.1


2. Create a file called runtime.txt to detail the Python version to use

pas@pass-mbp:~/bluemix-apps/python-demo$ cat runtime.txt
python-2.6.5


3. Push the application using the buildpack above.

pas@pass-mbp:~/bluemix-apps/python-demo$ cf push pas-phython-demo -m 128m -b https://github.com/heroku/heroku-buildpack-python.git
Creating app pas-phython-demo in org pasapi@au1.ibm.com / space dev as pasapi@au1.ibm.com...
OK

Creating route pas-phython-demo.mybluemix.net...
OK

Binding pas-phython-demo.mybluemix.net to pas-phython-demo...
OK

Uploading pas-phython-demo...
Uploading app files from: /Users/pas/ibm/bluemix/apps/python-demo
Uploading 1.9K, 7 files
Done uploading
OK

Starting app pas-phython-demo in org pasapi@au1.ibm.com / space dev as pasapi@au1.ibm.com...
-----> Downloaded app package (4.0K)
Cloning into '/tmp/buildpacks/heroku-buildpack-python'...
-----> Installing runtime (python-2.6.5)
-----> Installing dependencies with pip
/app/.heroku/python/lib/python2.6/site-packages/pip-6.1.0.dev0-py2.6.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
       You are using pip version 6.1.0.dev0, however version 6.1.1 is available.
       You should consider upgrading via the 'pip install --upgrade pip' command.
       Collecting Flask==0.10.1 (from -r requirements.txt (line 1))
/app/.heroku/python/lib/python2.6/site-packages/pip-6.1.0.dev0-py2.6.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
         Downloading Flask-0.10.1.tar.gz (544kB)
       Collecting Werkzeug>=0.7 (from Flask==0.10.1->-r requirements.txt (line 1))
         Downloading Werkzeug-0.10.4-py2.py3-none-any.whl (293kB)
       Collecting Jinja2>=2.4 (from Flask==0.10.1->-r requirements.txt (line 1))
         Downloading Jinja2-2.7.3.tar.gz (378kB)
       Collecting itsdangerous>=0.21 (from Flask==0.10.1->-r requirements.txt (line 1))
         Downloading itsdangerous-0.24.tar.gz (46kB)
       Collecting markupsafe (from Jinja2>=2.4->Flask==0.10.1->-r requirements.txt (line 1))
         Downloading MarkupSafe-0.23.tar.gz
       Installing collected packages: markupsafe, itsdangerous, Jinja2, Werkzeug, Flask
         Running setup.py install for markupsafe
         Running setup.py install for itsdangerous
         Running setup.py install for Jinja2
         Running setup.py install for Flask
       Successfully installed Flask-0.10.1 Jinja2-2.7.3 Werkzeug-0.10.4 itsdangerous-0.24 markupsafe-0.23
You are using pip version 6.1.0.dev0, however version 6.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
-----> Uploading droplet (25M)

0 of 1 instances running, 1 starting
0 of 1 instances running, 1 starting
1 of 1 instances running

App started


OK

App pas-phython-demo was started using this command `python hello.py`

Showing health and status for app pas-phython-demo in org pasapi@au1.ibm.com / space dev as pasapi@au1.ibm.com...
OK

requested state: started
instances: 1/1
usage: 128M x 1 instances
urls: pas-phython-demo.mybluemix.net
last uploaded: Mon Apr 20 07:10:24 +0000 2015

     state     since                    cpu    memory          disk          details
#0   running   2015-04-20 05:12:23 PM   0.0%   40.8M of 128M   94.4M of 1G




Monday, 6 December 2010

Access Coherence Cache from JRuby/Jython scripts

The following example show how to use JRuby or Jython to access Coherence caches. Although this is basic and simply using the cache config file out of coherence.jar it gives you enough to see how simple it is to join the cluster as member and access coherence.jar from these 2 scripting languages.

I could of easily used groovy here as well but decided to stick with jruby and jython for now. Also the script members that join the cluster are storage disabled meaning that they must connect to node which is storage enabled to add data to the cache.

For this demo I have the following installed.
  • Coherence 3.6
  • JDK 1.6
  • JRuby 1.5.6
  • Jython 2.5.1
PRE STEPS

1. Start a cache server as follows. We do this to ensure we have a storage enabled node. The cache config file being used is the one inside coherence.jar. 

C:\jdev\coherence\36\coherence\bin>cache-server.cmd

In the example below jy = jython and jrb = jruby executables.

JRUBY

1. Create a ruby script as follows.
require 'java'
require 'C:/jdev/coherence/36/coherence/lib/coherence.jar'

import com.tangosol.net.CacheFactory
import com.tangosol.net.NamedCache
import java.util.Date
import java.lang.System

puts "*********************************"
puts "Coherence 3.6 Example from JRUBY"
puts "*********************************"

print "Started at ", Date.new.toString, "\n"

begin

  # ensure we are storage disabled
  System.setProperty("tangosol.coherence.distributed.localstorage", "false")
  
  CacheFactory.ensureCluster

  # get named cache
  testcache = CacheFactory.getCache("Test")

  # add data to cache
  testcache.put("1", "pas apicella")

  # retrive data from cache
  puts "Cache entry 1 = " + testcache.get("1")
  
rescue 
 print "Failed to access Coherence Cluster ", $!, "\n"
 
ensure 
  CacheFactory.shutdown
  
end

print "Ended at ", Date.new.toString, "\n"

2. Run it as follows ensuring your reference coherence.jar correctly

> jrb coherence.rb

Output as follows


C:\jdev\scripting\demos\jruby>jrb coherence.rb
*********************************
Coherence 3.6 Example from JRUBY
*********************************
Started at Mon Dec 06 11:38:09 EST 2010
2010-12-06 11:38:09.939/0.855 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Loaded operational configuration from "jar:file:/C:
/jdev/coherence/36/coherence/lib/coherence.jar!/tangosol-coherence.xml"
2010-12-06 11:38:09.942/0.858 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Loaded operational overrides from "jar:file:/C:/jde
v/coherence/36/coherence/lib/coherence.jar!/tangosol-coherence-override-dev.xml"
2010-12-06 11:38:09.943/0.859 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Optional configuration override "/tangosol-coherence-
override.xml" is not specified
2010-12-06 11:38:09.946/0.862 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml"
is not specified


Oracle Coherence Version 3.6.0.0 Build 17229
 Grid Edition: Development mode
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.


2010-12-06 11:38:10.474/1.390 Oracle Coherence GE 3.6.0.0 (thread=main, member=n/a): TCMP bound to /10.187.114.243:8090 using SystemSoc
ketProvider
2010-12-06 11:38:11.683/2.600 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): This Member(Id=2, Timestamp=2010-12-06 11:38:
11.675, Address=10.187.114.243:8090, MachineId=50163, Location=machine:paslap-au,process:3824, Role=JrubyMain, Edition=Grid Edition, Mode=De
velopment, CpuCount=4, SocketCount=4) joined cluster "cluster:0xC4DB" with senior Member(Id=1, Timestamp=2010-12-06 11:37:53.952, Address=10
.187.114.243:8088, MachineId=50163, Location=machine:paslap-au,process:1280, Role=CoherenceServer, Edition=Grid Edition, Mode=Development, C
puCount=4, SocketCount=4)
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service Cluster with senior mem
ber 1
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service Management with senior
member 1
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service DistributedCache with s
enior member 1
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service ReplicatedCache with se
nior member 1
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service OptimisticCache with se
nior member 1
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service InvocationService with
senior member 1
2010-12-06 11:38:11.697/2.613 Oracle Coherence GE 3.6.0.0 (thread=main, member=n/a): Started cluster Name=cluster:0xC4DB


Group{Address=224.3.6.0, Port=36000, TTL=4}


MasterMemberSet
  (
  ThisMember=Member(Id=2, Timestamp=2010-12-06 11:38:11.675, Address=10.187.114.243:8090, MachineId=50163, Location=machine:paslap-au,proces
s:3824, Role=JrubyMain)
  OldestMember=Member(Id=1, Timestamp=2010-12-06 11:37:53.952, Address=10.187.114.243:8088, MachineId=50163, Location=machine:paslap-au,proc
ess:1280, Role=CoherenceServer)
  ActualMemberSet=MemberSet(Size=2, BitSetCount=2
    Member(Id=1, Timestamp=2010-12-06 11:37:53.952, Address=10.187.114.243:8088, MachineId=50163, Location=machine:paslap-au,process:1280, R
ole=CoherenceServer)
    Member(Id=2, Timestamp=2010-12-06 11:38:11.675, Address=10.187.114.243:8090, MachineId=50163, Location=machine:paslap-au,process:3824, R
ole=JrubyMain)
    )
  RecycleMillis=1200000
  RecycleSet=MemberSet(Size=0, BitSetCount=0
    )
  )


TcpRing{Connections=[1]}
IpMonitor{AddressListSize=0}


2010-12-06 11:38:11.727/2.643 Oracle Coherence GE 3.6.0.0 (thread=Invocation:Management, member=2): Service Management joined the clust
er with senior service member 1
2010-12-06 11:38:11.788/2.704 Oracle Coherence GE 3.6.0.0 (thread=main, member=2): Loaded cache configuration from "jar:file:/C:/jdev
/coherence/36/coherence/lib/coherence.jar!/coherence-cache-config.xml"
2010-12-06 11:38:12.017/2.933 Oracle Coherence GE 3.6.0.0 (thread=DistributedCache, member=2): Service DistributedCache joined the clus
ter with senior service member 1
Cache entry 1 = pas apicella
2010-12-06 11:38:12.124/3.040 Oracle Coherence GE 3.6.0.0 (thread=Invocation:Management, member=2): Service Management left the cluster


2010-12-06 11:38:12.129/3.045 Oracle Coherence GE 3.6.0.0 (thread=DistributedCache, member=2): Service DistributedCache left the cluste
r
2010-12-06 11:38:12.222/3.138 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=2): Service Cluster left the cluster
Ended at Mon Dec 06 11:38:12 EST 2010


C:\jdev\scripting\demos\jruby>


JYTHON

1. Create a jython script as follows
from com.tangosol.net import CacheFactory, NamedCache
from java.util import Date
from java.lang import System as javasystem

print "*********************************"
print "Coherence 3.6 Example from JYTHON"
print "*********************************"

print "Started at " + Date().toString()

try:

 javasystem.setProperty("tangosol.coherence.distributed.localstorage", "false")
  
 CacheFactory.ensureCluster()
 
 # get named cache
 testcache = CacheFactory.getCache("Test")
 
 # add data to cache
 testcache.put("1", "pas apicella")

 # retrive data from cache
 print "Cache entry 1 = " + testcache.get("1")

except:
    print "Unexpected error:", sys.exc_info()[0]
    raise
    
finally:
    CacheFactory.shutdown()
    
print "Ended at " + Date().toString()
    
    
2. Add coherence.jar to the classpath as shown below.

> set CLASSPATH=C:\jdev\coherence\36\coherence\lib\coherence.jar

3. Run it as follows ensuring your reference coherence.jar correctly

> jy coherence.py

Output as follows


C:\jdev\scripting\demos\jython>jy coherence.py
*********************************
Coherence 3.6 Example from JYTHON
*********************************
Started at Mon Dec 06 11:41:08 EST 2010
2010-12-06 11:41:08.838/1.916 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Loaded operational configuration from "jar:file:/C:
/jdev/coherence/36/coherence/lib/coherence.jar!/tangosol-coherence.xml"
2010-12-06 11:41:08.844/1.922 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Loaded operational overrides from "jar:file:/C:/jde
v/coherence/36/coherence/lib/coherence.jar!/tangosol-coherence-override-dev.xml"
2010-12-06 11:41:08.845/1.923 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Optional configuration override "/tangosol-coherence-
override.xml" is not specified
2010-12-06 11:41:08.851/1.929 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml"
is not specified


Oracle Coherence Version 3.6.0.0 Build 17229
 Grid Edition: Development mode
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.


2010-12-06 11:41:10.012/3.090 Oracle Coherence GE 3.6.0.0 (thread=main, member=n/a): TCMP bound to /10.187.114.243:8090 using SystemSoc
ketProvider
2010-12-06 11:41:11.191/4.269 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): This Member(Id=3, Timestamp=2010-12-06 11:41:
11.172, Address=10.187.114.243:8090, MachineId=50163, Location=machine:paslap-au,process:6172, Role=PythonUtilJython, Edition=Grid Edition,
Mode=Development, CpuCount=4, SocketCount=4) joined cluster "cluster:0xC4DB" with senior Member(Id=1, Timestamp=2010-12-06 11:37:53.952, Add
ress=10.187.114.243:8088, MachineId=50163, Location=machine:paslap-au,process:1280, Role=CoherenceServer, Edition=Grid Edition, Mode=Develop
ment, CpuCount=4, SocketCount=4)
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service Cluster with senior mem
ber 1
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service Management with senior
member 1
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service DistributedCache with s
enior member 1
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service ReplicatedCache with se
nior member 1
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service OptimisticCache with se
nior member 1
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service InvocationService with
senior member 1
2010-12-06 11:41:11.210/4.288 Oracle Coherence GE 3.6.0.0 (thread=main, member=n/a): Started cluster Name=cluster:0xC4DB


Group{Address=224.3.6.0, Port=36000, TTL=4}


MasterMemberSet
  (
  ThisMember=Member(Id=3, Timestamp=2010-12-06 11:41:11.172, Address=10.187.114.243:8090, MachineId=50163, Location=machine:paslap-au,proces
s:6172, Role=PythonUtilJython)
  OldestMember=Member(Id=1, Timestamp=2010-12-06 11:37:53.952, Address=10.187.114.243:8088, MachineId=50163, Location=machine:paslap-au,proc
ess:1280, Role=CoherenceServer)
  ActualMemberSet=MemberSet(Size=2, BitSetCount=2
    Member(Id=1, Timestamp=2010-12-06 11:37:53.952, Address=10.187.114.243:8088, MachineId=50163, Location=machine:paslap-au,process:1280, R
ole=CoherenceServer)
    Member(Id=3, Timestamp=2010-12-06 11:41:11.172, Address=10.187.114.243:8090, MachineId=50163, Location=machine:paslap-au,process:6172, R
ole=PythonUtilJython)
    )
  RecycleMillis=1200000
  RecycleSet=MemberSet(Size=0, BitSetCount=0
    )
  )


TcpRing{Connections=[1]}
IpMonitor{AddressListSize=0}


2010-12-06 11:41:11.243/4.321 Oracle Coherence GE 3.6.0.0 (thread=Invocation:Management, member=3): Service Management joined the clust
er with senior service member 1
2010-12-06 11:41:11.300/4.378 Oracle Coherence GE 3.6.0.0 (thread=main, member=3): Loaded cache configuration from "jar:file:/C:/jdev
/coherence/36/coherence/lib/coherence.jar!/coherence-cache-config.xml"
2010-12-06 11:41:11.438/4.516 Oracle Coherence GE 3.6.0.0 (thread=DistributedCache, member=3): Service DistributedCache joined the clus
ter with senior service member 1
Cache entry 1 = pas apicella
2010-12-06 11:41:11.484/4.562 Oracle Coherence GE 3.6.0.0 (thread=Invocation:Management, member=3): Service Management left the cluster


2010-12-06 11:41:11.488/4.566 Oracle Coherence GE 3.6.0.0 (thread=DistributedCache, member=3): Service DistributedCache left the cluste
r
2010-12-06 11:41:11.580/4.658 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=3): Service Cluster left the cluster
Ended at Mon Dec 06 11:41:11 EST 2010


C:\jdev\scripting\demos\jython>