Add support for running scripts (ScriptEndpoint) via CLI.#28
Add support for running scripts (ScriptEndpoint) via CLI.#28opalczynski merged 9 commits intoSyncano:developfrom coding-buzz:develop
Conversation
|
@ilu2112 please correct isort issues: |
| except NoOptionError: | ||
| LOG.error('Do a login first: syncano login.') | ||
| sys.exit(1) | ||
| api_path = ScriptEndpoint._meta.endpoints['run']['path'].format( |
There was a problem hiding this comment.
This would be nicer:
se = ScriptEndpoint.please.get('instance-name', 'script-name')
se.run()
response = se.run(**payload)
# process (if needed) and print response here
…odels instead of direct API calls.
|
@opalczynski It should be fine now, I have carried all Your suggestions for changes. |
|
@ilu2112 Sorry that writing so lately - but yesterday was a busy day. It's almost good :) I've checked it and have some proposals: A. Sometimes the --payload argument is not required at all, so you need to check if it was provided. B. You should definitely wrap C. I think that it would be nice to format the output different. First example above is when everything is good with script - then simply log response.result['stdout'] Second - is when some error occured: then we should output something like this: |
| sys.exit(1) | ||
| instance = connection.Instance.please.get(instance_name) | ||
| se = instance.script_endpoints.get(instance_name, script_endpoint_name) | ||
| data = json.loads(payload.strip() or '{}') |
There was a problem hiding this comment.
wrap this in try catch and log error if occurs
There was a problem hiding this comment.
additionally: payload can be None here - so check it first;
… improve output format.
|
This is really good! But can you update the README file with this new command? |
|
Yes, I will. I will also adapt logging to changes from pull request #32. |
| data = json.loads((payload or '').strip() or '{}') | ||
| response = se.run(**data) | ||
| if response.status == 'success': | ||
| print(json.dumps(response.result['stdout'], indent=4, sort_keys=True)) |
There was a problem hiding this comment.
I think You're almost there! :)
In Syncano script user can define their own custom response - then the field stdout is ignored and result is palces in response key, take a look at examples:
{u'response': {u'status': 200, u'content': u'{"one": 1}', u'content_type': u'application/json'},
u'stderr': u'', u'stdout': u''}
# output: "" (the print(json.dumps(....)))
{u'stderr': u'', u'stdout': u'Some string'}
# output: "Some string"
{u'stderr': u'', u'stdout': u'16'}
# output: "16"
{u'stderr': u'Traceback (most recent call last):\n File "/app/source/main.py", line 24, in <module>\n
2/0\nZeroDivisionError: integer division or modulo by zero', u'stdout': u''}
# output: ""
{u'response': {u'status': 200, u'content': u"{'one': 1}", u'content_type': u'applicattion/json'},
u'stderr': u'', u'stdout': u''}
# output: ""
Also the json.dumps for simple data types will wrap it with "": 12 -> "12" I think this is not needed here.
There was a problem hiding this comment.
One more thing: the custom response can be returned with standard response:
{u'response': {u'status': 200, u'content': u'{"one": 1}', u'content_type': u'application/json'},
u'stderr': u'', u'stdout': u'12'}
| LOG = get_logger('syncano-execute') | ||
|
|
||
|
|
||
| def print_response(response): |
There was a problem hiding this comment.
What about response.result['response'] ?
There was a problem hiding this comment.
@opalczynski I don't understand You, what do You mean? I see no response.result['response'] field in response.
There was a problem hiding this comment.
Consider this - this is a script code:
import json
set_response(HttpResponse(status_code=200, content=json.dumps({'one': 1}), content_type='application/json'))
print(12)
This:
s = Script.please.get(id=1)
t = s.run()
t.reload()
print(t._raw_data)
Will output:
{
'status': u'success',
'links': <syncano.models.fields.LinksWrapper object at 0x7fd2dd91d690>,
'executed_at': datetime.datetime(2016, 6, 13, 12, 50, 23, 99456),
'instance_name': 'hidden-lake-9362',
'result': {
u'response': {
u'status': 200,
u'content': u'{"one": 1}',
u'content_type': u'application/json'},
u'stderr': u'',
u'stdout': u'12'},
'duration': 38,
'script_id': 1,
'id': 93
}
I can't see in your solution handling of the custom response - maybe I've missed something?
There was a problem hiding this comment.
We're talking about ScriptEndpoint, right? Your script's returns
{
"one": 1
}
if executed from syncano-cli. Please check it Yourself.
There was a problem hiding this comment.
To clarify. ScriptEndpoint will return the actual custom response as @ilu2112 mentioned. Trace for it will include the structure @opalczynski mentioned. But here I believe we are only dealing with script endpoints so it seems ok: either structure has a result or should be returned as-is.
There was a problem hiding this comment.
Yep, my bad :) sry about this. It looks good now :)
|
👍 |
|
@ilu2112 Is this ready? :) If so - please resolve merge conflicts and merge ;) |
Closes issue #27. To test it manually simply type in
syncano execute --helpand follow the syntax.