module Mac::Group

Public Instance Methods

gid_next() click to toggle source

Gives the next gid not used on the system

@return [Fixnum] The next gid not used on the system

# File lib/beaker/host/mac/group.rb, line 93
def gid_next
  gid_last = execute("dscl . -list /Groups PrimaryGroupID | sort -k 2 -g | tail -1 | awk '{print $2}'")
  gid_last.to_i + 1
end
group_absent(name, &block) click to toggle source

Makes sure the group is absent, deleting it if necessary

@param [String] name Name of the group @param [Proc] block Additional actions or insertions

# File lib/beaker/host/mac/group.rb, line 86
def group_absent(name, &block)
  execute("if dscl . -list /Groups/#{name}; then dscl . -delete /Groups/#{name}; fi", {}, &block)
end
group_get(name) { |answer| ... } click to toggle source

Gets the group information in /etc/group format

@param [String] name Name of the group you want @param [Proc] block Additional actions or insertions

@yield [String] The actual mac dscacheutil output @return [String] Group information in /etc/group format @raise [FailTest] Raises an Assertion failure if it can’t find the name

queried for in the returned block
# File lib/beaker/host/mac/group.rb, line 31
def group_get(name)
  execute("dscacheutil -q group -a name #{name}") do |result|
    fail_test "failed to get group #{name}" unless /^name: #{name}/.match?(result.stdout)
    gi = {} # group info
    result.stdout.each_line do |line|
      pieces = line.split(': ')
      gi[pieces[0].to_sym] = pieces[1].strip if pieces[1] != nil
    end
    answer = "#{gi[:name]}:#{gi[:password]}:#{gi[:gid]}"

    yield answer if block_given?
    answer
  end
end
group_gid(name) click to toggle source

Gets the gid of the given group

@param [String] name Name of the group

@return [String] gid of the group

# File lib/beaker/host/mac/group.rb, line 51
def group_gid(name)
  gid = -1
  execute("dscacheutil -q group -a name #{name}") do |result|
    result.stdout.each_line do |line|
      if /^gid:/.match?(line)
        gid = (line[5, line.length - 5]).chomp
        break
      end
    end
    gid
  end
end
group_list() { |result| ... } click to toggle source

Gets a list of group names on the system

@param [Proc] block Additional actions or insertions

@return [Array<String>] The list of group names on the system

# File lib/beaker/host/mac/group.rb, line 9
def group_list
  execute('dscacheutil -q group') do |result|
    groups = []
    result.stdout.each_line do |line|
      groups << line.split(': ')[1].strip if /^name:/.match?(line)
    end

    yield result if block_given?

    groups
  end
end
group_present(name) click to toggle source

Makes sure the group is present, creating it if necessary

@param [String] name Name of the group @param [Proc] block Additional actions or insertions

# File lib/beaker/host/mac/group.rb, line 68
def group_present(name)
  group_exists = false
  execute("dscacheutil -q group -a name #{name}") do |result|
    group_exists = result.stdout.start_with?("name: #{name}")
  end

  return if group_exists

  gid = gid_next
  create_cmd = "dscl . create /Groups/#{name}"
  create_cmd << " && dscl . create /Groups/#{name} PrimaryGroupID #{gid}"
  execute(create_cmd)
end