Chef recipe to clone several git repos to separate folders

南楼画角 提交于 2020-01-03 02:58:29

问题


I need to clone several git repositories into separate folders (i.e /var/www/repo_name). It seems to me, I can do that with:

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

But how can I give several attributes to one recipe in the role?. Can I somehow use data bags for my needs? Is there any different way?


回答1:


Here is how you would accomplish it using data bags:

Assuming you have the following data bag structure:

data_bags
  git_repos
    repo_1.json
    repo_2.json

Assuming you have the following data bag item structure:

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

The attributes id, destination, and url are required. If revision, user, or action are omitted, default values will be used in the recipe:

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

As you can see, you could use data bags in this instance, but it's unclear to me why you would want to do so. In my opinion, this approach is far less intutive and much more difficult to reason about during debugging.




回答2:


A recipe contains resource declarations, so just declare multiple resources:

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

Some people favour a loop in ruby, I'm reluctant unless the cookbook is being driven (from a databag?). I like clear statements of intent in my chef recipes.



来源:https://stackoverflow.com/questions/25118153/chef-recipe-to-clone-several-git-repos-to-separate-folders

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!