add log setting

This commit is contained in:
pengtao 2021-06-22 19:17:37 +08:00
parent a0644d2598
commit 2431ecea20
8 changed files with 242 additions and 15 deletions

View File

@ -1,16 +1,28 @@
from pydantic import BaseConfig
from pydantic import BaseSettings
from typing import Optional
import os
from pydantic.fields import T
class Settings(BaseSettings):
app_name: str = "Kingsome API"
admin_email: str = "pengtao@kingsome.cn"
items_per_user: int = 50
is_debug: bool = True
class Settings(BaseConfig):
redis_host: str = "192.168.100.30" # reids 服务器IP
redis_port: int = 6379 # redis 端口
redis_db: int = 2 # redis db
mail_server: str = "smtp.exmail.qq.com" # 邮箱server
mail_user: str = "ops@kingsome.cn" # 邮箱用户名
mail_pswd: Optional[str] = os.getenv('mail_pswd') # 邮箱密码
x_token: str = "abc"
root_path_in_servers: Optional[bool] = False
root_path: str = '/api/v1'
settings = Settings()

10
log.txt
View File

@ -1,10 +0,0 @@
found query:aaa
message to asa
found query:aaa
message to asa
found query:aaa
message to asa
found query:aaa
message to asa
found query:aaa
message to asa

59
main.py
View File

@ -1,14 +1,16 @@
# uvicorn main:app --host=127.0.0.1 --port=8000 --reload
from config.config import settings
from fastapi import Depends, FastAPI, BackgroundTasks, Request
from dependencies import get_query_token, get_token_header
# from routers import items, users
# from internal import admin
from ops.common import common
from ops.deploy import deploy
from typing import Optional
from scripts.common.redis import get_redis_pool
import pdb
# import sys
# sys.path.append('.')
from fastapi.logger import logger as fastapi_logger
import logging
tags_metadata = [
# {
@ -26,10 +28,60 @@ tags_metadata = [
]
app_log = None
root = None
LOGGING_CONFIG = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"default": {
"()": "uvicorn.logging.DefaultFormatter",
"fmt": "%(asctime)s %(levelname)s %(name)s %(processName)s.%(threadName)s[%(process)d.%(thread)d]: %(message)s",
"use_colors": None,
},
"access": {
"()": "uvicorn.logging.AccessFormatter",
"fmt": '%(asctime)s %(levelname)s %(name)s %(processName)s.%(threadName)s[%(process)d.%(thread)d]: %(client_addr)s - "%(request_line)s" %(status_code)s',
},
},
"handlers": {
"default": {
"formatter": "default",
"class": "logging.StreamHandler",
"stream": "ext://sys.stderr",
},
"access": {
"formatter": "access",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
},
},
"loggers": {
"": {"handlers": ["default"], "level": "INFO"},
"uvicorn.error": {"level": "INFO"},
"uvicorn.access": {"handlers": ["access"], "level": "INFO", "propagate": False},
},
}
def init_log():
if settings.is_debug:
LOGGING_CONFIG["loggers"] = {
"": {"handlers": ["default"], "level": "DEBUG"},
"uvicorn.error": {"level": "DEBUG"},
"uvicorn.access": {"handlers": ["access"], "level": "DEBUG", "propagate": False},
}
return LOGGING_CONFIG
def create_app():
application = FastAPI(dependencies=[Depends(get_token_header)],
openapi_tags=tags_metadata)
application.include_router(common.router, prefix="/common")
application.include_router(deploy.router, prefix="/deploy")
init_log()
return application
@ -51,6 +103,7 @@ async def shutdown_event():
async def root(request: Request):
redis_client = request.app.state.redis
keys = await redis_client.get("online_devices")
app_log.info("get keys was {0} with {1}".format(keys, request.url))
if keys:
return {"message": "Hello Bigger Applications! {}".format(keys)}
else:
@ -84,4 +137,4 @@ async def send_notification(request: Request, email: str, background_tasks: Back
if __name__ == '__main__':
import uvicorn
uvicorn.run(app='main:app', host="127.0.0.1",
port=8010, reload=True, debug=True)
port=8010, reload=True, debug=True, log_config=LOGGING_CONFIG)

14
ops/deploy/deploy.py Normal file
View File

@ -0,0 +1,14 @@
# pip install fastapi-mail
from fastapi import APIRouter, BackgroundTasks, UploadFile, File, Form
from starlette.requests import Request
from starlette.responses import JSONResponse
from config.config import settings
from scripts.common.deploy import ProjectInfo, deploy_service
router = APIRouter()
@router.post("/server")
async def simple_send(project: ProjectInfo, tag: str, background_tasks: BackgroundTasks) -> JSONResponse:
background_tasks.add_task(deploy_service, project, tag)
return JSONResponse(status_code=200, content={"message": "{0} {1} deploy success!".format(project.name, tag)})

6
requests.txt Normal file
View File

@ -0,0 +1,6 @@
aioredis==1.3.1
fastapi==0.65.2
fastapi-mail==0.3.7
GitPython==3.1.18
pydantic==1.8.2
redis==3.2.1

View File

@ -0,0 +1,23 @@
import subprocess
import logging
def run_cmd(cmd, shell=True):
'''
Run command with arguments, wait to complete and return ``True`` on success.
:param cls: The class as implicit first argument.
:param cmd: Command string to be executed.
:returns : ``True`` on success, otherwise ``None``.
:rtype : ``bool``
'''
# logger = logger.getLogger('SPOT.INGEST.COMMON.UTIL')
logging.debug('Execute command: {0}'.format(cmd))
try:
subprocess.call(cmd, shell=shell)
return True
except Exception as exc:
logging.error('[{0}] {1}'.format(
exc.__class__.__name__, exc.message))

100
scripts/common/cvs.py Normal file
View File

@ -0,0 +1,100 @@
import os
from git.repo import Repo
from git.repo.fun import is_git_dir
class GitRepository(object):
"""
git仓库管理
"""
def __init__(self, local_path, repo_url, branch='master'):
self.local_path = local_path
self.repo_url = repo_url
self.repo = None
self.initial(repo_url, branch)
def initial(self, repo_url, branch):
"""
初始化git仓库
:param repo_url:
:param branch:
:return:
"""
if not os.path.exists(self.local_path):
os.makedirs(self.local_path)
git_local_path = os.path.join(self.local_path, '.git')
if not is_git_dir(git_local_path):
self.repo = Repo.clone_from(
repo_url, to_path=self.local_path, branch=branch)
else:
self.repo = Repo(self.local_path)
def pull(self):
"""
从线上拉最新代码
:return:
"""
self.repo.git.pull()
def branches(self):
"""
获取所有分支
:return:
"""
branches = self.repo.remote().refs
return [item.remote_head for item in branches if item.remote_head not in ['HEAD', ]]
def commits(self):
"""
获取所有提交记录
:return:
"""
commit_log = self.repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}',
max_count=50,
date='format:%Y-%m-%d %H:%M')
log_list = commit_log.split("\n")
return [eval(item) for item in log_list]
def tags(self):
"""
获取所有tag
:return:
"""
return [tag.name for tag in self.repo.tags]
def change_to_branch(self, branch):
"""
切换分值
:param branch:
:return:
"""
self.repo.git.checkout(branch)
def change_to_commit(self, branch, commit):
"""
切换commit
:param branch:
:param commit:
:return:
"""
self.change_to_branch(branch=branch)
self.repo.git.reset('--hard', commit)
def change_to_tag(self, tag):
"""
切换tag
:param tag:
:return:
"""
self.repo.git.checkout(tag)
if __name__ == '__main__':
local_path = os.path.join('codes', 'luffycity')
repo = GitRepository(local_path, 'https://gitee.com/wupeiqi/fuck.git')
branch_list = repo.branches()
print(branch_list)
repo.change_to_branch('dev')
repo.pull()

29
scripts/common/deploy.py Normal file
View File

@ -0,0 +1,29 @@
import re
from pydantic import BaseModel
from scripts.common.cvs import GitRepository
import os
from fastapi import HTTPException
from . import run_cmd
class ProjectInfo(BaseModel):
git_url: str
base_dir: str = "/data/back_apps"
host: str
pre_script: str
start_script: str
name: str
async def deploy_service(project: ProjectInfo, tag: str):
# git clone
local_path = project.base_dir+'/'+tag
#print(local_path, project.git_url, tag)
git = GitRepository(local_path=local_path, repo_url=project.git_url)
git.pull()
git.change_to_tag(tag)
# run pre scripts
run_cmd(project.pre_script)
# find tag files
pass
# run ansible-play deloy tag files && run start script in remote