• Shell脚本批量启停Docker


        最近日常测试中经常需要手动启动或停止docker,于是决定写一个Shell脚本来代替人工操作,另外该脚本,也可以通过Python脚本实行远程调用,详细如下所示:

    目前该脚本是将Container ID写死在脚本中,当然也可以通过传参给脚本来进行控制,大家可以改造一下。

    启动docker

    启动脚本详细如下所示:

    #!/bin/bash
    
    containerIDs="ad3e4d7fc407 a228730a915f ad3e4d7fc4099"
    statusLived="live"
    statusdead="Dead"
    notExistContainer="None"
    retryCount=3
    
    function GetContainerStatus(){
       containerExist=$(sudo docker ps -a | grep -i $1 | wc -l )   
       if [ ${containerExist} -gt 0 ]
         then
           pid=$(sudo docker stats --format "{{.PIDs}}" --no-stream $1 )
           if [ "${pid}" != "0" ]
             then   
               echo "${statusLived}"
           else
               echo "${statusdead}"
           fi
        else
          echo "${notExistContainer}" 
       fi
    }
    
    function StartContainer(){
      sudo docker restart $1
    }
    
    for containerID in ${containerIDs}
     do
       for((i=1;i<=${retryCount};i++))
       do
        status=$(GetContainerStatus ${containerID} )
        echo "Container ${containerID} status is ${status}"
      
        if [ "${status}" == ${statusLived}  ]
          then
            echo "Container ${containerID} already running"
            break
        fi
    
        if [ "${status}" == ${notExistContainer}  ]
          then
            echo "Container ${containerID} not existed"
            break
        fi
    
        if [ "${status}" == ${statusdead}  ]
          then
            echo "Container ${containerID} stopped ,start container"
            StartContainer ${containerID}
            verifyStatus=$(GetContainerStatus ${containerID} )
            if [ "${verifyStatus}" == ${statusLived}  ]
               then
                 echo "start container ${containerID} success "
                 break
            else
               echo "${i} retry start container"
               StartContainer ${containerID}
            fi
        fi
      done
    done
    

    停止docker

    停止脚本详细如下所示:

    #!/bin/bash
    
    containerIDs="589bda1309cd ad3e4d7fc407 a228730a915f ad3e4d7fc4099"
    statusLived="live"
    statusdead="Dead"
    notExistContainer="None"
    retryCount=3
    
    function GetContainerStatus(){
       containerExist=$(sudo docker ps -a | grep -i $1 | wc -l )   
       if [ ${containerExist} -gt 0 ]
         then
           pid=$(sudo docker stats --format "{{.PIDs}}" --no-stream $1 )
           if [ "${pid}" != "0" ]
             then   
               echo "${statusLived}"
           else
               echo "${statusdead}"
           fi
        else
          echo "${notExistContainer}" 
       fi
    }
    
    function StopContainer(){
      sudo docker stop $1
    }
    
    for containerID in ${containerIDs}
     do
      for ((i=1;i<=${retryCount};i++))
        do
         status=$(GetContainerStatus ${containerID} )
         echo "Container ${containerID} status is ${status}"
    
         if [ "${status}" == ${statusdead}  ]
          then
            echo "Container ${containerID} already stopped"
            break
         fi
    
         if [ "${status}" == ${notExistContainer}  ]
           then
            echo "Container ${containerID} not existed"
            break
         fi
    
         if [ "${status}" == ${statusLived}  ]
           then
             echo "Container ${containerID} is lived ,stop container"
             StopContainer ${containerID}
             verifyStatus=$(GetContainerStatus ${containerID} )
             if [ "${verifyStatus}" == ${statusdead}  ]
               then
                  echo "stop container ${containerID} success "
                  break
             else
                echo "${i} retry stop container"
                StopContainer ${containerID}
             fi
          fi
       done
    done
    

    Python调用脚本

    Python示例脚本如下所示:

    import paramiko
    
    def StartContainer(svr,port,user,pwd):
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(svr,port=port, username=user, password=pwd,timeout=5)
        client.exec_command("cd /home/TestCode/ && bash startContainer.sh")
    
    def StopContainer(svr,port,user,pwd):
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(svr, port=port, username=user, password=pwd, timeout=5)
        client.exec_command("cd /home/TestCode/ && bash stopContainer.sh ")
    

    本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注:
    MyQRCode.jpg

  • 相关阅读:
    ubuntu下zip文件操作
    android studio java: -source 1.6 中不支持 switch 中存在字符串 的错误
    手机代替汽车钥匙,苹果公司成功实现了,看看你的车支持不?
    1600多名谷歌员工要求公司停止向警方出售技术
    苹果地图通过电动汽车路线查找充电器
    东方联盟郭盛华发家史:8年来实现跨越式发展
    京东香港上市,开盘239港元,首日上涨5.7%
    这个新漏洞要注意!10亿个网络连接设备会面临被黑客入侵的风险
    黑客瞄准5G用户,新的移动互联网协议漏洞频现
    传感器是自动驾驶汽车未来的关键,它们是汽车的眼睛
  • 原文地址:https://www.cnblogs.com/surpassme/p/11822978.html
Copyright © 2020-2023  润新知