用saltstack命令下发文件

2019/05/22 16:49
阅读数 34
#coding = utf8
import shutil
import sys
import os
import subprocess

proxy_path = sys.executable.split('embedded')[0]
cache_dir = os.path.normpath(os.path.join(proxy_path, 'scripts', 'script'))
conf_path=os.path.join(proxy_path, 'conf')
path1=os.path.join(cache_dir,'p13390677_112040_Linux-x86-64_1of7.zip')
path2=os.path.join(cache_dir,'p13390677_112040_Linux-x86-64_2of7.zip')

if not os.path.exists(path1):
    shutil.copyfile('/opt/p13390677_112040_Linux-x86-64_1of7.zip',path1)
if not os.path.exists(path2):
    shutil.copyfile('/opt/p13390677_112040_Linux-x86-64_2of7.zip', path2)
print '文件准备成功,开始下发'


def run_cmd(cmd, cwd=None, runas=None):
    if not sys.platform.startswith('win') and runas and runas != 'root':
        cmd = 'su - {} -c "{}"'.format(runas, cmd)
    # logger.info(cmd)
    proc = subprocess.Popen(cmd,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT,
                            shell=True,
                            cwd=cwd)
    return proc


def get_output(cmd, cwd=None, wait=True, runas=None):
    proc = run_cmd(cmd, cwd=cwd, runas=runas)
    lines = []
    if wait:
        while proc.poll() is None:
            if proc.stdout:
                lines.extend(proc.stdout.readlines())
    lines.extend(proc.stdout.readlines())
    return lines


error=0

proxy_path = (os.sep).join(sys.executable.split(os.sep)[:-2])
cache_dir = os.path.normpath(os.path.join(proxy_path, 'scripts', 'script'))
conf_path=os.path.join(proxy_path.strip('embedded'), 'conf')

exec_path = os.path.normpath(os.path.join(proxy_path,'bin','python'))

salt_path = os.path.normpath(os.path.join(proxy_path,'bin', 'salt'))
cmd1="{} {} '{}' cp.get_file salt://script/p13390677_112040_Linux-x86-64_1of7.zip /opt/p13390677_112040_Linux-x86-64_1of7.zip -c {}".\
    format(exec_path,salt_path,agent_ip,conf_path)
res=get_output(cmd1)[1]
#print cmd1
if res.strip().strip('\n').strip('\r')!='/opt/p13390677_112040_Linux-x86-64_1of7.zip':
    error=1

cmd2="{} {} '{}' cp.get_file salt://script/p13390677_112040_Linux-x86-64_2of7.zip /opt/p13390677_112040_Linux-x86-64_2of7.zip -c {}".\
    format(exec_path,salt_path,agent_ip,conf_path)
res=get_output(cmd2)[1]
#print cmd2
#print res
if res.strip().strip('\n').strip('\r')!='/opt/p13390677_112040_Linux-x86-64_2of7.zip':
    error=1
if error==0:
    print '文件下发成功'
else:
    print '文件下发失败!'

  

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部