Module: Mac::User
Instance Attribute Summary
Attributes included from Beaker::CommandFactory
Instance Method Summary collapse
-
#gid_next ⇒ Fixnum
Gives the next gid not used on the system.
-
#uid_next ⇒ Fixnum
Gives the next uid not used on the system.
-
#user_absent(name, &block) ⇒ Object
Makes sure the user is absent, deleting them if necessary.
-
#user_get(name) {|Result| ... } ⇒ Result
Gets the user information in /etc/passwd format.
-
#user_list ⇒ Array<String>
Gets a list of user names on the system.
-
#user_present(name) ⇒ Object
Makes sure the user is present, creating them if necessary.
Methods included from Beaker::CommandFactory
Instance Method Details
#gid_next ⇒ Fixnum
Gives the next gid not used on the system
84 85 86 87 |
# File 'lib/beaker/host/mac/user.rb', line 84 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_next ⇒ Fixnum
Gives the next uid not used on the system
76 77 78 79 |
# File 'lib/beaker/host/mac/user.rb', line 76 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
69 70 71 |
# File 'lib/beaker/host/mac/user.rb', line 69 def user_absent(name, &block) execute("if dscl . -list /Users/#{name}; then dscl . -delete /Users/#{name}; fi", {}, &block) end |
#user_get(name) {|Result| ... } ⇒ Result
Note:
Calls POSIX-compliant ‘$ id -P <user>` to get /etc/passwd-style
Gets the user information in /etc/passwd format
output
34 35 36 37 38 39 40 41 |
# File 'lib/beaker/host/mac/user.rb', line 34 def user_get(name) execute("id -P #{name}") do |result| fail_test "failed to get user #{name}" unless /^#{name}:/.match?(result.stdout) yield result if block_given? result end end |
#user_list ⇒ Array<String>
Gets a 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 execute('dscacheutil -q user') do |result| users = [] result.stdout.each_line do |line| users << line.split(': ')[1].strip if /^name:/.match?(line) end yield result if block_given? users end end |
#user_present(name) ⇒ Object
Makes sure the user is present, creating them if necessary
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/beaker/host/mac/user.rb', line 47 def user_present(name) user_exists = false execute("dscacheutil -q user -a name #{name}") do |result| user_exists = result.stdout.start_with?("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 |