~ubuntu-branches/ubuntu/karmic/chef/karmic

« back to all changes in this revision

Viewing changes to chef/lib/chef/provider/group/pw.rb

  • Committer: Bazaar Package Importer
  • Author(s): Joshua Timberman
  • Date: 2009-08-12 12:18:48 UTC
  • Revision ID: james.westby@ubuntu.com-20090812121848-yl38hpd7nfsl51xz
Tags: upstream-0.7.8
ImportĀ upstreamĀ versionĀ 0.7.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Author:: Stephen Haynes (<sh@nomitor.com>)
 
3
# Copyright:: Copyright (c) 2009 Opscode, Inc.
 
4
# License:: Apache License, Version 2.0
 
5
#
 
6
# Licensed under the Apache License, Version 2.0 (the "License");
 
7
# you may not use this file except in compliance with the License.
 
8
# You may obtain a copy of the License at
 
9
 
10
#     http://www.apache.org/licenses/LICENSE-2.0
 
11
 
12
# Unless required by applicable law or agreed to in writing, software
 
13
# distributed under the License is distributed on an "AS IS" BASIS,
 
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
15
# See the License for the specific language governing permissions and
 
16
# limitations under the License.
 
17
#
 
18
 
 
19
class Chef
 
20
  class Provider
 
21
    class Group
 
22
      class Pw < Chef::Provider::Group
 
23
        
 
24
        def load_current_resource
 
25
          super
 
26
          raise Chef::Exceptions::Group, "Could not find binary /usr/sbin/pw for #{@new_resource}" unless ::File.exists?("/usr/sbin/pw")
 
27
        end
 
28
        
 
29
        # Create the group
 
30
        def create_group
 
31
          command = "pw groupadd"
 
32
          command << set_options
 
33
          command << set_members_option
 
34
          run_command(:command => command)
 
35
        end
 
36
        
 
37
        # Manage the group when it already exists
 
38
        def manage_group
 
39
          command = "pw groupmod"
 
40
          command << set_options
 
41
          command << set_members_option
 
42
          run_command(:command => command)
 
43
        end
 
44
        
 
45
        # Remove the group
 
46
        def remove_group
 
47
          run_command(:command => "pw groupdel #{@new_resource.group_name}")
 
48
        end
 
49
        
 
50
        # Little bit of magic as per Adam's useradd provider to pull and assign the command line flags
 
51
        #
 
52
        # ==== Returns
 
53
        # <string>:: A string containing the option and then the quoted value
 
54
        def set_options
 
55
          opts = " #{@new_resource.group_name}"
 
56
          { :gid => "-g" }.sort { |a,b| a[0] <=> b[0] }.each do |field, option|
 
57
            if @current_resource.send(field) != @new_resource.send(field)
 
58
              if @new_resource.send(field)
 
59
                Chef::Log.debug("#{@new_resource}: setting #{field.to_s} to #{@new_resource.send(field)}")
 
60
                opts << " #{option} '#{@new_resource.send(field)}'"
 
61
              end
 
62
            end
 
63
          end
 
64
          opts
 
65
        end
 
66
 
 
67
        # Set the membership option depending on the current resource states
 
68
        def set_members_option
 
69
          opt = ""
 
70
          unless @new_resource.members.empty?
 
71
            opt << " -M #{@new_resource.members.join(',')}"
 
72
            Chef::Log.debug("#{@new_resource}: setting group members to #{@new_resource.members.join(', ')}")
 
73
          else
 
74
            # New member list is empty so we should delete any old group members
 
75
            unless @current_resource.members.empty?
 
76
              opt << " -d #{@current_resource.members.join(',')}"
 
77
              Chef::Log.debug("#{@new_resource}: removing group members #{@current_resource.members.join(', ')}")
 
78
            else
 
79
              Chef::Log.debug("#{@new_resource}: not changing group members, the group has no members")
 
80
            end
 
81
          end
 
82
          opt
 
83
        end
 
84
        
 
85
      end
 
86
    end
 
87
  end
 
88
end
 
 
b'\\ No newline at end of file'