knife-data-bag-version 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ *.gem
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2013 Kristian Van Der Vliet
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,74 @@
1
+ # Knife data bag version
2
+
3
+ While Chef understands the concept of versioned Cookbooks, other resources such as Roles & Data Bags are not versioned. This can cause problems mixing different versions of the same Cookbook within an organization if the data bags are shared between versions.
4
+
5
+ The knife data bag version plugin attempts to provide a mechanism to version data bag items to match the cookbook version. Coupled with a Chef runtime library, recipes can load a given version of the item which will match its requirements.
6
+
7
+ ## Requirements
8
+
9
+ knife data bag version relies on thor-scmversion to manage the version information.
10
+
11
+ ## Installation
12
+
13
+ gem install knife-data-bag-version
14
+
15
+ ## How does it work?
16
+
17
+ Instead of storing each data bag item as pure JSON, the items are stored as ERB templates. knife data bag version then processes each template with Erubis and produces a JSON data bag item with the version information injected.
18
+
19
+ For example, assume we have a simple data bag item:
20
+
21
+ **my-data-bag-item.json**
22
+
23
+ {
24
+ "id": "my-data-bag-item",
25
+ "users": ["kristian", "paul", "pete", "david", "bill"]
26
+ }
27
+
28
+ For use with knife data bag version this becomes a template:
29
+
30
+ **my-data-bag-item.erb**
31
+
32
+ {
33
+ "id": "<%= DatabagVersion.id('my-data-bag-item') %>",
34
+ "users": ["kristian", "paul", "pete", "david", "bill"]
35
+ }
36
+
37
+ knife data bag version can then create a JSON file using this template:
38
+
39
+ $ knife data bag version
40
+ Templating data bag item data_bags/my-cookbook/my-data-bag-item
41
+
42
+ knife data bag version will emit a JSON file:
43
+
44
+ **my-data-bag-item_1_2_0.json**
45
+
46
+ {
47
+ "id": "my-data-bag-item_1_2_0",
48
+ "users": ["kristian", "paul", "pete", "david", "bill"]
49
+ }
50
+
51
+ The three digits will naturally depend on the major, minor & patch versions of the current Cookbook. The version information is obtained from thor-scmversion, which means the data bag version will always match the cookbook. Each versioned data bag item is distinct from other versions, so you can safely have E.g. "my-data-bag-item\_1\_1\_3", "my-data-bag-item\_1\_2\_0" and "my-data-bag-item\_1\_2\_1" uploaded to the same organization.
52
+
53
+ ## Limitations
54
+
55
+ The plugin relies on thor-scmversion, which means if you're not using it manage your cookbook versions knife data bag version will always version everything as version 0.0.1.
56
+
57
+ It assumes that all data bags are contained in a directory called "data\_bags", and it is assumed that the data bags are stored within the Cookbook itself. An example directory structure:
58
+
59
+ my-cookbook
60
+ metadata.rb
61
+ Thorfile
62
+ recipes
63
+ default.rb
64
+ libraries
65
+ my-library.rb
66
+ data_bags
67
+ my-cookbook
68
+ my-cookbook-item.erb
69
+
70
+ You would then run knife data bag version from the top of the tree, in the "my-cookbook" directory. If you run the knife data bag version and it can't find a directory called "data\_bags" it will fail silently.
71
+
72
+ How you load the data bags is currently an exercise for the reader, but I'll try to get a Chef runtime library into the Community Cookbooks repository soon.
73
+
74
+ This knife plugins is supplied without any warranty or guarantees regarding suitability for purpose.
@@ -0,0 +1,9 @@
1
+ require 'rake/testtask'
2
+
3
+ desc "Run unit tests"
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.pattern = 'test/unit/*_test.rb'
8
+ t.verbose = true
9
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |g|
4
+ g.name = 'knife-data-bag-version'
5
+ g.version = '1.1.0'
6
+ g.summary = "Create versioned data bag items"
7
+ g.description = "Processes data bag item templates with Erubis & thor-scmversion to create 'versioned' data bag items."
8
+ g.authors = ["Kristian Van Der Vliet"]
9
+ g.email = 'kvandervliet@dyn.com'
10
+ g.homepage = ''
11
+ g.license = 'Apache-2.0'
12
+
13
+ g.files = `git ls-files`.split($\)
14
+ g.executables = g.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+
16
+ g.add_runtime_dependency 'thor-scmversion'
17
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ require 'databag_version'
3
+
4
+ module Dyn
5
+ module Knife
6
+
7
+ # Version all data bags items
8
+
9
+ class DataBagVersion < Chef::Knife
10
+
11
+ banner 'knife data bag version'
12
+
13
+ option :quiet,
14
+ :short => "-q",
15
+ :long => "--quiet",
16
+ :boolean => "false",
17
+ :description => "Silence all informational output."
18
+
19
+ def run
20
+ DatabagVersion.process_all(config[:quiet])
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+ require 'thor-scmversion'
3
+ require 'erubis'
4
+
5
+ # A simple module for working with "versioned" data bags.
6
+ # This relies on thor-scmversion to provide the version information. Any
7
+ # data bag item templates are then processed with Erubis to insert the version
8
+ # information.
9
+
10
+ module DatabagVersion
11
+ class << self
12
+
13
+ def id(name)
14
+ scm_versioner = ThorSCMVersion.versioner
15
+ tag = scm_versioner.from_path
16
+
17
+ version = tag.to_s.gsub(/\./, '_')
18
+
19
+ "#{name}_#{version}"
20
+ end
21
+
22
+ def process_all(quiet = true, path = 'data_bags')
23
+ @be_quiet = quiet
24
+ Dir.foreach(path) do |dir|
25
+ # Skip everything that isn't a sub-directory
26
+ next unless File.directory?(dir)
27
+ # Process every sub-directory (but not current & parent, natch)
28
+ process_dir("#{path}/#{dir}") unless dir == '.' || dir == '..'
29
+ end if File.exist?(path)
30
+ end
31
+
32
+ private
33
+
34
+ def process_dir(path)
35
+ Dir.foreach(path) do |file|
36
+ process_file("#{path}/#{file}") if file.match('.erb')
37
+ end
38
+ end
39
+
40
+ def process_file(file)
41
+ path = File.dirname(file)
42
+ name = File.basename(file, '.erb')
43
+ id = id(name)
44
+
45
+ out = "#{path}/#{id}.json"
46
+
47
+ puts "Templating data bag item #{path}/#{name}" unless @be_quiet
48
+
49
+ template = File.read(file)
50
+ template = Erubis::Eruby.new(template)
51
+
52
+ File.write(out, template.result)
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,16 @@
1
+ require 'test/unit'
2
+ require 'databag_version'
3
+
4
+ class DBVTest < Test::Unit::TestCase
5
+ def test_id
6
+ assert_equal "derp_0_0_1",
7
+ DatabagVersion.id("derp")
8
+ end
9
+
10
+ def test_process_all
11
+ assert_nil DatabagVersion.process_all
12
+ assert_nothing_raised{ DatabagVersion.process_all(false) }
13
+ assert_nothing_raised{ DatabagVersion.process_all(false, "/this/path/shouldnt/exist") }
14
+ assert_nothing_raised{ DatabagVersion.process_all(false, "/tmp") }
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-data-bag-version
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kristian Van Der Vliet
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor-scmversion
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Processes data bag item templates with Erubis & thor-scmversion to create
31
+ 'versioned' data bag items.
32
+ email: kvandervliet@dyn.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - LICENSE
39
+ - README.md
40
+ - Rakefile
41
+ - knife-data-bag-version.gemspec
42
+ - lib/chef/knife/knife-data-bag-version.rb
43
+ - lib/databag_version.rb
44
+ - test/unit/knife-data-bag-version_test.rb
45
+ homepage: ''
46
+ licenses:
47
+ - Apache-2.0
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.23
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Create versioned data bag items
70
+ test_files: []
71
+ has_rdoc: