chef-rewind 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,4 @@
1
+ Hugo Lopes Tavares: Allow unwinding resources
1
2
  Peter Fern: Allow rewinding recipe_name
2
3
  Peter Fern: Shef extensions have moved in 11.x, add compatible require
3
4
  Peter Fern: Change deprecated Gemfile source `:rubygems` to `https://rubygems.org`
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
  # Chef::Rewind
2
2
 
3
3
  This adds a simple function to the Chef library scope to
4
- rewind an existing resource. If the given resource does not exist,
5
- a Chef::Exceptions::ResourceNotFound exception will be raised.
4
+ rewind or unwind an existing resource. If the given resource does not exist,
5
+ a `Chef::Exceptions::ResourceNotFound` exception will be raised.
6
6
 
7
- This function is designed to assist the library cookbook pattern.
7
+ These functions are designed to assist the library cookbook pattern.
8
8
 
9
- Effectively, the rewind resource allows you to monkeypatch a cookbook that you would rather not modify directly. It will modify some properties of a resource, during the complile phase, before chef-client actually starts the run phase.
9
+ Effectively, rewind/unwind resource allows you to monkeypatch a cookbook that you would rather not modify directly. It will modify some properties of a resource, during the complile phase, before chef-client actually starts the run phase.
10
10
 
11
11
  ## Installation
12
12
 
@@ -24,6 +24,8 @@ Or install it yourself as:
24
24
 
25
25
  ## Usage
26
26
 
27
+ ### rewind
28
+
27
29
  ```Ruby
28
30
  # file postgresql/recipes/server.rb
29
31
  user "postgres" do
@@ -44,11 +46,73 @@ end
44
46
 
45
47
  ```
46
48
 
47
- The user "postgres" will act once with the home directory
48
- '/var/lib/pgsql/9.2 and the cookbook_name attribute is now
49
- "my-postgresql" instead of "postgresql". This last part is
49
+ The user `postgres` will act once with the home directory
50
+ `/var/lib/pgsql/9.2` and the `cookbook_name` attribute is now
51
+ `my-postgresql` instead of `postgresql`. This last part is
50
52
  particularly important for templates and cookbook files.
51
53
 
54
+ ### unwind
55
+
56
+ ```Ruby
57
+ # file postgresql/recipes/server.rb
58
+ user "postgres" do
59
+ uid 26
60
+ home '/home/postgres'
61
+ supports :manage_home => true
62
+ end
63
+
64
+ # file my-postgresql/recipes/server.rb
65
+ chef_gem "chef-rewind"
66
+ require 'chef/rewind'
67
+
68
+ include_recipe "postgresql::server"
69
+
70
+ unwind "user[postgres]"
71
+
72
+ ```
73
+
74
+ This will completely remove the resource. It is useful
75
+ for resources that are impossible to change correctly.
76
+ Resource notifications, for example,
77
+ can't be overwritten by `rewind`, only appended.
78
+
79
+ So if you need to change notifications of a resource,
80
+ you need to `unwind` and redefine the resource. Example:
81
+
82
+ ```Ruby
83
+ # file cookbook-elasticsearch/recipes/default.rb
84
+ template "logging.yml" do
85
+ path "#{node.elasticsearch[:path][:conf]}/logging.yml"
86
+ source "logging.yml.erb"
87
+ owner node.elasticsearch[:user] and group node.elasticsearch[:user] and mode 0755
88
+
89
+ notifies :restart, 'service[elasticsearch]'
90
+ end
91
+
92
+ # file my-elasticsearch/recipes/default.rb
93
+ chef_gem "chef-rewind"
94
+ require 'chef/rewind'
95
+
96
+ unwind "template[logging.yml]"
97
+
98
+ template "logging.yml" do
99
+ path "#{node.elasticsearch[:path][:conf]}/logging.yml"
100
+ source "logging.yml.erb"
101
+ owner node.elasticsearch[:user] and group node.elasticsearch[:user] and mode 0755
102
+ cookbook_name "elasticsearch"
103
+
104
+ # this is the only change from original definition
105
+ notifies :run, 'execute[Custom ElasticSearch restarter]'
106
+ end
107
+
108
+ ```
109
+
110
+ This allows you to define your own ElasticSearch restart script.
111
+ It's impossible to `rewind` notifications,
112
+ thus you need to `unwind` and redefine it based on the original version.
113
+
114
+
115
+
52
116
  ## Gotchas *Important*
53
117
 
54
118
  The rewind method does not automatically change the cookbook_name
@@ -3,8 +3,8 @@ $:.unshift(File.dirname(__FILE__) + '/lib')
3
3
  Gem::Specification.new do |gem|
4
4
  gem.authors = ["Bryan Berry"]
5
5
  gem.email = ["bryan.berry@gmail.com"]
6
- gem.description = %q{Monkey patches Chef to allow rewinding of existing resources}
7
- gem.summary = %q{Monkey patches Chef to allow rewinding of existing resources}
6
+ gem.description = %q{Monkey patches Chef to allow rewinding and unwinding of existing resources}
7
+ gem.summary = %q{Monkey patches Chef to allow rewinding and unwinding of existing resources}
8
8
  gem.homepage = ""
9
9
 
10
10
  gem.files = `git ls-files`.split($\)
@@ -12,5 +12,5 @@ Gem::Specification.new do |gem|
12
12
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
13
  gem.name = "chef-rewind"
14
14
  gem.require_paths = ["lib"]
15
- gem.version = "0.0.6"
15
+ gem.version = "0.0.7"
16
16
  end
@@ -28,6 +28,20 @@ class Chef
28
28
  raise e
29
29
  end
30
30
  end
31
+
32
+ # unwinds an existing resource if it exists,
33
+ # otherwise raises the Chef::Exceptions::ResourceNotFound exception
34
+ # For example:
35
+ # # recipe postgresql::server defines user "postgres" and
36
+ # # sets the home directory to /var/pgsql/9.2
37
+ # include_recipe "postgresql::user"
38
+ #
39
+ # unwind "user[postgres]"
40
+ # === Parameters
41
+ # resource<String>:: String identifier for resource
42
+ def unwind(resource_id)
43
+ run_context.resource_collection.delete_resource resource_id
44
+ end
31
45
  end
32
46
  end
33
47
 
@@ -49,3 +63,16 @@ class Chef
49
63
  end
50
64
  end
51
65
  end
66
+
67
+
68
+ class Chef
69
+ class ResourceCollection
70
+ def delete_resource(resource_id)
71
+ lookup resource_id
72
+
73
+ # assumes `resource_id` is the same as `Chef::Resource#to_s`
74
+ @resources.delete_if {|r| r.to_s == resource_id }
75
+ @resources_by_name.delete resource_id
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'chef/rewind'
3
+
4
+ describe Chef::Recipe do
5
+
6
+ before(:each) do
7
+ @cookbook_repo = File.expand_path(File.join(File.dirname(__FILE__), "..", "data", "cookbooks"))
8
+ cl = Chef::CookbookLoader.new(@cookbook_repo)
9
+ cl.load_cookbooks
10
+ @cookbook_collection = Chef::CookbookCollection.new(cl)
11
+ @node = Chef::Node.new
12
+ @node.normal[:tags] = Array.new
13
+ @events = Chef::EventDispatch::Dispatcher.new
14
+ @run_context = Chef::RunContext.new(@node, @cookbook_collection, @events)
15
+ @recipe = Chef::Recipe.new("hjk", "test", @run_context)
16
+ end
17
+
18
+
19
+ describe "unwind" do
20
+ it "should remove resource when unwind is called" do
21
+ @recipe.zen_master "foobar" do
22
+ peace false
23
+ end
24
+
25
+ @recipe.unwind "zen_master[foobar]"
26
+
27
+ resources = @run_context.resource_collection.all_resources
28
+ resources.length.should == 0
29
+ end
30
+
31
+ it "should throw an error when unwinding a nonexistent resource" do
32
+ lambda do
33
+ @recipe.unwind "zen_master[foobar]"
34
+ end.should raise_error(Chef::Exceptions::ResourceNotFound)
35
+ end
36
+ end
37
+
38
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-rewind
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-20 00:00:00.000000000 Z
12
+ date: 2013-10-14 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Monkey patches Chef to allow rewinding of existing resources
14
+ description: Monkey patches Chef to allow rewinding and unwinding of existing resources
15
15
  email:
16
16
  - bryan.berry@gmail.com
17
17
  executables: []
@@ -73,6 +73,7 @@ files:
73
73
  - spec/support/shared/functional/securable_resource.rb
74
74
  - spec/support/shared/unit/api_error_inspector.rb
75
75
  - spec/support/shared/unit/platform_introspector.rb
76
+ - spec/unwind_recipe_spec.rb
76
77
  homepage: ''
77
78
  licenses: []
78
79
  post_install_message:
@@ -96,7 +97,7 @@ rubyforge_project:
96
97
  rubygems_version: 1.8.25
97
98
  signing_key:
98
99
  specification_version: 3
99
- summary: Monkey patches Chef to allow rewinding of existing resources
100
+ summary: Monkey patches Chef to allow rewinding and unwinding of existing resources
100
101
  test_files:
101
102
  - spec/data/cookbooks/angrybash/recipes/default.rb
102
103
  - spec/data/cookbooks/apache2/files/default/apache2_module_conf_generate.pl
@@ -145,4 +146,5 @@ test_files:
145
146
  - spec/support/shared/functional/securable_resource.rb
146
147
  - spec/support/shared/unit/api_error_inspector.rb
147
148
  - spec/support/shared/unit/platform_introspector.rb
149
+ - spec/unwind_recipe_spec.rb
148
150
  has_rdoc: