添加keys 发布接口

This commit is contained in:
pengtao 2019-10-25 16:01:43 +08:00
parent 6683a91921
commit 290c568c9c
4 changed files with 138 additions and 5 deletions

1
deploy/__init__.py Normal file
View File

@ -0,0 +1 @@
# -*- coding: utf-8 -*-

113
deploy/deploy_keys.py Normal file
View File

@ -0,0 +1,113 @@
# -*- coding: utf-8 -*-
from myops.mmysql import MysqlBase
from myops.mansible import AnsibleAPI
from myops.mylog import define_logger
import logging
import datetime
from config.config import args
import os
import tarfile
import svn
define_logger("/data/logs/ops/deploy_keys.log")
log = logging.getLogger(__name__)
class DeployKeys:
def __init__(self, project):
self.project = project
self.svn_base = "/data/publish/svn"
self.svn_paths = f"{self.svn_base}/{self.project}"
def run(self):
svn_path, ipadr = self.get_config()
if svn_path:
key_path = f"{svn_path}/keys"
args = dict()
tar_file = self.build_svn_tar(key_path)
hostfile = self.build_hostfile()
args['hostfile'] = hostfile
args['tar_file'] = tar_file
self.ansible_deploy(args)
def get_config(self):
sql = f"""SELECT
config_address,
run_server
FROM
`deploy_projectmanage`
WHERE
project_name="{self.project}"
and env='prod'"""
mydb = MysqlBase(**args)
data = mydb.query(sql)
ip_addr = list()
if data:
for line in data:
try:
svn_path = line[0]
ip_addr.append(line[1])
except Exception:
log.error(f"split {self.project} config failed", exc_info=True)
svn_path = None
return (svn_path, ip_addr)
def build_svn_tar(self, svn_path):
try:
log.info("svn export config start")
s = svn.remote.RemoteClient(svn_path)
s.export(self.svn_paths, force=True)
log.info("svn export config success")
except Exception:
log.error(f"get remote {self.project} svn failed", exc_info=True)
else:
log.info("tarfile {0} config to tar.gz start".format(self.project))
tar = tarfile.open(self.svn_paths + '.tar.gz', 'w:gz')
os.chdir(self.svn_paths)
for f in os.listdir(self.svn_paths):
tar.add(f)
tar.close()
# 判断是否有生成tar包
list_dir = os.listdir(self.svn_base)
tar_files = list()
for i in list_dir:
if i == "{0}.tar.gz".format(self.project) and os.path.isfile(os.path.join(self.basedir, i)):
tar_files.append(os.path.join(self.basedir, i))
if len(tar_files) == 1:
log.info("tarfile {0} config to tar.gz success".format(self.project))
return tar_files
else:
return None
def build_hostfile(self, ip_addr):
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
host_files = f"/tmp/host_{now}"
with open(host_files, 'w') as f:
for line in ip_addr:
f.writelines(line + '\n')
return host_files
def ansible_deploy(self, args):
an = AnsibleAPI(args['hostfile'])
data = {'tga': 'x', 'source': 'x', 'project': 'x'}
resule = an.run_playbook('deploy_keys.yml', **args)
if not (resule['failed'] or resule['unreachable']):
log.info(f"deploy keys with {self.p} success!")
else:
log.error(f"{self.p} deploy keys failed,{resule}", exc_info=True)
def main():
dk = DeployKeys()
dk.run()
if __name__ == "__main__":
main()

8
deploy/deploy_keys.yml Normal file
View File

@ -0,0 +1,8 @@
- hosts: all
tasks:
- name: rsync deploy file
copy: src={{ source }} dest=/data/apps/{{ dest_filename }}
- name: decomp file
shell: "cd /data/backups_app/ && mkdir -p {{ tag }} && tar zxvf {{ dest_filename }} -C {{ tag }} && chown -R kingsome. {{ tag }}"

21
web.py
View File

@ -13,7 +13,7 @@ from config.config import *
import pdb
from data_channel.gamelog_external import Build_Gamelog_Config
from data_channel.mp2shushu import Mp2shushu
from deploy.deploy_keys import DeployKeys
define_logger("/data/logs/myops/ops_interface.log")
log = logging.getLogger(__name__)
tornado.options.define("port", default=interface_port, type=int, help="run server on the given port.")
@ -27,13 +27,24 @@ class DispatchHandler(tornado.web.RequestHandler):
self._selfgamelogexternal()
elif self.get_query_argument('c') == 'Ops' and self.get_query_argument('a') == 'mp2ss':
yield self._selfmp2ss()
elif self.get_query_argument('c') == 'Deploy' and self.get_query_argument('a') == 'keys':
yield self._selfdeploykeys()
else:
self.write("pls check args!")
@gen.coroutine
def post(self):
if self.get_query_argument('c') == 'Ops' and self.get_query_argument('a') == 'upJumpRecording':
self._selfupJumpRecording()
# @gen.coroutine
# def post(self):
# if self.get_query_argument('c') == 'Ops' and self.get_query_argument('a') == 'upJumpRecording':
# self._selfupJumpRecording()
def _selfdeploykeys(self):
try:
project = self.get_query_argument('project')
dk = DeployKeys(project)
dk.run()
except Exception:
log.error(f"deploy keys with {project} failed", exc_info=True)
def _selfgamelogexternal(self):
try: