Module: Mac::Group

Includes:
Beaker::CommandFactory
Included in:
Host
Defined in:
lib/beaker/host/mac/group.rb

Instance Attribute Summary

Attributes included from Beaker::CommandFactory

#assertions

Instance Method Summary collapse

Methods included from Beaker::CommandFactory

#execute, #fail_test

Instance Method Details

#gid_nextFixnum

Gives the next gid not used on the system

Returns:

  • (Fixnum)

    The next gid not used on the system



92
93
94
95
# File 'lib/beaker/host/mac/group.rb', line 92

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) ⇒ Object

Makes sure the group is absent, deleting it if necessary

Parameters:

  • name (String)

    Name of the group

  • block (Proc)

    Additional actions or insertions



85
86
87
# File 'lib/beaker/host/mac/group.rb', line 85

def group_absent(name, &block)
  execute("if dscl . -list /Groups/#{name}; then dscl . -delete /Groups/#{name}; fi", {}, &block)
end

#group_get(name, &block) {|String| ... } ⇒ String

Gets the group information in /etc/group format

Parameters:

  • name (String)

    Name of the group you want

  • block (Proc)

    Additional actions or insertions

Yields:

  • (String)

    The actual mac dscacheutil output

Returns:

  • (String)

    Group information in /etc/group format

Raises:

  • (FailTest)

    Raises an Assertion failure if it can't find the name queried for in the returned block



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/beaker/host/mac/group.rb', line 31

def group_get(name, &block)
  execute("dscacheutil -q group -a name #{name}") do |result|
    fail_test "failed to get group #{name}" unless result.stdout =~ /^name: #{name}/
    gi = Hash.new  # group info
    result.stdout.each_line { |line|
      pieces = line.split(': ')
      gi[pieces[0].to_sym] = pieces[1].strip if pieces[1] != nil
    }
    answer = "#{gi[:name]}:#{gi[:password]}:#{gi[:gid]}"

    yield answer if block_given?
  end
end

#group_gid(name) ⇒ String

Gets the gid of the given group

Parameters:

  • name (String)

    Name of the group

Returns:

  • (String)

    gid of the group



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/beaker/host/mac/group.rb', line 50

def group_gid(name)
  gid = -1
  execute("dscacheutil -q group -a name #{name}") do |result|
    result.stdout.each_line { |line|
      if line =~ /^gid:/
        gid = (line[5, line.length - 5]).chomp
        break
      end
    }
    gid
  end
end

#group_list(&block) ⇒ Array<String>

Gets a list of group names on the system

Parameters:

  • block (Proc)

    Additional actions or insertions

Returns:

  • (Array<String>)

    The list of group names on the system



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/beaker/host/mac/group.rb', line 9

def group_list(&block)
  execute('dscacheutil -q group') do |result|
    groups = []
    result.stdout.each_line do |line|
      groups << line.split(': ')[1].strip if line =~ /^name:/
    end

    yield result if block_given?

    groups
  end
end

#group_present(name, &block) ⇒ Object

Makes sure the group is present, creating it if necessary

Parameters:

  • name (String)

    Name of the group

  • block (Proc)

    Additional actions or insertions



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/beaker/host/mac/group.rb', line 67

def group_present(name, &block)
  group_exists = false
  execute("dscacheutil -q group -a name #{name}") do |result|
    group_exists = result.stdout =~  /^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