bellboy 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,26 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+ *.swp
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
20
+
21
+ # Test inputs
22
+ Berksfile
23
+ target.json
24
+
25
+ # Build artifacts
26
+ VERSION
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,175 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
@@ -0,0 +1,126 @@
1
+ Bellboy
2
+ ======
3
+ Manage data bags and data bag items alongside Berkshelf.
4
+
5
+ Installation
6
+ ------------
7
+ Add Bellboy to your repository's `Gemfile`:
8
+
9
+ ```ruby
10
+ gem 'bellboy'
11
+ ```
12
+
13
+ Or run it as a standalone:
14
+
15
+ gem install bellboy
16
+
17
+ Usage
18
+ -----
19
+ Bellboy is intended to be used alongside Berkshelf, allowing Berkshelf to manage the cookbooks and their dependencies and Bellboy to manage Databags & Databag Items that are associated with each cookbook.
20
+
21
+ Bellboy can be used to version databag items (created from databag templates), upload the databags to a server, and download databags via. an API server in a similar way to Berkshelf.
22
+
23
+ Bellboy expects databags to be stored within the cookbook in a directory named `data_bags`. It will search this directory for databags, and then for databag items (and databag item templates) within each databag.
24
+
25
+ ### Versioning databags
26
+
27
+ Simply run Bellboy from the cookbook directory which contains the data bags:
28
+
29
+ bellboy version
30
+
31
+ Bellboy will obtain the current cookbook version information using `Thor-scmversion` and apply this version data to any databag item templates it finds.
32
+
33
+ Please see the documentation for [knife-databag-version](https://rubygems.org/gems/knife-data-bag-version) for more information on databag versioning.
34
+
35
+ ### Installing databags
36
+
37
+ bellboy install
38
+
39
+ If any cookbooks held in the current Berkshelf cookbook cache have a `Bellboy` file then Bellboy will download all of the databags and databag items that are specified.
40
+
41
+ Databags & databag items are stored within a directory named `data_bags` within each cookbook in the Berkshelf cache. Subsequently running Bellboy to upload databags will upload any databags & databag items that were downloaded by Bellboy.
42
+
43
+ ### Uploading databags
44
+
45
+ bellboy upload
46
+
47
+ Bellboy creates the databags on the remote server, uploads all databag items (files ending in `.json` that are found below the `data_bags` directory) and create the `Bellboy` file which tells Bellboy which databag items are associated with this cookbook.
48
+
49
+ Once you have uploaded the databags you can run `berks upload`. This ensures that Berkshelf uploads the `Bellboy` file along with the cookbook.
50
+
51
+ Examples
52
+ --------
53
+ ### Uploading databags
54
+
55
+ Assume the following cookbook:
56
+
57
+ myface/
58
+ recipes/
59
+ default.rb
60
+ metadata.rb
61
+ LICENSE
62
+ README.md
63
+ Berksfile
64
+ Thorfile
65
+ .gitignore
66
+
67
+ We add our databags within the cookbook
68
+
69
+ myface
70
+ ...
71
+ data_bags/
72
+ myface/
73
+ data.json
74
+ versioned_data.erb
75
+
76
+ Using Bellboy we run `bellboy version` to create versioned databag items:
77
+
78
+ $ cd myface
79
+ $ bellboy version
80
+ Templating data bag item ./data_bags/myface/versioned_data
81
+
82
+ This will create a new JSON file:
83
+
84
+ myface/
85
+ ...
86
+ data_bags/
87
+ myface/
88
+ data.json
89
+ versioned_data.erb
90
+ versioned_data_1_2_0.json
91
+
92
+ Now we can use Bellboy to upload the databag items to the Chef server:
93
+
94
+ $ bellboy upload
95
+ Uploading ./data_bags/myface/versioned_data_1_2_0.json
96
+
97
+ Bellboy will create the `Bellboy` file for us. When we upload the cookbook with Berkshelf, the `Bellboy` file will be uploaded.
98
+
99
+ ### Downloading databags
100
+
101
+ We download the cookbooks in the usual way with Berkshelf:
102
+
103
+ $ berks install
104
+ Installing myface (1.2.0) from site: 'https://example.com'
105
+
106
+ Berkshelf will download the `Bellboy` file as part of the cookbook and store it in its cookbook cache. We can use Bellboy to retrieve the databags:
107
+
108
+ $ bellboy install
109
+ Downloading databags for myface-1.2.0
110
+ Downloading data bag item myface/versioned_data_1_2_0 from 'https://example.com/databags/myface/versioned_data_1_2_0'
111
+
112
+ Configuration
113
+ -------------
114
+ Bellboy will use the same configuration locations as Berkshelf. These are:
115
+
116
+ ```text
117
+ $PWD/.berkshelf/config.json
118
+ $PWD/berkshelf/config.json
119
+ $PWD/berkshelf-config.json
120
+ $PWD/config.json
121
+ ~/.berkshelf/config.json
122
+ ```
123
+
124
+ You can also specify the path to the Berkshelf configuration file with the ```-c``` option:
125
+
126
+ bellboy install -c /path/to/berkshelf/config.json
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1 @@
1
+ require "thor/scmversion"
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |g|
4
+ g.name = 'bellboy'
5
+ g.version = IO.read(File.join(File.dirname(__FILE__), "VERSION")) rescue "0.0.1"
6
+ g.summary = "Manage data bags with Berkshelf"
7
+ g.description = "Version, install & upload data bag items that are stored alongside your Berkshelf Cookbooks"
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', '~> 0.18.0'
17
+ g.add_runtime_dependency 'berkshelf', '~> 2.0.10'
18
+ g.add_runtime_dependency 'faraday', '~> 0.8.0'
19
+ g.add_runtime_dependency 'ridley', '~> 1.5.0'
20
+ end
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+ #
4
+ # Copyright 2014 Dyn Inc.
5
+ #
6
+ # Authors: Kristian Van Der Vliet <kvandervliet@dyn.com>
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ $LOAD_PATH.push File.expand_path('../../lib', __FILE__)
20
+ require 'bellboy'
21
+
22
+ begin
23
+ Bellboy::Cli.start
24
+ rescue Bellboy::BellboyError => e
25
+ puts e
26
+ end
@@ -0,0 +1,71 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright 2014 Dyn Inc.
4
+ #
5
+ # Authors: Kristian Van Der Vliet <kvandervliet@dyn.com>
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ require 'berkshelf'
19
+ require 'ridley'
20
+
21
+ require 'bellboy/errors'
22
+ require 'bellboy/logger'
23
+ require 'bellboy/versioner'
24
+ require 'bellboy/installer'
25
+ require 'bellboy/uploader'
26
+
27
+ module Bellboy
28
+ DEFAULT_FILENAME = 'Bellboy'
29
+
30
+ class << self
31
+ def berks_from_file(filepath)
32
+ begin
33
+ Berkshelf::Berksfile.from_file(filepath)
34
+ rescue Berkshelf::BerksfileNotFound
35
+ Bellboy.logger.log "Berkfile #{filepath} could not be found."
36
+ exit
37
+ end
38
+ end
39
+
40
+ attr_accessor :logger
41
+
42
+ def berks_sources(berksfile)
43
+ resolver = Berkshelf.ui.mute { berksfile.resolve(berksfile.sources) }
44
+ resolver[:sources]
45
+ end
46
+
47
+ def ridley_connection(options = {})
48
+ ridley_options = options.slice(:ssl)
49
+ ridley_options[:server_url] = options[:server_url] || Berkshelf::Config.instance.chef.chef_server_url
50
+ ridley_options[:client_name] = options[:client_name] || Berkshelf::Config.instance.chef.node_name
51
+ ridley_options[:client_key] = options[:client_key] || Berkshelf::Config.instance.chef.client_key
52
+ ridley_options[:ssl] = { verify: (options[:ssl_verify] || Berkshelf::Config.instance.ssl.verify) }
53
+
54
+ unless ridley_options[:server_url].present?
55
+ fail Berkshelf::ChefConnectionError, 'Missing required attribute in your Berkshelf configuration: chef.server_url'
56
+ end
57
+
58
+ unless ridley_options[:client_name].present?
59
+ fail Berkshelf::ChefConnectionError, 'Missing required attribute in your Berkshelf configuration: chef.node_name'
60
+ end
61
+
62
+ unless ridley_options[:client_key].present?
63
+ fail Berkshelf::ChefConnectionError, 'Missing required attribute in your Berkshelf configuration: chef.client_key'
64
+ end
65
+
66
+ Ridley.new(ridley_options)
67
+ end
68
+ end
69
+ end
70
+
71
+ require_relative 'bellboy/cli'
@@ -0,0 +1,96 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright 2014 Dyn Inc.
4
+ #
5
+ # Authors: Kristian Van Der Vliet <kvandervliet@dyn.com>
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ require 'bellboy'
19
+ require 'thor'
20
+
21
+ module Bellboy
22
+ # Simple CLI. Mirrors the Berkshelf options & commands where possible.
23
+ class Cli < Thor
24
+ def initialize(*args)
25
+ super(*args)
26
+
27
+ Bellboy.logger = Bellboy::Logger.new(options)
28
+ end
29
+
30
+ class_option :verbose,
31
+ type: :boolean,
32
+ desc: 'Enable verbose output.',
33
+ aliases: '-v'
34
+ class_option :quiet,
35
+ type: :boolean,
36
+ desc: 'Silence all informational output.',
37
+ aliases: '-q',
38
+ default: false
39
+ class_option :config,
40
+ type: :string,
41
+ desc: 'Path to Berkshelf configuration to use.',
42
+ aliases: '-c',
43
+ banner: 'PATH'
44
+
45
+ desc 'version', 'Version all databag templates'
46
+ method_option :berksfile,
47
+ type: :string,
48
+ default: Berkshelf::DEFAULT_FILENAME,
49
+ desc: 'Path to a Berksfile to operate off of.',
50
+ aliases: '-b',
51
+ banner: 'PATH'
52
+ def version
53
+ berksfile = Bellboy.berks_from_file(options[:berksfile])
54
+
55
+ version_options = options.reverse_merge(verbose: false)
56
+
57
+ Bellboy::Versioner.version(berksfile, version_options)
58
+ end
59
+
60
+ desc 'install', 'Install databags for all Cookbooks known by Berkshelf'
61
+ method_option :berksfile,
62
+ type: :string,
63
+ default: Berkshelf::DEFAULT_FILENAME,
64
+ desc: 'Path to a Berksfile to operate off of.',
65
+ aliases: '-b',
66
+ banner: 'PATH'
67
+ method_option :bellboyfile,
68
+ type: :string,
69
+ default: Bellboy::DEFAULT_FILENAME,
70
+ desc: 'Path to a Bellboy file to operate off of.',
71
+ aliases: '-d',
72
+ banner: 'PATH'
73
+ def install
74
+ berksfile = Bellboy.berks_from_file(options[:berksfile])
75
+ Bellboy::Installer.install(berksfile, options)
76
+ end
77
+
78
+ desc 'upload', 'Upload all databags for all Cookbooks known by Berkshelf'
79
+ method_option :berksfile,
80
+ type: :string,
81
+ default: Berkshelf::DEFAULT_FILENAME,
82
+ desc: 'Path to a Berksfile to operate off of.',
83
+ aliases: '-b',
84
+ banner: 'PATH'
85
+ method_option :bellboyfile,
86
+ type: :string,
87
+ default: Bellboy::DEFAULT_FILENAME,
88
+ desc: 'Path to a Bellboy file to operate off of.',
89
+ aliases: '-d',
90
+ banner: 'PATH'
91
+ def upload
92
+ berksfile = Bellboy.berks_from_file(options[:berksfile])
93
+ Bellboy::Uploader.upload(berksfile, options)
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,63 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright 2014 Dyn Inc.
4
+ #
5
+ # Authors: Kristian Van Der Vliet <kvandervliet@dyn.com>
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ # Exceptions that are specific to Bellboy
20
+ module Bellboy
21
+ # Base class for all internal Bellboy errors
22
+ class BellboyError < StandardError
23
+ end
24
+
25
+ # Base for all Read/Write/Create errors
26
+ class BellboyIOError < BellboyError
27
+ def initialize(filepath)
28
+ @filepath = File.dirname(File.expand_path(filepath)) rescue filepath
29
+ end
30
+ end
31
+
32
+ # Couldn't create or write to a new data bag item
33
+ class DatabagWriteError < BellboyIOError
34
+ def to_s
35
+ "IO error writing to #{@filepath}"
36
+ end
37
+ end
38
+
39
+ # Couldn't read from a manifest
40
+ class DatabagReadError < BellboyIOError
41
+ def to_s
42
+ "IO error reading from #{@filepath}"
43
+ end
44
+ end
45
+
46
+ # Base for all API errors
47
+ class BellboyAPIError < BellboyError
48
+ def initialize(location)
49
+ @location = location
50
+ end
51
+ end
52
+
53
+ # An error occured during a Chef API call
54
+ class ChefConnectionError < BellboyAPIError
55
+ end
56
+
57
+ # An error occured during a Bellboy API call
58
+ class DatabagAPIError < BellboyAPIError
59
+ def to_s
60
+ "Databag API call failed for #{@location}"
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,104 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright 2014 Dyn Inc.
4
+ #
5
+ # Authors: Kristian Van Der Vliet <kvandervliet@dyn.com>
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ require 'bellboy'
19
+ require 'faraday'
20
+
21
+ module Bellboy
22
+ # Downloads data bags from the remote API for any Cookbook that contains a
23
+ # Bellboy manifest.
24
+ class Installer
25
+ class << self
26
+ def install(berksfile, options = {})
27
+ @bellboyfile = options[:bellboyfile]
28
+
29
+ local_sources = Bellboy.berks_sources(berksfile)
30
+
31
+ local_sources.each do |source|
32
+ Bellboy.logger.debug "Source: #{source.cached_cookbook.path}"
33
+
34
+ if File.exists?(File.join(source.cached_cookbook.path, @bellboyfile))
35
+ site = berksfile.locations.select { |loc| loc[:type] == :site }.first
36
+
37
+ fail Berkshelf::InvalidChefAPILocation if site.nil?
38
+
39
+ download_databags(source, site)
40
+ end
41
+
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def download_databags(source, site)
48
+ Bellboy.logger.log "Downloading databags for #{source.cached_cookbook.name}"
49
+
50
+ path = source.cached_cookbook.path
51
+ containerpath = File.join(path, 'data_bags')
52
+
53
+ begin
54
+ Dir.mkdir(containerpath) unless Dir.exists?(containerpath)
55
+ rescue SystemCallError => ex
56
+ raise Bellboy::DatabagWriteError.new(containerpath), ex
57
+ end
58
+
59
+ manifest = File.join(path, @bellboyfile)
60
+ begin
61
+ File.read(manifest).split.each do |line|
62
+ databag, item = line.split('/')
63
+
64
+ databagpath = File.join(containerpath, databag)
65
+ Dir.mkdir(databagpath) unless Dir.exists?(databagpath)
66
+
67
+ itempath = File.join(databagpath, "#{item}.json")
68
+ if File.exists?(itempath)
69
+ Bellboy.logger.verbose "Skipping download of #{itempath} as it already exists"
70
+ else
71
+ download_item(site, databag, item, itempath)
72
+ end
73
+
74
+ end
75
+
76
+ rescue SystemCallError, IOError => ex
77
+ raise Bellboy::DatabagReadError.new(manifest), ex
78
+ end
79
+ end
80
+
81
+ def download_item(site, databag, item, itempath)
82
+ location = File.join("#{site[:value]}", 'databags', databag, item)
83
+
84
+ Bellboy.logger.log "Downloading data bag item #{databag}/#{item} from '#{location}'"
85
+
86
+ response = Faraday.get(location)
87
+ if response.success?
88
+ begin
89
+ Bellboy.logger.debug "Creating data bag item #{itempath}"
90
+
91
+ item = File.open(itempath, 'w')
92
+ item.write(response.body)
93
+ item.close
94
+ rescue SystemCallError, IOError => ex
95
+ raise Bellboy::DatabagWriteError.new(databagpath), ex
96
+ end
97
+ else
98
+ Bellboy.logger.log "Failed to download #{location}"
99
+ fail Bellboy::DatabagAPIError, location
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright 2014 Dyn Inc.
4
+ #
5
+ # Authors: Kristian Van Der Vliet <kvandervliet@dyn.com>
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ module Bellboy
20
+ LOG_LEVEL_NONE = 0
21
+ LOG_LEVEL_INFO = 1
22
+ LOG_LEVEL_DEBUG = 2
23
+
24
+ # Simple stdout logger
25
+ class Logger
26
+ def initialize(options = {})
27
+ @loglevel = level(options)
28
+ end
29
+
30
+ # Output a normal informational message
31
+ def log(message)
32
+ puts(message) unless @loglevel < Bellboy::LOG_LEVEL_INFO
33
+ end
34
+
35
+ # Output a debug message
36
+ def debug(message)
37
+ puts(message) unless @loglevel < Bellboy::LOG_LEVEL_DEBUG
38
+ end
39
+ alias_method :verbose, :debug
40
+
41
+ private
42
+
43
+ def level(options)
44
+ if options[:quiet]
45
+ Bellboy::LOG_LEVEL_NONE
46
+ elsif options[:verbose]
47
+ Bellboy::LOG_LEVEL_DEBUG
48
+ else
49
+ Bellboy::LOG_LEVEL_INFO
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,112 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright 2014 Dyn Inc.
4
+ #
5
+ # Authors: Kristian Van Der Vliet <kvandervliet@dyn.com>
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ require 'bellboy'
19
+ require 'JSON'
20
+
21
+ module Bellboy
22
+ # Push data bags & data bag items to the Chef server
23
+ class Uploader
24
+ class << self
25
+ def upload(berksfile, options = {})
26
+ @bellboyfile = options[:bellboyfile]
27
+
28
+ local_sources = Bellboy.berks_sources(berksfile)
29
+ conn = Bellboy.ridley_connection(options)
30
+
31
+ local_sources.each do |source|
32
+ Bellboy.logger.debug "Source: #{source.cached_cookbook.path}"
33
+
34
+ upload_databags(source.cached_cookbook, conn)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def upload_databags(cookbook, conn)
41
+ Bellboy.logger.verbose "Uploading data bags for #{cookbook.name}"
42
+
43
+ path = File.join(cookbook.path, 'data_bags')
44
+ Dir.foreach(path) do |dir|
45
+ # Skip everything that isn't a sub-directory
46
+ subdir = File.join(path, dir)
47
+ next unless File.directory?(subdir)
48
+
49
+ # Process every sub-directory (but not current & parent, natch)
50
+ unless dir == '.' || dir == '..'
51
+ begin
52
+ conn.data_bag.create(name: dir) unless conn.data_bag.find(dir)
53
+ manifest = upload_databag_items(conn, File.join(path, dir), dir)
54
+
55
+ rescue Ridley::Errors::RidleyError => ex
56
+ raise ChefConnectionError, ex
57
+ end
58
+
59
+ create_manifest_file(cookbook.path, dir, manifest)
60
+ end
61
+ end if File.exist?(path)
62
+
63
+ end
64
+
65
+ def upload_databag_items(conn, path, name)
66
+ manifest = []
67
+
68
+ Dir.foreach(path) do |file|
69
+ # Upload all of the JSON files
70
+ if file.match('.json')
71
+ begin
72
+ Bellboy.logger.log("Uploading #{File.join(path, file)}")
73
+
74
+ itempath = File.join(path, file)
75
+ begin
76
+ item = JSON.parse(IO.read(itempath))
77
+ rescue SystemCallError, IOError
78
+ raise DatabagReadError, itempath
79
+ end
80
+
81
+ data_bag = conn.data_bag.find(name)
82
+ data_bag.item.create(item)
83
+
84
+ manifest << item['id']
85
+
86
+ rescue Ridley::Errors::RidleyError => ex
87
+ raise ChefConnectionError, ex
88
+ end
89
+
90
+ end
91
+ end
92
+
93
+ manifest
94
+ end
95
+
96
+ def create_manifest_file(path, data_bag, manifest)
97
+ begin
98
+ manifestfile = File.open(File.join(path, @bellboyfile), 'w')
99
+
100
+ manifest.each do |item|
101
+ manifestfile.write("#{data_bag}/#{item}\n")
102
+ end
103
+
104
+ manifestfile.close
105
+
106
+ rescue SystemCallError, IOError => ex
107
+ raise ChefConnectionError, ex
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright 2014 Dyn Inc.
4
+ #
5
+ # Authors: Kristian Van Der Vliet <kvandervliet@dyn.com>
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ require 'bellboy'
19
+ require 'databag_version'
20
+
21
+ module Bellboy
22
+ # Process any data bag templates into versioned data bag items
23
+ class Versioner
24
+ class << self
25
+ def version(berksfile, options = {})
26
+ local_sources = Bellboy.berks_sources(berksfile)
27
+
28
+ local_sources.each do |source|
29
+ Bellboy.logger.debug "Source: #{source.cached_cookbook.path}"
30
+
31
+ DatabagVersion.process_all(options[:verbose], "#{source.cached_cookbook.path}/data_bags") if
32
+ Dir.exists?("#{source.cached_cookbook.path}/data_bags")
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+ $LOAD_PATH.push File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'bellboy'
5
+ require_relative 'spec-helpers'
6
+
7
+ describe 'cli' do
8
+ it 'should output help information if run with no arguments' do
9
+ content = capture(:stdout) { Bellboy::Cli.start(%w[]) }
10
+ expect(content).to match(/Commands:/)
11
+ end
12
+
13
+ it 'should output help information for the \'help\' command' do
14
+ content = capture(:stdout) { Bellboy::Cli.start(%w[help]) }
15
+ expect(content).to match(/Commands:/)
16
+ end
17
+
18
+ it 'should output help information for the \'version\' command' do
19
+ content = capture(:stdout) { Bellboy::Cli.start(%w[help version]) }
20
+ expect(content).to match(/Version all databag templates/)
21
+ end
22
+
23
+ it 'should output help information for the \'install\' command' do
24
+ content = capture(:stdout) { Bellboy::Cli.start(%w[help install]) }
25
+ expect(content).to match(/Install databags for all Cookbooks known by Berkshelf/)
26
+ end
27
+
28
+ it 'should output help information for the \'upload\' command' do
29
+ content = capture(:stdout) { Bellboy::Cli.start(%w[help upload]) }
30
+ expect(content).to match(/Upload all databags for all Cookbooks known by Berkshelf/)
31
+ end
32
+
33
+ it 'should not fail to run the \'version\' command with an empty Berksfile' do
34
+ Bellboy::Cli.start(%w[version -b /dev/null])
35
+ end
36
+
37
+ it 'should not fail to run the \'install\' command with an empty Berksfile' do
38
+ Bellboy::Cli.start(%w[install -b /dev/null])
39
+ end
40
+
41
+ it 'should not fail to run the \'upload\' command with an empty Berksfile' do
42
+ Bellboy::Cli.start(%w[upload -b /dev/null])
43
+ end
44
+
45
+ it 'should fail to run with an invalid Berksfile' do
46
+ content = capture(:stdout) { lambda{ Bellboy::Cli.start(%w[version -b /i/dont/exist]) }.should exit_with_code(0) }
47
+ expect(content).to match(/could not be found/)
48
+ end
49
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ $LOAD_PATH.push File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'bellboy'
5
+
6
+ describe 'installer' do
7
+ before (:each) do
8
+ @berksfile = Bellboy.berks_from_file('/dev/null')
9
+ end
10
+
11
+ it 'should not throw any exceptions' do
12
+ Bellboy::Installer.install(@berksfile)
13
+ end
14
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ $LOAD_PATH.push File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'bellboy'
5
+
6
+ describe 'logger' do
7
+ it 'should log to stdout' do
8
+ logger = Bellboy::Logger.new( {} )
9
+ content = capture(:stdout) { logger.log 'test message #1' }
10
+ expect(content).to eq("test message #1\n")
11
+ end
12
+
13
+ it 'should not log to stdout when \'quiet\' is enabled' do
14
+ logger = Bellboy::Logger.new( {:quiet => true} )
15
+ content = capture(:stdout) { logger.log 'test message #2' }
16
+ expect(content).to eq("")
17
+ end
18
+
19
+ it 'should not log debug messages to stdout' do
20
+ logger = Bellboy::Logger.new( {} )
21
+ content = capture(:stdout) { logger.debug 'test message #3' }
22
+ expect(content).to eq("")
23
+ end
24
+
25
+ it 'should log debug messages when \'verbose\' is enabled' do
26
+ logger = Bellboy::Logger.new( {:verbose => true} )
27
+ content = capture(:stdout) { logger.debug 'test message #4' }
28
+ expect(content).to eq("test message #4\n")
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ RSpec::Matchers.define :exit_with_code do |exp_code|
2
+ actual = nil
3
+ match do |block|
4
+ begin
5
+ block.call
6
+ rescue SystemExit => e
7
+ actual = e.status
8
+ end
9
+ actual and actual == exp_code
10
+ end
11
+ failure_message_for_should do |block|
12
+ "expected to call exit(#{exp_code}) but exit" +
13
+ (actual.nil? ? " not called" : "(#{actual}) was called")
14
+ end
15
+ failure_message_for_should_not do |block|
16
+ "expected block not to call exit(#{exp_code})"
17
+ end
18
+ description do
19
+ "expect block to call exit(#{exp_code})"
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ $LOAD_PATH.push File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'bellboy'
5
+
6
+ describe 'uploader' do
7
+ before (:each) do
8
+ @berksfile = Bellboy.berks_from_file('/dev/null')
9
+ end
10
+
11
+ it 'should not throw any exceptions' do
12
+ Bellboy::Uploader.upload(@berksfile)
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ $LOAD_PATH.push File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'bellboy'
5
+
6
+ describe 'versioner' do
7
+ before (:each) do
8
+ @berksfile = Bellboy.berks_from_file('/dev/null')
9
+ end
10
+
11
+ it 'should not throw any exceptions' do
12
+ Bellboy::Versioner.version(@berksfile)
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bellboy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kristian Van Der Vliet
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.18.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.18.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: berkshelf
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.0.10
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.0.10
46
+ - !ruby/object:Gem::Dependency
47
+ name: faraday
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.8.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: ridley
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.5.0
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.5.0
78
+ description: Version, install & upload data bag items that are stored alongside your
79
+ Berkshelf Cookbooks
80
+ email: kvandervliet@dyn.com
81
+ executables:
82
+ - bellboy
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE
89
+ - README.md
90
+ - Rakefile
91
+ - Thorfile
92
+ - bellboy.gemspec
93
+ - bin/bellboy
94
+ - lib/bellboy.rb
95
+ - lib/bellboy/cli.rb
96
+ - lib/bellboy/errors.rb
97
+ - lib/bellboy/installer.rb
98
+ - lib/bellboy/logger.rb
99
+ - lib/bellboy/uploader.rb
100
+ - lib/bellboy/versioner.rb
101
+ - spec/cli_spec.rb
102
+ - spec/installer_spec.rb
103
+ - spec/logger_spec.rb
104
+ - spec/spec-helpers.rb
105
+ - spec/uploader_spec.rb
106
+ - spec/versioner_spec.rb
107
+ homepage: ''
108
+ licenses:
109
+ - Apache-2.0
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 1.8.23
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: Manage data bags with Berkshelf
132
+ test_files: []