def deploy_container_api(): form = request.form project = form['project'] if project == APPNAME_ERU_LB: raise ValueError('Unable to deploy eru-lb, do it on load balance page') p = Project.get_by_name(project) if not p.is_accessible(g.user): return {'reason': '你没有操作这个项目的权限'}, 403 version = form['version'] eru.deploy_private( group_name=g.user.group, pod_name=form['pod'], app_name=project, ncore=form.get('ncore', type=float), ncontainer=form.get('ncontainer', type=int), version=version, entrypoint=form['entrypoint'], env=form['env'], network_ids=form.getlist('network'), host_name=form.get('host'), args=form['extendargs'].split(' '), ) log = OPLog.create(g.user.id, OPLOG_ACTION.create_container) log.project_name = project log.image = '%s:%s' % (project, version)
def set_project_env(): form = request.form project = form['project'] env = form['env'] content = {} p = Project.get_by_name(project) if not p.is_accessible(g.user): return {'reason': '你没有操作这个项目的权限'}, 403 for line, i in enumerate(form['content'].split('\n')): if not i.strip(): continue kv = i.split('=', 1) if len(kv) == 1: raise ValueError('invalid env item %s at line %d' % (i, line + 1)) content[kv[0].strip()] = kv[1].strip() try: eru.set_app_env(project, env, **content) except Exception as e: print e.message log = OPLog.create(g.user.id, OPLOG_ACTION.set_project_env) log.data = content log.project_name = project
def api_register(): repo_url = request.form['repo_url'] app = _register_app(repo_url) project_name = app['appname'] log = OPLog.create(g.user.id, OPLOG_ACTION.create_project) log.project_name = project_name return project_name
def oplog(): date = request.args.get('date', None) if date: date = datetime.strptime(date, '%Y-%m-%d') # FIXME date = None logs = OPLog.get_by_user_id(g.user.id, kind=OPLOG_KIND.balancer, time=date, start=g.start, limit=g.limit) return render_template('/lb/logs.html', logs=logs)
def start_container(): cid = request.form['id'] name = eru.get_container(cid)['appname'] p = Project.get_by_name(name) if not p.is_accessible(g.user): return {'reason': '你没有操作这个项目的权限'}, 403 eru.start_container(cid) log = OPLog.create(g.user.id, OPLOG_ACTION.start_container) log.container_id = cid
def stop_container(): cid = request.form['id'] name = eru.get_container(cid)['appname'] if name == APPNAME_ERU_LB: raise ValueError('Unable to stop eru-lb, do it on load balance page') p = Project.get_by_name(name) if not p.is_accessible(g.user): return {'reason': '你没有操作这个项目的权限'}, 403 eru.stop_container(cid) log = OPLog.create(g.user.id, OPLOG_ACTION.stop_container) log.container_id = cid
def delete_balancer(balancer_id): balancer = Balancer.get(balancer_id) if not balancer: abort(404) if g.user.id != balancer.user_id: return {'msg': 'forbidden'}, 403 eru.remove_containers([balancer.container_id]) balancer.delete() log = OPLog.create(g.user.id, OPLOG_ACTION.delete_balancer) log.balancer_id = balancer_id log.container_id = balancer.container_id
def delete_record(record_id): r = BalanceRecord.get(record_id) if not r: return {'msg': 'not found'}, 404 if g.user.id != r.balancer.user_id: return {'msg': 'forbidden'}, 403 r.delete() log = OPLog.create(g.user.id, OPLOG_ACTION.delete_lb_record) log.balancer_id = r.balancer_id log.record_id = record_id log.data = {'domain': r.domain, 'appname': r.appname, 'entrypoint': r.entrypoint}
def grant_project(uid): user = User.get_by_uid(uid) if not user: return {'reason': '用户不存在'}, 404 name = request.form.get('project', '') p = Project.get_by_name(name) if not p: return {'reason': '项目不存在'}, 404 user.grant_project(name) log = OPLog.create(g.user.id, OPLOG_ACTION.grant_project) log.project_name = name log.acceptor = user.uid
def set_user(): u = User.get_by_uid(request.form['uid']) if u is None: raise ValueError('no such user') group = request.form['group'] if group: u.set_group(group) flags = request.form.getlist('privs') privilege = reduce(lambda x, y: x | y, [getattr(USER_ROLE, p.lower(), 0) for p in flags], 0) u.set_privilege(privilege) log = OPLog.create(g.user.id, OPLOG_ACTION.grant_privilege) log.privilege = privilege log.acceptor = u.uid
def _create_lb_container(pod, host): if not g.user.group: raise ValueError('you are not in a group') version = LB_IMAGE.split(':')[1] env = request.form['type'] entry = '%s-host' % env ncore = request.form.get('ncore', type=float, default=1) container_id = deploy_container(g.user.group, pod, entry, version, env, host, ncore) container = eru.get_container(container_id) b = Balancer.create(container['host'], g.user.group, g.user.id, container_id) log = OPLog.create(g.user.id, OPLOG_ACTION.create_balancer) log.container_id = container_id log.balancer_id = b.id log.data = {'host': host, 'pod': pod}
def api_build_image(): name = request.form['project'] app = eru.get_app(name) p = Project.get_by_name(name) if not p.is_accessible(g.user): return {'reason': '你没有操作这个项目的权限'}, 403 revision = request.form['revision'] _register_app(app['git'], revision) _get_project(app['git']) pod = request.form['pod'] image = 'docker-registry.intra.hunantv.com/nbeimage/%s' % request.form['image'] eru.build_image(g.user.group, pod, app['name'], image, revision) log = OPLog.create(g.user.id, OPLOG_ACTION.build_image) log.project_name = name log.image = '%s:%s' % (name, revision)
def records(balancer_id): balancer = Balancer.get(balancer_id) if not balancer: abort(404) if request.method == 'GET': return render_template('lb/balancer.html', b=balancer) appname = request.form['appname'] domain = request.form['domain'] entrypoint = request.form['entrypoint'] r = balancer.add_record(appname, entrypoint, domain) if r: log = OPLog.create(g.user.id, OPLOG_ACTION.create_lb_record) log.balancer_id = balancer_id log.record_id = r.id log.data = {'domain': domain, 'appname': appname, 'entrypoint': entrypoint} return redirect(url_for('lb.records', balancer_id=balancer_id))
def oplog(): logs = OPLog.get_by_user_id(g.user.id, kind=OPLOG_KIND.project, start=g.start, limit=g.limit) return render_template('/deploy/logs.html', logs=logs)
def oplog(): logs = OPLog.get_by_kind(OPLOG_KIND.admin) return render_template('/settings/logs.html', logs=logs)