Ticket #27566: patch-4382-mkusers-osx.diff

File patch-4382-mkusers-osx.diff, 1.9 KB (added by nigel@…, 13 years ago)
  • lib/puppet/provider/nameservice/directoryservice.rb

    old new  
    321321    password_hash
    322322  end
    323323
     324  # Unlike most other *nixes, OS X doesn't provide built in functionality
     325  # for automatically assigning uids and gids to accounts, so we set up these
     326  # methods for consumption by functionality like --mkusers
     327  # By default we restrict to a reasonably sane range for system accounts
     328  def self.next_system_id(id_type, min_id=20)
     329    dscl_args = ['.', '-list']
     330    if id_type == 'uid'
     331      dscl_args << '/Users' << 'uid'
     332    elsif id_type == 'gid'
     333      dscl_args << '/Groups' << 'gid'
     334    else
     335      fail("Invalid id_type #{id_type}. Only 'uid' and 'gid' supported")
     336    end
     337    dscl_out = dscl(dscl_args)
     338    # We're ok with throwing away negative uids here.
     339    ids = dscl_out.split.compact.collect { |l| l.to_i if l.match(/^\d+$/) }
     340    ids.compact!.sort! { |a,b| a.to_f <=> b.to_f }
     341    # We're just looking for an unused id in our sorted array.
     342    ids.each_index do |i|
     343      next_id = ids[i] + 1
     344      return next_id if ids[i+1] != next_id and next_id >= min_id
     345    end
     346  end
     347
     348
    324349  def ensure=(ensure_value)
    325350    super
    326351    # We need to loop over all valid properties for the type we're
     
    422447    # Now we create all the standard properties
    423448    Puppet::Type.type(@resource.class.name).validproperties.each do |property|
    424449      next if property == :ensure
    425       if value = @resource.should(property) and value != ""
     450      value = @resource.should(property)
     451      if property == :gid and value.nil?
     452        value = self.class.next_system_id(id_type='gid')
     453      end
     454      if property == :uid and value.nil?
     455        value = self.class.next_system_id(id_type='uid')
     456      end
     457      if value != "" and not value.nil?
    426458        if property == :members
    427459          add_members(nil, value)
    428460        else