diff --git a/ops/deploy/route.py b/ops/deploy/route.py index 0ea2fc6..6d13a6b 100644 --- a/ops/deploy/route.py +++ b/ops/deploy/route.py @@ -3,13 +3,30 @@ 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 +from scripts.common.deploy import ProjectInfo, deploy_service, ClientAliyun, sync_aliyun from uuid import uuid4 router = APIRouter() @router.post("/server", tags=["deploy"], summary="部署服务器端") -async def simple_send(project: ProjectInfo, tag: str, background_tasks: BackgroundTasks) -> JSONResponse: +async def deploy_server(project: ProjectInfo, tag: str, + background_tasks: BackgroundTasks) -> JSONResponse: uuid = uuid4().hex background_tasks.add_task(deploy_service, project, tag, uuid) - return JSONResponse(status_code=200, content={"message": "{0} {1} deploy success,uuid={2}!".format(project.name, tag, uuid)}) + return JSONResponse(status_code=200, + content={ + "message": + "{0} {1} deploy success,uuid={2}!".format( + project.name, tag, uuid) + }) + + +@router.post("/sync_aliyun", tags=["deploy"], summary="发布到阿里云CDN") +async def sync_aliyun(clientinfo: ClientAliyun, + background_tasks: BackgroundTasks) -> JSONResponse: + background_tasks.add_task(sync_aliyun, clientinfo) + return JSONResponse(status_code=200, + content={ + "message": + "{0} rsync aliyun CDN success!".format(clientinfo) + }) diff --git a/requests.txt b/requests.txt index b15df9a..9b5a66a 100644 --- a/requests.txt +++ b/requests.txt @@ -3,4 +3,5 @@ fastapi==0.65.2 fastapi-mail==0.3.7 GitPython==3.1.18 pydantic==1.8.2 -redis==3.2.1 \ No newline at end of file +redis==3.2.1 + diff --git a/scripts/common/deploy.py b/scripts/common/deploy.py index 06d8196..30f3631 100644 --- a/scripts/common/deploy.py +++ b/scripts/common/deploy.py @@ -22,6 +22,14 @@ class ProjectInfo(BaseModel): name: str +class ClientAliyun(BaseModel): + project_name: str + version: str + type: str = "res" + unzip: bool = False + filename: str + + def create_deploy_log(uuid): basedir = os.path.dirname(os.path.abspath(__file__)) log_path = os.path.join(basedir, 'logs') @@ -33,7 +41,8 @@ def create_deploy_log(uuid): return base_logger -async def deploy_service(request: Request, project: ProjectInfo, tag: str, uuid: str): +async def deploy_service(request: Request, project: ProjectInfo, tag: str, + uuid: str): client = request.app.state.redis dd = await client.set('x_token') yaml_files = "./playbook/1.yaml" @@ -50,10 +59,42 @@ async def deploy_service(request: Request, project: ProjectInfo, tag: str, uuid: logger.error("git failed") +async def sync_aliyun(clientinfo: ClientAliyun): + if not clientinfo: + logger.error(f"{clientinfo} some config error!") + if not clientinfo.unzip: + if clientinfo.project_name and clientinfo.version: + cmd = '''ssh 10.10.2.2 "source /etc/profile && cd /data/scripts/ && python3 sync_cdn_aliyun.py -p {project_name} -v {version} -t {type}"'''.format( + **clientinfo) + logger.info(f"start cmd={cmd}") + os.system(cmd) + return True + else: + return False + else: + if clientinfo.filename: + clientinfo.version = clientinfo.filename.split('.')[0] + if clientinfo.filename.endswith('.zip'): + unzip_cmd = '''ssh 10.10.2.2 "cd /data/ftp/data/{project_name} && /usr/bin/unzip -uoq {filename} && chown -R ftpuser. {version}"'''.format( + **clientinfo) + logger.info(f"unzip cmd={unzip_cmd}") + os.system(unzip_cmd) + sync_cmd = '''ssh 10.10.2.2 "source /etc/profile && cd /data/scripts/ && python3 sync_cdn_aliyun.py -p {project_name} -v {version} -t {type}"'''.format( + **clientinfo) + logger.info(f"sync aliyun with {sync_cmd}") + os.system(sync_cmd) + return True + else: + logger.error(f"filename ={clientinfo.filename} not define!") + return False + + logger.info(f"sync clinet {clientinfo}") + + @logger.trace def build_project(project: ProjectInfo, deploy_log: logger, tag: str): - # git clone - local_path = project.base_dir+'/'+tag + # git clone + local_path = project.base_dir + '/' + tag #print(local_path, project.git_url, tag) try: git = GitRepository(local_path=local_path, repo_url=project.git_url) @@ -75,7 +116,8 @@ def build_project(project: ProjectInfo, deploy_log: logger, tag: str): @logger.trace -def publish_remote(host: str, yaml_files: str, run_data: dict, deploy_log: logger): +def publish_remote(host: str, yaml_files: str, run_data: dict, + deploy_log: logger): # run ansible-play deloy tag files && run start script in remote hosts = write_host(host) an = AnsibleAPI(hosts)