1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
import os import yaml import commands import time import codecs
LOGFILE = '/var/log/pyrsync.log'
ENABLE_LOG = True
VERBOSE_MODE = True
BASEDIR = os.path.dirname(__file__)
def get_file_lists(dirpath): filelists = [] for filename in os.listdir(dirpath): filelists.append(os.path.join(dirpath,filename)) return filelists
def get_time(): return time.strftime('%Y-%m-%d %H:%M:%S ',time.localtime(time.time()))
def logger(line): if ENABLE_LOG: if os.path.isfile(LOGFILE): with codecs.open(LOGFILE,'a','utf-8') as f: f.write("{time}{logline}\n".format(time=get_time(), logline=line)) else: with codecs.open(LOGFILE,'w','utf-8') as f: f.write("{time}{logline}\n".format(time=get_time(), logline=line))
def logger_commands(status,result): logger('result:{status}'.format(status=status)) if VERBOSE_MODE: logger(result)
def runcommand(commandline): return commands.getstatusoutput(commandline)
def deploy_sences(isLocal,shellfile,actor=''): commandline = os.path.join(BASEDIR, 'script', shellfile) if isLocal: (status,result) = runcommand(commandline) logger_commands(status,result) else: upload_script = 'scp {command} {name}@{ip}:/tmp/pyrsync_{scriptName}'.format( command=commandline, name=actor['name'], ip=actor['ip'], scriptName=os.path.basename(commandline) ) (status,result) = runcommand(upload_script) logger_commands(status, result) commandline = 'ssh {name}@{ip} "chmod u+x /tmp/pyrsync_{command} && /tmp/pyrsync_{command} && rm -f /tmp/pyrsync_{command}"'.format( name=actor['name'], ip=actor['ip'], command=os.path.basename(commandline), ) (status,result) = runcommand(commandline) logger_commands(status, result)
def sync_files(actor,sence): rsyncCommand = 'rsync {action} --exclude-from="{exclude}" {name}@{ip}:{path} {savepath}'.format( action=actor['action'], exclude=os.path.join(BASEDIR,'exclude',sence['exclude']), name=actor['name'], ip=actor['ip'], port=actor['port'], path=sence['item'], savepath=sence['savepath'] ) logger('Command:{command}'.format(command=rsyncCommand)) (status,result) = runcommand(rsyncCommand) logger_commands(status,result)
def play_playbooks(filelists): for filelist in filelists: if os.path.isfile(filelist) and os.path.splitext(filelist)[-1]=='.yaml': with open(filelist,'r') as f: playbook = yaml.load(f) sences = playbook['playbook']['sences'] for sence in sences: if playbook['execute']['pre-local']: deploy_sences(True,playbook['execute']['pre-local']) if playbook['execute']['pre-remote']: deploy_sences(False, playbook['execute']['pre-remote'],playbook['actor']) sync_files(playbook['actor'],sence) if playbook['execute']['post-remote']: deploy_sences(False,playbook['execute']['post-remote'],playbook['actor']) if playbook['execute']['post-local']: deploy_sences(True,playbook['execute']['post-local'])
if __name__=='__main__': play_playbooks(get_file_lists(os.path.join(BASEDIR,'playbook')))
|