pushToRemote.sh.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/bash
  2. # check input
  3. if [ -z "$1" ]; then
  4. echo "*****************************************************************"
  5. echo
  6. echo " Please input image like below:"
  7. echo " registry.svicloud.com/powercloud/oms/server:V01R01B18-3043"
  8. echo
  9. echo "*****************************************************************"
  10. echo
  11. exit 1
  12. fi
  13. # if has another process
  14. retry=30
  15. while true; do
  16. ps_count=`ps -ef | grep -v grep | grep ${0#./*} | wc -l`
  17. retry_count=`expr $retry_count + 1`
  18. # 30 times
  19. if [ "$retry_count" -ge $retry ]; then
  20. echo "timeout after 90 seconds, exit."
  21. echo
  22. exit 1
  23. fi
  24. # has other process
  25. if [ $ps_count -ge 3 ] ; then
  26. echo "another process \"${0#./*}\" is running, retry after 3 seconds."
  27. sleep 3
  28. else
  29. break
  30. fi
  31. done
  32. # make a host entry
  33. if ! grep -q "registry.svicloud.com" /etc/hosts; then
  34. echo "151.106.8.135 registry.svicloud.com" >>/etc/hosts
  35. fi
  36. # comment to use local repo
  37. sed -i "/^[^#].*registry.svicloud.com/s/^/#/" /etc/hosts
  38. # login local repo
  39. docker login -u admin -p Changeme_123 registry.svicloud.com &>/dev/null || \
  40. { echo "docker login to local repo failed" && echo && exit 1; }
  41. # pull the image
  42. if docker pull $1; then
  43. sed -i "/^[#].*registry.svicloud.com/s/^#//" /etc/hosts
  44. sleep 2
  45. docker login -u admin -p "Gocm5]WeqaSufg" registry.svicloud.com &>/dev/null || \
  46. { echo "docker login to remote repo failed" && echo && exit 1; }
  47. echo
  48. if docker push $1; then
  49. echo "push success"
  50. else
  51. echo "push $1 to remote repo failed"
  52. echo
  53. exit 1
  54. fi
  55. else
  56. echo "pull local image $1 to local server failed"
  57. echo
  58. exit 1
  59. fi