Add Java/JRE/JVM in Python Flask Cloud Foundry/IBM Cloud application

a 夏天 提交于 2020-07-21 07:58:32

问题


I am running a python flask application in a Cloud Foundry/IBM Cloud-environment. In my application I try to connect to DB2 Warehouse on Cloud with the IBMDBPY-package. This packages needs a package called jaydebeapi to be able to run. For the jaydebeapi to work I think I need JRE/JVM somehow installed on the server. I tried adding the Server JRE for Linux based OS, but it didn't work either. My error I got before I tried to upload the Sever JRE was this:

idadb = IdaDataBase(dsn=jdbc) #Establish a connection to our DB2-service
1/20/2018 12:05:45 PM   ERR undefined   File "/home/vcap/deps/0/python./lib/python2.7/site-packages/ibmdbpy/base.py", line 282, in __init__
1/20/2018 12:05:45 PM   ERR undefined jpype.startJVM(jpype.getDefaultJVMPath(), '-Djava.class.path=%s' % jarpath)
1/20/2018 12:05:45 PM   ERR undefined   File "/home/vcap/deps/0/python/lib/python2.7/site-packages/jpype/_core.py", line 114, in get_default_jvm_path
1/20/2018 12:05:45 PM   ERR undefined   return finder.get_jvm_path()
1/20/2018 12:05:45 PM   ERR undefined   File "/home/vcap/deps/0/python/lib/python2.7/site-packages/jpype/_jvmfinder.py", line 121, in get_jvm_path
1/20/2018 12:05:45 PM   ERR undefined   jvm = method()
1/20/2018 12:05:45 PM   ERR undefined   File "/home/vcap/deps/0/python/lib/python2.7/site-packages/jpype/_jvmfinder.py", line 164, in _get_from_known_locations
1/20/2018 12:05:45 PM   ERR undefined   for home in self.find_possible_homes(self._locations):
1/20/2018 12:05:45 PM   ERR undefined   File "/home/vcap/deps/0/python/lib/python2.7/site-packages/jpype/_jvmfinder.py", line 95, in find_possible_homes
1/20/2018 12:05:45 PM   ERR undefined   for childname in sorted(os.listdir(parent)):
1/20/2018 12:05:45 PM   ERR undefined   OSError: [Errno 2] No such file or directory: '/usr/lib/jvm'
1/20/2018 12:05:46 PM   OUT undefined   Exit status 1

Does anyone know how I can solve this?


回答1:


After much trial and error, the solution that worked for me was a multi build pack deployment as described below:

cf push -b https://github.com/cloudfoundry/multi-buildpack

and in the root of your project include a multi-buildpack.yml with the following

buildpacks:
  - https://github.com/cloudfoundry/apt-buildpack
  - https://github.com/cloudfoundry/python-buildpack

and an apt.yml with the following:

--- 
packages: openjdk-8-jre
repos: deb http://ppa.launchpad.net/openjdk-r/ppa/ubuntu trusty main
keys: https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xEB9B1D8886F44E2A

In the runtime.txt file, (also in the root of the project) is the version of python python-3.6.6

Unfortunately, JAVA gets installed in your home deps directory and as such you'll have to create a JAVA_HOME environment variable in the manifest.yml.

JAVA_HOME: /home/vcap/deps/0/apt/usr/lib/jvm/java-8-openjdk-amd64/jre/

I also tried to add jre/bin to the path this way

PATH: /bin:/usr/bin:/home/vcap/deps/0/apt/usr/lib/jvm/java-8-openjdk-amd64/jre/bin

However the push wipes this out and installs only the default path /bin;/usr/bin fortunately for me, JAVA_HOME was enough to get jaydebapi to work with the database driver for the jar files I had. If you need this environment variable, maybe try using the python os package to issue a command to modify the path as part of the startup.




回答2:


To include a more modern answer. As I write this, most (all worth using) versions of Cloud Foundry you encounter will support multiple buildpacks out-of-the-box. Thus you don't need the multi-buildpack buildpack anymore.

Instead, you can simply cf push and specify multiple buildpacks.

https://docs.cloudfoundry.org/buildpacks/use-multiple-buildpacks.html

This can either be done by setting multiple -b flags to cf push or by using a manifest.yml file and doing something like this:

...
buildpacks:
  - buildpack_1
  - buildpack_2
...

In either case, the execute in the order you list them.

https://docs.cloudfoundry.org/devguide/deploy-apps/manifest-attributes.html#buildpack

The rest of the answer remains the same as @lamonaki's answer.

  • Invoke both the apt-buildpack and the Python buildpack, in that order.

  • Add the apt.yml file and in it, indicate the Java package you'd like to install.

    Ex from @lamonaki's answer:

    --- 
    packages: openjdk-8-jre
    repos: deb http://ppa.launchpad.net/openjdk-r/ppa/ubuntu trusty main
    keys: https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xEB9B1D8886F44E2A
    
  • Add runtime.txt to set the Python version you'd like to install

  • Add a .profile file to the root of the project, just like apt.yml and runtime.txt. In that, add lines for export JAVA_HOME=/home/vcap/deps/0/apt/usr/lib/jvm/java-8-openjdk-amd64/jre/ and export PATH=$PATH:$JAVA_HOME/bin and potentially LD_LIBRARY_PATH if you need to reference any custom shared libraries in your Java or Python code.

You might be thinking, why apt-buildpack instead of the Java buildpack. Unfortunately, the Java buildpack in its current incarnation only supports running as a final buildpack (i.e. the last buildpack in the list of buildpacks). That rules it out as a good candidate here since you want the Python buildpack to be last. The Java Cloud Native Buildpacks will resolve the issue, but as I write this, there are no Cloud Native Buildpacks which run natively on CF.



来源:https://stackoverflow.com/questions/48356280/add-java-jre-jvm-in-python-flask-cloud-foundry-ibm-cloud-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!