Skip to content

Instantly share code, notes, and snippets.

@btm
Created October 1, 2011 15:30
Show Gist options
  • Save btm/1256184 to your computer and use it in GitHub Desktop.
Save btm/1256184 to your computer and use it in GitHub Desktop.
Overriding Chef::Provider::File with a library to use wheel instead of root
# fbsd_group/recipes/default.rb
# Empty
btm@btm-mbp-dev:/tmp/chef-solo/cookbooks $ find fbsd_group/ test_group/
fbsd_group/
fbsd_group/libraries
fbsd_group/libraries/group.rb
fbsd_group/recipes
fbsd_group/recipes/default.rb
test_group/
test_group/recipes
test_group/recipes/default.rb
# /tmp/chef-solo/freebsd_groups.json
{ "run_list": ["recipe[fbsd_group]", "recipe[test_group]"] }
# fbsd_group/libraries/group.rb
class Chef
class Provider
class File
def load_current_resource
@current_resource = Chef::Resource::File.new(@new_resource.name)
@new_resource.path.gsub!(/\\/, "/") # for Windows
@current_resource.path(@new_resource.path)
if ::File.exist?(@current_resource.path) && ::File.readable?(@current_resource.path)
cstats = ::File.stat(@current_resource.path)
@current_resource.owner(cstats.uid)
@current_resource.group(cstats.gid)
@current_resource.mode(octal_mode(cstats.mode))
end
if @new_resource.group == "root"
@new_resource.group("wheel")
end
@current_resource
end
end
end
end
btm@btm-mbp-dev:/tmp/chef-solo/cookbooks $ sudo chef-solo -j /tmp/chef-solo/freebsd_groups.json
[Sat, 01 Oct 2011 08:23:56 -0700] INFO: *** Chef 0.10.5 ***
[Sat, 01 Oct 2011 08:23:57 -0700] INFO: Setting the run_list to ["recipe[fbsd_group]", "recipe[test_group]"] from JSON
[Sat, 01 Oct 2011 08:23:57 -0700] INFO: Run List is [recipe[fbsd_group], recipe[test_group]]
[Sat, 01 Oct 2011 08:23:57 -0700] INFO: Run List expands to [fbsd_group, test_group]
[Sat, 01 Oct 2011 08:23:57 -0700] INFO: Starting Chef Run for btm.foo.org
[Sat, 01 Oct 2011 08:23:57 -0700] INFO: Processing file[/tmp/group_other] action create (test_group::default line 1)
[Sat, 01 Oct 2011 08:23:57 -0700] INFO: file[/tmp/group_other] created file /tmp/group_other
[Sat, 01 Oct 2011 08:23:57 -0700] INFO: file[/tmp/group_other] owner changed to 0
[Sat, 01 Oct 2011 08:23:57 -0700] INFO: file[/tmp/group_other] group changed to 42
[Sat, 01 Oct 2011 08:23:57 -0700] INFO: Processing file[/tmp/group_wheel] action create (test_group::default line 6)
[Sat, 01 Oct 2011 08:23:57 -0700] INFO: file[/tmp/group_wheel] created file /tmp/group_wheel
[Sat, 01 Oct 2011 08:23:57 -0700] INFO: file[/tmp/group_wheel] owner changed to 0
[Sat, 01 Oct 2011 08:23:57 -0700] INFO: file[/tmp/group_wheel] group changed to 1002
[Sat, 01 Oct 2011 08:23:57 -0700] INFO: Chef Run complete in 0.011377 seconds
[Sat, 01 Oct 2011 08:23:57 -0700] INFO: Running report handlers
[Sat, 01 Oct 2011 08:23:57 -0700] INFO: Report handlers complete
btm@btm-mbp-dev:/tmp/chef-solo/cookbooks $ ls -l /tmp/group_other /tmp/group_wheel
-rw-r--r-- 1 root shadow 0 2011-10-01 08:23 /tmp/group_other
-rw-r--r-- 1 root wheel 0 2011-10-01 08:23 /tmp/group_wheel
# test_group/recipes/default.rb
file "/tmp/group_other" do
owner "root"
group "shadow"
end
file "/tmp/group_wheel" do
owner "root"
group "root"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment