knife-essentials 0.5 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,98 @@
1
+ class Chef
2
+ class Knife
3
+ class Raw < Chef::Knife
4
+ banner "raw REQUEST_PATH"
5
+
6
+ option :method,
7
+ :long => '--method METHOD',
8
+ :short => '-m METHOD',
9
+ :default => "GET",
10
+ :description => "RequestSpecifies the local repository layout. Values: default or full"
11
+
12
+ option :input,
13
+ :long => '--input FILE',
14
+ :short => '-i FILE',
15
+ :description => "Name of file to use for PUT or POST"
16
+
17
+ def run
18
+ if name_args.length == 0
19
+ show_usage
20
+ ui.fatal("You must provide the path you want to hit on the server")
21
+ exit(1)
22
+ elsif name_args.length > 1
23
+ show_usage
24
+ ui.fatal("Only one path accepted for knife raw")
25
+ exit(1)
26
+ end
27
+
28
+ path = name_args[0]
29
+ data = false
30
+ if config[:input]
31
+ data = IO.read(config[:input])
32
+ end
33
+ chef_rest = Chef::REST.new(Chef::Config[:chef_server_url])
34
+ puts api_request(chef_rest, config[:method].to_sym, chef_rest.create_url(name_args[0]), {}, data)
35
+ end
36
+
37
+ ACCEPT_ENCODING = "Accept-Encoding".freeze
38
+ ENCODING_GZIP_DEFLATE = "gzip;q=1.0,deflate;q=0.6,identity;q=0.3".freeze
39
+
40
+ def redirected_to(response)
41
+ return nil unless response.kind_of?(Net::HTTPRedirection)
42
+ # Net::HTTPNotModified is undesired subclass of Net::HTTPRedirection so test for this
43
+ return nil if response.kind_of?(Net::HTTPNotModified)
44
+ response['location']
45
+ end
46
+
47
+ def api_request(chef_rest, method, url, headers={}, data=false)
48
+ json_body = data
49
+ # json_body = data ? Chef::JSONCompat.to_json(data) : nil
50
+ # Force encoding to binary to fix SSL related EOFErrors
51
+ # cf. http://tickets.opscode.com/browse/CHEF-2363
52
+ # http://redmine.ruby-lang.org/issues/5233
53
+ # json_body.force_encoding(Encoding::BINARY) if json_body.respond_to?(:force_encoding)
54
+ headers = build_headers(chef_rest, method, url, headers, json_body)
55
+
56
+ chef_rest.retriable_rest_request(method, url, json_body, headers) do |rest_request|
57
+ response = rest_request.call {|r| r.read_body}
58
+
59
+ # response_body = chef_rest.decompress_body(response)
60
+ response_body = response.body
61
+
62
+ if response.kind_of?(Net::HTTPSuccess)
63
+ # This is where we differ from Chef::REST: we return the raw body always.
64
+ response_body
65
+ elsif redirect_location = redirected_to(response)
66
+ raise "Redirected to #{create_url(redirect_location)}"
67
+ follow_redirect {api_request(:GET, create_url(redirect_location))}
68
+ else
69
+ # have to decompress the body before making an exception for it. But the body could be nil.
70
+ # response.body.replace(decompress_body(response)) if response.body.respond_to?(:replace)
71
+
72
+ if response['content-type'] =~ /json/
73
+ exception = response_body
74
+ msg = "HTTP Request Returned #{response.code} #{response.message}: "
75
+ msg << (exception["error"].respond_to?(:join) ? exception["error"].join(", ") : exception["error"].to_s)
76
+ Chef::Log.info(msg)
77
+ end
78
+ puts response.body
79
+ response.error!
80
+ end
81
+ end
82
+ end
83
+
84
+ def build_headers(chef_rest, method, url, headers={}, json_body=false, raw=false)
85
+ # headers = @default_headers.merge(headers)
86
+ #headers['Accept'] = "application/json" unless raw
87
+ headers['Accept'] = "application/json" unless raw
88
+ headers["Content-Type"] = 'application/json' if json_body
89
+ headers['Content-Length'] = json_body.bytesize.to_s if json_body
90
+ # headers[Chef::REST::RESTRequest::ACCEPT_ENCODING] = Chef::REST::RESTRequest::ENCODING_GZIP_DEFLATE
91
+ headers.merge!(chef_rest.authentication_headers(method, url, json_body)) if chef_rest.sign_requests?
92
+ headers.merge!(Chef::Config[:custom_http_headers]) if Chef::Config[:custom_http_headers]
93
+ headers
94
+ end
95
+ end
96
+ end
97
+ end
98
+
@@ -1,4 +1,4 @@
1
1
  module ChefFS
2
- VERSION = "0.5"
2
+ VERSION = "0.5.1"
3
3
  end
4
4
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-essentials
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.5'
4
+ version: 0.5.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-10 00:00:00.000000000Z
12
+ date: 2012-05-11 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Universal knife verbs that work with your Chef repository
15
15
  email: jkeiser@opscode.com
@@ -25,6 +25,7 @@ files:
25
25
  - lib/chef/knife/diff.rb
26
26
  - lib/chef/knife/download.rb
27
27
  - lib/chef/knife/list.rb
28
+ - lib/chef/knife/raw.rb
28
29
  - lib/chef/knife/show.rb
29
30
  - lib/chef/knife/upload.rb
30
31
  - lib/chef_fs/command_line.rb