Пример #1
0
 def get_share_img(cls, user_name, id):
     graph = Graph.select().get(id)
     if graph is None:
         raise ServerException(msg='图片不存在')
     if not graph.is_published and user_name != graph.user_name:
         raise ServerException(msg='该图未发布')
     return graph
Пример #2
0
 def delete(cls, user_name, id):
     workspace = Workspace.select().get(id)
     if workspace.user_name != user_name:
         raise ServerException('无权限')
     planners = WeeklyPlanner.select().filter(
         WeeklyPlanner.workspace_id == id).all()
     if len(planners) != 0:
         raise ServerException('存在计划,请先删除计划')
     workspace.delete()
     return True
Пример #3
0
 def push(cls, image_name, repository, tag, console):
     try:
         console(f'开始推送镜像{image_name}到{repository},请稍等...')
         cls.client.api.tag(
             image=image_name,
             repository=repository,
             tag=tag
         )
         response = cls.client.api.push(
             repository=repository,
             tag=tag,
             stream=True,
             decode=True,
             auth_config={
                 'username': ConfigUtil.get_str_property(config.HARBOR_USERNAME),
                 'password': ConfigUtil.get_str_property(config.HARBOR_PASSWORD),
             }
         )
         for line in response:
             if line.get('error') is not None:
                 raise ServerException(msg=line.get('error'))
         console(f'推送镜像{image_name}到{repository}成功')
     except Exception as e:
         console(f'推送镜像{image_name}到{repository}失败:{e.__str__()}')
         raise e
Пример #4
0
 def save(cls, user_name, id, title, content):
     summary = QiniuService.get_summary(content)
     if title is None:
         title = 'untitled'
     if id is None:
         file_name = md5(title + content +
                         datetime.now().timestamp().__str__())
         url = QiniuService.upload_doc(content, file_name)
         catalogue_index = cls.get_max_catalogue_index(user_name, 1)
         article = Article(title=title,
                           file_key=file_name,
                           user_name=user_name,
                           url=url,
                           summary=summary,
                           catalogue_id=1,
                           catalogue_index=catalogue_index)
         article.insert()
         return Article.select().filter(
             Article.user_name == user_name,
             Article.file_key == file_name).one().id
     article = Article.select().get(id)
     if user_name != article.user_name:
         raise ServerException(msg=f'您没有权限修改{article.user_name}的文章')
     file_name = md5(title + content)
     if article.file_key == file_name:
         return id
     url = QiniuService.upload_doc(content, article.file_key, file_name)
     article.url = url
     article.file_key = file_name
     article.summary = summary
     Article.update(article)
     return id
Пример #5
0
 def delete_image(cls, user_name, id):
     image = Image.select().get(id)
     delete_status = QiniuService.delete_file(
         config.QI_NIU.get('img_bucket_name'), image.key)
     if not delete_status:
         raise ServerException('删除图片失败')
     image.delete()
     return True
Пример #6
0
 def get_base_project(cls):
     response = cls.search(q=cls.harbor_base_project_name)
     projects = response.get('project')
     for project in projects:
         if project.get('name') == cls.harbor_base_project_name:
             return project
     raise ServerException(
         msg=f'{cls.harbor_base_project_name}不存在,请检查harbor设置')
Пример #7
0
 def get_user_from_cas_resp(cls, response) -> User:
     resp = json.loads(response.text).get('serviceResponse')
     if resp.get('authenticationFailure'):
         raise ServerException(
             msg=f"{resp.get('authenticationFailure').get('description')}")
     else:
         attributes = resp.get('authenticationSuccess').get('attributes')
         return User(**attributes.get('user'))
Пример #8
0
 def add(cls, user_name, label):
     exist = Tag.select().filter(Tag.user_name == user_name, Tag.label
                                 == label).first() is None
     if not exist:
         raise ServerException(f'{label} 已存在')
     tag = Tag(user_name=user_name, label=label)
     tag.insert()
     return Tag.select().filter(Tag.user_name == user_name,
                                Tag.label == label).one().id
Пример #9
0
 def article_detail(cls, id, user_name):
     article = Article.select().get(id)
     if not article.is_published and user_name != article.user_name:
         raise ServerException('该文章未发布')
     cls.view_article(id, user_name)
     content = QiniuService.get_doc(article.url)
     result = article.get_json()
     result['content'] = content
     result['liked'] = cls.is_user_liked(user_name, id)
     return result
Пример #10
0
 def run(cls, cmd, console=print, t=True):
     assert cmd is not None
     assert console is not None
     console(cmd)
     p = Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=True)
     for line in iter(p.stdout.readline, b''):
         line = line.rstrip().decode('utf8')
         console(line)
     p.communicate()
     if p.returncode and t:
         raise ServerException(msg=f'命令{cmd}执行失败')
Пример #11
0
 def get_user_from_cas_resp(cls, response) -> User:
     resp = json.loads(response.text).get('serviceResponse')
     if resp.get('authenticationFailure'):
         raise ServerException(
             msg=f"{resp.get('authenticationFailure').get('description')}")
     else:
         attributes = resp.get('authenticationSuccess').get('attributes')
         return User(name=attributes.get('name')[0],
                     email=attributes.get('email')[0],
                     phone=attributes.get('phone')[0],
                     avatar=attributes.get('avatar')[0],
                     id=int(attributes.get('id')[0]))
Пример #12
0
 def view_article(cls, id, user_name):
     article = Article.select().get(id)
     if article is None or (not article.is_published
                            and user_name != article.user_name):
         raise ServerException('文章不存在')
     user_view = UserView(user_name=user_name, article_id=id)
     UserView.insert(user_view)
     view_num = UserView.select(
         func.count(distinct(UserView.user_name)).label('count')).filter(
             UserView.article_id == id).one()[0]
     article.view_num = view_num
     article.update()
Пример #13
0
 def save(cls, args: dict) -> bool:
     if cls.has_done(g.user.name):
         raise ServerException(msg='今天已经填过了', code=ErrorCode.FAILED)
     daily_info = DailyInfo(name=g.user.name,
                            date=datetime.now().strftime('%Y%m%d'),
                            in_sh=args.get('in_sh'),
                            health=args.get('health'),
                            symptom=json.dumps(args.get('symptom', [])),
                            contact_history=args.get('contact_history'),
                            access_public=args.get('access_public'),
                            return_date=args.get('return_date'),
                            note=args.get('note'))
     DailyInfo.insert(daily_info)
     return True
Пример #14
0
 def create_namespace(cls, namespace):
     api_instance = kubernetes.client.CoreV1Api(cls.get_api_client())
     try:
         return api_instance.read_namespace(name=namespace, pretty=True)
     except kubernetes.client.rest.ApiException as e:
         if e.reason == 'Not Found':
             body = kubernetes.client.V1Namespace()
             body.metadata = kubernetes.client.V1ObjectMeta(
                 name=namespace,
                 labels={
                     'name': namespace,
                 }
             )
             return api_instance.create_namespace(body=body, pretty=True)
     raise ServerException(f'创建namespace {namespace}失败')
Пример #15
0
 def gen_docker_file(self):
     target_dockerfile = f'{self.target_path}/dockerfile'
     if self.project.docker_template_id is None:
         src_dockerfile = f'{self.code_path}/dockerfile'
         if not os.path.exists(src_dockerfile):
             raise ServerException(f'{src_dockerfile}不存在')
         with open(src_dockerfile, 'r') as f:
             dockerfile = f.read()
     else:
         template = TemplateService.get_template_by_id(
             self.project.docker_template_id)
         docker_template = Template(template.content)
         dockerfile = docker_template.render(project=self.project.to_dict())
     with open(target_dockerfile, 'w') as f:
         f.write(dockerfile)
     return dockerfile
Пример #16
0
 def request(self):
     log.info(f'request from {self.__dict__}')
     response = None
     if self.method == 'GET':
         response = requests.get(self.url, headers=self.headers)
     if self.method == 'POST':
         response = requests.post(self.url,
                                  data=json.dumps(self.body),
                                  headers=self.headers,
                                  timeout=self.timeout)
     if self.method == 'PUT':
         response = requests.put(self.url,
                                 headers=self.headers,
                                 json=self.body)
     if self.method == 'DELETE':
         response = requests.delete(self.url, headers=self.headers)
     if response is None or response.status_code not in HttpCode.HTTP_SUCCESS:
         raise ServerException(ErrorCode.FAILED,
                               f'request failed{response.json()}')
     return response
Пример #17
0
 def deploy(cls, user, project_id, image_name):
     project = Project.select().get(project_id)
     deploy_log = DeployLog(
         project_name=project.name,
         project_id=project_id,
         image_name=image_name,
         user_name=user.name,
         status=DeployStatus.INIT,
         uuid=uuid.uuid4().hex
     )
     log.info(f'{deploy_log.uuid}:{user.name}正在发布{project.name} 镜像 {image_name}')
     try:
         namespace_response = K8sService.create_namespace(project.namespace)
         log.info(f'{deploy_log.uuid}:namespace {project.namespace} 校验成功:{namespace_response}')
         deploy_log.status = DeployStatus.NAMESPACE
         template = TemplateService.get_template_by_id(project.deployment_template_id)
         deploy_template = Template(template.content)
         deploy_template_yaml = yaml.safe_load(
             deploy_template.render(project=project.to_dict(), image_name=image_name))
         log.info(f'{deploy_log.uuid}:发布镜像{json.dumps(deploy_template_yaml, indent=2)}')
         deployment_response = K8sService.create_namespaced_deployment(
             name=project.name,
             namespace=project.namespace,
             body=deploy_template_yaml
         )
         log.info(f'{deploy_log.uuid}:镜像发布成功{deployment_response}')
         deploy_log.status = DeployStatus.DEPLOYMENT
         # 创建service
         cls.deploy_service(project=project)
         deploy_log.status = DeployStatus.SERVICE
         # 创建ingress
         if project.ingress_template_id:
             cls.deploy_ingress(project=project)
             deploy_log.status = DeployStatus.INGRESS
         return True
     except Exception as e:
         deploy_log.reason = e.__str__()
         log.exception(e)
         raise ServerException(msg='发布失败')
     finally:
         deploy_log.insert()
Пример #18
0
 def package_project(self):
     self.console(f'开始打包{self.project.name}')
     if self.project.build_type == BuildType.NPM:
         self.package_npm()
     elif self.project.build_type == BuildType.TAR:
         self.package_tar()
     elif self.project.build_type == BuildType.MVN:
         pass
     elif self.project.build_type == BuildType.GRADLE:
         self.package_gradle()
     elif self.project.build_type == BuildType.USER_DEFINE:
         pass
     else:
         raise ServerException(
             msg=f'unknown build type {self.project.build_type}')
     if self.project.nginx_template_id is not None:
         self.console(f'生成nginx file default.conf')
         self.console(self.gen_nginx_conf())
     self.console(f'生成dockerfile')
     self.console(self.gen_docker_file())
     self.console(f'{self.project.name}打包完成')
Пример #19
0
 def create_album(cls, user_name, id, title, cover_url, description):
     if id is not None:
         album = Album.select().get(id)
         assert album is not None
         album.title = title
         album.cover_url = cover_url
         album.description = description
         album.update()
         return id
     else:
         existed = Album.select().filter(Album.title == title, Album.user_name == user_name).first() is not None
         if existed:
             raise ServerException(msg='相册已存在')
         album = Album(
             title=title,
             cover_url=cover_url,
             description=description,
             user_name=user_name,
         )
         album.insert()
     return Album.select().filter(Album.title == title, Album.user_name == user_name).one().id
Пример #20
0
 def upload_image(cls, user_name, album_id, file):
     img = file.read()
     file_name = md5(img.decode('ISO-8859-1'))
     image = Image.select().filter(Image.user_name == user_name, Image.key == file_name).first()
     if image is not None:
         raise ServerException('图片已存在')
     url = QiniuService.upload_img(img, file_name=file_name)
     img_info = QiniuService.get_img_info(url)
     assert img_info is not None
     image = Image(
         album_id=album_id,
         user_name=user_name,
         key=file_name,
         url=url,
         width=img_info.get('width'),
         height=img_info.get('height'),
         size=img_info.get('size'),
         format=img_info.get('format'),
         color_model=img_info.get('colorModel')
     )
     image.insert()
     return Image.select().filter(Image.key == file_name).one()
Пример #21
0
 def upload_img(cls, img, file_name=None, new_name=None):
     auth = Auth(config.QI_NIU.get('access_key'),
                 config.QI_NIU.get('secret_key'))
     token = auth.upload_token(bucket=config.QI_NIU.get('img_bucket_name'))
     bucket = BucketManager(auth)
     if file_name is None:
         file_name = f'{md5(img)}'
     if new_name is None:
         new_name = file_name
     if len(img) >= 22 and img[0:22] == 'data:image/png;base64,':
         b64 = img.split(';base64,')[1]
         img = urlsafe_base64_decode(b64)
     file_stat = bucket.stat(bucket=config.QI_NIU.get('img_bucket_url'),
                             key=file_name)
     delete_status = None
     if file_stat[0] is not None:
         delete_status = bucket.delete(
             bucket=config.QI_NIU.get('img_bucket_url'), key=file_name)
     if isinstance(delete_status,
                   tuple) and delete_status[1].status_code != 200:
         raise ServerException(msg='更新图片失败')
     ret, res = put_data(token, key=new_name, data=img)
     return config.QI_NIU.get('img_bucket_url') + ret.get('key')
Пример #22
0
 def upload_doc(cls, data, file_name=None, new_name=None):
     auth = Auth(config.QI_NIU.get('access_key'),
                 config.QI_NIU.get('secret_key'))
     token = auth.upload_token(bucket=config.QI_NIU.get('doc_bucket_name'))
     bucket = BucketManager(auth)
     if file_name is None:
         file_name = f'{md5(data)}'
         new_name = file_name
     delete_status = cls.delete_file(
         bucket_name=config.QI_NIU.get('doc_bucket_name'),
         file_name=file_name)
     if not delete_status:
         raise ServerException('更新文件失败')
     file_path = f'./data/{new_name}'
     f = open(file_path, 'wb')
     f.write(bytes(data, encoding='utf8'))
     f.close()
     ret, res = put_file(token,
                         key=new_name,
                         file_path=file_path,
                         mime_type='text/plain')
     log.info(res)
     os.remove(file_path)
     return config.QI_NIU.get('doc_bucket_url') + ret.get('key')
Пример #23
0
 def get_log_content(cls, log_path):
     if log_path is None or log_path == '':
         raise ServerException('日志不存在')
     with open(log_path) as f:
         content = f.read()
     return content
Пример #24
0
 def delete(cls, tag_id):
     tag = Tag.select().get(tag_id)
     if tag is None:
         raise ServerException('tag不存在')
     tag.delete()
     return True