# class Chef::Provider::Group::Pw

### Public Instance Methods

create_group() click to toggle source

Create the group

    # File lib/chef/provider/group/pw.rb, line 39
    def create_group
      command = "pw groupadd"
      command << set_options
      member_options = set_members_options
      if member_options.empty?
        run_command(:command => command)
      else
        member_options.each do |option|
          run_command(:command => command + option)
        end
      end
    end

define_resource_requirements() click to toggle source
Calls superclass method Chef::Provider::Group#define_resource_requirements

    # File lib/chef/provider/group/pw.rb, line 28
    def define_resource_requirements
      super

      requirements.assert(:all_actions) do |a|
        a.assertion { ::File.exists?("/usr/sbin/pw") }
        a.failure_message Chef::Exceptions::Group, "Could not find binary /usr/sbin/pw for #{@new_resource}"
        # No whyrun alternative: this component should be available in the base install of any given system that uses it
      end
    end

load_current_resource() click to toggle source
Calls superclass method Chef::Provider::Group#load_current_resource

    # File lib/chef/provider/group/pw.rb, line 24
    def load_current_resource
      super
    end

manage_group() click to toggle source

Manage the group when it already exists

    # File lib/chef/provider/group/pw.rb, line 53
    def manage_group
      command = "pw groupmod"
      command << set_options
      member_options = set_members_options
      if member_options.empty?
        run_command(:command => command)
      else
        member_options.each do |option|
          run_command(:command => command + option)
        end
      end
    end

remove_group() click to toggle source

Remove the group

    # File lib/chef/provider/group/pw.rb, line 67
    def remove_group
      run_command(:command => "pw groupdel #{@new_resource.group_name}")
    end

set_members_options() click to toggle source

Set the membership option depending on the current resource states

    # File lib/chef/provider/group/pw.rb, line 85
    def set_members_options
      opts = [ ]
      members_to_be_added = [ ]
      members_to_be_removed = [ ]

      if @new_resource.append
        # Append is set so we will only add members given in the
        # members list and remove members given in the
        # excluded_members list.
        if @new_resource.members && !@new_resource.members.empty?
          @new_resource.members.each do |member|
            members_to_be_added << member if !@current_resource.members.include?(member)
          end
        end

        if @new_resource.excluded_members && !@new_resource.excluded_members.empty?
          @new_resource.excluded_members.each do |member|
            members_to_be_removed << member if @current_resource.members.include?(member)
          end
        end
      else
        # Append is not set so we're resetting the membership of
        # the group to the given members.
        members_to_be_added = @new_resource.members
        @current_resource.members.each do |member|
          # No need to re-add a member if it's present in the new
          # list of members
          if members_to_be_added.include? member
            members_to_be_added.delete member
          else
            members_to_be_removed << member
          end
        end
      end

      unless members_to_be_added.empty?
        Chef::Log.debug("#{@new_resource} adding group members: #{members_to_be_added.join(',')}")
        opts << " -m #{members_to_be_added.join(',')}"
      end

      unless members_to_be_removed.empty?
        Chef::Log.debug("#{@new_resource} removing group members: #{members_to_be_removed.join(',')}")
        opts << " -d #{members_to_be_removed.join(',')}"
      end

      opts
    end

set_options() click to toggle source

Little bit of magic as per Adam’s useradd provider to pull and assign the command line flags

#### Returns¶ ↑

<string>

  A string containing the option and then the quoted value

    # File lib/chef/provider/group/pw.rb, line 75
    def set_options
      opts = " #{@new_resource.group_name}"
      if @new_resource.gid && (@current_resource.gid != @new_resource.gid)
        Chef::Log.debug("#{@new_resource}: current gid (#{@current_resource.gid}) doesnt match target gid (#{@new_resource.gid}), changing it")
        opts << " -g '#{@new_resource.gid}'"
      end
      opts
    end