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,254 @@
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/config'
20
+ require 'chef/log'
21
+ require 'chef/mixin/params_validate'
22
+ require 'chef/platform'
23
+ require 'chef/resource'
24
+ Dir[File.join(File.dirname(__FILE__), 'provider/**/*.rb')].sort.each { |lib| require lib }
25
+
26
+ class Chef
27
+ class Platform
28
+
29
+ @platforms = {
30
+ :mac_os_x => {
31
+ :default => {
32
+ :package => Chef::Provider::Package::Macports
33
+ }
34
+ },
35
+ :freebsd => {
36
+ :default => {
37
+ :group => Chef::Provider::Group::Pw,
38
+ :package => Chef::Provider::Package::Freebsd,
39
+ :service => Chef::Provider::Service::Freebsd,
40
+ :user => Chef::Provider::User::Pw,
41
+ :cron => Chef::Provider::Cron
42
+ }
43
+ },
44
+ :ubuntu => {
45
+ :default => {
46
+ :package => Chef::Provider::Package::Apt,
47
+ :service => Chef::Provider::Service::Debian,
48
+ :cron => Chef::Provider::Cron,
49
+ }
50
+ },
51
+ :debian => {
52
+ :default => {
53
+ :package => Chef::Provider::Package::Apt,
54
+ :service => Chef::Provider::Service::Debian,
55
+ :cron => Chef::Provider::Cron,
56
+ }
57
+ },
58
+ :centos => {
59
+ :default => {
60
+ :service => Chef::Provider::Service::Redhat,
61
+ :cron => Chef::Provider::Cron,
62
+ :package => Chef::Provider::Package::Yum
63
+ }
64
+ },
65
+ :redhat => {
66
+ :default => {
67
+ :service => Chef::Provider::Service::Redhat,
68
+ :cron => Chef::Provider::Cron,
69
+ :package => Chef::Provider::Package::Yum
70
+ }
71
+ },
72
+ :gentoo => {
73
+ :default => {
74
+ :package => Chef::Provider::Package::Portage,
75
+ :service => Chef::Provider::Service::Gentoo,
76
+ :cron => Chef::Provider::Cron,
77
+ }
78
+ },
79
+ :solaris => {},
80
+ :default => {
81
+ :file => Chef::Provider::File,
82
+ :directory => Chef::Provider::Directory,
83
+ :link => Chef::Provider::Link,
84
+ :template => Chef::Provider::Template,
85
+ :remote_file => Chef::Provider::RemoteFile,
86
+ :remote_directory => Chef::Provider::RemoteDirectory,
87
+ :execute => Chef::Provider::Execute,
88
+ :mount => Chef::Provider::Mount::Mount,
89
+ :script => Chef::Provider::Script,
90
+ :service => Chef::Provider::Service::Init,
91
+ :perl => Chef::Provider::Script,
92
+ :python => Chef::Provider::Script,
93
+ :ruby => Chef::Provider::Script,
94
+ :bash => Chef::Provider::Script,
95
+ :csh => Chef::Provider::Script,
96
+ :user => Chef::Provider::User::Useradd,
97
+ :group => Chef::Provider::Group::Gpasswd,
98
+ :http_request => Chef::Provider::HttpRequest,
99
+ :route => Chef::Provider::Route,
100
+ :ifconfig => Chef::Provider::Ifconfig,
101
+ :ruby_block => Chef::Provider::RubyBlock
102
+ }
103
+ }
104
+
105
+ class << self
106
+ attr_accessor :platforms
107
+
108
+ include Chef::Mixin::ParamsValidate
109
+
110
+ def find(name, version)
111
+ provider_map = @platforms[:default].clone
112
+
113
+ name_sym = name
114
+ if name.kind_of?(String)
115
+ name.downcase!
116
+ name.gsub!(/\s/, "_")
117
+ name_sym = name.to_sym
118
+ end
119
+
120
+ if @platforms.has_key?(name_sym)
121
+ if @platforms[name_sym].has_key?(version)
122
+ Chef::Log.debug("Platform #{name.to_s} version #{version} found")
123
+ if @platforms[name_sym].has_key?(:default)
124
+ provider_map.merge!(@platforms[name_sym][:default])
125
+ end
126
+ provider_map.merge!(@platforms[name_sym][version])
127
+ elsif @platforms[name_sym].has_key?(:default)
128
+ provider_map.merge!(@platforms[name_sym][:default])
129
+ end
130
+ else
131
+ Chef::Log.debug("Platform #{name} not found, using all defaults. (Unsupported platform?)")
132
+ end
133
+ provider_map
134
+ end
135
+
136
+ def find_provider(platform, version, resource_type)
137
+ pmap = Chef::Platform.find(platform, version)
138
+ rtkey = resource_type
139
+ if resource_type.kind_of?(Chef::Resource)
140
+ return resource_type.provider if resource_type.provider
141
+ rtkey = resource_type.resource_name.to_sym
142
+ end
143
+ if pmap.has_key?(rtkey)
144
+ pmap[rtkey]
145
+ else
146
+ Chef::Log.error("#{rtkey.inspect} #{pmap.inspect}")
147
+ raise(
148
+ ArgumentError,
149
+ "Cannot find a provider for #{resource_type} on #{platform} version #{version}"
150
+ )
151
+ end
152
+ end
153
+
154
+ def find_platform_and_version(node)
155
+ platform = nil
156
+ version = nil
157
+
158
+ if node[:platform]
159
+ platform = node[:platform]
160
+ elsif node.attribute?("os")
161
+ platform = node[:os]
162
+ end
163
+
164
+ raise ArgumentError, "Cannot find a platform for #{node}" unless platform
165
+
166
+ if node[:platform_version]
167
+ version = node[:platform_version]
168
+ elsif node[:os_version]
169
+ version = node[:os_version]
170
+ elsif node[:os_release]
171
+ version = node[:os_release]
172
+ end
173
+
174
+ raise ArgumentError, "Cannot find a version for #{node}" unless version
175
+
176
+ return platform, version
177
+ end
178
+
179
+ def provider_for_node(node, resource_type)
180
+ find_provider_for_node(node, resource_type).new(node, resource_type)
181
+ end
182
+
183
+ def find_provider_for_node(node, resource_type)
184
+ platform, version = find_platform_and_version(node)
185
+ provider = find_provider(platform, version, resource_type)
186
+ end
187
+
188
+ def set(args)
189
+ validate(
190
+ args,
191
+ {
192
+ :platform => {
193
+ :kind_of => Symbol,
194
+ :required => false,
195
+ },
196
+ :version => {
197
+ :kind_of => String,
198
+ :required => false,
199
+ },
200
+ :resource => {
201
+ :kind_of => Symbol,
202
+ },
203
+ :provider => {
204
+ :kind_of => [ String, Symbol, Class ],
205
+ }
206
+ }
207
+ )
208
+ if args.has_key?(:platform)
209
+ if args.has_key?(:version)
210
+ if @platforms.has_key?(args[:platform])
211
+ if @platforms[args[:platform]].has_key?(args[:version])
212
+ @platforms[args[:platform]][args[:version]][args[:resource].to_sym] = args[:provider]
213
+ else
214
+ @platforms[args[:platform]][args[:version]] = {
215
+ args[:resource].to_sym => args[:provider]
216
+ }
217
+ end
218
+ else
219
+ @platforms[args[:platform]] = {
220
+ args[:version] => {
221
+ args[:resource].to_sym => args[:provider]
222
+ }
223
+ }
224
+ end
225
+ else
226
+ if @platforms.has_key?(args[:platform])
227
+ if @platforms[args[:platform]].has_key?(:default)
228
+ @platforms[args[:platform]][:default][args[:resource].to_sym] = args[:provider]
229
+ else
230
+ @platforms[args[:platform]] = { :default => { args[:resource].to_sym => args[:provider] } }
231
+ end
232
+ else
233
+ @platforms[args[:platform]] = {
234
+ :default => {
235
+ args[:resource].to_sym => args[:provider]
236
+ }
237
+ }
238
+ end
239
+ end
240
+ else
241
+ if @platforms.has_key?(:default)
242
+ @platforms[:default][args[:resource].to_sym] = args[:provider]
243
+ else
244
+ @platforms[:default] = {
245
+ args[:resource].to_sym => args[:provider]
246
+ }
247
+ end
248
+ end
249
+ end
250
+
251
+ end
252
+
253
+ end
254
+ end
@@ -0,0 +1,101 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Author:: Christopher Walters (<cw@opscode.com>)
4
+ # Copyright:: Copyright (c) 2008, 2009 Opscode, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require 'chef/mixin/from_file'
21
+ require 'chef/mixin/convert_to_class_name'
22
+ require 'chef/mixin/recipe_definition_dsl_core'
23
+
24
+ class Chef
25
+ class Provider
26
+
27
+ include Chef::Mixin::RecipeDefinitionDSLCore
28
+
29
+ attr_accessor :node, :new_resource, :current_resource
30
+
31
+ def initialize(node, new_resource, collection=nil, definitions={}, cookbook_loader=nil)
32
+ @node = node
33
+ @new_resource = new_resource
34
+ @current_resource = nil
35
+ @collection = collection
36
+ @definitions = definitions
37
+ @cookbook_loader = cookbook_loader
38
+ @cookbook_name = @new_resource.cookbook_name
39
+ end
40
+
41
+ def load_current_resource
42
+ raise Chef::Exceptions::Override, "You must override load_current_resource in #{self.to_s}"
43
+ end
44
+
45
+ def action_nothing
46
+ Chef::Log.debug("Doing nothing for #{@new_resource.to_s}")
47
+ true
48
+ end
49
+
50
+ protected
51
+
52
+ def recipe_eval(*args, &block)
53
+ provider_collection, @collection = @collection, Chef::ResourceCollection.new
54
+
55
+ instance_eval(*args, &block)
56
+ Chef::Runner.new(@node, @collection).converge
57
+
58
+ @collection = provider_collection
59
+ end
60
+
61
+ public
62
+
63
+ class << self
64
+ include Chef::Mixin::ConvertToClassName
65
+
66
+ def build_from_file(cookbook_name, filename)
67
+ pname = filename_to_qualified_string(cookbook_name, filename)
68
+
69
+ new_provider_class = Class.new self do |cls|
70
+
71
+ def load_current_resource
72
+ # silence Chef::Exceptions::Override exception
73
+ end
74
+
75
+ class << cls
76
+ include Chef::Mixin::FromFile
77
+
78
+ # setup DSL's shortcut methods
79
+ def action(name, &block)
80
+ define_method("action_#{name.to_s}") do
81
+ instance_eval(&block)
82
+ end
83
+ end
84
+ end
85
+
86
+ # load provider definition from file
87
+ cls.class_from_file(filename)
88
+ end
89
+
90
+ # register new class as a Chef::Provider
91
+ pname = filename_to_qualified_string(cookbook_name, filename)
92
+ class_name = convert_to_class_name(pname)
93
+ Chef::Provider.const_set(class_name, new_provider_class)
94
+ Chef::Log.debug("Loaded contents of #{filename} into a provider named #{pname} defined in Chef::Provider::#{class_name}")
95
+
96
+ new_provider_class
97
+ end
98
+ end
99
+
100
+ end
101
+ end
@@ -0,0 +1,187 @@
1
+ #
2
+ # Author:: Bryan McLellan (btm@loftninjas.org)
3
+ # Copyright:: Copyright (c) 2009 Bryan McLellan
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/log'
20
+ require 'chef/mixin/command'
21
+ require 'chef/provider'
22
+
23
+ class Chef
24
+ class Provider
25
+ class Cron < Chef::Provider
26
+ include Chef::Mixin::Command
27
+
28
+ def initialize(node, new_resource, collection=nil, definitions=nil, cookbook_loader=nil)
29
+ super(node, new_resource, collection, definitions, cookbook_loader)
30
+ @cron_exists = false
31
+ @cron_empty = false
32
+ end
33
+ attr_accessor :cron_exists, :cron_empty
34
+
35
+ def load_current_resource
36
+ crontab = String.new
37
+ @current_resource = Chef::Resource::Cron.new(@new_resource.name)
38
+ @current_resource.user(@new_resource.user)
39
+ status = popen4("crontab -l -u #{@new_resource.user}") do |pid, stdin, stdout, stderr|
40
+ stdout.each { |line| crontab << line }
41
+ end
42
+ if status.exitstatus > 1
43
+ raise Chef::Exceptions::Cron, "Error determining state of #{@new_resource.name}, exit: #{status.exitstatus}"
44
+ elsif status.exitstatus == 0
45
+ cron_found = false
46
+ crontab.each do |line|
47
+ case line
48
+ when /^# Chef Name: #{@new_resource.name}/
49
+ Chef::Log.debug("Found cron '#{@new_resource.name}'")
50
+ cron_found = true
51
+ @cron_exists = true
52
+ next
53
+ when /^MAILTO=(\S*)/
54
+ @current_resource.mailto($1) if cron_found
55
+ next
56
+ when /^PATH=(\S*)/
57
+ @current_resource.path($1) if cron_found
58
+ next
59
+ when /^SHELL=(\S*)/
60
+ @current_resource.shell($1) if cron_found
61
+ next
62
+ when /^HOME=(\S*)/
63
+ @current_resource.home($1) if cron_found
64
+ next
65
+ when /([0-9\*]+)\s*([0-9\*]+)\s*([0-9\*]+)\s*([0-9\*]+)\s*([0-9\*]+)\s*(.*)/
66
+ if cron_found
67
+ @current_resource.minute($1)
68
+ @current_resource.hour($2)
69
+ @current_resource.day($3)
70
+ @current_resource.month($4)
71
+ @current_resource.weekday($5)
72
+ @current_resource.command($6)
73
+ cron_found=false
74
+ end
75
+ next
76
+ else
77
+ next
78
+ end
79
+ end
80
+ Chef::Log.debug("Cron '#{@new_resource.name}' not found") unless @cron_exists
81
+ elsif status.exitstatus == 1
82
+ Chef::Log.debug("Cron empty for '#{@new_resource.user}'")
83
+ @cron_empty = true
84
+ end
85
+
86
+ @current_resource
87
+ end
88
+
89
+ def compare_cron
90
+ [ :minute, :hour, :day, :month, :weekday, :command, :mailto, :path, :shell, :home ].any? do |cron_var|
91
+ !@new_resource.send(cron_var).nil? && @new_resource.send(cron_var) != @current_resource.send(cron_var)
92
+ end
93
+ end
94
+
95
+ def action_create
96
+ crontab = String.new
97
+ newcron = String.new
98
+ cron_found = false
99
+
100
+ newcron << "# Chef Name: #{new_resource.name}\n"
101
+ [ :mailto, :path, :shell, :home ].each do |v|
102
+ newcron << "#{v.to_s.upcase}=#{@new_resource.send(v)}\n" if @new_resource.send(v)
103
+ end
104
+ newcron << "#{@new_resource.minute} #{@new_resource.hour} #{@new_resource.day} #{@new_resource.month} #{@new_resource.weekday} #{@new_resource.command}\n"
105
+
106
+ if @cron_exists
107
+ unless compare_cron
108
+ Chef::Log.debug("Skipping existing cron entry '#{@new_resource.name}'")
109
+ return
110
+ end
111
+ status = popen4("crontab -l -u #{@new_resource.user}") do |pid, stdin, stdout, stderr|
112
+ stdout.each_line do |line|
113
+ case line
114
+ when /^# Chef Name: #{@new_resource.name}\n/
115
+ cron_found = true
116
+ next
117
+ when /([0-9\*]+)\s*([0-9\*]+)\s*([0-9\*]+)\s*([0-9\*]+)\s*([0-9\*]+)\s*(.*)/
118
+ if cron_found
119
+ cron_found = false
120
+ crontab << newcron
121
+ next
122
+ end
123
+ else
124
+ next if cron_found
125
+ end
126
+ crontab << line
127
+ end
128
+ end
129
+
130
+ status = popen4("crontab -u #{@new_resource.user} -", :waitlast => true) do |pid, stdin, stdout, stderr|
131
+ crontab.each { |line| stdin.puts "#{line}" }
132
+ stdin.close rescue nil
133
+ end
134
+ Chef::Log.info("Updated cron '#{@new_resource.name}'")
135
+ @new_resource.updated = true
136
+ else
137
+ unless @cron_empty
138
+ status = popen4("crontab -l -u #{@new_resource.user}") do |pid, stdin, stdout, stderr|
139
+ stdout.each { |line| crontab << line }
140
+ end
141
+ end
142
+
143
+ crontab << newcron
144
+
145
+ status = popen4("crontab -u #{@new_resource.user} -", :waitlast => true) do |pid, stdin, stdout, stderr|
146
+ crontab.each { |line| stdin.puts "#{line}" }
147
+ stdin.close rescue nil
148
+ end
149
+ Chef::Log.info("Added cron '#{@new_resource.name}'")
150
+ @new_resource.updated = true
151
+ end
152
+ end
153
+
154
+ def action_delete
155
+ if @cron_exists
156
+ crontab = String.new
157
+ cron_found = false
158
+ status = popen4("crontab -l -u #{@new_resource.user}") do |pid, stdin, stdout, stderr|
159
+ stdout.each_line do |line|
160
+ case line
161
+ when /^# Chef Name: #{@new_resource.name}\n/
162
+ cron_found = true
163
+ next
164
+ when /([0-9\*]+)\s*([0-9\*]+)\s*([0-9\*]+)\s*([0-9\*]+)\s*([0-9\*]+)\s*(.*)/
165
+ if cron_found
166
+ cron_found = false
167
+ next
168
+ end
169
+ else
170
+ next if cron_found
171
+ end
172
+ crontab << line
173
+ end
174
+ end
175
+
176
+ status = popen4("crontab -u #{@new_resource.user} -", :waitlast => true) do |pid, stdin, stdout, stderr|
177
+ crontab.each { |line| stdin.puts "#{line}" }
178
+ stdin.close rescue nil
179
+ end
180
+ Chef::Log.debug("Deleted cron '#{@new_resource.name}'")
181
+ @new_resource.updated = true
182
+ end
183
+ end
184
+
185
+ end
186
+ end
187
+ end