Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
One-Shot cookbook allows you to run a recipe and then remove it from …
…your run_list
  • Loading branch information
mattray authored and jtimberman committed Feb 18, 2011
1 parent 27ba518 commit 542fa25
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 0 deletions.
49 changes: 49 additions & 0 deletions one-shot/README.md
@@ -0,0 +1,49 @@
Description
===========
This cookbook provides a framework for making single-use, one-shot recipes. By including the `one-shot` recipe in the node's run_list, on the next chef-client run the contents of the `one-shot::one-shot` recipe will be called. This is parameterized as an attribute, so you can change these out by setting the `["one_shot"]["recipe"]` to include different recipes.

Requirements
============
Written with Chef 0.9.12.

Testing
-------
Tested on Ubuntu 10.04 and 10.10 and tested with Chef 0.9.12.

Attributes
==========
For now this attribute has to exist in a Role, you can edit the node or you can edit the cookbook. Ideally you could pass an attribute from the command line.

* `["one_shot"]["recipe"]` - Default is `one-shot::one-shot`, but may be set to alternate recipes so you may have multiple recipes to use, depending on this attribute.

Recipes
=======
Default
-------
The default recipe includes the recipe referred to by the `["one_shot"]["recipe"]` attribute, executing it and then removing the `oneshot` recipe from the node's run_list.

One-Shot
--------
This is an example implementation of a one-shot recipe, it may be copied or modified as necessary. Access to additional recipes are made through the `["one_shot"]["recipe"]` attribute.

Usage
=====
Add the `one-shot` recipe to a node's run_list. Next chef-client run, that node will execute the recipe designated by the `["one_shot"]["recipe"]` attribute, then remove itself from the node's run_list.

License and Author
==================
Author:: Matt Ray <matt@opscode.com>

Copyright:: 2011 Opscode, Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
22 changes: 22 additions & 0 deletions one-shot/attributes/default.rb
@@ -0,0 +1,22 @@
#
# Author:: Matt Ray <matt@opscode.com>
# Cookbook Name:: zenoss
# Attributes:: default
#
# Copyright 2011 Opscode, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#additional one-shots can be added by changing the name of the recipe
default[:one_shot][:recipe] = "one-shot::one-shot"
31 changes: 31 additions & 0 deletions one-shot/metadata.json
@@ -0,0 +1,31 @@
{
"long_description": "",
"recipes": {
"one-shot::one-shot": "The recipe to be executed a single time.",
"default": "Includes the `one-shot` recipe and removes itself from the run_list."
},
"attributes": {
},
"providing": {
},
"dependencies": {
},
"maintainer": "Opscode, Inc.",
"replacing": {
},
"maintainer_email": "matt@opscode.com",
"groupings": {
},
"platforms": {
},
"license": "Apache 2.0",
"version": "0.1.0",
"recommendations": {
},
"suggestions": {
},
"name": "one-shot",
"description": "Runs the contents of the one-shot.rb and removes it from the run_list",
"conflicting": {
}
}
7 changes: 7 additions & 0 deletions one-shot/metadata.rb
@@ -0,0 +1,7 @@
maintainer "Opscode, Inc."
maintainer_email "matt@opscode.com"
license "Apache 2.0"
description "Runs the contents of the one-shot.rb and removes it from the run_list"
version "0.1"
recipe "one-shot::one-shot", "The recipe to be executed a single time."
recipe "default", "Includes the `one-shot` recipe and removes itself from the run_list."
31 changes: 31 additions & 0 deletions one-shot/recipes/default.rb
@@ -0,0 +1,31 @@
#
# Author:: Matt Ray <matt@opscode.com>
# Cookbook Name:: one-shot
# Recipe:: default
#
# Copyright 2011, Opscode, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

oneshot = node["one_shot"]["recipe"]

include_recipe oneshot

ruby_block "remove one-shot recipe #{oneshot}" do
block do
Chef::Log.info("One-Shot recipe #{oneshot} executed and removed from run_list")
node.run_list.remove("recipe[one-shot]") if node.run_list.include?("recipe[one-shot]")
end
action :create
end
21 changes: 21 additions & 0 deletions one-shot/recipes/one-shot.rb
@@ -0,0 +1,21 @@
#
# Author:: Matt Ray <matt@opscode.com>
# Cookbook Name:: one-shot
# Recipe:: one-shot
#
# Copyright 2011, Opscode, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

Chef::Log.info("one-shot::one-shot called and executed!")
21 changes: 21 additions & 0 deletions one-shot/recipes/two-shot.rb
@@ -0,0 +1,21 @@
#
# Author:: Matt Ray <matt@opscode.com>
# Cookbook Name:: one-shot
# Recipe:: two-shot
#
# Copyright 2011, Opscode, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

Chef::Log.info("one-shot::two-shot called and executed!")

0 comments on commit 542fa25

Please sign in to comment.