entrypoint.py 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright (c) 2016, Daniele Venzano
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
This module contains the entrypoint for the commandline Zoe client
"""

20
import datetime
21 22 23 24
import json
import logging
import os
import sys
25
import time
26
from argparse import ArgumentParser, Namespace, FileType, RawDescriptionHelpFormatter
Daniele Venzano's avatar
Daniele Venzano committed
27
from typing import Tuple
28 29

from zoe_cmd import utils
30
from zoe_lib.info import ZoeInfoAPI
Daniele Venzano's avatar
Daniele Venzano committed
31
from zoe_lib.services import ZoeServiceAPI
32
from zoe_lib.statistics import ZoeStatisticsAPI
33
from zoe_lib.exceptions import ZoeAPIException, InvalidApplicationDescription
34
from zoe_lib.executions import ZoeExecutionsAPI
35
from zoe_lib.applications import app_validate
36 37


38 39 40 41 42 43 44 45 46 47
def info_cmd(args_):
    """Queries the info endpoint."""
    info_api = ZoeInfoAPI(utils.zoe_url(), utils.zoe_user(), utils.zoe_pass())
    info = info_api.info()
    print("Zoe version: ", info['version'])
    print("Zoe API version: ", info['api_version'])
    print("ZApp format version: ", info['application_format_version'])
    print("Deployment name: ", info['deployment_name'])


48
def app_validate_cmd(args):
49
    """Validate an application description."""
50
    app_descr = json.load(args.jsonfile)
51
    try:
52 53 54
        app_validate(app_descr)
    except InvalidApplicationDescription as e:
        print(e)
55
    else:
56
        print("Static validation OK")
57 58 59


def app_get_cmd(args):
60
    """Extract an application description from an execution."""
61
    exec_api = ZoeExecutionsAPI(utils.zoe_url(), utils.zoe_user(), utils.zoe_pass())
62
    execution = exec_api.get(args.id)
63
    if execution is None:
64
        print("no such execution")
65
    else:
66
        json.dump(execution['description'], sys.stdout, sort_keys=True, indent=4)
67 68


Daniele Venzano's avatar
Daniele Venzano committed
69
def exec_list_cmd(args):
70
    """List executions"""
71
    exec_api = ZoeExecutionsAPI(utils.zoe_url(), utils.zoe_user(), utils.zoe_pass())
Daniele Venzano's avatar
Daniele Venzano committed
72 73 74 75 76 77 78 79 80 81 82 83 84
    filter_names = [
        'status',
        'name',
        'user_id',
        'limit',
        'earlier_than_submit',
        'earlier_than_start',
        'earlier_than_end',
        'later_than_submit',
        'later_than_start',
        'later_than_end'
    ]
    filters = {}
Daniele Venzano's avatar
Daniele Venzano committed
85 86 87
    for key, value in vars(args).items():
        if key in filter_names:
            filters[key] = value
Daniele Venzano's avatar
Daniele Venzano committed
88
    data = exec_api.list(**filters)
89
    for e in sorted(data.values(), key=lambda x: x['id']):
90
        print('Execution {} (User: {}, ID: {}): {}'.format(e['name'], e['user_id'], e['id'], e['status']))
91 92 93


def exec_start_cmd(args):
94
    """Submit an execution."""
95
    app_descr = json.load(args.jsonfile)
96
    app_validate(app_descr)
97
    exec_api = ZoeExecutionsAPI(utils.zoe_url(), utils.zoe_user(), utils.zoe_pass())
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    exec_id = exec_api.start(args.name, app_descr)
    if not args.synchronous:
        print("Application scheduled successfully with ID {}, use the exec-get command to check its status".format(exec_id))
    else:
        print("Application scheduled successfully with ID {}, waiting for status change".format(exec_id))
        old_status = 'submitted'
        while True:
            execution = exec_api.get(exec_id)
            current_status = execution['status']
            if old_status != current_status:
                print('Execution is now {}'.format(current_status))
                old_status = current_status
            if current_status == 'running':
                break
            time.sleep(1)
113 114 115


def exec_get_cmd(args):
116
    """Gather information about an execution."""
117
    exec_api = ZoeExecutionsAPI(utils.zoe_url(), utils.zoe_user(), utils.zoe_pass())
Daniele Venzano's avatar
Daniele Venzano committed
118
    cont_api = ZoeServiceAPI(utils.zoe_url(), utils.zoe_user(), utils.zoe_pass())
119
    execution = exec_api.get(args.id)
120 121 122 123
    if execution is None:
        print('Execution not found')
    else:
        print('Execution {} (ID: {})'.format(execution['name'], execution['id']))
124
        print('Application name: {}'.format(execution['description']['name']))
125
        print('Status: {}'.format(execution['status']))
126
        if execution['status'] == 'error':
127
            print('Last error: {}'.format(execution['error_message']))
128
        print()
129 130 131 132 133 134 135 136 137 138 139
        print('Time submit: {}'.format(datetime.datetime.fromtimestamp(execution['time_submit'])))

        if execution['time_start'] is None:
            print('Time start: {}'.format('not yet'))
        else:
            print('Time start: {}'.format(datetime.datetime.fromtimestamp(execution['time_start'])))

        if execution['time_end'] is None:
            print('Time end: {}'.format('not yet'))
        else:
            print('Time end: {}'.format(datetime.datetime.fromtimestamp(execution['time_end'])))
140
        print()
141

142 143 144 145 146 147 148 149 150
        endpoints = exec_api.endpoints(execution['id'])
        if len(endpoints) > 0:
            print('Exposed endpoints:')
        else:
            print('This ZApp does not expose any endpoint')
        for endpoint in endpoints:
            print(' - {}: {}'.format(endpoint[0], endpoint[1]))

        print()
151
        for c_id in execution['services']:
152
            service = cont_api.get(c_id)
153 154
            print('Service {} (ID: {})'.format(service['name'], service['id']))
            print(' - zoe status: {}'.format(service['status']))
155
            print(' - backend status: {}'.format(service['backend_status']))
156 157
            if service['error_message'] is not None:
                print(' - error: {}'.format(service['error_message']))
158 159 160


def exec_kill_cmd(args):
161
    """Kill an execution."""
162 163 164 165
    exec_api = ZoeExecutionsAPI(utils.zoe_url(), utils.zoe_user(), utils.zoe_pass())
    exec_api.terminate(args.id)


166
def exec_rm_cmd(args):
167
    """Delete an execution and kill it if necessary."""
168 169 170 171
    exec_api = ZoeExecutionsAPI(utils.zoe_url(), utils.zoe_user(), utils.zoe_pass())
    exec_api.delete(args.id)


172 173 174 175 176 177 178
def stats_cmd(args_):
    """Prints statistics on Zoe internals."""
    stats_api = ZoeStatisticsAPI(utils.zoe_url(), utils.zoe_user(), utils.zoe_pass())
    sched = stats_api.scheduler()
    print('Scheduler queue length: {}'.format(sched['queue_length']))
    print('Termination threads count: {}'.format(sched['termination_threads_count']))

179 180 181 182 183 184
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
ZOE_PASS: the password used for authentication'''


Daniele Venzano's avatar
Daniele Venzano committed
185
def process_arguments() -> Tuple[ArgumentParser, Namespace]:
186
    """Parse command line arguments."""
187 188 189 190 191
    parser = ArgumentParser(description="Zoe command-line client", epilog=ENV_HELP_TEXT, formatter_class=RawDescriptionHelpFormatter)
    parser.add_argument('--debug', action='store_true', help='Enable debug output')

    subparser = parser.add_subparsers()

192 193 194
    argparser_info = subparser.add_parser('info', help="Queries the API for supported versions")
    argparser_info.set_defaults(func=info_cmd)

195 196 197
    argparser_app_validate = subparser.add_parser('app-validate', help='Validate an application description')
    argparser_app_validate.add_argument('jsonfile', type=FileType("r"), help='Application description')
    argparser_app_validate.set_defaults(func=app_validate_cmd)
198

Francesco Pace's avatar
Francesco Pace committed
199
    argparser_exec_start = subparser.add_parser('start', help="Start an application")
200
    argparser_exec_start.add_argument('-s', '--synchronous', action='store_true', help="Do not detach immediately, wait for execution to start before exiting")
201
    argparser_exec_start.add_argument('name', help="Name of the execution")
202
    argparser_exec_start.add_argument('jsonfile', type=FileType("r"), help='Application description')
203 204
    argparser_exec_start.set_defaults(func=exec_start_cmd)

205
    argparser_app_list = subparser.add_parser('exec-ls', help="List all executions for the calling user")
Daniele Venzano's avatar
Daniele Venzano committed
206 207 208 209 210
    argparser_app_list.add_argument('--limit', type=int, help='Limit the number of executions')
    argparser_app_list.add_argument('--name', help='Show only executions with this name')
    argparser_app_list.add_argument('--user', help='Show only executions belonging to this user')
    argparser_app_list.add_argument('--status', choices=["submitted", "scheduled", "starting", "error", "running", "cleaning up", "terminated"], help='Show only executions with this status')
    argparser_app_list.add_argument('--earlier-than-submit', help='Show only executions submitted earlier than this timestamp (seconds since UTC epoch)')
211 212 213 214 215
    argparser_app_list.add_argument('--earlier-than-start', help='Show only executions started earlier than this timestamp (seconds since UTC epoch)')
    argparser_app_list.add_argument('--earlier-than-end', help='Show only executions ended earlier than this timestamp (seconds since UTC epoch)')
    argparser_app_list.add_argument('--later-than-submit', help='Show only executions submitted later than this timestamp (seconds since UTC epoch)')
    argparser_app_list.add_argument('--later-than-start', help='Show only executions started later than this timestamp (seconds since UTC epoch)')
    argparser_app_list.add_argument('--later-than-end', help='Show only executions ended later than this timestamp (seconds since UTC epoch)')
216 217
    argparser_app_list.set_defaults(func=exec_list_cmd)

218 219 220 221
    argparser_execution_get = subparser.add_parser('exec-get', help="Get execution status")
    argparser_execution_get.add_argument('id', type=int, help="Execution id")
    argparser_execution_get.set_defaults(func=exec_get_cmd)

222
    argparser_app_get = subparser.add_parser('exec-app-get', help="Retrieve an already defined application description")
223
    argparser_app_get.add_argument('id', help='The ID of the application')
224
    argparser_app_get.set_defaults(func=app_get_cmd)
225 226 227 228 229

    argparser_execution_kill = subparser.add_parser('terminate', help="Terminates an execution")
    argparser_execution_kill.add_argument('id', type=int, help="Execution id")
    argparser_execution_kill.set_defaults(func=exec_kill_cmd)

Daniele Venzano's avatar
Daniele Venzano committed
230 231 232
    argparser_execution_rm = subparser.add_parser('exec-rm', help="Deletes an execution")
    argparser_execution_rm.add_argument('id', type=int, help="Execution id")
    argparser_execution_rm.set_defaults(func=exec_rm_cmd)
233

234 235 236
    argparser_stats = subparser.add_parser('stats', help="Prints all available statistics")
    argparser_stats.set_defaults(func=stats_cmd)

237 238 239 240
    return parser, parser.parse_args()


def zoe():
241
    """Main entrypoint."""
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    parser, args = process_arguments()
    if args.debug:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)
    logging.getLogger("requests").setLevel(logging.WARNING)

    if not hasattr(args, "func"):
        parser.print_help()
        return

    if 'ZOE_URL' not in os.environ or 'ZOE_USER' not in os.environ or 'ZOE_PASS' not in os.environ:
        parser.print_help()
        return

    try:
        args.func(args)
    except ZoeAPIException as e:
        print(e.message)
    sys.exit(0)