Browse Source

add pullToLocal.sh

krrish 6 years ago
parent
commit
3e44b3fb94
2 changed files with 117 additions and 5 deletions
  1. 112 0
      build/pullToLocal.sh
  2. 5 5
      build/pushToRemote.sh

+ 112 - 0
build/pullToLocal.sh

@@ -0,0 +1,112 @@
+#!/bin/bash
+
+# check input
+if [ -z "$1" ]; then
+    echo "*************************************************************************************"
+    echo 
+    echo " Usage: ./pullToLocal.sh registry.svicloud.com/powercloud/oms/server:V01R01B18-3043"
+    echo
+    echo "*************************************************************************************"
+    echo 
+    exit 1
+fi
+
+# if has another process
+retry=30
+while true; do
+    ps_count=`ps -ef | grep -v grep | grep ${0#./*} | wc -l`
+    retry_count=`expr $retry_count + 1`
+
+    # 30 times
+    if [ "$retry_count" -ge $retry ]; then
+        echo "timeout after 90 seconds, exit."
+        echo 
+        exit 1
+    fi
+
+    # has other process
+    if [ $ps_count -ge 3 ] ; then
+        echo "another process \"${0#./*}\" is running, retry after 3 seconds."
+        sleep 3
+    else
+        break
+    fi
+
+done
+
+# make a host entry
+sed -i '/registry.svicloud.com/d' /etc/hosts
+echo "151.106.8.135 registry.svicloud.com" >>/etc/hosts
+
+
+# add proxy
+if ! systemctl show --property=Environment docker | grep -q HTTP_PROXY; then
+    mkdir -p /etc/systemd/system/docker.service.d/
+    echo "[Service]" > /etc/systemd/system/docker.service.d/proxy.conf
+    echo "Environment=\"HTTP_PROXY=http://pc.dongcj.com:1080/\" \"HTTPS_PROXY=http://pc.dongcj.com:1080\"" >> \
+      /etc/systemd/system/docker.service.d/proxy.conf
+    systemctl daemon-reload
+    systemctl restart docker
+fi
+
+# login remote repo
+docker login -u admin -p "Gocm5]WeqaSufg" registry.svicloud.com &>/dev/null || \
+  { echo "docker login to remote repo failed" && echo && exit 1; } 
+
+
+# pull the remote image
+if docker pull $1; then
+    echo
+    
+    # add local registry entry in hosts file
+    sed -i '/registry.svicloud.com/d' /etc/hosts
+    echo "192.168.100.23 registry.svicloud.com" >>/etc/hosts
+
+    # local repository, so no proxy allowed
+    if systemctl show --property=Environment docker | grep -q HTTP_PROXY; then
+        mkdir -p /etc/systemd/system/docker.service.d/
+        echo > /etc/systemd/system/docker.service.d/proxy.conf
+        systemctl daemon-reload
+        systemctl restart docker
+    fi
+    
+    echo -n "checking local registry status.."
+    sleep 1
+    ping registry.svicloud.com -c 2 &>/dev/null && echo ".ok" || { echo ".failed"; exit 3; }
+    
+    retry=5
+    retry_count=0
+    while true; do
+        retry_count=`expr $retry_count + 1`
+
+        # 5 times
+        if [ "$retry_count" -ge $retry ]; then
+            echo "login failed for 5 times, exit."
+            echo 
+            exit 1
+        fi
+
+        # login
+        if docker login -u admin -p "Changeme_123" registry.svicloud.com &>/dev/null; then
+            break
+         else
+            echo "login to remote registry.svicloud.com $retry_count"
+        fi
+    done
+
+
+
+    
+    if docker push $1; then
+        echo "push success"
+    else
+        echo "push $1 to local repo failed"
+        echo 
+        exit 1
+    fi
+else
+    echo "pull remote image $1 to local failed"
+    echo 
+    exit 1
+fi
+    

+ 5 - 5
build/pushToRemote.sh

@@ -34,10 +34,9 @@ while true; do
 
 done
 
-# make a host entry
-if ! grep -q "registry.svicloud.com" /etc/hosts; then
-    echo "151.106.8.135 registry.svicloud.com" >>/etc/hosts
-fi
+# make a hosts entry
+sed -i '/registry.svicloud.com/d' /etc/hosts
+echo "192.168.100.23 registry.svicloud.com" >>/etc/hosts
 
 # comment to use local repo
 sed -i "/^[^#].*registry.svicloud.com/s/^/#/" /etc/hosts
@@ -59,7 +58,8 @@ docker login -u admin -p Changeme_123 registry.svicloud.com &>/dev/null || \
 # pull the image
 if docker pull $1; then
     echo
-    sed -i "/^[#].*registry.svicloud.com/s/^#//" /etc/hosts
+    sed -i '/registry.svicloud.com/d' /etc/hosts
+    echo "151.106.8.135 registry.svicloud.com" >>/etc/hosts
     
     # add proxy
     if ! systemctl show --property=Environment docker | grep -q HTTP_PROXY; then