`
cryolite
  • 浏览: 573352 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

建个github的本地中转站

阅读更多
局域网内有多台开发机器,因为网络的原因,从github拉取更新时总是失败,这台成功了那台又不行;还有个原因是网络速度,拉取的工程有81M,网络传输也不快。于是琢磨在本地建个github的镜像库,所有机器都统一clone自这个本地镜像库里,然后由这个镜像库负责与github更新。

cd /storage/mirror_github/
git clone --mirror git://github.com/xxx/yyy.git


这样在本地/storage/mirror_github/yyy.git建立了github上yyy项目的一个镜像了,这是一个裸版本库(裸版本库一般约定以.git为后缀)。

在工作路径下,都从本地github库克隆:
cd /home/bob/workspace/
git clone file:///storage/mirror_github/yyy.git

......
git commit -m "balabala"

提交工作,然后本地推送更新到本地镜像库。
git push

进入镜像库,然后推送更新到github
cd /storage/mirror_github/yyy.git
git push

当有人在外地开发并将代码提交到github后,我们在本地镜像库中更新github
cd /storage/mirror_github/yyy.git
git fetch

注意这里必须用git fetch拉取更新镜像库。
然后所有局域网内开发机就可以从镜像库中同步了

这样局域网内就以本地镜像为中转与github间更新了。
对于rebar的依赖管理会有点麻烦,我们得再写个本地deps的rebar.mirror.config,每次rebar get-deps时指定这个配置文件
rebar -C rebar.mirror.config

但是对依赖的依赖比较麻烦,写了个rebar的插件解决这一问题。

主要想法是抢在在git clone运行之前通过sed将所以github.com换成本地的git镜像库地址。sed命令如下:
sed -E 's/(git|https):\/\/github.com/ssh:\/\/file:\/\/\/storage\/github-repos/g'

rebar先获取项目根目录下所有的依赖项目,然后进入每个依赖项目,获取每个依赖的依赖。
这个替换操作在每次rebar get-dep执行之后执行
'post_get-deps'(_Config, _AppFile) ->
    BaseDir = rebar_config:get_global(base_dir, undefined),
    RelDir = filename:join(BaseDir, "rel"),
    DepsDir = rebar_config:get_global(deps_dir, "deps"),
    CD = rebar_utils:get_cwd(),
    rebar_log:log(debug, "pWD: ~p, Base_Dir: ~s, Deps_Dir: ~s~n", [CD, BaseDir, DepsDir]),

    case lists:prefix(RelDir, CD) of
        true ->
            ok;
        false ->
            [ update_rebar_cfg_by_sed(Dep) || Dep <- rebar_config:get_local(Config, deps, [])],
            ok
    end.

update_rebar_cfg_by_sed(Dep) ->
    rebar_log:log(debug, "dEp: ~p~n", [Dep]),
    Da = erlang:element(1, Dep),

    BaseDir = rebar_config:get_global(base_dir, undefined),
    DepsDir = rebar_config:get_global(deps_dir, "deps"),
    CD = rebar_utils:get_cwd(),

    RC = filename:join([BaseDir, DepsDir, Da]),
    rebar_log:log(debug, "pwd: ~p, base_dir: ~s, deps_dir: ~s, RC = ~s~n", [CD, BaseDir, DepsDir, RC]),

    rebar_utils:sh("sed -E -f " ++ ?LAN_SED(BaseDir) ++ " " ++ filename:join(RC, "rebar.config") ++ " > /tmp/rebar.config", []),
    rebar_utils:sh("mv /tmp/rebar.config " ++ RC, []).



有个比较特殊的依赖(eleveldb)是通过脚本build_deps.sh获取其依赖的。需要将脚本中的github地址替换成本地。而且要在这个依赖的脚本执行之前将地址替换掉:
'pre_get-deps'(_Config, _AppFile) ->
    BaseDir = rebar_config:get_global(base_dir, undefined),
    DepsDir = rebar_config:get_global(deps_dir, "deps"),
    CD = rebar_utils:get_cwd(),
    case lists:prefix(filename:join([BaseDir, DepsDir, eleveldb]), CD) of
        true ->
            rebar_utils:sh("sed -E -f " ++ ?LAN_SED(BaseDir) ++ " c_src/build_deps.sh > /tmp/build_deps.sh", []),
            rebar_utils:sh("chmod +x /tmp/build_deps.sh", []),
            rebar_utils:sh("mv /tmp/build_deps.sh " ++ filename:join(CD, c_src), []);
        false ->
            skip
    end,
    ok.



此外,需要在根项目的rebar.config文件中指定插件:
echo '{plugin_dir, "lan"}.' >> rebar.config
echo '{plugins, [lan_deps_plugin]}.' >> rebar.config


参考:rebar插件指南



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics