Permalink
Browse files

Initial import.

  • Loading branch information...
fnichol committed Aug 4, 2011
0 parents commit 7a2b12350d732cd7411d6385516ea674512d18f0
Showing with 170 additions and 0 deletions.
  1. +66 −0 README.md
  2. +76 −0 libraries/solo_data_bags.rb
  3. +10 −0 metadata.rb
  4. +18 −0 recipes/default.rb
@@ -0,0 +1,66 @@
+# Description
+
+Adds [Data Bag][data_bag] support for older versions of *chef-solo*.
+
+# Requirements
+
+## Chef
+
+Chef versions lower than 0.10.4 may benefit. There is no reason to use this
+cookbook for Chef version 0.10.4 and higher as the feature is part of the
+core.
+
+## Platform
+
+Tested on Mac OS X 10.6.8 and Ubuntu 10.10 but should work any platform
+that is supported by Chef.
+
+## Cookbooks
+
+There are no external cookbook dependencies.
+
+# Recipes
+
+## default
+
+This recipe is a no-op and does nothing.
+
+# Attributes
+
+There are no attributes to define or configure.
+
+# Usage
+
+Simply include the cookbook in your chef repository and
+`libraries/solo_data_bags` will be loaded in the chef-solo run to add Data
+Bag support.
+
+# Development
+
+* Source hosted at [GitHub][repo]
+* Report issues/Questions/Feature requests on [GitHub Issues][issues]
+
+Pull requests are very welcome! Make sure your patches are well tested.
+Ideally create a topic branch for every seperate change you make.
+
+# License and Author
+
+Author:: Fletcher Nichol (<fnichol@nichol.ca>)
+
+Copyright 2011, Fletcher Nichol
+
+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.
+
+[data_bag]: http://wiki.opscode.com/display/chef/Data+Bags
+[repo]: https://github.com/fnichol/chef-solo_data_bags
+[issues]: https://github.com/fnichol/chef-solo_data_bags/issues
@@ -0,0 +1,76 @@
+# Thanks to https://gist.github.com/967980 for the original inspiration.
+# Code here was adapted from Chef codebase introduced in version 0.10.4.
+
+##
+# Determines if the version of Chef is older than 0.10.4.
+#
+# @return [true,false] whether or not this is an "old" chef gem
+def is_chef_version_old?
+ version = Chef::VERSION.split('.')
+ version.first == "0" && version.slice(1, 2).join('.').to_f < 10.4
+end
+
+if Chef::Config[:solo] && is_chef_version_old?
+ Chef::Log.info("Data bag support added for pre-0.10.4 chef-solo.")
+
+ class Chef
+ class DataBag
+ ##
+ # Load a Data Bag by name via local data_bag_path if run in solo mode.
+ # NOTE: `Chef::Config[:data_bag_path].first` exists for versions of
+ # vagrant 0.8.2 and older. It was setting the value to be an Array
+ # rather than a String.
+ def self.load_local(name)
+ unless File.directory?(Chef::Config[:data_bag_path].first)
+ raise Chef::Exceptions::InvalidDataBagPath, "Data bag path '#{Chef::Config[:data_bag_path]}' is invalid"
+ end
+
+ Dir.glob(File.join(Chef::Config[:data_bag_path].first, name, "*.json")).inject({}) do |bag, f|
+ item = JSON.parse(IO.read(f))
+ bag[item['id']] = item
+ bag
+ end
+ end
+ end
+
+ class DataBagItem
+ ##
+ # Load a Data Bag Item by name via local data_bag_path if run in solo
+ # mode.
+ def self.load_local(data_bag, name)
+ bag = Chef::DataBag.load_local(data_bag)
+ item = bag[name]
+
+ if item.kind_of?(DataBagItem)
+ item
+ else
+ item = from_hash(item)
+ item.data_bag(data_bag)
+ item
+ end
+ end
+ end
+
+ module Mixin
+ module Language
+ def data_bag(bag)
+ DataBag.validate_name!(bag.to_s)
+ rbag = DataBag.load_local(bag)
+ rbag.keys
+ rescue Exception
+ Log.error("Failed to list data bag items in data bag: #{bag.inspect}")
+ raise
+ end
+
+ def data_bag_item(bag, item)
+ DataBag.validate_name!(bag.to_s)
+ DataBagItem.validate_id!(item)
+ DataBagItem.load_local(bag, item)
+ rescue Exception
+ Log.error("Failed to load data bag item: #{bag.inspect} #{item.inspect}")
+ raise
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,10 @@
+maintainer "Fletcher Nichol"
+maintainer_email "fnichol@nichol.ca"
+license "Apache 2.0"
+description "Adds Data Bag support for older versions of chef-solo"
+long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
+version "0.0.1"
+
+%w{ debian ubuntu suse centos redhat fedora mac_os_x }.each do |os|
+ supports os
+end
@@ -0,0 +1,18 @@
+#
+# Cookbook Name:: solo_data_bags
+# Recipe:: default
+#
+# Copyright 2011, Fletcher Nichol
+#
+# 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.
+#

0 comments on commit 7a2b123

Please sign in to comment.