From 230d3bf90f3c39f3a96b1d05337c6b8de8bdadc2 Mon Sep 17 00:00:00 2001 From: pengtao Date: Tue, 5 Nov 2019 13:47:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0keys=20=E5=8F=91=E5=B8=83?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/deploy_keys.py | 24 ++++++++++++++++-------- deploy/deploy_keys.yml | 5 ++++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/deploy/deploy_keys.py b/deploy/deploy_keys.py index d3e8337..277ff76 100644 --- a/deploy/deploy_keys.py +++ b/deploy/deploy_keys.py @@ -8,28 +8,36 @@ from config.config import args import os import tarfile import svn - +import shutil define_logger("/data/logs/ops/deploy_keys.log") log = logging.getLogger(__name__) class DeployKeys: def __init__(self, project): + self.env='test' self.project = project - self.svn_base = "/data/publish/svn" + self.svn_base = f"/data/publish/svn/{self.env}" 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) + shutil.rmtree(svn_path) + else: + pass + os.system(f"mkdir -p {svn_path}") + key_path = f"{svn_path}/keys" + args = dict() + tar_file = self.build_svn_tar(key_path) + if tar_file: hostfile = self.build_hostfile() args['hostfile'] = hostfile args['tar_file'] = tar_file self.ansible_deploy(args) + else: + log.error(f"tar file no found with {self.project}") def get_config(self): @@ -40,7 +48,7 @@ class DeployKeys: `deploy_projectmanage` WHERE project_name="{self.project}" - and env='prod'""" + and env='{self.env}'""" mydb = MysqlBase(**args) data = mydb.query(sql) @@ -96,8 +104,8 @@ class DeployKeys: def ansible_deploy(self, args): an = AnsibleAPI(args['hostfile']) - data = {'tga': 'x', 'source': 'x', 'project': 'x'} - resule = an.run_playbook('deploy_keys.yml', **args) + data = {'dest_filename': 'x', 'source': 'x', 'project': 'x'} + resule = an.run_playbook('deploy_keys.yml', **data) if not (resule['failed'] or resule['unreachable']): log.info(f"deploy keys with {self.p} success!") else: diff --git a/deploy/deploy_keys.yml b/deploy/deploy_keys.yml index 081aa22..838aa63 100644 --- a/deploy/deploy_keys.yml +++ b/deploy/deploy_keys.yml @@ -4,5 +4,8 @@ - name: rsync deploy file copy: src={{ source }} dest=/data/apps/{{ dest_filename }} + - name: back old config file + shell: "rsync -avcp /data/app{{ project }}/keys /data/app{{ project }}/keys.old" + - name: decomp file - shell: "cd /data/backups_app/ && mkdir -p {{ tag }} && tar zxvf {{ dest_filename }} -C {{ tag }} && chown -R kingsome. {{ tag }}" \ No newline at end of file + shell: "cd /data/app/ && tar zxvf {{ dest_filename }} -C /data/apps/{{ project }} " \ No newline at end of file