Last active
August 3, 2017 06:15
-
-
Save zhouyl/58e748143b120fe7b3f925d2de3f76b2 to your computer and use it in GitHub Desktop.
rsync tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[exclude] | |
ignores=*.log,*.pyc | |
[hosts] | |
sms=myhost.com::prepare_home/sms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys, os, argparse, re, time, random, ConfigParser | |
def printf(message): | |
formatters = { | |
'note': '\033[0;36m', | |
'info': '\033[0;34m', | |
'succ': '\033[0;32m', | |
'warn': '\033[0;33m', | |
'err': '\033[0;31m', | |
'crit': '\033[0;37m\033[41m', | |
} | |
for (k, v) in formatters.items(): | |
message = re.sub('<%s>(.*?)</%s>' % (k, k), v + "\\1\033[0m", message) | |
print message | |
def err(message): | |
printf('\n' + message + '\n') | |
sys.exit() | |
def parse_argv(): | |
version = time.strftime('v%Y%m%d_%H%M%S_') + str(random.randint(1000, 9999)) | |
parser = argparse.ArgumentParser(description='版本发布工具', formatter_class=argparse.RawTextHelpFormatter) | |
parser.add_argument('-d', '--work-dir', action='store', dest='workdir', type=str, required=True, help='工作目录') | |
parser.add_argument('-v', '--version', action='store', dest='version', type=str, default=version, help='版本号,默认自动生成') | |
parser.add_argument('-t', '--target', action='store', dest='target', type=str, required=True, help='目标主机') | |
return parser.parse_args() | |
if __name__ == '__main__': | |
args = parse_argv() | |
if not os.path.isdir(args.workdir): | |
err('<err>无效的工作目录:</err> [ <warn>%s</warn> ]' % args.workdir) | |
workdir = os.path.realpath(args.workdir) | |
ini_conf = workdir + '/.sync.ini' | |
if not os.path.isfile(ini_conf): | |
err('<err>缺少配置文件:</err> [ <warn>%s</warn> ]' % ini_conf) | |
config = ConfigParser.ConfigParser() | |
config.read(ini_conf) | |
if not config.has_section('hosts'): | |
err('<err>配置文件缺少 hosts 参数!</err>') | |
hosts = dict(config.items('hosts')) | |
if args.target not in hosts: | |
err('<err>无效的目标主机:</err> [ <warn>%s</warn> ]' % args.target) | |
ignores = [] | |
if config.has_section('exclude'): | |
ignores = map(lambda x: x.strip(), config.get('exclude', 'ignores').split(',')) | |
cmd = 'rsync -avp %s/ %s/%s/' % (workdir, hosts.get(args.target).strip('/'), args.version) | |
for match in ignores: | |
cmd += ' --exclude=' + match | |
if os.path.isfile(workdir + '/exclude.txt'): | |
cmd += ' --exclude-from=%s/exclude.txt' % workdir | |
printf('\n<warn>执行命令:</warn>\n<info>%s</info>' % cmd) | |
printf("\n<warn>执行结果:</warn>\033[0;34m") | |
os.system(cmd) | |
print "\033[0m\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment