关于使用repo时repo init和repo sync失败的一个解决方案

       由于我国的网络的原因,在访问谷歌等一些国外网络资源时经常会遇到被拦截的情况,导致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

 

posted @ 2017-11-01 10:23  代码的搬运工  阅读(29442)  评论(0编辑  收藏  举报