Beispiel #1
0
    def post(self):
        """Add a container specification to Mozart"""
        name = request.form.get('name', request.args.get('name', None))
        url = request.form.get('url', request.args.get('url', None))
        version = request.form.get('version',
                                   request.args.get('version', None))
        digest = request.form.get('digest', request.args.get('digest', None))

        if not all((name, url, version, digest)):
            return {
                'success':
                False,
                'message':
                'Parameters (name, url, version, digest) must be supplied'
            }, 400

        container_obj = {
            'id': name,
            'digest': digest,
            'url': url,
            'version': version
        }
        mozart_es.index_document(index=CONTAINERS_INDEX,
                                 body=container_obj,
                                 id=name)

        return {
            'success': True,
            'message': "%s added to index %s" % (name, CONTAINERS_INDEX),
            'result': name
        }
Beispiel #2
0
    def post(self):
        """Add a HySDS IO specification"""
        spec = request.form.get('spec', request.args.get('spec', None))
        if spec is None:
            app.logger.error("spec not specified")
            raise Exception("'spec' must be supplied")

        try:
            obj = json.loads(spec)
            _id = obj['id']
        except (ValueError, KeyError, json.decoder.JSONDecodeError,
                Exception) as e:
            return {'success': False, 'message': e}, 400

        mozart_es.index_document(index=HYSDS_IOS_INDEX, body=obj, id=_id)
        return {
            'success': True,
            'message': "%s added to index %s" % (_id, HYSDS_IOS_INDEX),
            'result': _id
        }
Beispiel #3
0
    def post(self):
        """Add a Job Type specification JSON object."""
        spec = request.form.get('spec', request.args.get('spec', None))
        if spec is None:
            return {'success': False, 'message': 'spec object missing'}, 400

        try:
            obj = json.loads(spec)
            _id = obj['id']
        except (ValueError, KeyError, json.decoder.JSONDecodeError,
                Exception) as e:
            return {'success': False, 'message': e}, 400

        mozart_es.index_document(index=JOB_SPECS_INDEX, body=obj, id=_id)
        return {
            'success': True,
            'message':
            "job_spec %s added to index %s" % (_id, HYSDS_IOS_INDEX),
            'result': _id
        }
Beispiel #4
0
    def post(self):
        """create new user rule"""
        user_rules_index = app.config['USER_RULES_INDEX']

        request_data = request.json or request.form
        rule_name = request_data.get('rule_name')
        hysds_io = request_data.get('workflow')
        job_spec = request_data.get('job_spec')
        priority = int(request_data.get('priority', 0))
        query_string = request_data.get('query_string')
        kwargs = request_data.get('kwargs', '{}')
        queue = request_data.get('queue')
        tags = request_data.get('tags', [])
        time_limit = request_data.get('time_limit', None)
        soft_time_limit = request_data.get('soft_time_limit', None)
        disk_usage = request_data.get('disk_usage', None)
        enable_dedup = request_data.get('enable_dedup')
        if enable_dedup is not None:
            try:
                enable_dedup = inputs.boolean(enable_dedup)
            except ValueError as e:
                return {'success': False, 'message': str(e)}, 400

        username = "******"  # TODO: add user role and permissions, hard coded to "ops" for now

        if not rule_name or not hysds_io or not job_spec or not query_string or not queue:
            missing_params = []
            if not rule_name:
                missing_params.append('rule_name')
            if not hysds_io:
                missing_params.append('workflow')
            if not job_spec:
                missing_params.append('job_spec')
            if not query_string:
                missing_params.append('query_string')
            if not queue:
                missing_params.append('queue')
            return {
                'success': False,
                'message':
                'Params not specified: %s' % ', '.join(missing_params),
                'result': None,
            }, 400

        if len(rule_name) > 64:
            return {
                "success": False,
                "message": "rule_name needs to be less than 64 characters",
                "result": None,
            }, 400

        try:
            json.loads(query_string)
        except (ValueError, TypeError) as e:
            app.logger.error(e)
            return {
                'success': False,
                'message': 'invalid elasticsearch query JSON'
            }, 400

        try:
            json.loads(kwargs)
        except (ValueError, TypeError) as e:
            app.logger.error(e)
            return {'success': False, 'message': 'invalid JSON: kwargs'}, 400

        # check if rule name already exists
        rule_exists_query = {"query": {"term": {"rule_name": rule_name}}}
        existing_rules_count = mozart_es.get_count(index=user_rules_index,
                                                   body=rule_exists_query)
        if existing_rules_count > 0:
            return {
                'success': False,
                'message': 'user rule already exists: %s' % rule_name
            }, 409

        # check if job_type (hysds_io) exists in elasticsearch
        job_type = mozart_es.get_by_id(index=HYSDS_IOS_INDEX,
                                       id=hysds_io,
                                       ignore=404)
        if job_type['found'] is False:
            return {
                'success': False,
                'message': '%s not found' % hysds_io
            }, 400

        params = job_type['_source']['params']
        is_passthrough_query = check_passthrough_query(params)

        if type(tags) == str:
            tags = [tags]

        now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
        new_doc = {
            "workflow": hysds_io,
            "job_spec": job_spec,
            "priority": priority,
            "rule_name": rule_name,
            "username": username,
            "query_string": query_string,
            "kwargs": kwargs,
            "job_type": hysds_io,
            "enabled": True,
            "passthru_query": is_passthrough_query,
            "query_all": False,
            "queue": queue,
            "modified_time": now,
            "creation_time": now,
            "tags": tags
        }

        if time_limit and isinstance(time_limit, int):
            if time_limit <= 0 or time_limit > 86400 * 7:
                return {
                    'success': False,
                    'message': 'time_limit must be between 0 and 604800 (sec)'
                }, 400
            else:
                new_doc['time_limit'] = time_limit

        if soft_time_limit and isinstance(soft_time_limit, int):
            if soft_time_limit <= 0 or soft_time_limit > 86400 * 7:
                return {
                    'success': False,
                    'message':
                    'soft_time_limit must be between 0 and 604800 (sec)'
                }, 400
            else:
                new_doc['soft_time_limit'] = soft_time_limit

        if disk_usage:
            new_doc['disk_usage'] = disk_usage
        if enable_dedup is not None:
            new_doc['enable_dedup'] = enable_dedup

        result = mozart_es.index_document(index=user_rules_index,
                                          body=new_doc,
                                          refresh=True)
        return {'success': True, 'message': 'rule created', 'result': result}