async def reserve_environment(request): assign_type = request.args.get('assign_type', 'ci') assign_id = request.args.get('assign_id', request.remote_addr) product_id = request.args.get('product_id', 'ci') duration = float(request.args.get('duration', 0)) if duration <= 0: raise InvalidUsage('duration should be > 0') env = await Environment.find_one_and_update( {'$or': [ {'status': Environment.IDLE}, {'assign_to.id': assign_id, 'assign_to.type': assign_type}]}, {'$set': { 'status': Environment.BUSY, 'assign_to': { 'type': assign_type, 'product': product_id, 'id': assign_id, 'expire_in': duration + get_timestamp()}, 'update_time': get_timestamp()}}, return_document=ReturnDocument.AFTER) if not env: raise NotFound('no idle environment') return json(jsonify({'_id': env['_id'], 'name': env['name'], 'ip': env['ip']}))
async def create(cls, name, stock, price, label, flag=False): return await cls.insert_one({ 'name': name, 'stock': stock, 'price': price, 'label': label, 'flag': flag, 'create_time': get_timestamp(), 'update_time': get_timestamp() })
async def update_release(request): # TODO: update its features state _id = request.json.get('_id') if not _id: raise InvalidUsage('无效的请求参数!') name = request.json.get('name', '').strip() branch = request.json.get('branch', '').strip() product = ObjectId(request.json.get('product', '')) base_version = request.json.get('base_version') features = [ObjectId(f) for f in request.json.get('features', [])] if not name or not branch or not product or not features: raise InvalidUsage('参数错误!') record = await Release.find_one_and_update( {'_id': ObjectId(_id)}, {'$set': { 'name': name, 'branch': branch, 'product': product, 'base_version': base_version, 'features': features, 'update_time': get_timestamp(), }}) if not record: raise NotFound('找不到对应的release!') return json({})
async def update_environment(request): # NOTE: assign_to only be set on deployment _id = request.json.get('_id') if not _id: raise InvalidUsage('无效的请求参数!') force = request.json.get('force', False) name = request.json.get('name', '').strip() ip = request.json.get('ip', '').strip() user = request.json.get('user', '').strip() password = request.json.get('password', '').strip() filter_ = {'_id': ObjectId(_id)} update = { 'name': name, 'label': request.json.get('label', []), 'ip': ip, 'description': request.json.get('description', ''), 'user': user, 'password': password, 'settings': request.json.get('settings', {}), 'update_time': get_timestamp()} if 'status' in request.json: update['status'] = request.json['status'] if not force: filter_['status'] = {'$ne': Environment.BUSY} # idle <-> maint if not name or not ip: raise InvalidUsage('name和ip必须设置!') await Environment.find_one_and_update(filter_, {'$set': update}) return json({})
async def create(cls, product, user_id, total): return await cls.insert_one({ 'product': product, 'user_id': user_id, 'total': total, 'create_time': get_timestamp() })
async def close_feature(request): id_ = request.args.get('id') if not id_: raise InvalidUsage('无效的请求参数!') await Feature.find_one_and_update( {'_id': ObjectId(id_), 'status': {'$ne': Feature.DONE}}, {'$set': { 'status': Feature.CLOSED, 'update_time': get_timestamp()}}) return json({})
def create(cls, user_name, password, phone, sex, picture=None, score=0, role=0x03, money=0): return cls.create({ 'user_name': user_name, 'password': password, 'phone': phone, 'picture': picture, 'score': score, 'sex': sex, 'user_label': role, 'money': money, 'create_time': get_timestamp(), 'update_time': get_timestamp() })
async def close_release(request): id_ = request.args.get('id') if not id_: raise InvalidUsage('无效的请求参数!') record = await Release.find_one_and_update( {'_id': ObjectId(id_), 'status': {'$ne': Release.RELEASED}}, {'$set': { 'status': Release.CLOSED, 'update_time': get_timestamp()}}) if not record: raise NotFound('找不到对应的release!') return json({})
async def free_environment(request): assign_type = request.args.get('assign_type', 'ci') assign_id = request.args.get('assign_id', request.remote_addr) env_ip = request.args.get('ip', '') await Environment.find_one_and_update( { 'status': Environment.BUSY, 'ip': env_ip, 'assign_to.id': assign_id, 'assign_to.type': assign_type}, {'$set': { 'status': Environment.IDLE, 'assign_to': {}, 'update_time': get_timestamp()}}, return_document=ReturnDocument.AFTER) return json({})
async def update_product(request): _id = request.json.get('_id') if not _id: raise InvalidUsage('无效的请求参数!') name = request.json.get('name', '').strip() description = request.json.get('description', '').strip() components = [ObjectId(c) for c in request.json.get('components', [])] export_script = request.json.get('export_script', '').strip() deploy_script = request.json.get('deploy_script', '').strip() if not name: raise InvalidUsage('Product名称不能为空!') await Product.find_one_and_update( {'_id': ObjectId(_id)}, {'$set': { 'name': name, 'description': description, 'components': components, 'export_script': export_script, 'deploy_script': deploy_script, 'update_time': get_timestamp()}}) return json({})
async def update_feature(request): _id = request.json.get('_id') if not _id: raise InvalidUsage('无效的请求参数!') name = request.json.get('name', '').strip() description = request.json.get('description', '').strip() branch = request.json.get('branch', '').strip() product = ObjectId(request.json.get('product', '')) base_version = request.json.get('base_version') components = [ObjectId(c) for c in request.json.get('components', [])] if not name or not branch or not product or not components: raise InvalidUsage('参数错误!') await Feature.find_one_and_update( {'_id': ObjectId(_id)}, {'$set': { 'name': name, 'branch': branch, 'description': description, 'product': product, 'base_version': base_version, 'components': components, 'update_time': get_timestamp()}}) return json({})
async def update_component(request): _id = request.json.get('_id') if not _id: raise InvalidUsage('无效的请求参数!') name = request.json.get('name', '').strip() gitlab_project_id = request.json.get('gitlab_project_id') repo = request.json.get('repo', request.json.get('docker_repo', '')).strip() type_ = request.json.get('type', Component.TYPE_APP) dependencies = [ObjectId(c) for c in request.json.get('dependencies', [])] third_parties = request.json.get('third_parties', []) if not name or not gitlab_project_id: raise InvalidUsage('模块名称和Project ID不能为空!') await Component.find_one_and_update( {'_id': ObjectId(_id)}, {'$set': { 'name': name, 'gitlab_project_id': gitlab_project_id, 'repo': repo, 'type': type_, 'dependencies': dependencies, 'third_parties': third_parties, 'update_time': get_timestamp()}}) return json({})
async def create(cls, name): return await cls.insert_one({ 'name': name, 'create_time': get_timestamp(), 'update_time': get_timestamp() })