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 '文件下发失败!'
    

      

  • 相关阅读:
    分页
    ARC127C Binary Strings 思维 二进制 树
    ARC127A Leading 1s 数位DP
    【算法复习】背包问题 经典动态规划
    iOS15真机调试包下载
    Linux基础03 绝对路径/相对路径, 切换目录cd, 创建目录mkdir, 创建文件touch, 树状显示tree, 复制cp
    Jedis支持哨兵模式下认证
    19C ORA-00600: internal error code, arguments: [kkmmctbf:bad intcoln], [49]
    useMemo 和 useCallback 简单理解
    前端大屏页面布局经验
  • 原文地址:https://www.cnblogs.com/slqt/p/10906915.html
Copyright © 2011-2022 走看看