giraffesoft-chef 0.7.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. data/LICENSE +201 -0
  2. data/README.rdoc +135 -0
  3. data/bin/chef-client +26 -0
  4. data/bin/chef-solo +26 -0
  5. data/distro/debian/etc/init.d/chef-client +175 -0
  6. data/distro/debian/etc/init.d/chef-indexer +175 -0
  7. data/distro/debian/etc/init.d/chef-server +120 -0
  8. data/distro/debian/man/man1/chef-indexer.1 +42 -0
  9. data/distro/debian/man/man1/chef-server.1 +108 -0
  10. data/distro/debian/man/man8/chef-client.8 +61 -0
  11. data/distro/debian/man/man8/chef-solo.8 +58 -0
  12. data/distro/redhat/etc/chef/client.rb +16 -0
  13. data/distro/redhat/etc/chef/indexer.rb +10 -0
  14. data/distro/redhat/etc/chef/server.rb +22 -0
  15. data/distro/redhat/etc/init.d/chef-client +74 -0
  16. data/distro/redhat/etc/init.d/chef-indexer +76 -0
  17. data/distro/redhat/etc/init.d/chef-server +77 -0
  18. data/lib/chef.rb +49 -0
  19. data/lib/chef/application.rb +98 -0
  20. data/lib/chef/application/agent.rb +18 -0
  21. data/lib/chef/application/client.rb +209 -0
  22. data/lib/chef/application/indexer.rb +141 -0
  23. data/lib/chef/application/server.rb +18 -0
  24. data/lib/chef/application/solo.rb +214 -0
  25. data/lib/chef/client.rb +421 -0
  26. data/lib/chef/compile.rb +170 -0
  27. data/lib/chef/config.rb +141 -0
  28. data/lib/chef/cookbook.rb +171 -0
  29. data/lib/chef/cookbook/metadata.rb +407 -0
  30. data/lib/chef/cookbook/metadata/version.rb +87 -0
  31. data/lib/chef/cookbook_loader.rb +180 -0
  32. data/lib/chef/couchdb.rb +176 -0
  33. data/lib/chef/daemon.rb +170 -0
  34. data/lib/chef/exceptions.rb +36 -0
  35. data/lib/chef/file_cache.rb +205 -0
  36. data/lib/chef/log.rb +39 -0
  37. data/lib/chef/mixin/check_helper.rb +31 -0
  38. data/lib/chef/mixin/checksum.rb +37 -0
  39. data/lib/chef/mixin/command.rb +386 -0
  40. data/lib/chef/mixin/convert_to_class_name.rb +48 -0
  41. data/lib/chef/mixin/create_path.rb +56 -0
  42. data/lib/chef/mixin/deep_merge.rb +36 -0
  43. data/lib/chef/mixin/find_preferred_file.rb +92 -0
  44. data/lib/chef/mixin/from_file.rb +50 -0
  45. data/lib/chef/mixin/generate_url.rb +49 -0
  46. data/lib/chef/mixin/language.rb +79 -0
  47. data/lib/chef/mixin/params_validate.rb +197 -0
  48. data/lib/chef/mixin/recipe_definition_dsl_core.rb +77 -0
  49. data/lib/chef/mixin/template.rb +84 -0
  50. data/lib/chef/node.rb +406 -0
  51. data/lib/chef/node/attribute.rb +412 -0
  52. data/lib/chef/openid_registration.rb +181 -0
  53. data/lib/chef/platform.rb +254 -0
  54. data/lib/chef/provider.rb +101 -0
  55. data/lib/chef/provider/cron.rb +187 -0
  56. data/lib/chef/provider/deploy.rb +281 -0
  57. data/lib/chef/provider/deploy/revision.rb +70 -0
  58. data/lib/chef/provider/deploy/timestamped.rb +33 -0
  59. data/lib/chef/provider/directory.rb +72 -0
  60. data/lib/chef/provider/execute.rb +58 -0
  61. data/lib/chef/provider/file.rb +191 -0
  62. data/lib/chef/provider/git.rb +198 -0
  63. data/lib/chef/provider/group.rb +120 -0
  64. data/lib/chef/provider/group/gpasswd.rb +50 -0
  65. data/lib/chef/provider/group/groupadd.rb +78 -0
  66. data/lib/chef/provider/group/pw.rb +88 -0
  67. data/lib/chef/provider/group/usermod.rb +57 -0
  68. data/lib/chef/provider/http_request.rb +102 -0
  69. data/lib/chef/provider/ifconfig.rb +131 -0
  70. data/lib/chef/provider/link.rb +157 -0
  71. data/lib/chef/provider/mount.rb +117 -0
  72. data/lib/chef/provider/mount/mount.rb +208 -0
  73. data/lib/chef/provider/package.rb +160 -0
  74. data/lib/chef/provider/package/apt.rb +110 -0
  75. data/lib/chef/provider/package/dpkg.rb +109 -0
  76. data/lib/chef/provider/package/freebsd.rb +153 -0
  77. data/lib/chef/provider/package/macports.rb +105 -0
  78. data/lib/chef/provider/package/portage.rb +124 -0
  79. data/lib/chef/provider/package/rpm.rb +99 -0
  80. data/lib/chef/provider/package/rubygems.rb +136 -0
  81. data/lib/chef/provider/package/yum-dump.py +106 -0
  82. data/lib/chef/provider/package/yum.rb +175 -0
  83. data/lib/chef/provider/remote_directory.rb +126 -0
  84. data/lib/chef/provider/remote_file.rb +141 -0
  85. data/lib/chef/provider/route.rb +118 -0
  86. data/lib/chef/provider/ruby_block.rb +15 -0
  87. data/lib/chef/provider/script.rb +42 -0
  88. data/lib/chef/provider/service.rb +135 -0
  89. data/lib/chef/provider/service/debian.rb +64 -0
  90. data/lib/chef/provider/service/freebsd.rb +157 -0
  91. data/lib/chef/provider/service/gentoo.rb +54 -0
  92. data/lib/chef/provider/service/init.rb +71 -0
  93. data/lib/chef/provider/service/redhat.rb +62 -0
  94. data/lib/chef/provider/service/simple.rb +115 -0
  95. data/lib/chef/provider/subversion.rb +148 -0
  96. data/lib/chef/provider/template.rb +143 -0
  97. data/lib/chef/provider/user.rb +170 -0
  98. data/lib/chef/provider/user/pw.rb +113 -0
  99. data/lib/chef/provider/user/useradd.rb +107 -0
  100. data/lib/chef/queue.rb +145 -0
  101. data/lib/chef/recipe.rb +144 -0
  102. data/lib/chef/resource.rb +380 -0
  103. data/lib/chef/resource/apt_package.rb +34 -0
  104. data/lib/chef/resource/bash.rb +33 -0
  105. data/lib/chef/resource/cron.rb +179 -0
  106. data/lib/chef/resource/csh.rb +33 -0
  107. data/lib/chef/resource/deploy.rb +350 -0
  108. data/lib/chef/resource/deploy_revision.rb +35 -0
  109. data/lib/chef/resource/directory.rb +76 -0
  110. data/lib/chef/resource/dpkg_package.rb +34 -0
  111. data/lib/chef/resource/execute.rb +127 -0
  112. data/lib/chef/resource/file.rb +84 -0
  113. data/lib/chef/resource/gem_package.rb +41 -0
  114. data/lib/chef/resource/git.rb +36 -0
  115. data/lib/chef/resource/group.rb +70 -0
  116. data/lib/chef/resource/http_request.rb +52 -0
  117. data/lib/chef/resource/ifconfig.rb +134 -0
  118. data/lib/chef/resource/link.rb +78 -0
  119. data/lib/chef/resource/macports_package.rb +29 -0
  120. data/lib/chef/resource/mount.rb +135 -0
  121. data/lib/chef/resource/package.rb +80 -0
  122. data/lib/chef/resource/perl.rb +33 -0
  123. data/lib/chef/resource/portage_package.rb +33 -0
  124. data/lib/chef/resource/python.rb +33 -0
  125. data/lib/chef/resource/remote_directory.rb +91 -0
  126. data/lib/chef/resource/remote_file.rb +60 -0
  127. data/lib/chef/resource/route.rb +135 -0
  128. data/lib/chef/resource/ruby.rb +33 -0
  129. data/lib/chef/resource/ruby_block.rb +20 -0
  130. data/lib/chef/resource/scm.rb +129 -0
  131. data/lib/chef/resource/script.rb +51 -0
  132. data/lib/chef/resource/service.rb +134 -0
  133. data/lib/chef/resource/subversion.rb +33 -0
  134. data/lib/chef/resource/template.rb +60 -0
  135. data/lib/chef/resource/timestamped_deploy.rb +31 -0
  136. data/lib/chef/resource/user.rb +98 -0
  137. data/lib/chef/resource_collection.rb +204 -0
  138. data/lib/chef/resource_definition.rb +67 -0
  139. data/lib/chef/rest.rb +238 -0
  140. data/lib/chef/role.rb +231 -0
  141. data/lib/chef/run_list.rb +156 -0
  142. data/lib/chef/runner.rb +130 -0
  143. data/lib/chef/search.rb +88 -0
  144. data/lib/chef/search/result.rb +64 -0
  145. data/lib/chef/search_index.rb +77 -0
  146. data/lib/chef/tasks/chef_repo.rake +347 -0
  147. data/lib/chef/util/file_edit.rb +125 -0
  148. data/lib/chef/util/fileedit.rb +121 -0
  149. metadata +293 -0
@@ -0,0 +1,143 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 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
+ require 'chef/provider/file'
20
+ require 'chef/mixin/template'
21
+ require 'chef/mixin/checksum'
22
+ require 'chef/mixin/find_preferred_file'
23
+ require 'chef/rest'
24
+ require 'chef/file_cache'
25
+ require 'uri'
26
+ require 'tempfile'
27
+
28
+ class Chef
29
+ class Provider
30
+ class Template < Chef::Provider::File
31
+
32
+ include Chef::Mixin::Checksum
33
+ include Chef::Mixin::Template
34
+ include Chef::Mixin::FindPreferredFile
35
+
36
+ def action_create
37
+ Chef::Log.debug(@node.run_state.inspect)
38
+ raw_template_file = nil
39
+
40
+ cookbook_name = @new_resource.cookbook || @new_resource.cookbook_name
41
+
42
+ Chef::Log.debug("looking for template #{@new_resource.source} in cookbook #{cookbook_name.inspect}")
43
+
44
+ cache_file_name = "cookbooks/#{cookbook_name}/templates/default/#{@new_resource.source}"
45
+ template_cache_name = "#{cookbook_name}_#{@new_resource.source}"
46
+
47
+ if Chef::Config[:solo]
48
+ filename = find_preferred_file(
49
+ cookbook_name,
50
+ :template,
51
+ @new_resource.source,
52
+ @node[:fqdn],
53
+ @node[:platform],
54
+ @node[:platform_version]
55
+ )
56
+ Chef::Log.debug("Using local file for template:#{filename}")
57
+ cache_file_name = Pathname.new(filename).relative_path_from(Pathname.new(Chef::Config[:file_cache_path])).to_s
58
+ elsif @node.run_state[:template_cache].has_key?(template_cache_name)
59
+ Chef::Log.debug("I have already fetched the template for #{@new_resource} once this run, not checking again.")
60
+ template_updated = false
61
+ else
62
+ r = Chef::REST.new(Chef::Config[:template_url])
63
+
64
+ current_checksum = nil
65
+
66
+ if Chef::FileCache.has_key?(cache_file_name)
67
+ current_checksum = self.checksum(Chef::FileCache.load(cache_file_name, false))
68
+ else
69
+ Chef::Log.debug("Template #{@new_resource} is not in the template cache")
70
+ end
71
+
72
+ template_url = generate_url(
73
+ @new_resource.source,
74
+ "templates",
75
+ {
76
+ :checksum => current_checksum
77
+ }
78
+ )
79
+
80
+ template_updated = true
81
+ begin
82
+ raw_template_file = r.get_rest(template_url, true)
83
+ rescue Net::HTTPRetriableError => e
84
+ if e.response.kind_of?(Net::HTTPNotModified)
85
+ template_updated = false
86
+ Chef::Log.debug("Cached template for #{@new_resource} is unchanged")
87
+ else
88
+ raise e
89
+ end
90
+ end
91
+
92
+ # We have checked the cache for this template this run
93
+ @node.run_state[:template_cache][template_cache_name] = true
94
+ end
95
+
96
+ if template_updated
97
+ Chef::Log.debug("Updating template for #{@new_resource} in the cache")
98
+ Chef::FileCache.move_to(raw_template_file.path, cache_file_name)
99
+ end
100
+
101
+ context = {}
102
+ context.merge!(@new_resource.variables)
103
+ context[:node] = @node
104
+ template_file = render_template(Chef::FileCache.load(cache_file_name), context)
105
+
106
+ update = false
107
+
108
+ if ::File.exists?(@new_resource.path)
109
+ @new_resource.checksum(self.checksum(template_file.path))
110
+ if @new_resource.checksum != @current_resource.checksum
111
+ Chef::Log.debug("#{@new_resource} changed from #{@current_resource.checksum} to #{@new_resource.checksum}")
112
+ Chef::Log.info("Updating #{@new_resource} at #{@new_resource.path}")
113
+ update = true
114
+ end
115
+ else
116
+ Chef::Log.info("Creating #{@new_resource} at #{@new_resource.path}")
117
+ update = true
118
+ end
119
+
120
+ if update
121
+ backup
122
+ FileUtils.cp(template_file.path, @new_resource.path)
123
+ @new_resource.updated = true
124
+ else
125
+ Chef::Log.debug("#{@new_resource} is unchanged")
126
+ end
127
+
128
+ set_owner if @new_resource.owner != nil
129
+ set_group if @new_resource.group != nil
130
+ set_mode if @new_resource.mode != nil
131
+ end
132
+
133
+ def action_create_if_missing
134
+ if ::File.exists?(@new_resource.path)
135
+ Chef::Log.debug("Template #{@new_resource} exists, taking no action.")
136
+ else
137
+ action_create
138
+ end
139
+ end
140
+
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,170 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 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
+ require 'chef/provider'
20
+ require 'chef/mixin/command'
21
+ require 'chef/resource/user'
22
+ require 'etc'
23
+
24
+ class Chef
25
+ class Provider
26
+ class User < Chef::Provider
27
+
28
+ include Chef::Mixin::Command
29
+
30
+ attr_accessor :user_exists, :locked
31
+
32
+ def initialize(node, new_resource, collection=nil, definitions=nil, cookbook_loader=nil)
33
+ super(node, new_resource, collection, definitions, cookbook_loader)
34
+ @user_exists = true
35
+ @locked = nil
36
+ end
37
+
38
+ def convert_group_name
39
+ if @new_resource.gid.is_a? String
40
+ @new_resource.gid Etc.getgrnam(@new_resource.gid).gid
41
+ end
42
+ rescue ArgumentError => e
43
+ raise Chef::Exceptions::User, "Couldn't lookup integer GID for group name #{@new_resource.gid}"
44
+ end
45
+
46
+ def load_current_resource
47
+ @current_resource = Chef::Resource::User.new(@new_resource.name)
48
+ @current_resource.username(@new_resource.username)
49
+
50
+ user_info = nil
51
+ begin
52
+ user_info = Etc.getpwnam(@new_resource.username)
53
+ rescue ArgumentError => e
54
+ @user_exists = false
55
+ Chef::Log.debug("User #{@new_resource.username} does not exist")
56
+ end
57
+
58
+ if user_info
59
+ @current_resource.uid(user_info.uid)
60
+ @current_resource.gid(user_info.gid)
61
+ @current_resource.comment(user_info.gecos)
62
+ @current_resource.home(user_info.dir)
63
+ @current_resource.shell(user_info.shell)
64
+ @current_resource.password(user_info.passwd)
65
+
66
+ if @new_resource.password && @current_resource.password == 'x'
67
+ begin
68
+ require 'shadow'
69
+ rescue LoadError
70
+ Chef::Log.error("You must have ruby-shadow installed for password support!")
71
+ raise Chef::Exceptions::MissingLibrary, "You must have ruby-shadow installed for password support!"
72
+ else
73
+ shadow_info = Shadow::Passwd.getspnam(@new_resource.username)
74
+ @current_resource.password(shadow_info.sp_pwdp)
75
+ end
76
+ end
77
+
78
+ if @new_resource.gid
79
+ convert_group_name
80
+ end
81
+ end
82
+
83
+ @current_resource
84
+ end
85
+
86
+ # Check to see if the user needs any changes
87
+ #
88
+ # === Returns
89
+ # <true>:: If a change is required
90
+ # <false>:: If the users are identical
91
+ def compare_user
92
+ [ :uid, :gid, :comment, :home, :shell, :password ].any? do |user_attrib|
93
+ !@new_resource.send(user_attrib).nil? && @new_resource.send(user_attrib) != @current_resource.send(user_attrib)
94
+ end
95
+ end
96
+
97
+ def action_create
98
+ case @user_exists
99
+ when false
100
+ create_user
101
+ Chef::Log.info("Created #{@new_resource}")
102
+ @new_resource.updated = true
103
+ else
104
+ if compare_user
105
+ manage_user
106
+ Chef::Log.info("Altered #{@new_resource}")
107
+ @new_resource.updated = true
108
+ end
109
+ end
110
+ end
111
+
112
+ def action_remove
113
+ if @user_exists
114
+ remove_user
115
+ @new_resource.updated = true
116
+ Chef::Log.info("Removed #{@new_resource}")
117
+ end
118
+ end
119
+
120
+ def action_manage
121
+ if @user_exists && compare_user
122
+ manage_user
123
+ @new_resource.updated = true
124
+ Chef::Log.info("Managed #{@new_resource}")
125
+ end
126
+ end
127
+
128
+ def action_modify
129
+ if @user_exists
130
+ if compare_user
131
+ manage_user
132
+ @new_resource.updated = true
133
+ Chef::Log.info("Modified #{@new_resource}")
134
+ end
135
+ else
136
+ raise Chef::Exceptions::User, "Cannot modify #{@new_resource} - user does not exist!"
137
+ end
138
+ end
139
+
140
+ def action_lock
141
+ if @user_exists
142
+ if check_lock() == false
143
+ lock_user
144
+ @new_resource.updated = true
145
+ Chef::Log.info("Locked #{@new_resource}")
146
+ else
147
+ Chef::Log.debug("No need to lock #{@new_resource}")
148
+ end
149
+ else
150
+ raise Chef::Exceptions::User, "Cannot lock #{@new_resource} - user does not exist!"
151
+ end
152
+ end
153
+
154
+ def action_unlock
155
+ if @user_exists
156
+ if check_lock() == true
157
+ unlock_user
158
+ @new_resource.updated = true
159
+ Chef::Log.info("Unlocked #{@new_resource}")
160
+ else
161
+ Chef::Log.debug("No need to unlock #{@new_resource}")
162
+ end
163
+ else
164
+ raise Chef::Exceptions::User, "Cannot unlock #{@new_resource} - user does not exist!"
165
+ end
166
+ end
167
+
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,113 @@
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
+ require 'chef/provider/user'
20
+
21
+ class Chef
22
+ class Provider
23
+ class User
24
+ class Pw < Chef::Provider::User
25
+
26
+ def load_current_resource
27
+ super
28
+ raise Chef::Exceptions::User, "Could not find binary /usr/sbin/pw for #{@new_resource}" unless ::File.exists?("/usr/sbin/pw")
29
+ end
30
+
31
+ def create_user
32
+ command = "pw useradd"
33
+ command << set_options
34
+ run_command(:command => command)
35
+ modify_password
36
+ end
37
+
38
+ def manage_user
39
+ command = "pw usermod"
40
+ command << set_options
41
+ run_command(:command => command)
42
+ modify_password
43
+ end
44
+
45
+ def remove_user
46
+ command = "pw userdel #{@new_resource.username}"
47
+ command << " -r" if @new_resource.supports[:manage_home]
48
+ run_command(:command => command)
49
+ end
50
+
51
+ def check_lock
52
+ case @current_resource.password
53
+ when /^\*LOCKED\*/
54
+ @locked = true
55
+ else
56
+ @locked = false
57
+ end
58
+ @locked
59
+ end
60
+
61
+ def lock_user
62
+ run_command(:command => "pw lock #{@new_resource.username}")
63
+ end
64
+
65
+ def unlock_user
66
+ run_command(:command => "pw unlock #{@new_resource.username}")
67
+ end
68
+
69
+ def set_options
70
+ opts = " #{@new_resource.username}"
71
+
72
+ field_list = {
73
+ 'comment' => "-c",
74
+ 'home' => "-d",
75
+ 'gid' => "-g",
76
+ 'uid' => "-u",
77
+ 'shell' => "-s"
78
+ }
79
+ field_list.sort{ |a,b| a[0] <=> b[0] }.each do |field, option|
80
+ field_symbol = field.to_sym
81
+ if @current_resource.send(field_symbol) != @new_resource.send(field_symbol)
82
+ if @new_resource.send(field_symbol)
83
+ Chef::Log.debug("Setting #{@new_resource} #{field} to #{@new_resource.send(field_symbol)}")
84
+ opts << " #{option} '#{@new_resource.send(field_symbol)}'"
85
+ end
86
+ end
87
+ end
88
+ if @new_resource.supports[:manage_home]
89
+ Chef::Log.debug("Managing the home directory for #{@new_resource}")
90
+ opts << " -m"
91
+ end
92
+ opts
93
+ end
94
+
95
+ def modify_password
96
+ if @current_resource.password != @new_resource.password
97
+ Chef::Log.debug("#{new_resource}: updating password")
98
+ command = "pw usermod #{@new_resource.username} -H 0"
99
+ status = popen4(command, :waitlast => true) do |pid, stdin, stdout, stderr|
100
+ stdin.puts "#{@new_resource.password}"
101
+ end
102
+
103
+ unless status.exitstatus == 0
104
+ raise Chef::Exceptions::User, "pw failed - #{status.inspect}!"
105
+ end
106
+ else
107
+ Chef::Log.debug("#{new_resource}: no change needed to password")
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,107 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2008 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
+ require 'chef/provider/user'
20
+
21
+ class Chef
22
+ class Provider
23
+ class User
24
+ class Useradd < Chef::Provider::User
25
+ def create_user
26
+ command = "useradd"
27
+ command << set_options
28
+ run_command(:command => command)
29
+ end
30
+
31
+ def manage_user
32
+ command = "usermod"
33
+ command << set_options
34
+ run_command(:command => command)
35
+ end
36
+
37
+ def remove_user
38
+ command = "userdel"
39
+ command << " -r" if @new_resource.supports[:manage_home]
40
+ command << " #{@new_resource.username}"
41
+ run_command(:command => command)
42
+ end
43
+
44
+ def check_lock
45
+ status = popen4("passwd -S #{@new_resource.username}") do |pid, stdin, stdout, stderr|
46
+ status_line = stdout.gets.split(' ')
47
+ case status_line[1]
48
+ when /^P/
49
+ @locked = false
50
+ when /^N/
51
+ @locked = false
52
+ when /^L/
53
+ @locked = true
54
+ end
55
+ end
56
+
57
+ unless status.exitstatus == 0
58
+ raise Chef::Exceptions::User, "Cannot determine if #{@new_resource} is locked!"
59
+ end
60
+
61
+ @locked
62
+ end
63
+
64
+ def lock_user
65
+ run_command(:command => "usermod -L #{@new_resource.username}")
66
+ end
67
+
68
+ def unlock_user
69
+ run_command(:command => "usermod -U #{@new_resource.username}")
70
+ end
71
+
72
+ def set_options
73
+ opts = ''
74
+
75
+ field_list = {
76
+ 'comment' => "-c",
77
+ 'gid' => "-g",
78
+ 'uid' => "-u",
79
+ 'shell' => "-s",
80
+ 'password' => "-p"
81
+ }
82
+ field_list.sort{ |a,b| a[0] <=> b[0] }.each do |field, option|
83
+ field_symbol = field.to_sym
84
+ if @current_resource.send(field_symbol) != @new_resource.send(field_symbol)
85
+ if @new_resource.send(field_symbol)
86
+ Chef::Log.debug("Setting #{@new_resource} #{field} to #{@new_resource.send(field_symbol)}")
87
+ opts << " #{option} '#{@new_resource.send(field_symbol)}'"
88
+ end
89
+ end
90
+ end
91
+ if @current_resource.home != @new_resource.home && @new_resource.home
92
+ if @new_resource.supports[:manage_home]
93
+ Chef::Log.debug("Managing the home directory for #{@new_resource}")
94
+ opts << " -d '#{@new_resource.home}' -m"
95
+ else
96
+ Chef::Log.debug("Setting #{@new_resource} home to #{@new_resource.home}")
97
+ opts << " -d '#{@new_resource.home}'"
98
+ end
99
+ end
100
+ opts << " #{@new_resource.username}"
101
+ opts
102
+ end
103
+
104
+ end
105
+ end
106
+ end
107
+ end