myops/scripts/common/deploy.py
2021-06-23 15:40:36 +08:00

40 lines
1.1 KiB
Python

import re
from pydantic import BaseModel
from scripts.common.cvs import GitRepository
import os
from fastapi import HTTPException, Request
from . import run_cmd
from .ansible import AnsibleAPI, write_host
from scripts.logger import logger
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(request: Request, project: ProjectInfo, tag: str, uuid: str):
client = request.app.state.redis
dd = await client.set('x_token')
# 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
hosts = write_host(project.host)
an = AnsibleAPI(hosts)
run_data = {
"desc": "a"
}
run_out = an.run_playbook('test,yml', run_data)
logger.info(run_out)
# run ansible-play deloy tag files && run start script in remote