zoukankan      html  css  js  c++  java
  • 用saltstack命令下发文件

    #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('
    ').strip('
    ')!='/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('
    ').strip('
    ')!='/opt/p13390677_112040_Linux-x86-64_2of7.zip':
        error=1
    if error==0:
        print '文件下发成功'
    else:
        print '文件下发失败!'
    

      

  • 相关阅读:
    Java tomcat max-http-header-size配置导致的oom
    Idea修改jvm参数
    Java List的SubList使用问题
    Java Arrays.asList的三个坑
    Java 重写equals的时候为什么一定要重写hashcode-一个例子
    远心镜头
    镜头常识总结
    halcon中保存图像jpeg的压缩比
    红外光 相机拍照
    电磁波的穿透能力总结
  • 原文地址:https://www.cnblogs.com/slqt/p/10906915.html
Copyright © 2011-2022 走看看