add deploy_sync_aliyun

This commit is contained in:
pengtao 2021-10-15 14:03:07 +08:00
parent 97fc396df9
commit e3122df228
3 changed files with 68 additions and 8 deletions

View File

@ -3,13 +3,30 @@ from fastapi import APIRouter, BackgroundTasks, UploadFile, File, Form
from starlette.requests import Request from starlette.requests import Request
from starlette.responses import JSONResponse from starlette.responses import JSONResponse
from config.config import settings 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 from uuid import uuid4
router = APIRouter() router = APIRouter()
@router.post("/server", tags=["deploy"], summary="部署服务器端") @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 uuid = uuid4().hex
background_tasks.add_task(deploy_service, project, tag, uuid) 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)
})

View File

@ -3,4 +3,5 @@ fastapi==0.65.2
fastapi-mail==0.3.7 fastapi-mail==0.3.7
GitPython==3.1.18 GitPython==3.1.18
pydantic==1.8.2 pydantic==1.8.2
redis==3.2.1 redis==3.2.1

View File

@ -22,6 +22,14 @@ class ProjectInfo(BaseModel):
name: str name: str
class ClientAliyun(BaseModel):
project_name: str
version: str
type: str = "res"
unzip: bool = False
filename: str
def create_deploy_log(uuid): def create_deploy_log(uuid):
basedir = os.path.dirname(os.path.abspath(__file__)) basedir = os.path.dirname(os.path.abspath(__file__))
log_path = os.path.join(basedir, 'logs') log_path = os.path.join(basedir, 'logs')
@ -33,7 +41,8 @@ def create_deploy_log(uuid):
return base_logger 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 client = request.app.state.redis
dd = await client.set('x_token') dd = await client.set('x_token')
yaml_files = "./playbook/1.yaml" yaml_files = "./playbook/1.yaml"
@ -50,10 +59,42 @@ async def deploy_service(request: Request, project: ProjectInfo, tag: str, uuid:
logger.error("git failed") 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 @logger.trace
def build_project(project: ProjectInfo, deploy_log: logger, tag: str): def build_project(project: ProjectInfo, deploy_log: logger, tag: str):
# git clone # git clone
local_path = project.base_dir+'/'+tag local_path = project.base_dir + '/' + tag
#print(local_path, project.git_url, tag) #print(local_path, project.git_url, tag)
try: try:
git = GitRepository(local_path=local_path, repo_url=project.git_url) 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 @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 # run ansible-play deloy tag files && run start script in remote
hosts = write_host(host) hosts = write_host(host)
an = AnsibleAPI(hosts) an = AnsibleAPI(hosts)