Commit 65be3d33 authored by Daniele Venzano's avatar Daniele Venzano

Fix the terminate and delete semantics

parent 4d747ce7
......@@ -87,14 +87,14 @@ class APIEndpoint:
raise zoe_api.exceptions.ZoeAuthException()
if e.is_active():
raise zoe_api.exceptions.ZoeException('Cannot delete a running execution, terminate it first')
self.execution_terminate(uid, role, exec_id)
status, message = self.master.execution_delete(exec_id)
if status:
self.sql.execution_delete(exec_id)
return True, ''
else:
status, message = self.master.execution_delete(exec_id)
if status:
self.sql.execution_delete(exec_id)
return True, ''
else:
raise zoe_api.exceptions.ZoeException(message)
raise zoe_api.exceptions.ZoeException(message)
def service_by_id(self, uid, role, service_id) -> zoe_lib.sql_manager.Service:
s = self.sql.service_list(id=service_id, only_one=True)
......
......@@ -17,7 +17,7 @@ import sys
from flask import Blueprint
from flask_restful import Api
from zoe_api.rest_api.execution import ExecutionAPI, ExecutionCollectionAPI
from zoe_api.rest_api.execution import ExecutionAPI, ExecutionCollectionAPI, ExecutionDeleteAPI
from zoe_api.rest_api.info import InfoAPI
from zoe_api.rest_api.service import ServiceAPI
......@@ -33,6 +33,7 @@ def api_init(api_endpoint) -> Blueprint:
api.add_resource(InfoAPI, API_PATH + '/info', resource_class_kwargs={'api_endpoint': api_endpoint})
api.add_resource(ExecutionAPI, API_PATH + '/execution/<int:execution_id>', resource_class_kwargs={'api_endpoint': api_endpoint})
api.add_resource(ExecutionDeleteAPI, API_PATH + '/execution/delete/<int:execution_id>', resource_class_kwargs={'api_endpoint': api_endpoint})
api.add_resource(ExecutionCollectionAPI, API_PATH + '/execution', resource_class_kwargs={'api_endpoint': api_endpoint})
api.add_resource(ServiceAPI, API_PATH + '/service/<int:container_id>', resource_class_kwargs={'api_endpoint': api_endpoint})
api.add_resource(QueryAPI, API_PATH + '/query', resource_class_kwargs={'api_endpoint': api_endpoint})
......
......@@ -36,9 +36,7 @@ class ExecutionAPI(Resource):
@catch_exceptions
def delete(self, execution_id: int):
"""
This method is called when a user wants to stop an execution. To actually delete the execution,
the user has to delete the 'parent' application.
:param execution_id: the execution to be deleted
:param execution_id: the execution to be terminated
:return:
"""
uid, role = get_auth(request)
......@@ -50,6 +48,25 @@ class ExecutionAPI(Resource):
return '', 204
class ExecutionDeleteAPI(Resource):
def __init__(self, api_endpoint: zoe_api.api_endpoint.APIEndpoint):
self.api_endpoint = api_endpoint
@catch_exceptions
def delete(self, execution_id: int):
"""
:param execution_id: the execution to be deleted
:return:
"""
uid, role = get_auth(request)
success, message = self.api_endpoint.execution_delete(uid, role, execution_id)
if not success:
raise zoe_api.exceptions.ZoeRestAPIException(message, 400)
return '', 204
class ExecutionCollectionAPI(Resource):
def __init__(self, api_endpoint: zoe_api.api_endpoint.APIEndpoint):
self.api_endpoint = api_endpoint
......
......@@ -44,7 +44,7 @@ def app_validate_cmd(args):
def app_get_cmd(args):
exec_api = ZoeExecutionsAPI(utils.zoe_url(), utils.zoe_user(), utils.zoe_pass())
execution = exec_api.execution_get(args.id)
execution = exec_api.get(args.id)
if execution is None:
print("no such execution")
else:
......@@ -62,14 +62,14 @@ def exec_start_cmd(args):
app_descr = json.load(args.jsonfile)
app_validate(app_descr)
exec_api = ZoeExecutionsAPI(utils.zoe_url(), utils.zoe_user(), utils.zoe_pass())
ret = exec_api.execution_start(args.name, app_descr)
ret = exec_api.start(args.name, app_descr)
print("Application scheduled successfully with ID {}, use the exec-get command to check its status".format(ret))
def exec_get_cmd(args):
exec_api = ZoeExecutionsAPI(utils.zoe_url(), utils.zoe_user(), utils.zoe_pass())
cont_api = ZoeServiceAPI(utils.zoe_url(), utils.zoe_user(), utils.zoe_pass())
execution = exec_api.execution_get(args.id)
execution = exec_api.get(args.id)
if execution is None:
print('Execution not found')
else:
......@@ -104,6 +104,11 @@ def exec_kill_cmd(args):
exec_api.terminate(args.id)
def exec_rm_cmd(args):
exec_api = ZoeExecutionsAPI(utils.zoe_url(), utils.zoe_user(), utils.zoe_pass())
exec_api.delete(args.id)
ENV_HELP_TEXT = '''To use this tool you need also to define three environment variables:
ZOE_URL: point to the URL of the Zoe Scheduler (ex.: http://localhost:5000/
ZOE_USER: the username used for authentication
......@@ -140,6 +145,10 @@ def process_arguments() -> Namespace:
argparser_execution_kill.add_argument('id', type=int, help="Execution id")
argparser_execution_kill.set_defaults(func=exec_kill_cmd)
argparser_execution_kill = subparser.add_parser('exec-rm', help="Deletes an execution")
argparser_execution_kill.add_argument('id', type=int, help="Execution id")
argparser_execution_kill.set_defaults(func=exec_rm_cmd)
return parser, parser.parse_args()
......
......@@ -33,7 +33,7 @@ class ZoeExecutionsAPI(ZoeAPIBase):
"""
Terminates an execution.
:param execution_id: the execution to delete
:param execution_id: the execution to terminate
:return: True if the operation was successful, False otherwise
:type execution_id: int
......@@ -45,6 +45,22 @@ class ZoeExecutionsAPI(ZoeAPIBase):
else:
raise ZoeAPIException(data['message'])
def delete(self, execution_id):
"""
Deletes an execution.
:param execution_id: the execution to delete
:return: True if the operation was successful, False otherwise
:type execution_id: int
:rtype: bool
"""
data, status_code = self._rest_delete('/execution/delete/' + str(execution_id))
if status_code == 204:
return
else:
raise ZoeAPIException(data['message'])
def list(self):
"""
Returns a list of all executions for the calling user, all of them if the user is admin.
......@@ -57,7 +73,7 @@ class ZoeExecutionsAPI(ZoeAPIBase):
else:
raise ZoeAPIException(data['message'])
def execution_get(self, execution_id):
def get(self, execution_id):
"""
Retrieve the Execution object for an existing execution.
......@@ -73,7 +89,7 @@ class ZoeExecutionsAPI(ZoeAPIBase):
else:
return None
def execution_start(self, name, application_description):
def start(self, name, application_description):
"""
Submit an application to the master to start a new execution.
......
......@@ -258,7 +258,11 @@ class Execution(Base):
self.sql_manager.execution_update(self.id, error_message=self.error_message)
def is_active(self):
return self._status == 'scheduled' or self._status == 'running'
"""
Returns True if the execution is in the scheduler
:return:
"""
return self._status == self.SCHEDULED_STATUS or self._status == self.RUNNING_STATUS or self._status == self.STARTING_STATUS or self._status == self.CLEANING_UP_STATUS
@property
def status(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment