chefã®ä¾åå®ç¾©(depends)ãåéããã¦ãã話
chefã§ããmetadata.rb
ä¸ã®depends
ãä¿è¨¼ããä¾åæ§ãã©ãã¾ã§ãªã®ããåéããã¦ããã®ã§ã¡ã¢ãã¦ããã¾ãã
çµè«ããããã¨ã dependsæå®ã¯attributeã®åç
§å
ã¨ãã¦èªã¿è¾¼ãã ã ã®æ©è½ã§ããã
- dependsã«ããä¿è¨¼ããããã¨
- dependsåç §å cookbookã®attributeãæµç¨ã§ãããã¨
- dependsã§ä¿è¨¼ãããªããã¨
- å¿ ãããdependsåç §å cookbookãå®è¡ããå¿ è¦ããªããã¨
- ã¾ãã¦ããdependsåç §å ããè¨è¼å cookbookãããå ã«ãå®è¡ãããå¿ è¦ã¯ãªããã¨
ãã®ããä¿è¨¼ãããªããã¨ããã§ããã¨ã°ããåéããã¦ããã®ã§ãdependsã«é ¼ã£ãéçºããã¦ãããã¨ã©ã¼ã«ãªããã«ãããâ¦ï¼ã¨ãªã£ã¦ãã¾ã£ãã®ã§ããã
å ¬å¼ã®è¨è¿°
以ä¸ã®ããã«è¨è¼ãããã¾ãã
Use to show that a cookbook has a dependency on another cookbook.
ããã ãã ã¨åãããªãã§ããã
ãã¹ã
äºåæºå
cookbook aã¨cookbook bãä½ã£ã¦ããã¾ãã
æ¦ã以ä¸ã®ãããªæ§é ã§ãã(æ¤è¨¼ã§ç¡é¢ä¿ãªã¨ããã¯çç¥ãã¦ãã¾ã)
chef-repo âcookbooks ââa âââattributes/default.rb ââârecipes/default.rb âââmetadata.rb ââb â âattributes/default.rb â ârecipes/default.rb â âmetadata.rb âlocalhost.json
cookbook bã®metadata.rb
ä¸ã§a
ã¸ã®ä¾åæ§ãå®ç¾©ãã¾ãã
cookbooks/b/metadata.rb
<ç¥> depends 'a'
cookbook a,bå ã§ã¯ããããattributeãå®ç¾©ãã¦ããã¾ãã
cookbooks/a/attributes/default/rb
default['a']['value'] = "attribute a"
cookbooks/b/attributes/default/rb
default['b']['value'] = "attribute b"
cookbook bã®recipeã§ã¯ãcookbook a,bã®attributeãåç §ããããã«ãã¾ãã
cookbooks/b/recipes/default.rb
log "this is cookbook b" log "attribute b: #{node['b']['value']}" log "attribute a: #{node['a']['value']}"
chef-solo
å®è¡æã¯ãcookbook bã®ã¿ãå®è¡ããããã«ãã¾ãã
{ "run_list":[ "recipe[b]" ] }
ãã®ã¾ã¾å®è¡ããå ´å
dependsæå®ãããã°ããã¨ãä»cookbookãå®è¡ããªãã£ãã¨ãã¦ããattributeã¯å©ç¨ã§ãã¾ãã
$ sudo chef-solo -c solo.rb -j ./localhost.json Starting Chef Client, version 12.0.3 Compiling Cookbooks... Converging 3 resources Recipe: b::default * log[this is cookbook b] action write * log[attribute b: attribute b] action write * log[attribute a: attribute a] action write Running handlers: Running handlers complete Chef Client finished, 3/3 resources updated in 1.688893613 seconds
dependsæå®ããããä»cookbookã®attributeãåç §ããå ´å
attributeãåç §ã§ããªãã®ã§ãå½ç¶ã¨ã©ã¼ã«ãªãã¾ãã
cookbooks/b/metadata.rb
<ç¥> # depends 'a' # ã³ã¡ã³ãã¢ã¦ã
å®è¡æã¨ã©ã¼ã
$ sudo chef-solo -c solo.rb -j ./localhost.json Starting Chef Client, version 12.0.3 Compiling Cookbooks... ================================================================================ Recipe Compile Error in /home/vagrant/chef-repo/cookbooks/b/recipes/default.rb ================================================================================ NoMethodError ------------- undefined method `[]' for nil:NilClass Cookbook Trace: --------------- /home/vagrant/chef-repo/cookbooks/b/recipes/default.rb:12:in `from_file' Relevant File Content: ---------------------- /home/vagrant/chef-repo/cookbooks/b/recipes/default.rb: 5: # Copyright 2015, YOUR_COMPANY_NAME 6: # 7: # All rights reserved - Do Not Redistribute 8: # 9: 10: log "this is cookbook b" 11: log "attribute b: #{node['b']['value']}" 12>> log "attribute a: #{node['a']['value']}" 13: <ç¥>
attributeä¸ã®ä¾åé¢ä¿ããªããã¦ãä¾åå cookbookãå©ç¨ããªãå ´å
cookbook aã®attributeãåç §ããªãããã«ãã¾ãã
cookbooks/b/recipes/default.rb
log "this is cookbook b" log "attribute b: #{node['b']['value']}" # log "attribute a: #{node['a']['value']}" # ã³ã¡ã³ãã¢ã¦ã
cookbook bã®aã¸ã®ä¾åæ§ãå ã«æ»ãã¦ããã¾ãã
cookbooks/b/metadata.rb
<ç¥> depends 'a'
ãã®æç¹ã§å®è¡ããã¨ãã¨ã©ã¼ã¯çºçããæ£å¸¸çµäºãã¾ãã
$ sudo chef-solo -c solo.rb -j ./localhost.json Starting Chef Client, version 12.0.3 Compiling Cookbooks... Converging 2 resources Recipe: b::default * log[this is cookbook b] action write * log[attribute b: attribute b] action write Running handlers: Running handlers complete Chef Client finished, 2/2 resources updated in 41.895869267 seconds
ãã¤ã³ãã¯ã dependsã«ããcookbook aãæå®ãã¦ããã«ãé¢ããããaãå®è¡ããªãã¦ãåé¡ããªã ãã¨ã§ãã
ä¾åå cookbookãé ç½®ããªãå ´å
ã§ã¯ãcookbook aãããããé ç½®ããªãã£ããâ¦ã¨ã©ã¼ã«ãªãã¾ãã
chef-repo âcookbooks ââ# aãã£ã¬ã¯ããªãæ¶ã ââb â âattributes/default.rb â ârecipes/default.rb â âmetadata.rb âlocalhost.json
å®è¡æã¨ã©ã¼ã
$ sudo chef-solo -c solo.rb -j ./localhost.json Starting Chef Client, version 12.0.3 Compiling Cookbooks... Running handlers: [2015-03-29T00:11:22+00:00] ERROR: Running exception handlers Running handlers complete [2015-03-29T00:11:22+00:00] ERROR: Exception handlers complete [2015-03-29T00:11:22+00:00] FATAL: Stacktrace dumped to /tmp/chef-solo/chef-stacktrace.out Chef Client failed. 0 resources updated in 41.570472421 seconds [2015-03-29T00:11:22+00:00] ERROR: Cookbook a not found. If you're loading a from another cookbook, make sure you configure the dependency in your metadata [2015-03-29T00:11:23+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
ã¾ã¨ã
metadata.rb
ä¸ã®dependsæå®ã«ããåãã£ããã¨ã¯ä»¥ä¸ã§ãã
- dependsåç §å ã®cookbookã¯cookbook_pathã«é ç½®ãã¦ããå¿ è¦ããã
- ãã ããé ç½®ããã ãã§ãããå®è¡ãããå¿ è¦ã¯ãªã
ãcookbook aãå®è¡ããã¦ããã§ãªãã¨cookbook bãå®è¡ããã¦ã¯ãªããªããã¨ããã·ãã¥ã¨ã¼ã·ã§ã³æã«ãdepends
ãè¨è¼ããã°ä¾åé¢ä¿ãé åºæ§ã(å®è¡ã¯ãããã¨ã)ãã§ãã¯ãããã¨æã£ãã®ã§ãããå
¨ç¶ãããªãã¨ã¯ãªãã£ãã§ãâ¦ãä¾ãã°ããjdkãã¤ã³ã¹ãã¼ã«ãã¦ããelasticsearchãã¤ã³ã¹ãã¼ã«ãã¨ãããrubyãã¤ã³ã¹ãã¼ã«ãã¦ããå種gemãã¤ã³ã¹ãã¼ã«ãã¨ãããããã£ãã±ã¼ã¹ã¯ããå¾ãã¨æãã®ã§ãããããã¯depends
ã§ãã§ãã¯ããã¹ãã§ã¯ãªãã£ããã§ããâ¦ã
æå¾ ãã¦ããã®ã¯ã
"recipe[gem]", "recipe[ruby]"
ã¿ãããªæ¸ãæ¹ãlocalhost.json
ä¸ã§æ¸ããã¨ãããrubyãå
ã«å®è¡ããå¿
è¦ãããããã¨ã¨ã©ã¼ãåºãã¦ãããã¨ãã
"recipe[gem]"
ã¨æ¸ããã¨ãã«ãrubyãå®è¡ããã¦ãªãããã¨ã¨ã©ã¼ãåºãã¦ããããã¨ã ã£ãã®ã§ãããããã¯ãªããâ¦ã
ãããããã²ã¨ã¤ã®cookbookã«include_recipe
ãè¨è¼ãããã¨ã§é åºæ§ãä¾åæ§ãå®ç¾©ããã®ãçæ³ãªã®ãã¨æãã¾ããããããæ±²ã¿åããã¨ãã§ããã«è¦å´ãã話ã§ããã