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,204 @@
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/resource'
21
+
22
+ class Chef
23
+ class ResourceCollection
24
+ include Enumerable
25
+
26
+ def initialize
27
+ @resources = Array.new
28
+ @resources_by_name = Hash.new
29
+ @insert_after_idx = nil
30
+ end
31
+
32
+ def [](index)
33
+ @resources[index]
34
+ end
35
+
36
+ def []=(index, arg)
37
+ is_chef_resource(arg)
38
+ @resources[index] = arg
39
+ @resources_by_name[arg.to_s] = index
40
+ end
41
+
42
+ def <<(*args)
43
+ args.flatten.each do |a|
44
+ is_chef_resource(a)
45
+ @resources << a
46
+ @resources_by_name[a.to_s] = @resources.length - 1
47
+ end
48
+ end
49
+
50
+ def insert(resource)
51
+ is_chef_resource(resource)
52
+ if @insert_after_idx
53
+ # in the middle of executing a run, so any resources inserted now should
54
+ # be placed after the most recent addition done by the currently executing
55
+ # resource
56
+ @resources.insert(@insert_after_idx + 1, resource)
57
+ # update name -> location mappings and register new resource
58
+ @resources_by_name.each_key do |key|
59
+ @resources_by_name[key] += 1 if @resources_by_name[key] > @insert_after_idx
60
+ end
61
+ @resources_by_name[resource.to_s] = @insert_after_idx + 1
62
+ @insert_after_idx += 1
63
+ else
64
+ @resources << resource
65
+ @resources_by_name[resource.to_s] = @resources.length - 1
66
+ end
67
+ end
68
+
69
+ def push(*args)
70
+ args.flatten.each do |a|
71
+ is_chef_resource(a)
72
+ @resources.push(a)
73
+ @resources_by_name[a.to_s] = @resources.length - 1
74
+ end
75
+ end
76
+
77
+ def each
78
+ @resources.each do |r|
79
+ yield r
80
+ end
81
+ end
82
+
83
+ def execute_each_resource
84
+ @resources.each_with_index do |r, idx|
85
+ @insert_after_idx = idx
86
+ yield r
87
+ end
88
+ end
89
+
90
+ def each_index
91
+ @resources.each_index do |i|
92
+ yield i
93
+ end
94
+ end
95
+
96
+ def lookup(resource)
97
+ lookup_by = nil
98
+ if resource.kind_of?(Chef::Resource)
99
+ lookup_by = resource.to_s
100
+ elsif resource.kind_of?(String)
101
+ lookup_by = resource
102
+ else
103
+ raise ArgumentError, "Must pass a Chef::Resource or String to lookup"
104
+ end
105
+ res = @resources_by_name[lookup_by]
106
+ unless res
107
+ raise ArgumentError, "Cannot find a resource matching #{lookup_by} (did you define it first?)"
108
+ end
109
+ @resources[res]
110
+ end
111
+
112
+ # Find existing resources by searching the list of existing resources. Possible
113
+ # forms are:
114
+ #
115
+ # resources(:file => "foobar")
116
+ # resources(:file => [ "foobar", "baz" ])
117
+ # resources("file[foobar]", "file[baz]")
118
+ # resources("file[foobar,baz]")
119
+ #
120
+ # Returns the matching resource, or an Array of matching resources.
121
+ #
122
+ # Raises an ArgumentError if you feed it bad lookup information
123
+ # Raises a Runtime Error if it can't find the resources you are looking for.
124
+ def resources(*args)
125
+ results = Array.new
126
+ args.each do |arg|
127
+ case arg
128
+ when Hash
129
+ results << find_resource_by_hash(arg)
130
+ when String
131
+ results << find_resource_by_string(arg)
132
+ else
133
+ raise ArgumentError, "resources takes arguments as a hash or strings!"
134
+ end
135
+ end
136
+ flat_results = results.flatten
137
+ flat_results.length == 1 ? flat_results[0] : flat_results
138
+ end
139
+
140
+ # Serialize this object as a hash
141
+ def to_json(*a)
142
+ instance_vars = Hash.new
143
+ self.instance_variables.each do |iv|
144
+ instance_vars[iv] = self.instance_variable_get(iv)
145
+ end
146
+ results = {
147
+ 'json_class' => self.class.name,
148
+ 'instance_vars' => instance_vars
149
+ }
150
+ results.to_json(*a)
151
+ end
152
+
153
+ def self.json_create(o)
154
+ collection = self.new()
155
+ o["instance_vars"].each do |k,v|
156
+ collection.instance_variable_set(k.to_sym, v)
157
+ end
158
+ collection
159
+ end
160
+
161
+ private
162
+
163
+ def find_resource_by_hash(arg)
164
+ results = Array.new
165
+ arg.each do |resource_name, name_list|
166
+ names = name_list.kind_of?(Array) ? name_list : [ name_list ]
167
+ names.each do |name|
168
+ res_name = "#{resource_name.to_s}[#{name}]"
169
+ results << lookup(res_name)
170
+ end
171
+ end
172
+ return results
173
+ end
174
+
175
+ def find_resource_by_string(arg)
176
+ results = Array.new
177
+ case arg
178
+ when /^(.+)\[(.+?),(.+)\]$/
179
+ resource_type = $1
180
+ arg =~ /^.+\[(.+)\]$/
181
+ resource_list = $1
182
+ resource_list.split(",").each do |name|
183
+ resource_name = "#{resource_type}[#{name}]"
184
+ results << lookup(resource_name)
185
+ end
186
+ when /^(.+)\[(.+)\]$/
187
+ resource_type = $1
188
+ name = $2
189
+ resource_name = "#{resource_type}[#{name}]"
190
+ results << lookup(resource_name)
191
+ else
192
+ raise ArgumentError, "You must have a string like resource_type[name]!"
193
+ end
194
+ return results
195
+ end
196
+
197
+ def is_chef_resource(arg)
198
+ unless arg.kind_of?(Chef::Resource)
199
+ raise ArgumentError, "Members must be Chef::Resource's"
200
+ end
201
+ true
202
+ end
203
+ end
204
+ end
@@ -0,0 +1,67 @@
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/mixin/from_file'
20
+ require 'chef/mixin/params_validate'
21
+
22
+ class Chef
23
+ class ResourceDefinition
24
+
25
+ include Chef::Mixin::FromFile
26
+ include Chef::Mixin::ParamsValidate
27
+
28
+ attr_accessor :name, :params, :recipe, :node
29
+
30
+ def initialize(node=nil)
31
+ @name = nil
32
+ @params = Hash.new
33
+ @recipe = nil
34
+ @node = node
35
+ end
36
+
37
+ def define(resource_name, prototype_params=nil, &block)
38
+ unless resource_name.kind_of?(Symbol)
39
+ raise ArgumentError, "You must use a symbol when defining a new resource!"
40
+ end
41
+ @name = resource_name
42
+ if prototype_params
43
+ unless prototype_params.kind_of?(Hash)
44
+ raise ArgumentError, "You must pass a hash as the prototype parameters for a definition."
45
+ end
46
+ @params = prototype_params
47
+ end
48
+ if Kernel.block_given?
49
+ @recipe = block
50
+ else
51
+ raise ArgumentError, "You must pass a block to a definition."
52
+ end
53
+ true
54
+ end
55
+
56
+ # When we do the resource definition, we're really just setting new values for
57
+ # the paramaters we prototyped at the top. This method missing is as simple as
58
+ # it gets.
59
+ def method_missing(symbol, *args)
60
+ @params[symbol] = args.length == 1 ? args[0] : args
61
+ end
62
+
63
+ def to_s
64
+ "#{name.to_s}"
65
+ end
66
+ end
67
+ end
data/lib/chef/rest.rb ADDED
@@ -0,0 +1,238 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Author:: Thom May (<thom@clearairturbulence.org>)
4
+ # Copyright:: Copyright (c) 2008 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/params_validate'
21
+ require 'chef/openid_registration'
22
+ require 'net/https'
23
+ require 'uri'
24
+ require 'json'
25
+ require 'tempfile'
26
+ require 'singleton'
27
+
28
+ class Chef
29
+ class REST
30
+
31
+ class CookieJar < Hash
32
+ include Singleton
33
+ end
34
+
35
+ attr_accessor :url, :cookies
36
+
37
+ def initialize(url)
38
+ @url = url
39
+ @cookies = CookieJar.instance
40
+ end
41
+
42
+ # Register for an OpenID
43
+ def register(user, pass, validation_token=nil)
44
+ Chef::Log.debug("Registering #{user} for an openid")
45
+ registration = nil
46
+ begin
47
+ registration = get_rest("registrations/#{user}")
48
+ rescue Net::HTTPServerException => e
49
+ unless e.message =~ /^404/
50
+ raise e
51
+ end
52
+ end
53
+ unless registration
54
+ post_rest(
55
+ "registrations",
56
+ {
57
+ :id => user,
58
+ :password => pass,
59
+ :validation_token => validation_token
60
+ }
61
+ )
62
+ end
63
+ end
64
+
65
+ # Authenticate
66
+ def authenticate(user, pass)
67
+ Chef::Log.debug("Authenticating #{user} via openid")
68
+ response = post_rest('openid/consumer/start', {
69
+ "openid_identifier" => "#{Chef::Config[:openid_url]}/openid/server/node/#{user}",
70
+ "submit" => "Verify"
71
+ })
72
+ post_rest(
73
+ "#{Chef::Config[:openid_url]}#{response["action"]}",
74
+ { "password" => pass }
75
+ )
76
+ end
77
+
78
+ # Send an HTTP GET request to the path
79
+ #
80
+ # === Parameters
81
+ # path:: The path to GET
82
+ # raw:: Whether you want the raw body returned, or JSON inflated. Defaults
83
+ # to JSON inflated.
84
+ def get_rest(path, raw=false)
85
+ run_request(:GET, create_url(path), false, 10, raw)
86
+ end
87
+
88
+ # Send an HTTP DELETE request to the path
89
+ def delete_rest(path)
90
+ run_request(:DELETE, create_url(path))
91
+ end
92
+
93
+ # Send an HTTP POST request to the path
94
+ def post_rest(path, json)
95
+ run_request(:POST, create_url(path), json)
96
+ end
97
+
98
+ # Send an HTTP PUT request to the path
99
+ def put_rest(path, json)
100
+ run_request(:PUT, create_url(path), json)
101
+ end
102
+
103
+ def create_url(path)
104
+ if path =~ /^(http|https):\/\//
105
+ URI.parse(path)
106
+ else
107
+ URI.parse("#{@url}/#{path}")
108
+ end
109
+ end
110
+
111
+ # Actually run an HTTP request. First argument is the HTTP method,
112
+ # which should be one of :GET, :PUT, :POST or :DELETE. Next is the
113
+ # URL, then an object to include in the body (which will be converted with
114
+ # .to_json) and finally, the limit of HTTP Redirects to follow (10).
115
+ #
116
+ # Typically, you won't use this method -- instead, you'll use one of
117
+ # the helper methods (get_rest, post_rest, etc.)
118
+ #
119
+ # Will return the body of the response on success.
120
+ def run_request(method, url, data=false, limit=10, raw=false)
121
+ http_retry_delay = Chef::Config[:http_retry_delay]
122
+ http_retry_count = Chef::Config[:http_retry_count]
123
+
124
+ raise ArgumentError, 'HTTP redirect too deep' if limit == 0
125
+
126
+ http = Net::HTTP.new(url.host, url.port)
127
+ if url.scheme == "https"
128
+ http.use_ssl = true
129
+ if Chef::Config[:ssl_verify_mode] == :verify_none
130
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
131
+ end
132
+ if File.exists?(Chef::Config[:ssl_client_cert])
133
+ http.cert = OpenSSL::X509::Certificate.new(File.read(Chef::Config[:ssl_client_cert]))
134
+ http.key = OpenSSL::PKey::RSA.new(File.read(Chef::Config[:ssl_client_key]))
135
+ end
136
+ end
137
+ http.read_timeout = Chef::Config[:rest_timeout]
138
+ headers = Hash.new
139
+ unless raw
140
+ headers = {
141
+ 'Accept' => "application/json",
142
+ }
143
+ end
144
+ if @cookies.has_key?("#{url.host}:#{url.port}")
145
+ headers['Cookie'] = @cookies["#{url.host}:#{url.port}"]
146
+ end
147
+ req = nil
148
+ case method
149
+ when :GET
150
+ req_path = "#{url.path}"
151
+ req_path << "?#{url.query}" if url.query
152
+ req = Net::HTTP::Get.new(req_path, headers)
153
+ when :POST
154
+ headers["Content-Type"] = 'application/json' if data
155
+ req = Net::HTTP::Post.new(url.path, headers)
156
+ req.body = data.to_json if data
157
+ when :PUT
158
+ headers["Content-Type"] = 'application/json' if data
159
+ req = Net::HTTP::Put.new(url.path, headers)
160
+ req.body = data.to_json if data
161
+ when :DELETE
162
+ req_path = "#{url.path}"
163
+ req_path << "?#{url.query}" if url.query
164
+ req = Net::HTTP::Delete.new(req_path, headers)
165
+ else
166
+ raise ArgumentError, "You must provide :GET, :PUT, :POST or :DELETE as the method"
167
+ end
168
+
169
+ # Optionally handle HTTP Basic Authentication
170
+ req.basic_auth(url.user, url.password) if url.user
171
+
172
+ Chef::Log.debug("Sending HTTP Request via #{req.method} to #{req.path}")
173
+ res = nil
174
+ tf = nil
175
+ http_retries = 1
176
+
177
+ begin
178
+ res = http.request(req) do |response|
179
+ if raw
180
+ tf = Tempfile.new("chef-rest")
181
+ # Stolen from http://www.ruby-forum.com/topic/166423
182
+ # Kudos to _why!
183
+ size, total = 0, response.header['Content-Length'].to_i
184
+ response.read_body do |chunk|
185
+ tf.write(chunk)
186
+ size += chunk.size
187
+ if size == 0
188
+ Chef::Log.debug("#{req.path} done (0 length file)")
189
+ elsif total == 0
190
+ Chef::Log.debug("#{req.path} (zero content length)")
191
+ else
192
+ Chef::Log.debug("#{req.path}" + " %d%% done (%d of %d)" % [(size * 100) / total, size, total])
193
+ end
194
+ end
195
+ tf.close
196
+ tf
197
+ else
198
+ response.read_body
199
+ end
200
+ response
201
+ end
202
+ rescue Errno::ECONNREFUSED
203
+ Chef::Log.error("Connection refused connecting to #{url.host}:#{url.port} for #{req.path} #{http_retries}/#{http_retry_count}")
204
+ sleep(http_retry_delay)
205
+ retry if (http_retries += 1) < http_retry_count
206
+ raise Errno::ECONNREFUSED, "Connection refused connecting to #{url.host}:#{url.port} for #{req.path}, giving up"
207
+ rescue Timeout::Error
208
+ Chef::Log.error("Timeout connecting to #{url.host}:#{url.port} for #{req.path}, retry #{http_retries}/#{http_retry_count}")
209
+ sleep(http_retry_delay)
210
+ retry if (http_retries += 1) < http_retry_count
211
+ raise Timeout::Error, "Timeout connecting to #{url.host}:#{url.port} for #{req.path}, giving up"
212
+ end
213
+
214
+ if res.kind_of?(Net::HTTPSuccess)
215
+ if res['set-cookie']
216
+ @cookies["#{url.host}:#{url.port}"] = res['set-cookie']
217
+ end
218
+ if res['content-type'] =~ /json/
219
+ JSON.parse(res.body)
220
+ else
221
+ if raw
222
+ tf
223
+ else
224
+ res.body
225
+ end
226
+ end
227
+ elsif res.kind_of?(Net::HTTPFound) or res.kind_of?(Net::HTTPMovedPermanently)
228
+ if res['set-cookie']
229
+ @cookies["#{url.host}:#{url.port}"] = res['set-cookie']
230
+ end
231
+ run_request(:GET, create_url(res['location']), false, limit - 1, raw)
232
+ else
233
+ res.error!
234
+ end
235
+ end
236
+
237
+ end
238
+ end