On Tue, Oct 12, 2010 at 6:39 PM, Jesse A Wolfe <[email protected]> wrote: > +1. I don't love the looking-for-id section, but I can't think of another > way to do it.
I don't love *anything* about this provider :( but yeah, I spent a while thinking there must be a more elegant way to find the id, and failed to come up with anything. The recent issue with rubycocoa breaking everything feels like I can't put off refactoring this all that much longer, but I'm not sure it will make it into 2.7. > > On Mon, Oct 11, 2010 at 3:58 PM, Nigel Kersten <[email protected]> wrote: >> >> Signed-off-by: Nigel Kersten <[email protected]> >> --- >> .../provider/nameservice/directoryservice.rb | 34 >> +++++++++++++++++++- >> 1 files changed, 33 insertions(+), 1 deletions(-) >> >> diff --git a/lib/puppet/provider/nameservice/directoryservice.rb >> b/lib/puppet/provider/nameservice/directoryservice.rb >> index 965a2aa..106d99e 100644 >> --- a/lib/puppet/provider/nameservice/directoryservice.rb >> +++ b/lib/puppet/provider/nameservice/directoryservice.rb >> @@ -321,6 +321,31 @@ class DirectoryService < >> Puppet::Provider::NameService >> password_hash >> end >> >> + # Unlike most other *nixes, OS X doesn't provide built in functionality >> + # for automatically assigning uids and gids to accounts, so we set up >> these >> + # methods for consumption by functionality like --mkusers >> + # By default we restrict to a reasonably sane range for system accounts >> + def self.next_system_id(id_type, min_id=20) >> + dscl_args = ['.', '-list'] >> + if id_type == 'uid' >> + dscl_args << '/Users' << 'uid' >> + elsif id_type == 'gid' >> + dscl_args << '/Groups' << 'gid' >> + else >> + fail("Invalid id_type #{id_type}. Only 'uid' and 'gid' supported") >> + end >> + dscl_out = dscl(dscl_args) >> + # We're ok with throwing away negative uids here. >> + ids = dscl_out.split.compact.collect { |l| l.to_i if l.match(/^\d+$/) >> } >> + ids.compact!.sort! { |a,b| a.to_f <=> b.to_f } >> + # We're just looking for an unused id in our sorted array. >> + ids.each_index do |i| >> + next_id = ids[i] + 1 >> + return next_id if ids[i+1] != next_id and next_id >= min_id >> + end >> + end >> + >> + >> def ensure=(ensure_value) >> super >> # We need to loop over all valid properties for the type we're >> @@ -422,7 +447,14 @@ class DirectoryService < >> Puppet::Provider::NameService >> # Now we create all the standard properties >> Puppet::Type.type(@resource.class.name).validproperties.each do >> |property| >> next if property == :ensure >> - if value = @resource.should(property) and value != "" >> + value = @resource.should(property) >> + if property == :gid and value.nil? >> + value = self.class.next_system_id(id_type='gid') >> + end >> + if property == :uid and value.nil? >> + value = self.class.next_system_id(id_type='uid') >> + end >> + if value != "" and not value.nil? >> if property == :members >> add_members(nil, value) >> else >> -- >> 1.7.2.3 >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Puppet Developers" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/puppet-dev?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "Puppet Developers" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/puppet-dev?hl=en. > -- nigel -- You received this message because you are subscribed to the Google Groups "Puppet Developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.
