summaryrefslogtreecommitdiff
path: root/lib/chef/provider/group/groupadd.rb
blob: 544fee430451782ef30896b27831df2dde88b7ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#
# Author:: AJ Christensen (<aj@opscode.com>)
# Copyright:: Copyright (c) 2008 OpsCode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

class Chef
  class Provider
    class Group
      class Groupadd < Chef::Provider::Group
        
        def required_binaries
          [ "/usr/sbin/groupadd",
            "/usr/sbin/groupmod",
            "/usr/sbin/groupdel" ]
        end

        def load_current_resource
          super
        end

        def define_resource_requirements
          super
          required_binaries.each do |required_binary|
            requirements.assert(:all_actions) do |a| 
              a.assertion { ::File.exists?(required_binary) } 
              a.failure_message Chef::Exceptions::Group, "Could not find binary #{required_binary} for #{@new_resource}" 
              # No whyrun alternative: this component should be available in the base install of any given system that uses it
            end
          end
        end

        # Create the group
        def create_group
          command = "groupadd"
          command << set_options
          command << groupadd_options
          run_command(:command => command)
          modify_group_members    
        end
        
        # Manage the group when it already exists
        def manage_group
          command = "groupmod"
          command << set_options
          run_command(:command => command)
          modify_group_members
        end
        
        # Remove the group
        def remove_group
          run_command(:command => "groupdel #{@new_resource.group_name}")
        end
        
        def modify_group_members
          raise Chef::Exceptions::Group, "you must override modify_group_members in #{self.to_s}"
        end
        # Little bit of magic as per Adam's useradd provider to pull the assign the command line flags
        #
        # ==== Returns
        # <string>:: A string containing the option and then the quoted value
        def set_options
          opts = ""
          { :gid => "-g" }.sort { |a,b| a[0] <=> b[0] }.each do |field, option|
            if @current_resource.send(field) != @new_resource.send(field)
              if @new_resource.send(field)
                opts << " #{option} '#{@new_resource.send(field)}'"
                Chef::Log.debug("#{@new_resource} set #{field.to_s} to #{@new_resource.send(field)}")
              end
            end
          end
          opts << " #{@new_resource.group_name}"
        end

        def groupadd_options
          opts = ''
          opts << " -r" if @new_resource.system
          opts
        end

      end
    end
  end
end