def code2build(): payload = request.json user_id = g.user.id puid = payload.get('p_uid',0) try: user = session.query(User.name, User.spwd, Url.path ,Url.aliasname).join(R_User_Url, R_User_Url.user_id == User.id).\ join(Url,R_User_Url.url_id == Url.id).filter(User.id == user_id).filter(Url.uid == puid).first() except Exception as e: abort(Response(e,status=400)) # print('---user----',user) if user : username = user[0] spwd = dencryption(user[1]) path = user[2] proname = user[3] #Url.aliasname 非汉字 # logging.info('********',username,path,proname) ssh = SSH() try: ssh.conn(DK_ADDRESS, 'build', '******') #连接build进行打包 for ss in ssh.line_buffered(ssh.Execmd('bash /home/build/code2build.sh {} {} {}'.format(path, username, spwd))): print(ss) except Exception as e: logging.error(e) abort(Response('connect error',status=500)) # docker 构建 镜像 try: with Docker() as dk: dk.build(username) dk.create_docker(username,proname) container_id = dk.container.id[:12] container_name = dk.container.name container_port = dk.fin_port except Exception as e: logging.error(e) abort(Response(e,status=500)) post = Post() try: post.svn_url = path #url path post.project_name = proname #Url.aliasname post.author_id = g.user.id post.container_name = container_name post.container_id = container_id post.port = container_port except Exception as e: logging.error(e) session.add(post) try: session.commit() except Exception as e: session.rollback() with Docker() as dk: dk.delete(container_id) abort(Response(e,status=500)) return Response(status=200) # web_url = '{}:{}'.format(dkcfg.LOCAL_HOST, container_port) else: abort(Response('not found!',status=400))
def get_path(): user_id = g.user.id paths = session.query(User.id,Url.name, Url.uid,Url.path).join(R_User_Url, R_User_Url.user_id == User.id).\ join(Url,R_User_Url.url_id == Url.id).filter(User.id == user_id).all() return jsonify(path=[ {'pro_name':name, 'p_uid':uid, 'spath':url } for _,name,uid,url in paths])
def dk_list(): user_id = g.user.id clist = session.query(Post).filter(Post.author_id == user_id).all() # print('clist------',clist) return jsonify(posts=[ {'project_name': l.project_name, 'svn_url': l.svn_url, # 'container_name':l.container_name, 'ctr_name': l.container_name, 'ctr_id': l.container_id, # 'status':[l.state] 'ctr_port':l.port, 'status': l.state } for l in clist])
def login(): payload = request.json #账户email验证 name = payload.get('name') user = session.query(User).filter(User.name == name).first() session.close() #密码验证 try: if user and bcrypt.checkpw(payload['password'].encode(), user.password.encode()): #将登录的密码与数据库中的密码进行比较并进行相应的动作 return jsonify(id=user.id, name=user.name, token=gen_token(user.id)) else: abort(401) except Exception as e: logging.error('certification failed',e)
def check_auth_token(token): serializer = Serializer(secret_key=SECRET_KEY) try: s = serializer.loads(token) except BadSignature: # raise exc.HTTPUnauthorized('token is invalid') abort(401) abort(Response('token is invalid')) except SignatureExpired: # raise exc.HTTPUnauthorized('token is expire') abort(401) abort(Response('token is expire')) user = session.query(User).get(s['user_id']) session.close() return user
def images(): payload = request.args id = g.user.id action = payload.get('action') container_id = payload.get('ctrid') with Docker() as dk: if action == 'start': dk.start(container_id) elif action == 'stop': dk.stop(container_id) elif action == 'delete': dk.delete(container_id) #数据库操作未同步更新 user = session.query(User).filter(User.id == id).first()
def reg(): payload = request.json name = payload.get('name') if session.query(User).filter(User.name == name).first() is not None: abort(409) abort(Response("{} is exist!".format(name))) try: user = User() user.name = payload.get('name') user.password = bcrypt.hashpw(payload['password'].encode(), bcrypt.gensalt()) #对密码进行加密,存放到数据库当中 # user.spwd = base64.b64encode(payload['password'].encode()) except Exception as e: print('reg error:',e) # exc.HTTPBadRequest() session.add(user) try: session.commit() return jsonify(id=user.id, name=user.name, token=gen_token(user.id)) except: session.rollback() abort(500) abort(Response('reg failed'))
def _wrapper(self,*args,**kwargs): base_ports = tuple(x for x in range(*dkcfg.ports)) # [8000-9000) ports = session.query(Post).values(Post.port) self.fin_port = random.choice(list(set(base_ports).difference(set(ports)))) return fn(self,*args,**kwargs)