Shell腳本啓停Docker

    最近平常測試中常常須要手動啓動或中止docker,因而決定寫一個Shell腳原本代替人工操做,另外該腳本,也能夠經過Python腳本實行遠程調用,詳細以下所示:php

目前該腳本是將Container ID寫死在腳本中,固然也能夠經過傳參給腳原本進行控制,你們能夠改造一下。python

啓動docker

啓動腳本詳細以下所示:docker

#!/bin/bash

containerID="589bda1309cd"
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 ((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

中止docker

中止腳本詳細以下所示:bash

#!/bin/bash

containerID="589bda1309cd"
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 ((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

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 ")
相關文章
相關標籤/搜索