knife-preflight 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/chef/knife/preflight.rb +97 -123
  3. metadata +4 -8
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OWFjOWVhZWY2ZmEzM2ZlNDU1M2MyMDBhYmQ0NDFiZDczZDc4M2YzOQ==
5
+ data.tar.gz: !binary |-
6
+ NzEyY2NiMDQ4ZTg0OGFhNzI2ZDdkYzJmZjBhMTU4MTRiNDdkMzAwNg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ Yjc5OWExNDMwYzE0MjcwZDIyOTYwNmNjN2JlODljZWY1MWMzZGJhOGI4ZmZh
10
+ MzE5NzlmOWU2NDExZWM2MTNjMmI1ZDUzYWMyOWIzNzIwYjkwMWQ3YmJhYmEz
11
+ NDNmOWE5YjVkOGM3YTMwZmZiNmZlZDVkNjhkOGRiNzYwNmZlYWM=
12
+ data.tar.gz: !binary |-
13
+ Yjk0MWQ5NDc0YzM2N2RkYTgwYzc0ZTY0YjFhNmZlOGYyMjI4ZmMyMWJiODFj
14
+ ODg5ZWEzZWU5YzZlY2ZmNTI4Yjg1MjgwZTJmOGE1OTkxYjQ3YmU3OThlNDU5
15
+ Yjc3OTI0ZDczM2QzMzViMTY4MTRmZWRkOTRiYWZiMWRiYTllNjU=
@@ -13,154 +13,128 @@ require 'chef/knife/core/node_presenter'
13
13
  module KnifePreflight
14
14
  class Preflight < Chef::Knife
15
15
 
16
- deps do
17
- require 'chef/node'
18
- require 'chef/environment'
19
- require 'chef/api_client'
20
- require 'chef/search/query'
21
- end
16
+ deps do
17
+ require 'chef/node'
18
+ require 'chef/environment'
19
+ require 'chef/api_client'
20
+ require 'chef/search/query'
21
+ end
22
22
 
23
- include Chef::Knife::Core::NodeFormattingOptions
23
+ include Chef::Knife::Core::NodeFormattingOptions
24
24
 
25
- banner "knife preflight QUERY (options)"
25
+ banner "knife preflight QUERY (options)"
26
26
 
27
- option :sort,
28
- :short => "-o SORT",
29
- :long => "--sort SORT",
30
- :description => "The order to sort the results in",
31
- :default => nil
27
+ option :sort,
28
+ :short => "-o SORT",
29
+ :long => "--sort SORT",
30
+ :description => "The order to sort the results in",
31
+ :default => nil
32
32
 
33
- option :start,
34
- :short => "-b ROW",
35
- :long => "--start ROW",
36
- :description => "The row to start returning results at",
37
- :default => 0,
38
- :proc => lambda { |i| i.to_i }
33
+ option :start,
34
+ :short => "-b ROW",
35
+ :long => "--start ROW",
36
+ :description => "The row to start returning results at",
37
+ :default => 0,
38
+ :proc => lambda { |i| i.to_i }
39
39
 
40
- option :rows,
41
- :short => "-R INT",
42
- :long => "--rows INT",
43
- :description => "The number of rows to return",
44
- :default => 1000,
45
- :proc => lambda { |i| i.to_i }
40
+ option :rows,
41
+ :short => "-R INT",
42
+ :long => "--rows INT",
43
+ :description => "The number of rows to return",
44
+ :default => 1000,
45
+ :proc => lambda { |i| i.to_i }
46
46
 
47
47
 
48
- def run
49
- if config[:query] && @name_args[0]
50
- ui.error "please specify query as an argument or an option via -q, not both"
51
- ui.msg opt_parser
52
- exit 1
53
- end
54
- raw_query = config[:query] || @name_args[0]
55
- if !raw_query || raw_query.empty?
56
- ui.error "no query specified"
57
- ui.msg opt_parser
58
- exit 1
59
- end
48
+ def run
49
+ if config[:query] && @name_args[0]
50
+ ui.error "please specify query as an argument or an option via -q, not both"
51
+ ui.msg opt_parser
52
+ exit 1
53
+ end
54
+ raw_query = config[:query] || @name_args[0]
55
+ if !raw_query || raw_query.empty?
56
+ ui.error "no query specified"
57
+ ui.msg opt_parser
58
+ exit 1
59
+ end
60
+
61
+ result_count_nodes = perform_query(raw_query, 'node')
62
+
63
+ result_count_roles = perform_query(raw_query, 'role')
60
64
 
65
+ ui.msg("Found #{result_count_nodes} nodes and #{result_count_roles} roles using the specified search criteria")
66
+ end
67
+
68
+ def perform_query(raw_query, type='node')
69
+ q = Chef::Search::Query.new
70
+
71
+ if type == 'node'
61
72
  cookbook = raw_query.split("::").first
62
73
  unconstrained_env_search = Chef::Search::Query.new
63
74
  unconstrained_envs = unconstrained_env_search.search('environment', "NOT cookbook_versions:#{cookbook}").first.map{|e|e.name}
75
+ end
64
76
 
65
- q_nodes = Chef::Search::Query.new
77
+ # strip default if it exists to simplify logic
78
+ raw_query = raw_query.sub("::default", "")
79
+ escaped_query = raw_query.sub( "::", "\\:\\:")
66
80
 
67
- escaped_query = raw_query.sub( "::", "\\:\\:")
68
-
69
- if !raw_query.include? "::"
70
- node_query = "recipes:*#{escaped_query} OR recipes:*#{escaped_query}\\:\\:default"
71
- ui.msg("Searching for nodes containing #{raw_query} OR #{raw_query}::default in their expanded run_list...\n")
72
- elsif raw_query.include? "::default"
73
- node_query = "recipes:*#{escaped_query} OR recipes:*#{escaped_query.gsub( "\\:\\:default","")}"
74
- ui.msg("Searching for nodes containing #{raw_query} OR #{raw_query.gsub( "::default","")} in their expanded run_list...\n")
81
+ if !raw_query.include? "::"
82
+ if type == 'node'
83
+ search_query = "recipes:*#{escaped_query} OR recipes:*#{escaped_query}\\:\\:default"
84
+ search_query += " OR " + search_query.gsub("recipes", "last_seen_recipes")
75
85
  else
76
- node_query = "recipes:*#{escaped_query}"
77
- ui.msg("Searching for nodes containing #{raw_query} in their expanded run_list...\n")
86
+ search_query = "run_list:recipe\\[#{escaped_query}\\] OR run_list:recipe\\[#{escaped_query}\\:\\:default\\]"
78
87
  end
79
- query_nodes = URI.escape(node_query, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
80
-
81
- result_items_nodes = []
82
- result_count_nodes = 0
83
-
84
- rows = config[:rows]
85
- start = config[:start]
86
- begin
87
- q_nodes.search('node', query_nodes, config[:sort], start, rows) do |node_item|
88
- formatted_item_node = format_for_display(node_item)
89
- if formatted_item_node.respond_to?(:has_key?) && !formatted_item_node.has_key?('id')
90
- formatted_item_node.normal['id'] = node_item.has_key?('id') ? node_item['id'] : node_item.name
91
- end
92
- result_items_nodes << formatted_item_node
93
- result_count_nodes += 1
94
- end
95
- rescue Net::HTTPServerException => e
96
- msg = Chef::JSONCompat.from_json(e.response.body)["error"].first
97
- ui.error("knife preflight failed: #{msg}")
98
- exit 1
99
- end
100
-
101
- if ui.interchange?
102
- output({:results => result_count_nodes, :rows => result_items_nodes})
88
+ ui.msg("Searching for #{type}s containing #{raw_query} OR #{raw_query}::default in their expanded run_list or added via include_recipe...\n")
89
+ else
90
+ if type == 'node'
91
+ search_query = "recipes:*#{escaped_query}"
92
+ search_query += " OR " + search_query.gsub("recipes", "last_seen_recipes")
103
93
  else
104
- ui.msg "#{result_count_nodes} Nodes found"
105
- ui.msg("\n")
106
- result_items_nodes.each do |item|
107
- output("#{item.name}#{unconstrained_envs.include?(item.chef_environment) ? " - in environment '#{item.chef_environment}', no version constraint for '#{cookbook}' cookbook" : nil}")
108
- end
94
+ search_query = "run_list:recipe\\[#{escaped_query}\\]"
109
95
  end
96
+ ui.msg("Searching for #{type}s containing #{raw_query} in their expanded run_list or added via include_recipe......\n")
97
+ end
110
98
 
111
- ui.msg("\n")
112
- ui.msg("\n")
113
-
99
+ query = URI.escape(search_query, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
114
100
 
115
- q_roles = Chef::Search::Query.new
116
-
117
- if !raw_query.include? "::"
118
- role_query = "run_list:recipe\\[#{escaped_query}\\] OR run_list:recipe\\[#{escaped_query}\\:\\:default\\]"
119
- ui.msg("Searching for roles containing #{raw_query} OR #{raw_query}::default in their expanded run_list...\n")
120
- elsif raw_query.include? "::default"
121
- role_query = "run_list:recipe\\[#{escaped_query}\\] OR run_list:recipe\\[#{escaped_query.gsub( "\\:\\:default","")}\\]"
122
- ui.msg("Searching for roles containing #{raw_query} OR #{raw_query.gsub( "::default","")} in their expanded run_list...\n")
123
- else
124
- role_query = "run_list:recipe\\[#{escaped_query}\\]"
125
- ui.msg("Searching for roles containing #{raw_query} in their expanded run_list...\n")
126
- end
101
+ result_items = []
102
+ result_count = 0
127
103
 
128
- query_roles = URI.escape(role_query,
129
- Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
130
-
131
- result_items_roles = []
132
- result_count_roles = 0
133
-
134
- rows = config[:rows]
135
- start = config[:start]
136
- begin
137
- q_roles.search('role', query_roles, config[:sort], start, rows) do |role_item|
138
- formatted_item_role = format_for_display(role_item)
139
- if formatted_item_role.respond_to?(:has_key?) && !formatted_item_role.has_key?('id')
140
- formatted_item_role['id'] = role_item.has_key?('id') ? role_item['id'] : role_item.name
141
- end
142
- result_items_roles << formatted_item_role
143
- result_count_roles += 1
104
+ rows = config[:rows]
105
+ start = config[:start]
106
+ begin
107
+ q.search(type, query, config[:sort], start, rows) do |item|
108
+ formatted_item = format_for_display(item)
109
+ if formatted_item.respond_to?(:has_key?) && !formatted_item.has_key?('id')
110
+ formatted_item.normal['id'] = item.has_key?('id') ? item['id'] : item.name
144
111
  end
145
- rescue Net::HTTPServerException => e
146
- msg = Chef::JSONCompat.from_json(e.response.body)["error"].first
147
- ui.error("knife preflight failed: #{msg}")
148
- exit 1
112
+ result_items << formatted_item
113
+ result_count += 1
149
114
  end
115
+ rescue Net::HTTPServerException => e
116
+ msg = Chef::JSONCompat.from_json(e.response.body)["error"].first
117
+ ui.error("knife preflight failed: #{msg}")
118
+ exit 1
119
+ end
150
120
 
151
- if ui.interchange?
152
- output({:results => result_count_roles, :rows => result_items_roles})
153
- else
154
- ui.msg "#{result_count_roles} Roles found"
155
- ui.msg("\n")
156
- result_items_roles.each do |role_item|
157
- output(role_item.name)
121
+ if ui.interchange?
122
+ output({:results => result_count, :rows => result_items})
123
+ else
124
+ ui.msg "#{result_count} #{type.capitalize}s found"
125
+ ui.msg("\n")
126
+ result_items.each do |item|
127
+ if item.instance_of?(Chef::Node)
128
+ output("#{item.name}#{unconstrained_envs.include?(item.chef_environment) ? " - in environment #{item.chef_environment}, no version constraint for #{cookbook} cookbook" : nil}")
129
+ else
130
+ output(item.name)
158
131
  end
159
132
  end
160
-
161
- ui.msg("\n")
162
- ui.msg("\n")
163
- ui.msg("Found #{result_count_nodes} nodes and #{result_count_roles} roles using the specified search criteria")
164
133
  end
134
+
135
+ ui.msg("\n")
136
+ ui.msg("\n")
137
+ return result_count
165
138
  end
166
139
  end
140
+ end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-preflight
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
5
- prerelease:
4
+ version: 0.1.7
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jon Cowie
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-01-25 00:00:00.000000000 Z
11
+ date: 2014-10-13 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: chef
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -38,26 +35,25 @@ files:
38
35
  - lib/chef/knife/preflight.rb
39
36
  homepage: https://github.com/jonlives/knife-preflight
40
37
  licenses: []
38
+ metadata: {}
41
39
  post_install_message:
42
40
  rdoc_options:
43
41
  - --charset=UTF-8
44
42
  require_paths:
45
43
  - lib
46
44
  required_ruby_version: !ruby/object:Gem::Requirement
47
- none: false
48
45
  requirements:
49
46
  - - ! '>='
50
47
  - !ruby/object:Gem::Version
51
48
  version: '0'
52
49
  required_rubygems_version: !ruby/object:Gem::Requirement
53
- none: false
54
50
  requirements:
55
51
  - - ! '>='
56
52
  - !ruby/object:Gem::Version
57
53
  version: '0'
58
54
  requirements: []
59
55
  rubyforge_project: knife-preflight
60
- rubygems_version: 1.8.24
56
+ rubygems_version: 2.4.1
61
57
  signing_key:
62
58
  specification_version: 2
63
59
  summary: Knife plugin for checking what your cookbook changes will affect