【问题标题】:Chef recipe to clone several git repos to separate folders将多个 git 存储库克隆到单独文件夹的厨师食谱
【发布时间】:2014-08-04 23:21:08
【问题描述】:

我需要将几个 git 存储库克隆到单独的文件夹中(即/var/www/repo_name)。 在我看来,我可以这样做:

git node['git_folder'] do 
  repository node['git_repository']
  reference "master"
  action :sync
  user "username"
end

但是我怎样才能为角色中的一个配方赋予多个属性呢?我可以以某种方式使用数据包来满足我的需求吗?有什么不同的方法吗?

【问题讨论】:

    标签: chef-infra


    【解决方案1】:

    以下是使用数据包完成此任务的方法:

    假设你有如下数据包结构:

    data_bags
      git_repos
        repo_1.json
        repo_2.json
    

    假设你有如下数据包项结构:

    {
      "id": "repo_1",
      "destination": "/var/www/repo_1",
      "url": "https://repo.to/clone.git,
      "revision": "master",
      "user": "username",
      "action": "sync"
    }
    

    iddestinationurl 属性是必需的。如果 revisionuseraction 被省略,配方中将使用默认值:

    data_bag('git_repos').each do |name|
      repo = data_bag_item('git_repos', name)
    
      git repo['destination'] do 
        repository repo['url']
        reference  repo['revision'] || 'master'
        user       repo['user'] || 'username'
        action     repo['action'] ? repo['action'].to_sym : :sync
      end
    end
    

    如您所见,您可以在这种情况下使用数据包,但我不清楚您为什么要这样做。在我看来,这种方法远没有那么直观,而且在调试过程中也更难以推理。

    【讨论】:

    • 还有一个小问题.. 如果我需要按项目名称对数据包项进行分组,而不是为配方提供单个属性,例如节点 ['git-clone']['project'],我怎样才能把它放进食谱?.. 提前谢谢。
    • @Blasterdick 请在另一个帖子上提出另一个问题。
    【解决方案2】:

    一个配方包含资源声明,因此只需声明多个资源:

    git "/var/www/repo1" do
      repository https://github.com/myname/repo1.git
      action :checkout
    end
    
    git "/var/www/repo2" do
      repository https://github.com/myname/repo2.git
      action :checkout
    end
    
    git "/var/www/repo3" do
      repository https://github.com/myname/repo3.git
      action :checkout
    end
    

    有些人喜欢 ruby​​ 中的循环,除非正在驱动食谱(来自数据包?),否则我很不情愿。我喜欢在我的厨师食谱中明确表达意图。

    【讨论】:

    • 感谢您的回复。这个配方将在一堆服务器中使用,并且在每个服务器中都会有不同的属性。在我看来,最好像在 users::sysadmins recipe do 中那样做。
    • 我已经做了一个像你这样的食谱,但我只是好奇,如果我可以在这里使用数据包..
    • @Blasterdick 看起来 Seth 提供了一个数据包示例。
    猜你喜欢
    • 1970-01-01
    • 2015-11-27
    • 2021-11-01
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 2015-10-23
    相关资源
    最近更新 更多