# class Chef::Provider::Group::Groupadd

### Public Instance Methods

add_member(member) click to toggle source

    # File lib/chef/provider/group/groupadd.rb, line 98
    def add_member(member)
      raise Chef::Exceptions::Group, "you must override add_member in #{self.to_s}"
    end

create_group() click to toggle source

Create the group

    # File lib/chef/provider/group/groupadd.rb, line 46
    def create_group
      command = "groupadd"
      command << set_options
      command << groupadd_options
      run_command(:command => command)
      modify_group_members
    end

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

    # File lib/chef/provider/group/groupadd.rb, line 34
    def define_resource_requirements
      super
      required_binaries.each do |required_binary|
        requirements.assert(:all_actions) do |a|
          a.assertion { ::File.exists?(required_binary) }
          a.failure_message Chef::Exceptions::Group, "Could not find binary #{required_binary} for #{@new_resource}"
          # No whyrun alternative: this component should be available in the base install of any given system that uses it
        end
      end
    end

groupadd_options() click to toggle source

    # File lib/chef/provider/group/groupadd.rb, line 127
    def groupadd_options
      opts = ''
      opts << " -r" if @new_resource.system
      opts << " -o" if @new_resource.non_unique
      opts
    end

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

    # File lib/chef/provider/group/groupadd.rb, line 30
    def load_current_resource
      super
    end

manage_group() click to toggle source

Manage the group when it already exists

    # File lib/chef/provider/group/groupadd.rb, line 55
    def manage_group
      command = "groupmod"
      command << set_options
      run_command(:command => command)
      modify_group_members
    end

modify_group_members() click to toggle source

    # File lib/chef/provider/group/groupadd.rb, line 67
    def modify_group_members
      if @new_resource.append
        if @new_resource.members && !@new_resource.members.empty?
          members_to_be_added = [ ]
          @new_resource.members.each do |member|
            members_to_be_added << member if !@current_resource.members.include?(member)
          end
          members_to_be_added.each do |member|
            Chef::Log.debug("#{@new_resource} appending member #{member} to group #{@new_resource.group_name}")
            add_member(member)
          end
        end

        if @new_resource.excluded_members && !@new_resource.excluded_members.empty?
          members_to_be_removed = [ ]
          @new_resource.excluded_members.each do |member|
            members_to_be_removed << member if @current_resource.members.include?(member)
          end

          members_to_be_removed.each do |member|
            Chef::Log.debug("#{@new_resource} removing member #{member} from group #{@new_resource.group_name}")
            remove_member(member)
          end
        end
      else
        members_description = @new_resource.members.empty? ? "none" : @new_resource.members.join(", ")
        Chef::Log.debug("#{@new_resource} setting group members to: #{members_description}")
        set_members(@new_resource.members)
      end
    end

remove_group() click to toggle source

Remove the group

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

remove_member(member) click to toggle source

    # File lib/chef/provider/group/groupadd.rb, line 102
    def remove_member(member)
      raise Chef::Exceptions::Group, "you must override remove_member in #{self.to_s}"
    end

required_binaries() click to toggle source

    # File lib/chef/provider/group/groupadd.rb, line 24
    def required_binaries
      [ "/usr/sbin/groupadd",
        "/usr/sbin/groupmod",
        "/usr/sbin/groupdel" ]
    end

set_members(members) click to toggle source

    # File lib/chef/provider/group/groupadd.rb, line 106
    def set_members(members)
      raise Chef::Exceptions::Group, "you must override set_members in #{self.to_s}"
    end

set_options() click to toggle source

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

#### Returns¶ ↑

<string>

  A string containing the option and then the quoted value

    # File lib/chef/provider/group/groupadd.rb, line 114
    def set_options
      opts = ""
      { :gid => "-g" }.sort { |a,b| a[0] <=> b[0] }.each do |field, option|
        if @current_resource.send(field) != @new_resource.send(field)
          if @new_resource.send(field)
            opts << " #{option} '#{@new_resource.send(field)}'"
            Chef::Log.debug("#{@new_resource} set #{field.to_s} to #{@new_resource.send(field)}")
          end
        end
      end
      opts << " #{@new_resource.group_name}"
    end