deploy git project

This commit is contained in:
pengtao 2021-06-15 16:49:50 +08:00
parent 075c43d2a9
commit 2d80f7be3f
3 changed files with 126 additions and 3 deletions

17
1.yaml Normal file
View File

@ -0,0 +1,17 @@
- hosts: all
tasks:
- name: make directory for gz files
shell: "mkdir -p /data/backups_app"
- name: rsync deploy file
copy: src={{ source }} dest=/data/backups_app/{{ source }}
- name: decomp file
#shell: cd /data/backups_app/ && mkdir -p {{ tag }}
shell: "cd /data/backups_app/ && mkdir -p {{ desc_path }} && tar zxvf {{ source }} -C {{ desc_path }} && chown -R {{ user }}. {{ desc_path }}"
- name: create symbolic
shell: "cd {{ paths }} && rm -f {{ name }} && ln -s /data/backups_app/{{ tag }} {{ name }}"
- name: restart service
shell: "su {{ user }} -c 'cd /data/apps/{{ paths }}/{{ name }} && {{ start_script }}'"

93
deploy/deploy_git.py Normal file
View File

@ -0,0 +1,93 @@
# -*- coding: utf-8 -*-
# python3 deploy_keys.py cloud_php
#
from ops.mmysql import MysqlBase
from ops.mansible import AnsibleAPI
from ops.mlog import define_logger
import logging
import datetime
import os
import pdb
define_logger("/data/logs/ops/deploy_git.log")
log = logging.getLogger(__name__)
dbargs = dict()
dbargs['host'] = '10.10.3.5'
dbargs['user'] = 'ops'
dbargs['pswd'] = 'deploy2018'
dbargs['db'] = 'ywplatform'
class DeployGit:
def __init__(self, name):
self.name = name
self.now = datetime.datetime.now().strftime("%Y%m%dH%M%S")
def run(self):
sql = "select hosts,paths,user,url,pre_script,start_script from ops_deploy_git where name='{name}' and in_used=1;".format(
name=self.name)
mydb = MysqlBase(**dbargs)
data = mydb.query(sql)
pdb.set_trace()
self.infos = list()
if data:
for line in data:
if line:
hosts, paths, user, url, pre_script, start_script = line
self.infos.append(
(self.name, hosts, paths, user, url, pre_script, start_script))
else:
return False
self.infos = list(set(self.infos))
if self._build_tag():
self._deploy_remote()
else:
self.write("build {name} failed!".format(name=self.name))
def _build_tag(self):
url = self.infos[0][4]
pre_script = self.infos[0][5]
self.tag_name = "{name}{now}".format(name=self.name, now=self.now)
build_cmd = "cd /data/publish/git/{name} && git clone {url} && cd {name} && sh {pre_script} && cd .. && tar tar zcvf {tag_name}.tar.gz {name} ".format(
name=self.name, url=url, tag_name=self.tag_name, pre_script=pre_script)
os.system(build_cmd)
self.tag_file = os.path.isfile(
"/data/publish/git/"+self.tag_name+'tag.gz')
if self.tag_file:
return True
else:
return False
def _deploy_remote(self):
for line in self.infos:
if line:
hosts, paths, user, _, _, start_script = line
hostfile = self.write_host(hosts)
an = AnsibleAPI(hostfile)
data = {'paths': paths, 'desc_path': self.tag_name, 'user': user,
'source': self.tag_file, 'name': self.name, 'start_script': start_script}
resule = an.run_playbook(
'/data/git/dev_ops/ansible/playbook/publish_git.yml', **data)
if not (resule['failed'] or resule['unreachable']):
log.info(f"deploy success! resule={resule}")
return True
else:
log.error(f"deploy failed info={resule}")
return False
def write_host(self, ip_addr):
host_file = "%s/host%s" % ('/tmp',
datetime.datetime.now().strftime("%Y%m%dH%M%S"))
with open(host_file, 'a+') as f:
for x in ip_addr:
f.writelines(x + '\n')
return host_file
def main():
d = DeployGit('dev_ops')
d.run()
if __name__ == '__main__':
main()

19
web.py
View File

@ -14,6 +14,7 @@ import pdb
from data_channel.gamelog_external import Build_Gamelog_Config
from data_channel.mp2shushu import Mp2shushu
from deploy.deploy_keys import DeployKeys
from deploy.deploy_git import DeployGit
from deploy.clear_service_data import ClearService
from online.online_data import OnlineData2mysql
import json
@ -47,14 +48,26 @@ class DispatchHandler(tornado.web.RequestHandler):
def post(self):
pdb.set_trace()
data = json.loads(self.request.body)
print(data)
if self.get_query_argument('c') == 'Ops' and self.get_query_argument('a') == 'git_webhook':
if data.get('event_name', None) == 'push':
yield self._git_webhook()
else:
self.write("pls check args!")
def _git_webhook(self):
pass
token = self.request.headers.get('X-Gitlab-Token', None)
if token == "kingsome_devops_937711":
name = json.loads(self.request.body).get(
'project', None).get('name', None)
branch = json.loads(self.request.body).get(
'project', None).get('default_branch', None)
if branch == "master":
d = DeployGit(name)
d.run()
else:
self.write("pls check args {name} {branch}!".format(
name=name, branch=branch))
else:
self.write("{token} not in define!".format(token=token))
def _user_online(self):
post_data = {}