Class: AutomateIt::AccountManager::POSIX

Inherits:
BaseDriver show all
Defined in:
lib/automateit/account_manager/posix.rb

Overview

AccountManager::POSIX

A POSIX driver for the AccountManager.

Constant Summary

Constants inherited from Plugin::Driver

Plugin::Driver::BASE_DRIVER_NAME

Constants included from Constants

Constants::HELPERS_DIR, Constants::INSTALL_DIR, Constants::PERROR, Constants::PEXEC, Constants::PNOTE, Constants::WARNING_BOILERPLATE

Instance Attribute Summary

Attributes inherited from Plugin::Driver

#manager

Attributes inherited from Common

#interpreter

Instance Method Summary collapse

Methods inherited from Plugin::Driver

abstract_driver, #available?, base_driver, base_driver?, depends_on, inherited, manager_token, #setup

Methods inherited from Plugin::Base

#setup, #token, token

Methods inherited from Common

#initialize, #log, #nitpick, #noop, #noop=, #noop?, #preview, #preview=, #preview?, #preview_for, #setup, #superuser?, #writing, #writing=, #writing?

Constructor Details

This class inherits a constructor from AutomateIt::Common

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object

Dispatch common names to Etc, but don’t define these methods here because that would make available? and suitability think these exist, when in fact, they’re just wrappers.



130
131
132
133
134
135
136
137
# File 'lib/automateit/account_manager/posix.rb', line 130

def method_missing(symbol, *args, &block)
  case symbol
  when :users, :has_user?, :groups, :has_group?, :groups_for_user, :users_for_group, :users_to_groups
    manager.send(symbol, *args, &block)
  else
    super(symbol, *args, &block)
  end
end

Instance Method Details

#add_group(groupname, opts = {}) ⇒ Object

See AccountManager#add_group



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/automateit/account_manager/posix.rb', line 68

def add_group(groupname, opts={})
  modified = false
  unless has_group?(groupname)
    modified = true

    cmd = "groupadd"
    cmd << " -g #{opts[:gid]}" if opts[:gid]
    cmd << " #{groupname}"
    interpreter.sh(cmd)

    manager.invalidate(:groups)
  end

  if opts[:members]
    modified = true
    add_users_to_group(opts[:members], groupname)
  end

  return modified ? groups[groupname] : false
end

#add_groups_to_user(groups, username) ⇒ Object

See AccountManager#add_groups_to_user



47
48
49
50
51
52
53
54
# File 'lib/automateit/account_manager/posix.rb', line 47

def add_groups_to_user(groups, username)
  return _add_groups_to_user_helper(groups, username) do |missing, username|
    targets = (groups_for_user(username) + missing).uniq

    cmd = "usermod -G #{targets.join(',')} #{username}"
    interpreter.sh(cmd)
  end
end

#add_user(username, opts = {}) ⇒ Object

See AccountManager#add_user



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/automateit/account_manager/posix.rb', line 15

def add_user(username, opts={})
  return _add_user_helper(username, opts) do |username, opts|
    cmd = "useradd"
    cmd << " -c #{opts[:description] || username}"
    cmd << " -d #{opts[:home]}" if opts[:home]
    cmd << " -m" unless opts[:create_home] == false
    cmd << " -G #{opts[:groups].join(',')}" if opts[:groups]
    cmd << " -s #{opts[:shell] || "/bin/bash"}"
    cmd << " -u #{opts[:uid]}" if opts[:uid]
    cmd << " -g #{opts[:gid]}" if opts[:gid]
    cmd << " #{username} < /dev/null"
    cmd << " > /dev/null 2>&1 | grep -v blocks" if opts[:quiet]
    interpreter.sh(cmd)
  end
end

#add_users_to_group(users, groupname) ⇒ Object

See AccountManager#add_users_to_group



104
105
106
107
108
109
110
111
112
# File 'lib/automateit/account_manager/posix.rb', line 104

def add_users_to_group(users, groupname)
  _add_users_to_group_helper(users, groupname) do |missing, groupname|
    for username in missing
      targets = (groups_for_user(username) + [groupname]).uniq
      cmd = "usermod -G #{targets.join(',')} #{username}"
      interpreter.sh(cmd)
    end
  end
end

#remove_group(groupname, opts = {}) ⇒ Object

See AccountManager#remove_group



93
94
95
96
97
98
99
100
101
# File 'lib/automateit/account_manager/posix.rb', line 93

def remove_group(groupname, opts={})
  return false unless has_group?(groupname)
  cmd = "groupdel #{groupname}"
  interpreter.sh(cmd)

  manager.invalidate(:groups)

  return true
end

#remove_groups_from_user(groups, username) ⇒ Object

See AccountManager#remove_groups_from_user



57
58
59
60
61
62
63
# File 'lib/automateit/account_manager/posix.rb', line 57

def remove_groups_from_user(groups, username)
  return _remove_groups_from_user_helper(groups, username) do |present, username|
    matches = (groups_for_user(username) - [groups].flatten).uniq
    cmd = "usermod -G #{matches.join(',')} #{username}"
    interpreter.sh(cmd)
  end
end

#remove_user(username, opts = {}) ⇒ Object

See AccountManager#remove_user



35
36
37
38
39
40
41
42
43
44
# File 'lib/automateit/account_manager/posix.rb', line 35

def remove_user(username, opts={})
  return _remove_user_helper(username, opts) do |username, opts|
    # Options: -r -- remove the home directory and mail spool
    cmd = "userdel"
    cmd << " -r" unless opts[:remove_home] == false
    cmd << " #{username}"
    cmd << " > /dev/null" if opts[:quiet]
    interpreter.sh(cmd)
  end
end

#remove_users_from_group(users, groupname) ⇒ Object

See AccountManager#remove_users_from_group



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/automateit/account_manager/posix.rb', line 115

def remove_users_from_group(users, groupname)
  _remove_users_from_group_helper(users, groupname) do |present, groupname|
    u2g = users_to_groups
    for username in present
      user_groups = u2g[username]
      # FIXME tries to include non-present groups, should use some variant of present
      cmd = "usermod -G #{(user_groups.to_a-[groupname]).join(',')} #{username}"
      interpreter.sh(cmd)
    end
  end
end

#suitability(method, *args) ⇒ Object

:nodoc:



7
8
9
10
# File 'lib/automateit/account_manager/posix.rb', line 7

def suitability(method, *args) # :nodoc:
  # Level must be higher than Portable
  return available? ? 2 : 0
end