knife-scrub 0.1.2 → 0.2.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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZjE4YjEwNjRhOTU0ZTNhYzcyNGJkOGE1Njk4MWNlMDRkMWU5M2QyNA==
5
+ data.tar.gz: !binary |-
6
+ ZjQzYjQxYTg5NGMwZWI0N2JjY2M3MmRmZDM1ODkyYTZjMjI4MjZkZQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NjNiY2MwMzk4Nzk1NzcxZTRlMDAwOGQxZWUxYzQ3YWU1M2U1ZTBjYTcwYWNi
10
+ YzA2NTA1YzZiMTAwNDc2MjIzMmU4OTM0YzE3Yzk1ZDQ5ZGNjZDM4MWIyMTcw
11
+ YzU2YTRlNDA0YTMxMjBkMWIzYzA4NTJhYzg2NjhkYTI5NWQ0Zjc=
12
+ data.tar.gz: !binary |-
13
+ MWMzZTJiOTUyMGYyZmEyZjc5YTk3YjZlNTJiZjI3NDI3YmNlYzdlYjc5NGIw
14
+ ZDZmZjM2MTM1ZTk4MmZjNzU1OWFkYzk5YmNiMDg3MjYzM2NjMzY2NDkyZjdm
15
+ ZWFmNzBkNmI0NTkwMjI2ZGE1NWRlMzczNzdiM2IwN2FmODJmMzY=
@@ -0,0 +1,116 @@
1
+ #
2
+ # Author:: Tobias Schmidt (<ts@soundcloud.com>)
3
+ # Copyright:: Copyright (c) 2013 SoundCloud, 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
+ class Chef
20
+ class Knife
21
+ module Scrub
22
+ class AttributeExtractor
23
+
24
+ class Base
25
+ def initialize(node, precedence)
26
+ @node = node
27
+ @precedence = precedence
28
+ end
29
+
30
+ def has_key?(key)
31
+ raise NotImplementedError
32
+ end
33
+
34
+ def fetch(key)
35
+ raise NotImplementedError
36
+ end
37
+
38
+ def delete(key)
39
+ raise NotImplementedError
40
+ end
41
+
42
+ protected
43
+
44
+ def attributes
45
+ @node.send(@precedence)
46
+ end
47
+
48
+ def extract(data, nested_value_spec)
49
+ spec = nested_value_spec.split(".")
50
+ key = spec.pop
51
+ spec.each do |attr|
52
+ if data.nil?
53
+ nil # don't get no method error on nil
54
+ elsif data.respond_to?(attr.to_sym)
55
+ data = data.send(attr.to_sym)
56
+ elsif data.respond_to?(:[])
57
+ data = data[attr]
58
+ else
59
+ data = begin
60
+ data.send(attr.to_sym)
61
+ rescue NoMethodError
62
+ nil
63
+ end
64
+ end
65
+ end
66
+ [data, key]
67
+ end
68
+
69
+ end
70
+
71
+ class Simple < Base
72
+ def has_key?(path)
73
+ base, key = extract(attributes, path)
74
+ !base.nil? && base.has_key?(key)
75
+ end
76
+
77
+ def fetch(path)
78
+ base, key = extract(attributes, path)
79
+ base.fetch(key)
80
+ end
81
+
82
+ def delete(path)
83
+ base, key = extract(attributes, path)
84
+ base.delete(key)
85
+ end
86
+ end
87
+
88
+ class Weird < Base
89
+ def has_key?(path)
90
+ base, key = extract(attributes, path)
91
+ !base.nil? && base.component_has_key?(base.send(@precedence), key)
92
+ end
93
+
94
+ def fetch(path)
95
+ base, key = extract(attributes, path)
96
+ base.value_at_current_nesting(base.send(@precedence), key).fetch(key)
97
+ end
98
+
99
+ def delete(path)
100
+ base, key = extract(attributes, path)
101
+ base.delete_from_component(base.send(@precedence), key)
102
+ end
103
+ end
104
+
105
+ def self.create(node, precedence = :normal)
106
+ if node.attribute.respond_to?(:component_has_key?)
107
+ Weird.new(node, precedence)
108
+ else
109
+ Simple.new(node, precedence)
110
+ end
111
+ end
112
+
113
+ end
114
+ end
115
+ end
116
+ end
@@ -27,6 +27,7 @@ class Chef
27
27
 
28
28
  deps do
29
29
  require 'chef/search/query'
30
+ require 'chef/knife/scrub/attribute_extractor'
30
31
  end
31
32
 
32
33
  option :query,
@@ -44,19 +45,19 @@ class Chef
44
45
 
45
46
  search = Chef::Search::Query.new
46
47
  search.search('node', config[:query]) do |node|
47
- parent, key = extract(node.normal, prefix)
48
+ extractor = Scrub::AttributeExtractor.create(node)
48
49
 
49
- unless parent && parent.component_has_key?(parent.normal, key)
50
+ unless extractor.has_key?(prefix)
50
51
  ui.msg format(node, "unknown normal attribute #{prefix}")
51
52
  next
52
53
  end
53
54
 
54
- value = parent.value_at_current_nesting(parent.normal, key).fetch(key)
55
+ value = extractor.fetch(prefix)
55
56
  unless ui.confirm format(node, "Do you want to delete #{value.inspect}")
56
57
  next
57
58
  end
58
59
 
59
- if deleted = parent.delete_from_component(parent.normal, key)
60
+ if deleted = extractor.delete(prefix)
60
61
  node.save
61
62
  ui.msg format(node, "deleted normal attribute #{deleted.inspect}")
62
63
  else
@@ -71,27 +72,6 @@ class Chef
71
72
  "#{ui.color(node.fqdn, :cyan)}: #{text}"
72
73
  end
73
74
 
74
- def extract(data, nested_value_spec)
75
- spec = nested_value_spec.split(".")
76
- key = spec.pop
77
- spec.each do |attr|
78
- if data.nil?
79
- nil # don't get no method error on nil
80
- elsif data.respond_to?(attr.to_sym)
81
- data = data.send(attr.to_sym)
82
- elsif data.respond_to?(:[])
83
- data = data[attr]
84
- else
85
- data = begin
86
- data.send(attr.to_sym)
87
- rescue NoMethodError
88
- nil
89
- end
90
- end
91
- end
92
- [data, key]
93
- end
94
-
95
75
  end
96
76
  end
97
77
  end
@@ -18,7 +18,7 @@
18
18
 
19
19
  module Knife
20
20
  module Scrub
21
- VERSION = '0.1.2'
21
+ VERSION = '0.2.0'
22
22
  MAJOR, MINOR, TINY = VERSION.split('.')
23
23
  end
24
24
  end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-scrub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Tobias Schmidt
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-17 00:00:00.000000000 Z
11
+ date: 2013-10-01 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
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ! '>='
44
39
  - !ruby/object:Gem::Version
@@ -56,37 +51,31 @@ files:
56
51
  - README.md
57
52
  - Rakefile
58
53
  - knife-scrub.gemspec
54
+ - lib/chef/knife/scrub/attribute_extractor.rb
59
55
  - lib/chef/knife/scrub_attributes.rb
60
56
  - lib/knife-scrub/version.rb
61
57
  homepage: https://github.com/soundcloud/knife-scrub
62
58
  licenses:
63
59
  - Apache 2.0
60
+ metadata: {}
64
61
  post_install_message:
65
62
  rdoc_options: []
66
63
  require_paths:
67
64
  - lib
68
65
  required_ruby_version: !ruby/object:Gem::Requirement
69
- none: false
70
66
  requirements:
71
67
  - - ! '>='
72
68
  - !ruby/object:Gem::Version
73
69
  version: '0'
74
- segments:
75
- - 0
76
- hash: 1430354937757579316
77
70
  required_rubygems_version: !ruby/object:Gem::Requirement
78
- none: false
79
71
  requirements:
80
72
  - - ! '>='
81
73
  - !ruby/object:Gem::Version
82
74
  version: '0'
83
- segments:
84
- - 0
85
- hash: 1430354937757579316
86
75
  requirements: []
87
76
  rubyforge_project:
88
- rubygems_version: 1.8.25
77
+ rubygems_version: 2.1.2
89
78
  signing_key:
90
- specification_version: 3
79
+ specification_version: 4
91
80
  summary: knife plugin to scrub normal attributes of chef-server
92
81
  test_files: []