def search_server(): """ 根据服务id查询相关信息,用于修改server信息回显 :return: """ json_data = return_json(request) server_id = json_data.get("server_id", None) if not server_id: code = -1 msg = "server_id is required" return response_tem(code=code, msg=msg) try: search_param = {"is_delete": False, "id": server_id} data = Server.query.filter_by(**search_param).first() if not data: code = -1 msg = "没有找到该server" return response_tem(code=code, msg=msg) info = { "name": data.name, "ip": data.ip, "port": data.port, "user_name": data.user_name, "user_password": data.user_password } code = 0 msg = "查询成功" except Exception as e: logger.error(e) code = -1 msg = "查询失败" info = None return response_tem(code, data=info, msg=msg)
def delete_server(): """ 根据id删除某个server :return: """ json_data = return_json(request) server_id = json_data.get("server_id", None) if not server_id: code = -1 msg = "server_id is required" return response_tem(code=code, msg=msg) try: search_param = {"is_delete": False, "id": server_id} server_info = Server.query.filter_by(**search_param).first() if not server_info: code = -1 msg = "没找到该server, 可能已经删除" return response_tem(code=code, msg=msg) server_info.is_delete = True db.session.add(server_info) db.session.commit() code = 0 msg = "删除成功" except Exception as e: logger.error(e) code = -1 msg = "删除失败" return response_tem(code=code, msg=msg)
def files(): """ 查询已上传文件的信息, 包括文件名 文件大小, 文件类型 , 最后更新时间 :return: """ json_data = return_json(request) page = json_data.get("page", None) page_size = json_data.get("page_size", 10) if not (page and page_size): code = -1 msg = "page和page_size参数必填" return response_tem(code=code, msg=msg) if type(page) is str: if not page.isdigit(): code = -1 msg = "page应该是数字类型" return response_tem(code=code, msg=msg) if type(page_size) is str: if not page_size.isdigit(): code = -1 msg = "page_size应该是数字类型" return response_tem(code=code, msg=msg) page = int(page) page_size = int(page_size) all_info = [] try: search_param = {"is_delete": False} pagination = File.query.filter_by(**search_param).paginate( page, page_size, 0) all_data = pagination.total all_page = pagination.pages now_page = pagination.page all_file = pagination.items for info in all_file: file_info = { "file_id": info.id, "name": info.name, "type": info.type, "size": info.size, "update_time": info.update_time } all_info.append(file_info) code = 0 msg = "查询成功" except Exception as e: logger.error(e) code = -1 msg = "查询失败" all_data = None all_page = None now_page = None return response_tem(code=code, data=all_info, msg=msg, all_data=all_data, all_page=all_page, page=now_page)
def project(): """ 查询所有项目信息 :return: """ json_data = return_json(request) page = json_data.get("page", None) page_size = json_data.get("page_size", 10) if not (page and page_size): code = -1 msg = "page和page_size参数必填" return response_tem(code=code, msg=msg) if type(page) is str: if not page.isdigit(): code = -1 msg = "page应该是数字类型" return response_tem(code=code, msg=msg) if type(page_size) is str: if not page_size.isdigit(): code = -1 msg = "page_size应该是数字类型" return response_tem(code=code, msg=msg) page = int(page) page_size = int(page_size) all_info = [] try: search_param = {"is_delete": False} pagination = Project.query.filter_by(**search_param).paginate( page, page_size, 0) all_data = pagination.total all_page = pagination.pages now_page = pagination.page all_proj = pagination.items for proj in all_proj: proj_info = { "id": proj.id, "project_name": proj.project_name, "update_time": proj.update_time } all_info.append(proj_info) code = 0 msg = "查询成功" except Exception as e: logger.error(e) code = -1 msg = "查询失败" all_data = None all_page = None now_page = None return response_tem(code=code, msg=msg, data=all_info, all_data=all_data, all_page=all_page, page=now_page)
def modify_server(): """ 根据server_id修改server信息 :return: """ json_data = return_json(request) server_id = json_data.get("server_id", None) name = json_data.get("name", None) ip = json_data.get("ip", None) port = json_data.get("port", None) user_name = json_data.get("user_name", None) user_password = json_data.get("user_password", None) if not server_id: code = -1 msg = "server_id is required" return response_tem(code=code, msg=msg) param_list = [name, ip, port, user_name, user_password] for param in param_list: if not param: code = -1 msg = "all param is required" return response_tem(code=code, msg=msg) search_param = {"is_delete": False, "id": server_id} try: server_info = Server.query.filter_by(**search_param).first() if server_info.name != name: param = {"is_delete": False, "name": name} server_list = Server.query.filter_by(**param).all() if len(server_list): code = -1 msg = "该名称已存在" return response_tem(code=code, msg=msg) if server_info.ip != ip: param = {"is_delete": False, "ip": ip} server_list = Server.query.filter_by(**param).all() if len(server_list): code = -1 msg = "该ip已存在" return response_tem(code=code, msg=msg) server_info.name = name server_info.ip = ip server_info.port = port server_info.user_name = user_name server_info.user_password = user_password db.session.add(server_info) db.session.commit() code = 0 msg = "修改成功" except Exception as e: logger.error(e) code = -1 msg = "修改失败" return response_tem(code=code, msg=msg)
def add_server(): """ add server :return: """ json_data = return_json(request) name = json_data.get("name", None) ip = json_data.get("ip", None) port = json_data.get("port", None) user_name = json_data.get("user_name", None) user_password = json_data.get("user_password", None) param_list = [name, ip, port, user_name, user_password] for param in param_list: if param is None: code = -1 msg = "all param is required" return response_tem(code=code, msg=msg) try: param = {"is_delete": False, "name": name} server_list = Server.query.filter_by(**param).all() if len(server_list): code = -1 msg = "该名称已存在" return response_tem(code=code, msg=msg) param = {"is_delete": False, "ip": ip} server_list = Server.query.filter_by(**param).all() if len(server_list): code = -1 msg = "该ip已存在" return response_tem(code=code, msg=msg) server_param = { "name": name, "ip": ip, "port": str(port), "user_name": user_name, "user_password": user_password } new_server = Server(**server_param) db.session.add(new_server) db.session.commit() code = 0 msg = "添加成功" except Exception as e: logger.error(e) code = -1 msg = "添加失败" return response_tem(code=code, msg=msg)
def project_detail(): """ 根据项目id查看项目详情,项目说明以及项目包含的服务等 :return: """ json_data = return_json(request) project_id = json_data.get("project_id", None) if not project_id: code = -1 msg = "project_id is required" return response_tem(code=code, msg=msg) all_info = [] try: search_param = {"is_delete": False, "id": project_id} proj_obj = Project.query.filter_by(**search_param).first() if not proj_obj: code = -1 msg = "查询失败, 该项目不存在或已被删除" return response_tem(code=code, msg=msg) project_name = proj_obj.project_name all_service_obj = proj_obj.services all_service_obj = [ service for service in all_service_obj if service.is_delete == False ] service_info_list = [] for service_obj in all_service_obj: service_info = { "service_id": service_obj.id, "service_name": service_obj.service_name, "service_instruction": service_obj.service_instruction } service_info_list.append(service_info) proj_info = { "project_name": project_name, "service_info": service_info_list } all_info.append(proj_info) code = 0 msg = "查询成功" except Exception as e: logger.error(e) code = -1 msg = "查询失败" return response_tem(code=code, msg=msg, data=all_info)
def delete_file(): """ 删除指定上传的文件 :return: """ json_data = return_json(request) file_id = json_data.get("file_id", None) if not file_id or len(file_id) != 32: code = -1 msg = "File_id is required, and its length should be 32" return response_tem(code=code, msg=msg) try: search_param = {"is_delete": False, "id": file_id} file_obj = File.query.filter_by(**search_param).first() if not file_obj: code = -1 msg = "没有找到该文件, 可能已被删除" return response_tem(code=code, msg=msg) file_path = file_obj.path dirname, name, suffix = get_file_name(file_path) str_time = localtime() file_obj.name = name + str_time file_obj.is_delete = True db.session.add(file_obj) if os.path.exists(file_path): new_name = name + str_time + "." + suffix # 带时间的文件名 new_file_path = os.path.join(dirname, new_name) os.rename(file_path, new_file_path) file_obj.path = new_file_path db.session.add(file_obj) dir_path = os.path.join(dirname, name) # 解压文件目录 db.session.commit() if os.path.isdir(dir_path): new_dir_path = os.path.join(dirname, name + str_time) os.rename(dir_path, new_dir_path) code = 0 msg = "文件删除成功" except Exception as e: logger.error(e) code = -1 msg = "文件删除失败" return response_tem(code=code, msg=msg)
def version(): """ 根据服务id获取该服务所有有效版本号 :return: """ json_data = return_json(request) service_id = json_data.get("service_id", None) if not service_id: code = -1 msg = "service_id is required" return response_tem(code=code, msg=msg) all_info = [] try: search_param = {"is_delete": False, "id": service_id} service_obj = Service.query.filter_by(**search_param).first() if not service_obj: code = -1 msg = "查询失败, 该服务不存在或已被删除" return response_tem(code=code, msg=msg) all_version_obj = service_obj.versions all_version_obj = [ version for version in all_version_obj if version.is_delete == False ] for version_obj in all_version_obj: version_info = { "version_id": version_obj.id, "version_number": version_obj.version_number, "version_instruction": version_obj.version_instruction } all_info.append(version_info) code = 0 msg = "查询成功" except Exception as e: logger.error(e) code = -1 msg = "查询失败" return response_tem(code=code, msg=msg, data=all_info)
def login(): """ 登录接口 :param username :param password :return: """ json_data = return_json(request) name = json_data.get("name", None) password = json_data.get("password", None) if name is None or password is None: code = -1 msg = "name and password is required" return response_tem(code=code, msg=msg) search_param = { "is_delete": False, "name": name, } data = User.query.filter_by(**search_param).first() if not data: code = -1 msg = "用户不存在" elif data.check_password_hash(password): code = 0 msg = "登录成功" name = name expires = datetime.timedelta(days=30) access_token = create_access_token(name, expires_delta=expires) # access_token = create_access_token(identity=name) return response_tem(code=code, msg=msg, name=name, access_token=access_token) else: code = -1 msg = "密码错误" return response_tem(code=code, msg=msg)
def service_detail(): """ 根据服务id查询服务详细信息, 服务的运行情况和依赖服务信息等 :return: """ json_data = return_json(request) service_id = json_data.get("service_id", None) if not service_id: code = -1 msg = "service_id is required" return response_tem(code=code, msg=msg) all_info = [] try: search_param = {"is_delete": False, "id": service_id} service_obj = Service.query.filter_by(**search_param).first() if not service_obj: code = -1 msg = "查询失败, 该服务不存在或已被删除" return response_tem(code=code, msg=msg) service_instruction = service_obj.service_instruction service_name = service_obj.service_name all_version_obj = service_obj.versions all_version_obj = [ version for version in all_version_obj if version.is_delete == False ] service_info_list = [] rely_info_list = [] for version_obj in all_version_obj: all_rel_obj = version_obj.servers all_rel_obj = [rel for rel in all_rel_obj if rel.status == 1] if all_rel_obj: version_number = version_obj.version_number for rel in all_rel_obj: rel_info = { "service_name": service_name, "server_ip": rel.server.ip, "server_name": rel.server.name, "status": rel.status, "version_number": version_number } service_info_list.append(rel_info) all_rely_obj = service_obj.dependencys all_rely_obj = [ rely for rely in all_rely_obj if rely.is_delete == False ] for rely_obj in all_rely_obj: dependency_name = rely_obj.dependency_name version_number = rely_obj.version_number search_param = { "is_delete": False, "service_name": dependency_name } dep_service = Service.query.filter_by( **search_param).first() # 依赖服务对象 if not dep_service: rely_info = { "service_name": dependency_name, "status": 0, "version_number": version_number } rely_info_list.append(rely_info) continue all_dep_version = dep_service.versions dep_version_list = [ version for version in all_dep_version if version.version_number == version_number ] if dep_version_list: # 依赖的版本存在 dep_version = dep_version_list[0] all_dep_rel = dep_version.servers all_dep_rel = [rel for rel in all_dep_rel if rel.status == 1] if all_dep_rel: #存在正在执行的版本 for rel in all_dep_rel: rely_info = { "service_name": dependency_name, "server_ip": rel.server.ip, "server_name": rel.server.name, "status": rel.status, "version_number": version_number } rely_info_list.append(rely_info) else: # 该本版未执行 rely_info = { "service_name": dependency_name, "status": 0, "version_number": version_number } rely_info_list.append(rely_info) else: # 依赖版本不存在 rely_info = { "service_name": dependency_name, "status": 0, "version_number": version_number } rely_info_list.append(rely_info) info = { "service_instruction": service_instruction, "service_info": service_info_list, "rely_info": rely_info_list } all_info.append(info) code = 0 msg = "查询成功" except Exception as e: logger.error(e) code = -1 msg = "查询失败" return response_tem(code=code, msg=msg, data=all_info)
def delete_project(): """ 根据项目id删除某个项目,如果该项目已经部署到远程,则删除失败 :return: """ json_data = return_json(request) project_id = json_data.get("project_id", None) if not project_id: code = -1 msg = "project_id is required" return response_tem(code=code, msg=msg) all_info = [] flag = False try: search_param = {"is_delete": False, "id": project_id} proj_obj = Project.query.filter_by(**search_param).first() if not proj_obj: code = -1 msg = "删除失败,该项目不存在" return response_tem(code=code, msg=msg) all_service_obj = proj_obj.services all_service_obj = [ service for service in all_service_obj if service.is_delete == False ] for service_obj in all_service_obj: service_name = service_obj.service_name all_version_obj = service_obj.versions all_version_obj = [ version for version in all_version_obj if version.is_delete == False ] for version in all_version_obj: server_infos = [] version_number = version.version_number all_server_obj = version.servers all_server_obj = [ server for server in all_server_obj if server.status == 1 ] if all_server_obj: for server_obj in all_server_obj: server_info = { "ip": server_obj.server.ip, "name": server_obj.server.name } server_infos.append(server_info) flag = True service_info = { "service_name": service_name, "version_number": version_number, "server_infos": server_infos } all_info.append(service_info) if flag: code = -1 msg = "该项目已有服务部署成功,请卸载服务后再删除" return response_tem(code=code, msg=msg, data=all_info) else: proj_obj.is_delete = True db.session.add(proj_obj) db.session.commit() code = 0 msg = "删除项目成功" except Exception as e: logger.error(e) code = -1 msg = "删除项目出错" return response_tem(code=code, msg=msg)
def server_list(): """ 查询所有server列表 :return: """ json_data = return_json(request) page = json_data.get("page", None) page_size = json_data.get("page_size", 10) if not (page and page_size): code = -1 msg = "page和page_size参数必填" return response_tem(code=code, msg=msg) if type(page) is str: if not page.isdigit(): code = -1 msg = "page应该是数字类型" return response_tem(code=code, msg=msg) if type(page_size) is str: if not page_size.isdigit(): code = -1 msg = "page_size应该是数字类型" return response_tem(code=code, msg=msg) page = int(page) page_size = int(page_size) all_info = [] try: search_param = {"is_delete": False} pagination = Server.query.filter_by(**search_param).paginate( page, page_size, 0) all_data = pagination.total all_page = pagination.pages now_page = pagination.page all_server = pagination.items for info in all_server: rel_list = info.files f_list = [] # 存储该服务器中 部署的所有项目 for rel in rel_list: f_list.append(rel.file.name) server_info = { "server_id": info.id, "name": info.name, "ip": info.ip, "port": info.port, "user_name": info.user_name, "user_password": info.user_password, "status": f_list } all_info.append(server_info) code = 0 msg = "查询成功" except Exception as e: logger.error(e) code = -1 msg = "查询失败" all_data = None all_page = None now_page = None return response_tem(code=code, msg=msg, data=all_info, all_data=all_data, all_page=all_page, page=now_page)