chef-boneyard / chef-provisioning

A library for creating machines and infrastructures idempotently in Chef.
Apache License 2.0
524 stars 164 forks source link

Document add_machine_options #383

Open jkeiser opened 9 years ago

jkeiser commented 9 years ago

You can do both of these things now:

with_machine_options foo: 1
add_machine_options bar: 2
machine 'blah' do
  add_machine_options baz: 3
end

And the machine_options for "blah" will be { foo: 1, bar: 2, baz: 3 }.

We should document the crap out of it somewhere. Probably on chef.io docs.

epcim commented 9 years ago

Does add_machine_options within the machine block either perform overloading the "bar" variable already defined?

epcim commented 9 years ago

I am actually looking for a way to overload machine_options => bootstrap_options => flavor_ref attribute. To have a default, defined with with_machine_options and overload to different, on the machine block.

jkeiser commented 9 years ago

Yep, should be fine. You should try it, obviously, because it's possible I'm misunderstanding what you want :)

epcim commented 9 years ago

Well, what's essential is recursive merge of hashes. Like the following example updating the image_ref (not yet tested). My experience few days back was that :bootstrap_options => {} will replace whole bootstrap_option hash.

machine 'ops.ci.lab' do
  attribute %w(apt compile_time_update), true
  chef_environment CHEF_ENV

  # flavor m.small
  #add_machine_options :bootstrap_options => { :flavor_ref => '7c438087-10d7-4688-ac01-48614c2af3ff'}
...
...
end
jkeiser commented 9 years ago

Yep if you use add machine options it will recursive merge.

dblessing commented 9 years ago

@jkeiser Is there anyway to change this behavior? I'd be willing to bet most users expect add_machine_options to override hash keys set in with_machine_options. In fact, that just bit us. If we can't change the behavior for these existing methods is there a method we can add to achieve this behavior when needed?

For our use-case we assign the exact same iam_instance_profile to 99% of our instances and only wish to change that for the 1%. Due to this behavior we have to set the iam_instance_profile explicitly on all machine definitions.

Disregard this statement.

jkeiser commented 9 years ago

What happens if you set it to nil on those machines where you really don't want it set to anything?

jkeiser commented 9 years ago

There are ways, I just suspect this will look more like you want.

dblessing commented 9 years ago

We don't want it unset. Rather set to another value. However that leads to the behavior you describe above.

What do you mean by your last comment?

jkeiser commented 9 years ago

I'm confused. Would you mind writing an example?

jkeiser commented 9 years ago

Maybe this is what needs to be clearer: hashes and arrays are merged, and primitive values like strings get replaced.

jkeiser commented 9 years ago

If you wanted to override the iam instance profile using add machine options, and its a string, I would expect ads machine options to override.

dblessing commented 9 years ago

@jkeiser Please disregard my comments. My vision is apparently blurry - bar != baz :smirk:

jkeiser commented 9 years ago

OK! Sounds like this might do what you need then?

keen99 commented 9 years ago

@jkeiser so how do we make this work for bootstrap options?

(using the latest that bundle update got me for gem 'chef-provisioning', github: 'chef/chef-provisioning' and gem 'chef-provisioning-aws', github: 'chef/chef-provisioning-aws' from today)

this doesn't work -

require 'chef/provisioning'

require 'chef/provisioning/aws_driver'

with_driver 'aws'

with_machine_options({
  :bootstrap_options => { :instance_type => 't2.micro' }
})

machine 'blah' do
  add_machine_options :bootstrap_options => { subnet: "testvpc-us-west-2c" }
  action :ready
end

Result is -

================================================================================
Error executing action `ready` on resource 'machine[blah]'
================================================================================

TypeError
---------
can't dup NilClass

Resource Declaration:
---------------------
# In /Users/draistrick/git/fanduel/chef-work/metal-test/test-add.rb

 12: machine 'blah' do
 13:   add_machine_options :bootstrap_options => { subnet: "testvpc-us-west-2c" }
 14:   action :ready
 15: end

Compiled Resource:
------------------
# Declared in /Users/draistrick/git/fanduel/chef-work/metal-test/test-add.rb:12:in `from_file'

machine("blah") do
  action [:ready]
  retries 0
  retry_delay 2
  default_guard_interpreter :default
  chef_server {:chef_server_url=>"chefzero://localhost:8889", :options=>{:client_name=>"draistrick-macbook.local", :signing_key_filename=>nil, :api_version=>"0"}}
  driver "aws"
  machine_options #<Cheffish::MergedConfig:0x007ff172af32f8 @configs=[{:bootstrap_options=>{:subnet=>"testvpc-us-west-2c"}}, {:bootstrap_options=>{:instance_type=>"t2.micro"}}], @merge_arrays={}>
  declared_type :machine
  cookbook_name "@recipe_files"
  recipe_name "/Users/draistrick/git/fanduel/chef-work/metal-test/test-add.rb"
end
lamont commented 8 years ago

Hi everybody. I just wanted to point out that this feature (which I like and use) still appears to be undocumented except for here. And that's the title of the issue. So I'm bumping the thread cause additional documentation on this (and much of chef provisioning) would be of great use.

dgarstang commented 7 years ago

Almost a year later. Still not documented.

rlewkowicz commented 7 years ago

I'm going to toss in a little bumperino to point out that it's still not documented (although I think chef kinda wants provisioning to go away)