GithubHelp home page GithubHelp logo

tomaszklosinski / chef-rewind Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bryanwb/chef-rewind

0.0 3.0 0.0 178 KB

monkeypatch chef to edit existing resources in place

License: MIT License

chef-rewind's Introduction

Chef::Rewind

This adds a simple function to the Chef library scope to rewind or unwind an existing resource. If the given resource does not exist, a Chef::Exceptions::ResourceNotFound exception will be raised.

These functions are designed to assist the library cookbook pattern.

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.

Installation

Add this line to your application's Gemfile:

gem 'chef-rewind'

And then execute:

$ bundle

Or install it yourself as:

$ gem install chef-rewind

Usage

rewind

# file postgresql/recipes/server.rb
user "postgres" do
  uid  26
  home '/home/postgres'
  supports  :manage_home => true
end

# file my-postgresql/recipes/server.rb
chef_gem "chef-rewind"
require 'chef/rewind'

include_recipe "postgresql::server"

rewind "user[postgres]" do
  home '/var/lib/pgsql/9.2'
end

The user postgres will act once with the home directory /var/lib/pgsql/9.2 and the cookbook_name attribute is now my-postgresql instead of postgresql. This last part is particularly important for templates and cookbook files.

unwind

# file postgresql/recipes/server.rb
user "postgres" do
  uid  26
  home '/home/postgres'
  supports  :manage_home => true
end

# file my-postgresql/recipes/server.rb
chef_gem "chef-rewind"
require 'chef/rewind'

include_recipe "postgresql::server"

unwind "user[postgres]"

This will completely remove the resource. It is useful for resources that are impossible to change correctly. Resource notifications, for example, can't be overwritten by rewind, only appended.

So if you need to change notifications of a resource, you need to unwind and redefine the resource. Example:

# file cookbook-elasticsearch/recipes/default.rb
template "logging.yml" do
  path "#{node.elasticsearch[:path][:conf]}/logging.yml"
  source "logging.yml.erb"
  owner node.elasticsearch[:user] and group node.elasticsearch[:user] and mode 0755

  notifies :restart, 'service[elasticsearch]'
end

# file my-elasticsearch/recipes/default.rb
chef_gem "chef-rewind"
require 'chef/rewind'

unwind "template[logging.yml]"

template "logging.yml" do
  path  "#{node.elasticsearch[:path][:conf]}/logging.yml"
  source "logging.yml.erb"
  owner node.elasticsearch[:user] and group node.elasticsearch[:user] and mode 0755
  cookbook_name "elasticsearch"

  # this is the only change from original definition
  notifies :run, 'execute[Custom ElasticSearch restarter]'
end

This allows you to define your own ElasticSearch restart script. It's impossible to rewind notifications, thus you need to unwind and redefine it based on the original version.

Gotchas Important

The rewind method does not automatically change the cookbook_name attribute for a resource to the current cookbook. Doing so could cause some unexpected behavior, particularly for less expert chef users.

Example

# file postgresql/recipes/server.rb
template "/var/pgsql/data/postgresql.conf" do
  source  "postgresql.conf.erb"
  owner "postgres"
end

# file my-postgresql/recipes/server.rb
chef_gem "chef-rewind"
require 'chef/rewind'

include_recipe "postgresql::server"
# my-postgresql.conf.erb located inside my-postgresql/templates/default/my-postgresql.conf.erb
rewind :template => "/var/pgsql/data/postgresql.conf" do
  source "my-postgresql.conf.erb"
  cookbook_name "my-postgresql"
end

If you do not specify cookbook_name the rewind function will likely return an error since Chef will look in the postgresql cookbook for the source file and not in the my-postgresql cookbook.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

chef-rewind's People

Contributors

aabes avatar bryanwb avatar hltbra avatar nathenharvey avatar pdf avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.