from flask import request, g, abort from flask_restx import Resource, fields from pyinfrabox.utils import validate_uuid from pyinfraboxutils.ibflask import OK from pyinfraboxutils.ibrestplus import api, response_model from pyinfraboxutils.token import encode_project_token ns = api.namespace('Tokens', path='/api/v1/projects/<project_id>/tokens', description='Token related operations') project_token_model = api.model( 'ProjectToken', { 'description': fields.String(required=True), 'scope_push': fields.Boolean(required=True), 'scope_pull': fields.Boolean(required=True), 'id': fields.String(required=False) }) @ns.route('/') @api.doc(responses={403: 'Not Authorized'}) class Tokens(Resource): @api.marshal_list_with(project_token_model) def get(self, project_id): ''' Returns project's tokens ''' p = g.db.execute_many_dict( '''
import re from flask import request, g, abort from flask_restplus import Resource, fields from pyinfrabox.utils import validate_uuid from pyinfraboxutils.ibflask import OK from pyinfraboxutils.ibrestplus import api, response_model from pyinfraboxutils.secrets import encrypt_secret ns = api.namespace('Secrets', path='/api/v1/projects/<project_id>/secrets', description='Secret related operations') secret_model = api.model('Secret', { 'name': fields.String(required=True), 'id': fields.String(required=True), }) add_secret_model = api.model( 'AddSecret', { 'name': fields.String(required=True, max_length=255), 'value': fields.String(required=True, max_length=1024 * 128), }) @ns.route('/') @api.doc(responses={403: 'Not Authorized'}) class Secrets(Resource): name_pattern = re.compile('^[a-zA-Z0-9_]+$')
from flask import request, g, abort from flask_restx import Resource, fields from pyinfraboxutils.ibflask import OK from pyinfraboxutils.ibrestplus import api, response_model ns = api.namespace('Vault', path='/api/v1/projects/<project_id>/vault', description='Vault service related operations') project_vault_model = api.model( 'VaultService', { 'name': fields.String(required=True), 'url': fields.String(required=True), 'namespace': fields.String(required=False), 'version': fields.String(required=True), 'token': fields.String(required=True), 'ca': fields.String(required=False), 'role_id': fields.String(required=False), 'secret_id': fields.String(required=False), 'id': fields.String(required=False) }) @ns.route('/') @api.doc(responses={403: 'Not Authorized'}) class Tokens(Resource): @api.marshal_with(project_vault_model) def get(self, project_id): '''one Returns project's vault service
from uuid import UUID from flask import request, g, abort from flask_restx import Resource, fields from pyinfraboxutils.ibflask import OK from pyinfraboxutils.ibopa import opa_push_collaborator_data from pyinfraboxutils.ibrestplus import api, response_model ns = api.namespace('Collaborators', path='/api/v1/projects/<project_id>/collaborators', description='Collaborator related operations') collaborator_model = api.model( 'Collaborator', { 'name': fields.String(required=True), 'id': fields.String(required=False), 'email': fields.String(required=False), 'avatar_url': fields.String(required=False), 'username': fields.String(required=False), 'role': fields.String(required=False) }) add_collaborator_model = api.model( 'AddCollaborator', { 'username': fields.String(required=True), 'role': fields.String(required=False) }) change_collaborator_model = api.model('AddCollaborator', {'role': fields.String(required=True)})
from flask import g from flask_restx import Resource from pyinfraboxutils.ibrestplus import api ns = api.namespace('Commits', path='/api/v1/projects/<project_id>/commits', description='Commit related operations', doc=False) @ns.route('/<commit_id>', doc=False) @api.response(403, 'Not Authorized') class Commit(Resource): def get(self, project_id, commit_id): p = g.db.execute_many_dict(''' SELECT c.* FROM commit c WHERE c.id = %s AND c.project_id = %s ''', [commit_id, project_id]) return p
import os import requests from flask import g, abort, request from flask_restplus import Resource, fields from pyinfrabox.utils import validate_uuid from pyinfraboxutils import get_logger, get_root_url from pyinfraboxutils.ibrestplus import api, response_model from pyinfraboxutils.ibflask import OK from pyinfraboxutils.ibopa import opa_push_project_data, opa_push_collaborator_data from Crypto.PublicKey import RSA ns = api.namespace('Projects', path='/api/v1/projects', description='Project related operations') logger = get_logger('project') project_model = api.model( 'Project', { 'id': fields.String(required=True), 'name': fields.String(required=True), 'type': fields.String(required=True), 'public': fields.Boolean(required=True), 'userrole': fields.String(required=False) }) add_project_schema = { 'type': "object",
from werkzeug.datastructures import FileStorage from pyinfrabox.utils import validate_uuid4 from pyinfrabox.badge import validate_badge from pyinfrabox.markup import validate_markup from pyinfrabox.testresult import validate_result from pyinfrabox import ValidationError from pyinfraboxutils import get_env from pyinfraboxutils.token import encode_job_token from pyinfraboxutils.ibrestplus import api from pyinfraboxutils.ibflask import job_token_required, app from pyinfraboxutils.storage import storage from pyinfraboxutils.secrets import decrypt_secret ns = api.namespace('api/job', description='Job runtime related operations') def allowed_file(filename, extensions): return '.' in filename and filename.rsplit('.', 1)[1].lower() in extensions def delete_file(path): if os.path.exists(path): try: os.remove(path) except Exception as error: app.logger.warn("Failed to delete file: %s", error) @ns.route("/job")
import os from flask_restplus import Resource, fields from pyinfraboxutils.ibrestplus import api ns = api.namespace('Settings', path='/api/v1/settings', description='Settings related operations') settings_model = api.model( 'User', { 'INFRABOX_GITHUB_ENABLED': fields.Boolean, 'INFRABOX_GERRIT_ENABLED': fields.Boolean, 'INFRABOX_ACCOUNT_SIGNUP_ENABLED': fields.Boolean, 'INFRABOX_ACCOUNT_LDAP_ENABLED': fields.Boolean, 'INFRABOX_ROOT_URL': fields.String, 'INFRABOX_GENERAL_REPORT_ISSUE_URL': fields.String, 'INFRABOX_CLUSTER_NAME': fields.String, 'INFRABOX_GITHUB_LOGIN_ENABLED': fields.Boolean, }) @ns.route('') class Settings(Resource): @api.marshal_with(settings_model) def get(self): ''' Returns the cluster settings ''' github_enabled = os.environ['INFRABOX_GITHUB_ENABLED'] == 'true'
import os from flask import jsonify from flask_restplus import Resource from pyinfraboxutils.ibrestplus import api settings_ns = api.namespace('api/v1/settings', description="Api settings") @settings_ns.route('/') class Settings(Resource): def get(self): github_enabled = os.environ['INFRABOX_GITHUB_ENABLED'] == 'true' o = { 'INFRABOX_GITHUB_ENABLED': github_enabled, 'INFRABOX_GERRIT_ENABLED': os.environ['INFRABOX_GERRIT_ENABLED'] == 'true', 'INFRABOX_ACCOUNT_SIGNUP_ENABLED': os.environ['INFRABOX_ACCOUNT_SIGNUP_ENABLED'] == 'true', 'INFRABOX_ACCOUNT_LDAP_ENABLED': os.environ['INFRABOX_ACCOUNT_LDAP_ENABLED'] == 'true', 'INFRABOX_ROOT_URL': os.environ['INFRABOX_ROOT_URL'], 'INFRABOX_GENERAL_REPORT_ISSUE_URL': os.environ['INFRABOX_GENERAL_REPORT_ISSUE_URL'] } if github_enabled: o['INFRABOX_GITHUB_LOGIN_ENABLED'] = os.environ[
#pylint: disable=unused-argument from flask import g, jsonify, abort, send_file from flask_restplus import Resource, fields from pyinfraboxutils.ibflask import check_job_belongs_to_project from pyinfraboxutils.ibrestplus import api from pyinfraboxutils.storage import storage ns = api.namespace('Jobs', path='/api/v1/projects/<project_id>/jobs/<job_id>', description='Settings related operations') limits_model = api.model( 'LimitsModel', { 'cpu': fields.Integer(min=1, attribute='cpu'), 'memory': fields.Integer(min=128, attribute='memory') }) dependency_model = api.model( 'DependencyModel', { 'on': fields.List(fields.String), 'job': fields.String, 'job-id': fields.String }) resource_model = api.model('ResourceModel', {'limits': fields.Nested(limits_model)}) job_model = api.model( 'JobModel', { 'id': fields.String,
import os from flask import jsonify from flask_restplus import Resource from pyinfraboxutils.ibrestplus import api project = api.namespace('api/dashboard/projects/', description='Project related operations') settings = api.namespace('api/dashboard/settings/', description='Settings') user = api.namespace('api/dashboard/user/', description='User') account = api.namespace('api/dashboard/account/', description='Account') github = api.namespace('api/dashboard/github/', description='GitHub') github_auth = api.namespace('github/', description='GitHub Auth') @settings.route('/') class Settings(Resource): def get(self): o = {
from flask import g, abort from flask_restx import Resource, fields from pyinfraboxutils.ibflask import OK from pyinfraboxutils.ibrestplus import api ns = api.namespace('User', path='/api/v1/user', description='User related operations') user_model = api.model('User', { 'github_id': fields.Integer, 'username': fields.String, 'avatar_url': fields.String, 'name': fields.String, 'email': fields.String, 'id': fields.String, 'role': fields.String(enum=['user', 'devops', 'admin']) }) @ns.route('') @api.doc(responses={403: 'Not Authorized'}) @api.doc(responses={404: 'User not found'}) class User(Resource): @api.marshal_with(user_model) def get(self): ''' Returns information about the current user '''
import re from flask import request, g, abort from flask_restx import Resource, fields from pyinfrabox.utils import validate_uuid from pyinfraboxutils.ibflask import OK from pyinfraboxutils.ibrestplus import api, response_model ns = api.namespace('SSHKeys', path='/api/v1/projects/<project_id>/sshkeys', description='SSH Key related operations') sshkey_model = api.model( 'CronJob', { 'name': fields.String(required=True), 'id': fields.String(required=True), 'secret': fields.String(required=True), }) add_sshkey_model = api.model( 'AddCronJob', { 'name': fields.String(required=True, max_length=255), 'secret': fields.String(required=True, max_length=255), }) @ns.route('/') @api.doc(responses={403: 'Not Authorized'}) class SSHKeys(Resource):
import os from email.utils import parseaddr from flask import g, request, abort from flask_restplus import Resource, fields import bcrypt from pyinfraboxutils import get_logger from pyinfraboxutils.ibflask import OK from pyinfraboxutils.ibrestplus import api, response_model from pyinfraboxutils.token import encode_user_token ns = api.namespace('Account', path='/api/v1/account', description='Account related operations') login_model = api.model( 'Login', { 'email': fields.String(required=True), 'password': fields.String(required=True), }) logger = get_logger('login') @ns.route('/login') class Login(Resource): @api.expect(login_model) @api.response(200, 'Success', response_model)
from flask import g from flask_restplus import Resource, fields from pyinfraboxutils.ibrestplus import api from api.handlers.job import job_model ns = api.namespace('Builds', path='/api/v1/projects/<project_id>/builds/', description='Build related operations', params={ 'project_id': 'The project ID', 'build_id': 'The build ID' }) build_model = api.model( 'BuildModel', { 'id': fields.String, 'build_number': fields.Integer, 'restart_counter': fields.Integer }) @ns.route('/') @api.doc(responses={403: 'Not Authorized'}) class Builds(Resource): @api.marshal_list_with(build_model) def get(self, project_id): ''' Returns the latest 100 builds of the project '''
from flask import g, abort from flask_restplus import Resource from pyinfraboxutils.ibflask import auth_required from pyinfraboxutils.ibrestplus import api ns = api.namespace('api/v1/user', description="Users related operations") @ns.route('/') class User(Resource): @auth_required(['user'], check_project_access=False) def get(self): user = g.db.execute_one_dict(''' SELECT github_id, username, avatar_url, name, email FROM "user" WHERE id = %s ''', [g.token['user']['id']]) if not user: abort(404) return user
from flask import g from flask_restplus import Resource, fields from pyinfraboxutils.ibflask import auth_required from pyinfraboxutils.ibrestplus import api from job import job_model ns = api.namespace('api/v1/projects/<project_id>/builds', description='Build related operations') build_model = api.model( 'BuildModel', { 'id': fields.String, 'build_number': fields.Integer, 'restart_counter': fields.Integer }) @ns.route('/') class Builds(Resource): @auth_required(['user', 'project']) @api.marshal_list_with(build_model) def get(self, project_id): p = g.db.execute_many_dict( ''' SELECT id, build_number, restart_counter FROM build WHERE project_id = %s ORDER BY build_number DESC, restart_counter DESC LIMIT 100
import re from flask import request, g, abort from flask_restplus import Resource, fields from pyinfrabox.utils import validate_uuid from pyinfraboxutils.ibflask import OK from pyinfraboxutils.ibrestplus import api, response_model from croniter import croniter ns = api.namespace('CronJobs', path='/api/v1/projects/<project_id>/cronjobs', description='Cronjob related operations') cronjob_model = api.model('CronJob', { 'name': fields.String(required=True), 'id': fields.String(required=True), 'minute': fields.String(required=True), 'hour': fields.String(required=True), 'day_month': fields.String(required=True), 'month': fields.String(required=True), 'day_week': fields.String(required=True), 'sha': fields.String(required=True), 'infrabox_file': fields.String(required=True, max_length=255), }) add_cronjob_model = api.model('AddCronJob', { 'name': fields.String(required=True, max_length=255), 'minute': fields.String(required=True, max_length=255), 'hour': fields.String(required=True, max_length=255),
import requests from flask import g, abort, Response, send_file, request from flask_restplus import Resource, fields from pyinfraboxutils import get_logger from pyinfraboxutils.ibflask import OK from pyinfraboxutils.ibrestplus import api, response_model from pyinfraboxutils.storage import storage from pyinfraboxutils.token import encode_user_token logger = get_logger('api') ns = api.namespace('Jobs', path='/api/v1/projects/<project_id>/jobs/', description='Commit related operations') @ns.route('/', doc=False) @api.response(403, 'Not Authorized') class Jobs(Resource): def get(self, project_id): jobs = g.db.execute_many_dict(''' WITH github_builds AS ( --- get the last 10 builds for each branch SELECT builds.id, builds.project_id, builds.commit_id, null::uuid as source_upload_id, build_number, restart_counter FROM ( SELECT b.id, c.id as commit_id, p.id as project_id, ROW_NUMBER() OVER(PARTITION BY p.id ORDER BY build_number DESC, restart_counter DESC) AS r, build_number, restart_counter FROM build b INNER JOIN project p
#pylint: disable=unused-argument from flask import g, jsonify, abort, send_file from flask_restplus import Resource, fields from pyinfraboxutils.ibflask import auth_required, check_job_belongs_to_project from pyinfraboxutils.ibrestplus import api from pyinfraboxutils.storage import storage ns = api.namespace('api/v1/projects/<project_id>/jobs', description='Job related operations', tag="test") limits_model = api.model( 'LimitsModel', { 'cpu': fields.Integer(min=1, attribute='cpu'), 'memory': fields.Integer(min=128, attribute='memory') }) dependency_model = api.model( 'DependencyModel', { 'on': fields.List(fields.String), 'job': fields.String, 'job-id': fields.String }) resource_model = api.model('ResourceModel', {'limits': fields.Nested(limits_model)}) job_model = api.model( 'JobModel', { 'id': fields.String,
from datetime import datetime from flask import g, request from flask_restplus import Resource from pyinfraboxutils.ibrestplus import api ns = api.namespace('Internal', path='/internal/api', description='Project related operations') @ns.route("/job/consoleupdate", doc=False) class ConsoleUpdate(Resource): def post(self): records = request.json data = {} for r in records: if 'kubernetes' not in r: continue job_id = r['kubernetes']['labels']['job-name'][:-4] a = float(r['date']) date = datetime.fromtimestamp(float(a)).strftime("%H:%M:%S") log = "%s|%s" % (date, r['log']) log = log.replace('\x00', '\n') if not data.get(job_id): data[job_id] = ""
from pyinfraboxutils.ibrestplus import api admin = api.namespace('api/v1/admin/', description='Admin') project = api.namespace('api/v1/projects/', description='Project related operations') settings = api.namespace('api/v1/settings/', description='Settings') user = api.namespace('api/v1/user/', description='User') account = api.namespace('api/v1/account/', description='Account') github = api.namespace('api/v1/github/', description='GitHub') github_auth = api.namespace('github/', description='GitHub Auth')