Class: Wright::Provider::Group

Inherits:
Wright::Provider show all
Defined in:
lib/wright/provider/group.rb,
lib/wright/provider/group/gnu_passwd.rb,
lib/wright/provider/group/darwin_directory_service.rb

Overview

Group provider. Used as a base class for Resource::Group providers.

Direct Known Subclasses

DarwinDirectoryService, GnuPasswd

Defined Under Namespace

Classes: DarwinDirectoryService, GnuPasswd

Constant Summary

Constants inherited from Wright::Provider

PROVIDER_DIR

Instance Method Summary collapse

Methods inherited from Wright::Provider

#env, #exec_or_fail, #initialize, #unless_dry_run, #unless_uptodate, #updated?

Constructor Details

This class inherits a constructor from Wright::Provider

Instance Method Details

#add_member(_member, _group) ⇒ Object (private)


113
114
115
# File 'lib/wright/provider/group.rb', line 113

def add_member(_member, _group)
  fail NotImplementedError
end

#createvoid

This method returns an undefined value.

Creates or updates the group.


12
13
14
15
16
17
18
19
# File 'lib/wright/provider/group.rb', line 12

def create
  unless_uptodate(:create, "group already created: '#{group_name}'") do
    unless_dry_run("create group: '#{group_name}'") do
      ensure_group_exists
      set_members unless members_uptodate?
    end
  end
end

#create_groupObject (private)


101
102
103
# File 'lib/wright/provider/group.rb', line 101

def create_group
  fail NotImplementedError
end

#ensure_group_existsObject (private)


50
51
52
53
54
55
56
# File 'lib/wright/provider/group.rb', line 50

def ensure_group_exists
  if group_exists?
    set_gid unless gid_uptodate?
  else
    create_group
  end
end

#gidObject (private)


38
39
40
# File 'lib/wright/provider/group.rb', line 38

def gid
  @resource.gid
end

#gid_uptodate?Boolean (private)


82
83
84
# File 'lib/wright/provider/group.rb', line 82

def gid_uptodate?
  gid.nil? || group_data.gid == gid
end

#group_dataObject (private)


78
79
80
# File 'lib/wright/provider/group.rb', line 78

def group_data
  Wright::Util::User.safe_getgrnam(group_name)
end

#group_exists?Boolean (private)


90
91
92
# File 'lib/wright/provider/group.rb', line 90

def group_exists?
  !group_data.nil?
end

#group_nameObject (private)


34
35
36
# File 'lib/wright/provider/group.rb', line 34

def group_name
  @resource.name
end

#membersObject (private)


42
43
44
# File 'lib/wright/provider/group.rb', line 42

def members
  @resource.members
end

#members_uptodate?Boolean (private)


86
87
88
# File 'lib/wright/provider/group.rb', line 86

def members_uptodate?
  members.nil? || group_data.mem == members
end

#removevoid

This method returns an undefined value.

Removes the group.


24
25
26
27
28
29
30
# File 'lib/wright/provider/group.rb', line 24

def remove
  unless_uptodate(:remove, "group already removed: '#{group_name}'") do
    unless_dry_run("remove group: '#{group_name}'") do
      remove_group
    end
  end
end

#remove_groupObject (private)


105
106
107
# File 'lib/wright/provider/group.rb', line 105

def remove_group
  fail NotImplementedError
end

#remove_member(_member, _group) ⇒ Object (private)


117
118
119
# File 'lib/wright/provider/group.rb', line 117

def remove_member(_member, _group)
  fail NotImplementedError
end

#set_gidObject (private)


109
110
111
# File 'lib/wright/provider/group.rb', line 109

def set_gid
  fail NotImplementedError
end

#set_membersObject (private)


94
95
96
97
98
99
# File 'lib/wright/provider/group.rb', line 94

def set_members
  new_members = members - group_data.mem
  unwanted_members = group_data.mem - members
  new_members.each { |m| add_member(m, group_name) }
  unwanted_members.each { |m| remove_member(m, group_name) }
end

#system_group?Boolean (private)


46
47
48
# File 'lib/wright/provider/group.rb', line 46

def system_group?
  @resource.system
end

#uptodate?(action) ⇒ Bool (private)

Checks if the group is up-to-date for a given action.

Raises:

  • (ArgumentError)

    if the action is invalid


67
68
69
70
71
72
73
74
75
76
# File 'lib/wright/provider/group.rb', line 67

def uptodate?(action)
  case action
  when :create
    group_exists? && gid_uptodate? && members_uptodate?
  when :remove
    !group_exists?
  else
    fail ArgumentError, "invalid action '#{action}'"
  end
end