djoos-cookbooks / composer

Repository for composer cookbook
http://community.opscode.com/cookbooks/composer
MIT License
27 stars 38 forks source link

global_configs - Delay evaluation until converge phase #68

Open webframp opened 8 years ago

webframp commented 8 years ago

Hi there! Thanks for this cookbook.

I ran into a fun issue due to the delightful multi-phase execution model of chef figured I'd report it. I did find a workaround, but it's just slightly ugly.

The root issue is this spot in the global_configs recipe which is troublesome with a custom user: https://github.com/djoos-cookbooks/composer/blob/master/recipes/global_configs.rb#L12

When node['composer']['global_configs'] contains a user that does not yet exist, but will be created during the chef recipe, then composer::global_configs will fail with something like this beautiful message:

``` ================================================================================ Recipe Compile Error in /tmp/kitchen/cache/cookbooks/bethel_packaging/recipes/default.rb ================================================================================ ArgumentError ------------- user satis doesn't exist Cookbook Trace: --------------- /tmp/kitchen/cache/cookbooks/composer/recipes/global_configs.rb:12:in `home' /tmp/kitchen/cache/cookbooks/composer/recipes/global_configs.rb:12:in `block in from_file' /tmp/kitchen/cache/cookbooks/composer/recipes/global_configs.rb:11:in `each_pair' /tmp/kitchen/cache/cookbooks/composer/recipes/global_configs.rb:11:in `from_file' /tmp/kitchen/cache/cookbooks/composer/recipes/default.rb:11:in `from_file' /tmp/kitchen/cache/cookbooks/bethel_packaging/recipes/satis.rb:16:in `from_file' /tmp/kitchen/cache/cookbooks/bethel_packaging/recipes/default.rb:9:in `from_file' Relevant File Content: ---------------------- /tmp/kitchen/cache/cookbooks/composer/recipes/global_configs.rb: 5: # Copyright (c) 2016, David Joos 6: # 7: 8: configs = node['composer']['global_configs'] 9: 10: unless configs.nil? 11: configs.each_pair do |user, user_configs| 12>> user_composer_dir = "#{Dir.home(user)}/.composer" 13: 14: directory user_composer_dir do 15: owner user 16: group user 17: mode 0755 18: action :create 19: end 20: 21: user_configs.nil? && next Platform: --------- x86_64-linux ```

yuck ^

The workaround is to make your user resource (wherever it is) run during the compile phase and it looks like this:

user satis_user do
  comment 'local user for satis'
  manage_home true
  action :nothing
end.run_action(:create)

With that little method tagged onto the end of the resource it worked as intended I think. So it may be useful to do one of:

  1. Make a note in the README of this behavior
  2. Rework global_configs in some way to use a lazy eval style so it only runs during the converge phase

Hope this info is helpful in some way.

daften commented 7 years ago

I've just worked on this, but failed miserably. The problem is that lazy is not really intended to be used in a recipe. And you can only use it for variables or attributes it seems. For environment variables like here, it doesn't seem to work: I get

           ---- Begin output of composer config --global secure-http false ----
           STDOUT:
           STDERR: /opt/chef/embedded/lib/ruby/gems/2.4.0/gems/mixlib-shellout-2.2.7/lib/mixlib/shellout/unix.rb:174:in `[]=': no implicit conversion of Chef::DelayedEvaluator into String (TypeError)
            from /opt/chef/embedded/lib/ruby/gems/2.4.0/gems/mixlib-shellout-2.2.7/lib/mixlib/shellout/unix.rb:174:in `block in set_environment'
            from /opt/chef/embedded/lib/ruby/gems/2.4.0/gems/mixlib-shellout-2.2.7/lib/mixlib/shellout/unix.rb:173:in `each'
            from /opt/chef/embedded/lib/ruby/gems/2.4.0/gems/mixlib-shellout-2.2.7/lib/mixlib/shellout/unix.rb:173:in `set_environment'

ruby_block, lambda have been tried too, to no avail. I think the only way to do this properly is to rewrite some of the recipe as a resource, but that is too much work for me ATM. Feel free to open up a PR for this though :)

daften commented 7 years ago

BTW, i've committed my work on a separate branch on my fork of this cookbook, and added the basis for tests for this as well, feel free to work from there.