pushToRemote.sh 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/bin/bash
  2. #
  3. # check input
  4. if [ -z "$1" ]; then
  5. echo "*****************************************************************"
  6. echo
  7. echo " Please input image like below:"
  8. echo " registry.svicloud.com/powercloud/oms/server:V01R01B18-3043"
  9. echo
  10. echo "*****************************************************************"
  11. echo
  12. exit 1
  13. fi
  14. # if has another process
  15. retry=30
  16. while true; do
  17. ps_count=`ps -ef | grep -v grep | grep ${0#./*} | wc -l`
  18. retry_count=`expr $retry_count + 1`
  19. # 30 times
  20. if [ "$retry_count" -ge $retry ]; then
  21. echo "timeout after 90 seconds, exit."
  22. echo
  23. exit 1
  24. fi
  25. # has other process
  26. if [ $ps_count -ge 3 ] ; then
  27. echo "another process \"${0#./*}\" is running, retry after 3 seconds."
  28. sleep 3
  29. else
  30. break
  31. fi
  32. done
  33. # feature
  34. echo
  35. echo " 在这台服务器上推拉镜像到法国,会比其它服务器快至少 10 倍的速度 :)"
  36. echo " 点解...?"
  37. echo
  38. which cstream &>/dev/nulll && \
  39. echo " 科学上网,你值得拥有! --- Need help? QQ: 85509336" | cstream -b1 -t10 || \
  40. echo " 科学上网,你值得拥有! --- Need help? QQ: 85509336" && sleep 1
  41. echo
  42. # make a host entry
  43. if ! grep -q "registry.svicloud.com" /etc/hosts; then
  44. echo "151.106.8.135 registry.svicloud.com" >>/etc/hosts
  45. fi
  46. # comment to use local repo
  47. sed -i "/^[^#].*registry.svicloud.com/s/^/#/" /etc/hosts
  48. # login local repo
  49. docker login -u admin -p Changeme_123 registry.svicloud.com &>/dev/null || \
  50. { echo "docker login to local repo failed" && echo && exit 1; }
  51. # pull the image
  52. if docker pull $1; then
  53. sed -i "/^[#].*registry.svicloud.com/s/^#//" /etc/hosts
  54. sleep 2
  55. echo "checking remote registry status with ping 5 packages"
  56. ping registry.svicloud.com -c 5
  57. docker login -u admin -p "Gocm5]WeqaSufg" registry.svicloud.com &>/dev/null || \
  58. { echo "docker login to remote repo failed" && echo && exit 1; }
  59. echo
  60. if docker push $1; then
  61. echo "push success"
  62. else
  63. echo "push $1 to remote repo failed"
  64. echo
  65. exit 1
  66. fi
  67. else
  68. echo "pull local image $1 to local server failed"
  69. echo
  70. exit 1
  71. fi