Class: OsCtld::SystemUsers

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Lockable, Singleton
Defined in:
lib/osctld/system_users.rb

Overview

Cache for existing system users

Constant Summary collapse

PREFIX =
'uns'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

#initializeSystemUsers

Returns a new instance of SystemUsers



23
24
25
26
27
# File 'lib/osctld/system_users.rb', line 23

def initialize
  init_lock
  @users = []
  load_users
end

Instance Attribute Details

#usersObject (readonly, protected)

Returns the value of attribute users



59
60
61
# File 'lib/osctld/system_users.rb', line 59

def users
  @users
end

Instance Method Details

#add(name, ugid, homedir) ⇒ Object

Add new user to the system

Parameters:

  • name (String)
  • ugid (Integer)
  • homedir (String)


33
34
35
36
37
38
# File 'lib/osctld/system_users.rb', line 33

def add(name, ugid, homedir)
  fail 'user already exists' if include?(name)
  syscmd("groupadd -g #{ugid} #{name}")
  syscmd("useradd -u #{ugid} -g #{ugid} -d #{homedir} #{name}")
  exclusively { users << name }
end

#include?(name) ⇒ Boolean

Check if a system user exists

Parameters:

  • name (String)

Returns:

  • (Boolean)


50
51
52
# File 'lib/osctld/system_users.rb', line 50

def include?(name)
  inclusively { users.include?(name) }
end

#load_usersObject (protected)



61
62
63
64
65
66
67
68
69
70
# File 'lib/osctld/system_users.rb', line 61

def load_users
  exclusively do
    users.clear

    syscmd('getent passwd')[:output].split("\n").each do |line|
      name = line.split(':').first
      users << name if name.start_with?(PREFIX)
    end
  end
end

#log_typeObject



54
55
56
# File 'lib/osctld/system_users.rb', line 54

def log_type
  'system-users'
end

#remove(name) ⇒ Object

Remove user from the system

Parameters:

  • name (String)


42
43
44
45
46
# File 'lib/osctld/system_users.rb', line 42

def remove(name)
  syscmd("userdel -f #{name}")
  syscmd("groupdel #{name}", valid_rcs: [6])
  exclusively { users.delete(name) }
end