def get_component_by_id(user, c_id): component = v1_utils.verify_existence_and_get(c_id, _TABLE) topic = v1_utils.verify_existence_and_get(component['topic_id'], models.TOPICS) export_control.verify_access_to_topic(user, topic) return base.get_resource_by_id(user, component, _TABLE, _EMBED_MANY)
def update_components(user, c_id): component = v1_utils.verify_existence_and_get(c_id, _TABLE) if_match_etag = utils.check_and_get_etag(flask.request.headers) topic = v1_utils.verify_existence_and_get(component['topic_id'], models.TOPICS) export_control.verify_access_to_topic(user, topic) values = flask.request.json check_json_is_valid(update_component_schema, values) values['etag'] = utils.gen_etag() where_clause = sql.and_( _TABLE.c.etag == if_match_etag, _TABLE.c.id == c_id ) query = _TABLE.update().returning(*_TABLE.columns).where(where_clause).\ values(**values) result = flask.g.db_conn.execute(query) if not result.rowcount: raise dci_exc.DCIConflict('Component', c_id) return flask.Response( json.dumps({'component': result.fetchone()}), 200, headers={'ETag': values['etag']}, content_type='application/json' )
def create_jobs(user): values = flask.request.json check_json_is_valid(create_job_schema, values) values.update(v1_utils.common_values_dict()) components_ids = values.pop('components') if user.is_not_remoteci(): raise dci_exc.DCIException('Only remoteci can create job') topic_id = values.get('topic_id') topic = v1_utils.verify_existence_and_get(topic_id, models.TOPICS) export_control.verify_access_to_topic(user, topic) previous_job_id = values.get('previous_job_id') if previous_job_id: v1_utils.verify_existence_and_get(previous_job_id, _TABLE) values.update({ 'status': 'new', 'remoteci_id': user.id, 'topic_id': topic_id, 'user_agent': flask.request.environ.get('HTTP_USER_AGENT'), 'client_version': flask.request.environ.get('HTTP_CLIENT_VERSION'), 'previous_job_id': previous_job_id, 'team_id': user.teams_ids[0], 'product_id': topic['product_id'], 'duration': 0 }) # create the job and feed the jobs_components table with flask.g.db_conn.begin(): query = _TABLE.insert().values(**values) flask.g.db_conn.execute(query) jobs_components_to_insert = [] for cmpt_id in components_ids: v1_utils.verify_existence_and_get(cmpt_id, models.COMPONENTS) jobs_components_to_insert.append({ 'job_id': values['id'], 'component_id': cmpt_id }) if jobs_components_to_insert: flask.g.db_conn.execute(models.JOIN_JOBS_COMPONENTS.insert(), jobs_components_to_insert) return flask.Response(json.dumps({'job': values}), 201, headers={'ETag': values['etag']}, content_type='application/json')
def get_topic_by_id(user, topic_id): args = check_and_get_args(flask.request.args.to_dict()) topic = v1_utils.verify_existence_and_get(topic_id, _TABLE) if user.is_not_super_admin() and user.is_not_epm() and user.is_not_feeder( ): # noqa if not user.is_read_only_user(): export_control.verify_access_to_topic(user, topic) if 'teams' in args['embed']: raise dci_exc.Unauthorized() return base.get_resource_by_id(user, topic, _TABLE, _EMBED_MANY)
def download_component_file(user, c_id, f_id): store = dci_config.get_store('components') component = v1_utils.verify_existence_and_get(c_id, _TABLE) topic = v1_utils.verify_existence_and_get(component['topic_id'], models.TOPICS) export_control.verify_access_to_topic(user, topic) component_file = v1_utils.verify_existence_and_get( f_id, models.COMPONENT_FILES) file_path = files_utils.build_file_path(component['topic_id'], c_id, f_id) # Check if file exist on the storage engine store.head(file_path) _, file_descriptor = store.get(file_path) return flask.send_file(file_descriptor, mimetype=component_file['mime'])
def list_components_files(user, c_id): component = v1_utils.verify_existence_and_get(c_id, _TABLE) topic = v1_utils.verify_existence_and_get(component['topic_id'], models.TOPICS) export_control.verify_access_to_topic(user, topic) args = check_and_get_args(flask.request.args.to_dict()) query = v1_utils.QueryBuilder(models.COMPONENTFILES, args, _CF_COLUMNS) query.add_extra_condition(models.COMPONENTFILES.c.component_id == c_id) nb_rows = query.get_number_of_rows(models.COMPONENTFILES, models.COMPONENTFILES.c.component_id == c_id) # noqa rows = query.execute(fetchall=True) rows = v1_utils.format_result(rows, models.COMPONENTFILES.name, None, None) return flask.jsonify({'component_files': rows, '_meta': {'count': nb_rows}})
def get_component_file(user, c_id, f_id): component = v1_utils.verify_existence_and_get(c_id, _TABLE) topic = v1_utils.verify_existence_and_get(component['topic_id'], models.TOPICS) export_control.verify_access_to_topic(user, topic) COMPONENT_FILES = models.COMPONENT_FILES where_clause = sql.and_(COMPONENT_FILES.c.id == f_id, COMPONENT_FILES.c.component_id == c_id) query = sql.select([COMPONENT_FILES]).where(where_clause) component_f = flask.g.db_conn.execute(query).fetchone() if component_f is None: raise dci_exc.DCINotFound('Component File', f_id) res = flask.jsonify({'component_file': component_f}) return res
def get_all_components(user, topic_id): topic = v1_utils.verify_existence_and_get(topic_id, _TABLE) export_control.verify_access_to_topic(user, topic) return components.get_all_components(user, topic_id=topic['id'])
def schedule_jobs(user): """Dispatch jobs to remotecis. The remoteci can use this method to request a new job. Before a job is dispatched, the server will flag as 'killed' all the running jobs that were associated with the remoteci. This is because they will never be finished. """ values = flask.request.json check_json_is_valid(schedule_job_schema, values) values.update({ 'id': utils.gen_uuid(), 'created_at': get_utc_now().isoformat(), 'updated_at': get_utc_now().isoformat(), 'etag': utils.gen_etag(), 'status': 'new', 'remoteci_id': user.id, 'duration': 0, 'user_agent': flask.request.environ.get('HTTP_USER_AGENT'), 'client_version': flask.request.environ.get('HTTP_CLIENT_VERSION'), }) topic_id = values.pop('topic_id') dry_run = values.pop('dry_run') if dry_run: component_types = components.get_component_types_from_topic(topic_id) _components = components.get_last_components_by_type( component_types, topic_id) return flask.Response(json.dumps({ 'components': _components, 'job': None }), 201, content_type='application/json') # check remoteci remoteci = v1_utils.verify_existence_and_get(user.id, models.REMOTECIS) if remoteci['state'] != 'active': message = 'RemoteCI "%s" is disabled.' % remoteci['id'] raise dci_exc.DCIException(message, status_code=412) # check primary topic topic = v1_utils.verify_existence_and_get(topic_id, models.TOPICS) product_id = topic['product_id'] if topic['state'] != 'active': msg = 'Topic %s:%s not active.' % (topic_id, topic['name']) raise dci_exc.DCIException(msg, status_code=412) export_control.verify_access_to_topic(user, topic) # check secondary topic topic_id_secondary = values.pop('topic_id_secondary') if topic_id_secondary: topic_secondary = v1_utils.verify_existence_and_get( topic_id_secondary, models.TOPICS) if topic_secondary['state'] != 'active': msg = 'Topic %s:%s not active.' % (topic_id_secondary, topic['name']) raise dci_exc.DCIException(msg, status_code=412) export_control.verify_access_to_topic(user, topic_secondary) remotecis.kill_existing_jobs(remoteci['id']) components_ids = values.pop('components_ids') values = _build_job(product_id, topic_id, remoteci, components_ids, values, topic_id_secondary=topic_id_secondary) return flask.Response(json.dumps({'job': values}), 201, headers={'ETag': values['etag']}, content_type='application/json')