Module: Mac::User

Includes:
Beaker::CommandFactory
Included in:
Host
Defined in:
lib/beaker/host/mac/user.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



89
90
91
92
# File 'lib/beaker/host/mac/user.rb', line 89

def gid_next
  gid_last = execute("dscl . -list /Users PrimaryGroupID | sort -k 2 -g | tail -1 | awk '{print $2}'")
  gid_last.to_i + 1
end

#uid_nextFixnum

Gives the next uid not used on the system

Returns:

  • (Fixnum)

    The next uid not used on the system



81
82
83
84
# File 'lib/beaker/host/mac/user.rb', line 81

def uid_next
  uid_last = execute("dscl . -list /Users UniqueID | sort -k 2 -g | tail -1 | awk '{print $2}'")
  uid_last.to_i + 1
end

#user_absent(name, &block) ⇒ Object

Makes sure the user is absent, deleting them if necessary

Parameters:

  • name (String)

    Name of the user

  • block (Proc)

    Additional actions or insertions



74
75
76
# File 'lib/beaker/host/mac/user.rb', line 74

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

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

Gets the user information in /etc/passwd format

Parameters:

  • name (String)

    Name of the user

  • block (Proc)

    Additional actions or insertions

Yields:

  • (String)

    The actual mac dscacheutil output

Returns:

  • (String)

    User information in /etc/passwd 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
44
45
46
# File 'lib/beaker/host/mac/user.rb', line 31

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

    yield result if block_given?
  end
  answer
end

#user_list(&block) ⇒ Array<String>

Gets a list of user names on the system

Parameters:

  • block (Proc)

    Additional actions or insertions

Returns:

  • (Array<String>)

    The list of user names on the system



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

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

    yield result if block_given?

    users
  end
end

#user_present(name, &block) ⇒ Object

Makes sure the user is present, creating them if necessary

Parameters:

  • name (String)

    Name of the user

  • block (Proc)

    Additional actions or insertions



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/beaker/host/mac/user.rb', line 52

def user_present(name, &block)
  user_exists = false
  execute("dscacheutil -q user -a name #{name}") do |result|
     user_exists = result.stdout =~  /^name: #{name}/
  end

  return if user_exists

  uid = uid_next
  gid = gid_next
  create_cmd  =     "dscl . create /Users/#{name}"
  create_cmd << " && dscl . create /Users/#{name} NFSHomeDirectory /Users/#{name}"
  create_cmd << " && dscl . create /Users/#{name} UserShell /bin/bash"
  create_cmd << " && dscl . create /Users/#{name} UniqueID #{uid}"
  create_cmd << " && dscl . create /Users/#{name} PrimaryGroupID #{gid}"
  execute(create_cmd)
end