由於我國的網絡的原因,在訪問谷歌等一些國外網絡資源時經常會遇到被攔截的情況,導致repo等一些代碼管理工具拉取代碼網絡受限受阻,下面提供一個可以參考的簡單解決方案。
1、repo init時的遇到fatal: Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle問題
先嘗試關閉一下防火牆,如果還是不行,在進行嘗試下面的方法。
方法1:
獲取鏡像:
1 mkdir ~/bin 2 PATH=~/bin:$PATH 3 curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo 4 chmod a+x ~/bin/repo
方法2:
先執行下面的命令單獨克隆repo,然后將git-repo目錄里面的repo文件復制到bin目錄,在同步源碼的工作目錄新建.repo文件夾,把git-repo重命名為repo復制到.repo目錄下,然后再重新執行repo init.
1 git clone https://gerrit-googlesource.lug.ustc.edu.cn/git-repo
2、repo sync同步拉取代碼時,經常會出現卡住或者失敗的情況
解決方法1:修改dns為google提供的8.8.8.8的dns服務器。
ubuntu下修改方法如下:
1)編輯 /etc/resolvconf/resolv.conf.d/base 文件(文件默認是空的),在里面添加下面兩行代碼
nameserver 8.8.8.8
nameserver 8.8.4.4
2)執行 resolvconf -u命令,然后使用命令 cat /etc/resolv.conf查看dns配置文件如果發現已經添加了新加的兩行dns server就可以了。
解決方法2:添加repo sync失敗后自動重新執行的腳本
如果修改完dns服務后依然會有repo sync的情況,就只能通過持續執行repo sync命令的方式來解決了。
1)獲取網絡檢測工具ifstat
由於repo sync在執行時被卡住后,網卡的流入流量會變小(在沒有其他需要下載或上網的進程執行的情況下),所以可以考慮通過檢測網卡流入流量的變小情況來判斷repo sync執行卡住,進行重新執行(由於repo sync支持斷點續傳)。
所以先要安裝網絡檢測工具ifstat,ubuntu獲取方法為sudo apt-get install ifstat
2)運行重復執行腳本
將如下腳本代碼保存為.sh后綴的shell文件后,執行腳本,如果使用xshell連接的虛擬機或服務器可以考慮用&的方式在后台運行(如果在windows復制網頁的shell代碼,最好用notepad等編輯工具轉換為Unix文件的utf-8編碼格式,否則可能會導致執行失敗)。
下面的腳本也有一些問題,沒辦法判斷repo sync執行是否完成了,即使執行完成了還是會重新執行,不過不影響代碼拉取成功,如果有更好的解決方案歡迎和我一起討論。
1 #!/bin/bash 2 3 #殺掉repo sync進程 4 kill_reposync() { 5 PID=`ps aux |grep python|grep [r]epo |awk '{print $2}'` 6 [[ -n $PID ]] && kill $PID 7 } 8 9 #啟動reposync進程 10 start_reposync() { 11 repo sync & 12 } 13 14 #重啟reposync進程 15 restart_sync() { 16 kill_reposync 17 start_reposync 18 } 19 20 #網絡檢測相關閾值 21 th_net_value="30" #實際檢測,repo sync卡住時,網卡入口數據小於10 22 th_retry_times=100 #低於網絡檢測閾值次數限制 23 ((count_low=0)) 24 25 restart_sync 26 27 28 while [[ 1 ]]; do 29 # 用ifstat檢測網速 30 cur_speed=`ifstat 1 1 | tail -n 1 | awk '{print $1}'` 31 32 result=$(echo "$cur_speed < $th_net_value" | bc) 33 if [[ $result == "1" ]]; then 34 ((count_low++)) 35 else 36 ((count_low=0)) 37 fi 38 if ((count_low > th_retry_times)); then 39 ((count_low=0)) 40 echo "restart repo sync" 41 restart_sync 42 fi 43 done