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

      

  • 相关阅读:
    SpringBoot 2.X整合Mybatis
    SpringBoot 2.X集成 jdbc自动配置原理探究
    15 道 Spring Boot 高频面试题,看完直接当面霸【入门实用】
    SpringBoot整合Thymeleaf-基于SpringBoot2.X版本
    vue-manage-system 后台管理系统开发总结
    Node.js 应用:Koa2 使用 JWT 进行鉴权
    Parcel:常见技术栈的集成方式
    HTML5 桌面通知:Notification API
    Electron 实战桌面计算器应用
    Node.js 入门:Express + Mongoose 基础使用
  • 原文地址:https://www.cnblogs.com/slqt/p/10906915.html
Copyright © 2020-2023  润新知