knife-preflight 0.1.0

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.
Files changed (3) hide show
  1. data/README.md +16 -0
  2. data/lib/chef/knife/preflight.rb +147 -0
  3. metadata +79 -0
@@ -0,0 +1,16 @@
1
+ # knife-preflight
2
+
3
+ A preflight plugin for Chef::Knife which lets you see which nodes and roles use a particular cookbook before you upload it.
4
+
5
+ ## Preface
6
+
7
+ Searches the expanded run_lists of all nodes along with the run_list of all roles for the specified cookbook
8
+
9
+ ## What it does
10
+
11
+ knife preflight apache2::default
12
+ will return a list of all nodes containing this cookbook in their expanded run_list followed by all nodes with the cookbook in their expanded run_list
13
+
14
+ ## Notes
15
+ This will currently only search for cookbooks. It won't work if you specify a role on the command line because I've tried to avoid duplication of functionality which knife makes obvious.
16
+
@@ -0,0 +1,147 @@
1
+ #
2
+ # Author:: Jon Cowie (<jonlives@gmail.com>)
3
+ # Copyright:: Copyright (c) 2011 Jon Cowie
4
+ # License:: GPL
5
+
6
+ # Based on the knife chef plugin by:
7
+ # Adam Jacob (<adam@opscode.com>)
8
+ # Copyright:: Copyright (c) 2009 Opscode, Inc.
9
+
10
+ require 'chef/knife'
11
+ require 'chef/knife/core/node_presenter'
12
+
13
+ module KnifePreflight
14
+ class Preflight < Chef::Knife
15
+
16
+ deps do
17
+ require 'chef/node'
18
+ require 'chef/environment'
19
+ require 'chef/api_client'
20
+ require 'chef/search/query'
21
+ end
22
+
23
+ include Chef::Knife::Core::NodeFormattingOptions
24
+
25
+ banner "knife preflight QUERY (options)"
26
+
27
+ option :sort,
28
+ :short => "-o SORT",
29
+ :long => "--sort SORT",
30
+ :description => "The order to sort the results in",
31
+ :default => nil
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 }
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 }
46
+
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
60
+
61
+ q_nodes = Chef::Search::Query.new
62
+
63
+
64
+ escaped_query = raw_query.sub( "::", "\\:\\:")
65
+ node_query = "recipes:*#{escaped_query}"
66
+ query_nodes = URI.escape(node_query,
67
+ Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
68
+
69
+ result_items_nodes = []
70
+ result_count_nodes = 0
71
+
72
+ ui.msg("Searching for nodes containing #{raw_query} in their expanded run_list...\n")
73
+
74
+ rows = config[:rows]
75
+ start = config[:start]
76
+ begin
77
+ q_nodes.search('node', query_nodes, config[:sort], start, rows) do |node_item|
78
+ formatted_item_node = format_for_display(node_item)
79
+ if formatted_item_node.respond_to?(:has_key?) && !formatted_item_node.has_key?('id')
80
+ formatted_item_node['id'] = node_item.has_key?('id') ? node_item['id'] : node_item.name
81
+ end
82
+ result_items_nodes << formatted_item_node
83
+ result_count_nodes += 1
84
+ end
85
+ rescue Net::HTTPServerException => e
86
+ msg = Chef::JSONCompat.from_json(e.response.body)["error"].first
87
+ ui.error("knife preflight failed: #{msg}")
88
+ exit 1
89
+ end
90
+
91
+ if ui.interchange?
92
+ output({:results => result_count_nodes, :rows => result_items_nodes})
93
+ else
94
+ ui.msg "#{result_count_nodes} Nodes found"
95
+ ui.msg("\n")
96
+ result_items_nodes.each do |item|
97
+ output(item.name)
98
+ end
99
+ end
100
+
101
+ ui.msg("\n")
102
+ ui.msg("\n")
103
+
104
+
105
+ q_roles = Chef::Search::Query.new
106
+ role_query = "run_list:recipe\\[#{escaped_query}\\]"
107
+ query_roles = URI.escape(role_query,
108
+ Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
109
+
110
+ result_items_roles = []
111
+ result_count_roles = 0
112
+
113
+ ui.msg("Searching for roles containing #{raw_query} in their run_list...\n")
114
+
115
+ rows = config[:rows]
116
+ start = config[:start]
117
+ begin
118
+ q_roles.search('role', query_roles, config[:sort], start, rows) do |role_item|
119
+ formatted_item_role = format_for_display(role_item)
120
+ if formatted_item_role.respond_to?(:has_key?) && !formatted_item_role.has_key?('id')
121
+ formatted_item_role['id'] = role_item.has_key?('id') ? role_item['id'] : role_item.name
122
+ end
123
+ result_items_roles << formatted_item_role
124
+ result_count_roles += 1
125
+ end
126
+ rescue Net::HTTPServerException => e
127
+ msg = Chef::JSONCompat.from_json(e.response.body)["error"].first
128
+ ui.error("knife preflight failed: #{msg}")
129
+ exit 1
130
+ end
131
+
132
+ if ui.interchange?
133
+ output({:results => result_count_roles, :rows => result_items_roles})
134
+ else
135
+ ui.msg "#{result_count_roles} Roles found"
136
+ ui.msg("\n")
137
+ result_items_roles.each do |role_item|
138
+ output(role_item.name)
139
+ end
140
+ end
141
+
142
+ ui.msg("\n")
143
+ ui.msg("\n")
144
+ ui.msg("Found #{result_count_nodes} nodes and #{result_count_roles} roles using the specified search criteria")
145
+ end
146
+ end
147
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-preflight
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Jon Cowie
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-01-31 00:00:00 +00:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: chef
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 10
31
+ - 4
32
+ version: 0.10.4
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: Knife plugin for checking what your cookbook changes will affect
36
+ email: jonlives@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.md
43
+ files:
44
+ - README.md
45
+ - lib/chef/knife/preflight.rb
46
+ has_rdoc: true
47
+ homepage: https://github.com/jonlives/knife-preflight
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project: knife-preflight
74
+ rubygems_version: 1.3.7
75
+ signing_key:
76
+ specification_version: 2
77
+ summary: Knife plugin for checking what your cookbook changes will affect
78
+ test_files: []
79
+