knife-crawl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # knife-crawl
2
+
3
+ A plugin for Chef::Knife which displays the roles that are included recursively within a role and optionally displays all the roles that include it.
4
+
5
+ ## Usage
6
+
7
+ Supply a role name to get a dump of its hierarchy, pass -i for the roles that also include it
8
+
9
+ ```
10
+ % knife crawl VMDevStack -i ✹
11
+
12
+ VMDevStack child hierarchy:
13
+ * VMDevStack
14
+ * SharedDevStack
15
+
16
+ VMDevStack is included in the following roles:
17
+ * VMBase
18
+ ```
19
+
20
+ ## Installation
21
+
22
+ Drop into your ~/.chef/plugins/knife/ dir
@@ -0,0 +1,127 @@
1
+ #
2
+ # Author:: John Goulah (<jgoulah@gmail.com>)
3
+ # Copyright:: Copyright (c) 2011 John Goulah
4
+ #
5
+ ## Licensed under the Apache License, Version 2.0 (the "License");
6
+ ## you may not use this file except in compliance with the License.
7
+ ## You may obtain a copy of the License at
8
+ ##
9
+ ## http://www.apache.org/licenses/LICENSE-2.0
10
+ ##
11
+ ## Unless required by applicable law or agreed to in writing, software
12
+ ## distributed under the License is distributed on an "AS IS" BASIS,
13
+ ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ ## See the License for the specific language governing permissions and
15
+ ## limitations under the License.
16
+ ##
17
+ #
18
+
19
+ module GoulahKnifePlugins
20
+ class Crawl < Chef::Knife
21
+
22
+ banner "knife crawl ROLE"
23
+
24
+ def initialize(*args)
25
+ @level = 0
26
+ super
27
+ end
28
+
29
+ deps do
30
+ require 'chef/role'
31
+ require 'chef/search/query'
32
+ end
33
+
34
+ option :included,
35
+ :short => '-i',
36
+ :long => '--included',
37
+ :boolean => true,
38
+ :default => false,
39
+ :description => "Find the roles this role is included in"
40
+
41
+ def run
42
+ unless name_args.size == 1
43
+ puts "You need to supply a role"
44
+ show_usage
45
+ exit 1
46
+ end
47
+
48
+ if config[:verbosity] == 1
49
+ @verbose = true
50
+ end
51
+
52
+ role_name = name_args.first
53
+
54
+ output "\n#{role_name} child hierarchy:"
55
+ output " * " + role_name
56
+ crawl_role(role_name)
57
+
58
+ if config[:included]
59
+ output "\n#{role_name} is included in the following roles:"
60
+ included_from(role_name)
61
+ end
62
+ end
63
+
64
+ def included_from(role_name)
65
+ @level += 1
66
+ q_roles = Chef::Search::Query.new
67
+ query = "run_list:role\\[#{role_name}\\]"
68
+
69
+ begin
70
+ rows, start, total = q_roles.search('role', query, nil, 0, 1000)
71
+
72
+ if total == 0
73
+ output "none"
74
+ end
75
+
76
+ rows.each do |role|
77
+ output " * " + role.name
78
+ end
79
+ rescue Net::HTTPServerException => e
80
+ msg = Chef::JSONCompat.from_json(e.response.body)["error"].first
81
+ ui.error("knife crawl failed: #{msg}")
82
+ exit 1
83
+ end
84
+ end
85
+
86
+ def crawl_role(role_name)
87
+ @level += 1
88
+ role = Chef::Role.load(role_name)
89
+
90
+ if !has_roles? role and @verbose
91
+ output " - no further roles found under " + role_name
92
+ end
93
+
94
+ loop_run_list role do |item|
95
+ output " * " + item.name
96
+ crawl_role(item.name)
97
+ end
98
+
99
+ @level -= 1
100
+ end
101
+
102
+ def loop_run_list(role, &blk)
103
+ role.run_list.each do |item|
104
+ if item.role?
105
+ yield item
106
+ end
107
+ end
108
+ end
109
+
110
+ def output(msg)
111
+ ui.msg(indent_str + msg)
112
+ end
113
+
114
+ def indent_str
115
+ str = ""
116
+ @level.times { str << " " }
117
+ return str
118
+ end
119
+
120
+ # search the run_list for roles (since it has roles and recipes)
121
+ def has_roles?(role)
122
+ loop_run_list(role) { return true }
123
+ return false
124
+ end
125
+
126
+ end
127
+ end
@@ -0,0 +1,5 @@
1
+ module Knife
2
+ module Crawl
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-crawl
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - John Goulah
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-02 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: A plugin for Chef::Knife which displays the roles that are included recursively within a role and optionally displays all the roles that include it.
22
+ email:
23
+ - jgoulah@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - README.md
32
+ - lib/chef/knife/crawl.rb
33
+ - lib/knife-crawl/version.rb
34
+ homepage: https://github.com/jgoulah/knife-crawl
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 3
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.13
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: A plugin for Chef::Knife which displays the roles that are included recursively within a role and optionally displays all the roles that include it.
67
+ test_files: []
68
+