标签:unless 目标 tac run oca pre exist 3.3 func
示例:
1、程序集主目录:service下
2、sls 目录: service_sls 下
ServerName 服务 sls 示例:
1、发布 servername.sls
{% set time = salt[‘cmd.run‘](‘D:\script\getinfo.py gettime‘) %} {% set service_name = ‘ServerName‘ %} Services_dir: file.directory: - name: D:\Services - unless: - test -d D:\Services backup_dir: file.directory: - name: D:\Services\backup - unless: - test -d D:\Services\back - require: - file: Services_dir temp_dir: file.directory: - name: D:\Services\temp - unless: - test -d D:\Services\temp backup_service_dir: file.directory: - name: D:\Services\backup\{{service_name}} - require: - file: backup_dir temp_service_dir: file.directory: - name: D:\Services\temp\{{service_name}} - require: - file: temp_dir backup_time_dir: file.directory: - name: D:\Services\backup\{{service_name}}\{{time}} - require: - file: backup_service_dir deploy_time_dir: file.directory: - name: D:\Services\temp\{{service_name}}\{{time}} - require: - file: temp_service_dir backup_service: cmd.run: - name: xcopy /s D:\{{service_name}}\*.* D:\Services\backup\{{service_name}}\{{time}}\{{service_name}}\ - require: - file: backup_time_dir deploy_service_to_temp: file.recurse: - name: D:\Services\temp\{{service_name}}\{{time}}\{{service_name}} - source: - salt://{{service_name}}/files/{{service_name}} - require: - cmd: backup_service - file: deploy_time_dir deploy_service_to_public: cmd.run: - name: D:\script\dircopy.py D:\Services\temp\{{service_name}}\{{time}}\{{service_name}}\ D:\{{service_name}} - require: - file: deploy_service_to_temp
发布 指令 :
salt -L ‘172.16.3.33-35‘ state.sls service.servername
2、回滚备份 backup.sls
{% set time = salt[‘cmd.run‘](‘D:\script\getinfo.py gettime‘) %} {% set service_name = ‘ServerName‘ %} {% set lastbackup = salt[‘cmd.run‘](‘D:\script\getinfo.py getbackup "D:\\Services\\backup\\ServerName"‘) %} service_backup: cmd.run: - name: D:\script\dircopy.py D:\Services\backup\{{service_name}}\{{lastbackup}}\{{service_name}}\ D:\{{service_name}}\
回滚 指令 :
salt -L ‘172.16.3.33-35‘ state.sls service.backup
3、涉及的脚本放置在客户端机器 D:\script 目录下:
getinfo.py
#coding=utf-8 import os import time import sys def getTime(*vars): date = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())) return date def getLastBackup(*vars): if not os.path.exists(vars[0]): print ‘%r: 备份路径不存在‘ % vars[0] sys.exit(1) try: os.chdir(vars[0]) except OSError, e: print e sys.exit(1) else: lastbackup = sorted(os.listdir(os.curdir)).pop() return lastbackup def usage(): print ‘Usage: python getinfo.py [ gettime | getbackup <service>]‘ funcdict = {‘gettime‘: getTime, ‘getbackup‘: getLastBackup} def main(): if len(sys.argv) < 2: usage() sys.exit(1) option = sys.argv[1:] if option[0] in funcdict.keys(): print funcdict[option[0]](*option[1:]) else: usage() if __name__ == ‘__main__‘: main()
dircopy.py
#coding=utf-8 import shutil import os import sys def dircopy(pathsrc, pathdst): os.path.walk(pathsrc, visit, ‘‘) def visit(arg, dirname, names): dir = dirname.replace(sys.argv[1], ‘‘) # print ‘当前目录: ‘, dirname # print ‘当前相对目录:‘, dir # print ‘当前目录下的文件:‘, names # print ‘PathDst‘, PathDst # print ‘+‘ # print dir dirnamedst = os.path.join(sys.argv[2], dir) # print ‘目标目录:‘, dirnamedst if not os.path.exists(dirnamedst): os.mkdir(dirnamedst) # print ‘创建目标目录成功‘ for file in names: # print ‘判断文件:‘, file if os.path.isdir(os.path.join(dirname, file)) and not os.path.exists(os.path.join(dirnamedst, file)): os.mkdir(os.path.join(dirnamedst, file)) # print ‘创建目录:‘, os.path.join(dirnamedst, file) elif os.path.isfile(os.path.join(dirname, file)): shutil.copy2(os.path.join(dirname, file), os.path.join(dirnamedst, file)) # print ‘拷贝文件:‘, os.path.join(dirname, file) # print ‘-‘*50 # print ‘开始下一次遍历‘ # print def main(): if len(sys.argv) != 3: print ‘Usage: dircopy.py src dst‘ sys.exit(1) else: if not sys.argv[1].endswith(os.path.sep): sys.argv[1] = sys.argv[1] + os.path.sep if not sys.argv[2].endswith(os.path.sep): sys.argv[2] = sys.argv[2] + os.path.sep dircopy(sys.argv[1], sys.argv[2]) if __name__ == ‘__main__‘: main()
标签:unless 目标 tac run oca pre exist 3.3 func
原文地址:https://www.cnblogs.com/sharesdk/p/10821862.html