Reputation: 21
I have a bunch of python code that is basically executed via Click CLI framework entry points.
I am exploring how to make some of CLI functions into WebActions, and was looking IBM Cloud Functions, which is basically Apache OpenWhisk.
I am brand new to OpenWhisk and IBM CloudFunctions.
I am following the Help Docs here:
https://console.bluemix.net/docs/openwhisk/openwhisk_actions.html#creating-python-actions
trying to mimic the virtualenv method.
When I translate their basic example to be Click CLI Commands as follows:
(below is the contents of a file __main__.py
which started out as a file named hello_too.py
but changed as following along with IBM Docs)
import click
@click.command()
@click.argument('params', nargs=-1)
def main(params):
#name = args.get("name", "stranger")
greeting = "Hello " + "foo" + "!"
print(greeting)
return {"greeting": greeting}
if __name__ == "__main__":
main()
and then zip it and upload (as per their virtualenv example) as web action I get the following error
{
"error": "The action did not produce a valid JSON response: Internal Server Error"
}
I saw on some other blogs, that running python with -i
is a good sanity check for OpenWhisk runtime.
When I run this code with -i
I get a stack-trace around system exit.
Traceback (most recent call last):
File "hello_too.py", line 12, in <module>
main()
File "/Users/mcmasty/projects/ppf-github/experiments/ibm-api-connect/venv/lib/python2.7/site-packages/click/core.py", line 722, in __call__
return self.main(*args, **kwargs)
File "/Users/mcmasty/projects/ppf-github/experiments/ibm-api-connect/venv/lib/python2.7/site-packages/click/core.py", line 700, in main
ctx.exit()
File "/Users/mcmasty/projects/ppf-github/experiments/ibm-api-connect/venv/lib/python2.7/site-packages/click/core.py", line 484, in exit
sys.exit(code)
SystemExit: 0
but when I run the example code, non-Click enabled, the interactive interpreter does complain.
Any advice at easiest path to port Click CLI scripts to be OpenWhisk Actions / IBM Cloud Functions ?
standalone_mode
to change SystemExit
behavior, but could not seem to get it to workecho
and using --main echo
option on OpenWhisk action create. (Same result)json.dumps()
), either through return or via writing to stdout, both in zip packaging and Docker image packaging... (same results)Since the python dictionary is basically the hard-coded result, my best guess right now is that this stack trace when running click enabled script is the root of my problem when deploying to IBM Cloud Functions.
Thanks in advance.
Additional info in response to comments
Code provided above. That code is in in a file called __main__.py
(as per the IBM Docs https://console.bluemix.net/docs/openwhisk/openwhisk_actions.html#creating-python-actions )
then following the IBM Docs its...
docker run --rm -v "$PWD:/tmp" openwhisk/python2action bash -c "cd tmp && virtualenv virtualenv && source virtualenv/bin/activate && pip install -r requirements.txt"
the only requirement in requirements.txt is click
then also following along the IBM Docs
zip -r hello_too.zip virtualenv __main__.py
and a sanity check
python -i hello_too.zip
Throws the SystemExit exception / stack trace similar to example above.
But
python hello_too.zip
completes normally.
Then deploy to Cloud Functions / Web Actions
ibmcloud wsk action create hello_too --kind python:2 hello_too.zip --web true
then invoke via command line
ibmcloud wsk action invoke --result hello_too
I get the following message:
{
"error": "The action did not produce a valid JSON response: Internal Server Error"
}
but the hard-coded response
return {"greeting": greeting}
is identical to their sample code in the "Creating and invoking a Python action section https://console.bluemix.net/docs/openwhisk/openwhisk_actions.html#creating-python-actions
so I am assuming this is not root-cause of the issue. (I ran their sample code, as outlined in the docs and returning a Python dict
worked fine.)
Its just when I try to use click version of python, I am getting stuck.
Upvotes: 0
Views: 413
Reputation: 4339
The click
module is causing a runtime error which kills the underlying Python process running the code.
The click
module is designed to build command line interface tools. Python code for OpenWhisk Actions is dynamically evaluated and invoked by an existing Python script. You will need to re-factor your application to expose the core functions through raw functions than the click
module.
Upvotes: 0