appbundler 0.1.0.beta.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NWM2ZTg4NzM2MDA5Njg1ZGFmMmMzZTZhNzFiZGQ1ZTQxMGM5NDcxNw==
5
+ data.tar.gz: !binary |-
6
+ MDMxNTdmODAwZmFjZjA1NzQ5NDAwNjUzZjU1NjZiNDY5N2U3YWQ2Mw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ Y2JjYTRkYjEwMjA5MDZjMzYyZmU5YTExYjZlY2RmNDZjMWNhZjQ0YjE2YzRm
10
+ OGE0YTFjYjc2Y2M1MWMzNTcxY2IyODU3YTIxY2Q4NzEyY2U2M2ZhODhhOTRl
11
+ NmViNjk0YjA5Y2U4YTE4NzUzNDc3ZWQwOTA3YzEyNmQ4MDFlNWE=
12
+ data.tar.gz: !binary |-
13
+ Yjk3ZjE1MzU1NzA2ZGEwOTllYjljZjI5YzVlZWU1MTg3OWY5MWNhOGU4N2Q0
14
+ ODYxNWNhNmM4YzY0NjM2YzIwM2JmZjk4YzlhYzk1YmY3ZTg5Njg0ODAxYmNl
15
+ ZTFhZjFmY2UwNDBjYmYxM2ZkMjliMTdhZDVlZjQ5YzNhYjMxNDc=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ -cfs
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 2.0.0
4
+ - 2.1.0
5
+
6
+ branches:
7
+ only:
8
+ - master
9
+
10
+ script: bundle exec rspec --color --format progress
11
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in appbundler.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,202 @@
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.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Appbundler
2
+
3
+ Appbundler reads a Gemfile.lock and generates code with
4
+ `gem "some-dep", "= VERSION"` statements to lock the app's dependencies
5
+ to the versions selected by bundler. This code is used in binstubs for
6
+ the application so that running (e.g.) `chef-client` on the command line
7
+ activates the locked dependencies for `chef` before running the command.
8
+
9
+ This provides the following benefits:
10
+ * The application loads faster because rubygems is not resolving
11
+ dependency constraints at runtime.
12
+ * The application runs with the same dependencies that it would if
13
+ bundler was used, so we can test applications (that will be installed
14
+ in an omnibus package) using the default bundler workflow.
15
+ * There's no need to `bundle exec` or patch the bundler runtime into the
16
+ app.
17
+ * The app can load gems not included in the Gemfile/gemspec. Our use
18
+ case for this is to load plugins (e.g., for knife and test kitchen).
19
+ * A user can use rvm and still use the application (see below).
20
+ * The application is protected from installation of incompatible
21
+ dependencies.
22
+
23
+ # Usage
24
+
25
+ Clone this project (gem coming soon) and bundle install:
26
+
27
+ ```
28
+ git clone https://github.com/opscode/appbundler.git
29
+ cd appbundler
30
+ bundle install
31
+ ```
32
+
33
+ Clone whatever project you want to appbundle somewhere else, and bundle
34
+ install it:
35
+
36
+ ```
37
+ mkdir ~/oc
38
+ cd ~/oc
39
+ git clone https://github.com/opscode/chef.git
40
+ cd chef
41
+ bundle install
42
+ ```
43
+
44
+ Create a bin directory where your bundled binstubs will live:
45
+
46
+ ```
47
+ mkdir ~/appbundle-bin
48
+ # Add to your PATH if you like
49
+ ```
50
+
51
+ Now you can app bundle your project (chef in our example):
52
+
53
+ ```
54
+ bin/appbundler ~/oc/chef ~/appbundler-bin
55
+ ```
56
+
57
+ Now you can run all of the app's executables with locked down deps:
58
+
59
+ ```
60
+ ~/appbunlder-bin/chef-client -v
61
+ ```
62
+
63
+
64
+ # RVM
65
+
66
+ The generated binstubs explicitly disable rvm, so the above won't work
67
+ if you're using rvm. This is intentional, because our use case is for
68
+ omnibus applications where rvm's environment variables can break the
69
+ embedded application by making ruby look for gems in rvm's gem repo.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'appbundler/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "appbundler"
8
+ spec.version = Appbundler::VERSION
9
+ spec.authors = ["danielsdeleo"]
10
+ spec.email = ["dan@opscode.com"]
11
+ spec.description = %q{Extracts a dependency solution from bundler's Gemfile.lock to speed gem activation}
12
+ spec.summary = spec.description
13
+ spec.homepage = ""
14
+ spec.license = "Apache2"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec", "~> 2.13"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "mixlib-shellout", "~> 1.0"
25
+ end
data/bin/appbundler ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ Kernel.trap(:INT) { exit 1 }
4
+
5
+ begin
6
+ require 'appbundler/cli'
7
+ rescue LoadError
8
+ $:.unshift File.expand_path("../../lib", __FILE__)
9
+ require 'appbundler/cli'
10
+ end
11
+
12
+ Appbundler::CLI.run(ARGV)
13
+
data/lib/appbundler.rb ADDED
@@ -0,0 +1,4 @@
1
+
2
+ module Appbundler
3
+ end
4
+
@@ -0,0 +1,136 @@
1
+ require 'bundler'
2
+ require 'pp'
3
+
4
+ module Appbundler
5
+ class App
6
+
7
+ BINSTUB_FILE_VERSION=1
8
+
9
+ attr_reader :app_root
10
+ attr_reader :target_bin_dir
11
+
12
+ def self.demo
13
+ demo = new("/Users/ddeleo/oc/chef")
14
+
15
+ knife = demo.executables.grep(/knife/).first
16
+ puts demo.binstub(knife)
17
+ end
18
+
19
+ def initialize(app_root, target_bin_dir)
20
+ @app_root = app_root
21
+ @target_bin_dir = target_bin_dir
22
+ end
23
+
24
+ def write_executable_stubs
25
+ executables_to_create = executables.map do |real_executable_path|
26
+ basename = File.basename(real_executable_path)
27
+ stub_path = File.join(target_bin_dir, basename)
28
+ [real_executable_path, stub_path]
29
+ end
30
+
31
+ executables_to_create.each do |real_executable_path, stub_path|
32
+ File.open(stub_path, "wb", 0755) do |f|
33
+ f.write(binstub(real_executable_path))
34
+ end
35
+ end
36
+
37
+ executables_to_create
38
+ end
39
+
40
+ def name
41
+ File.basename(app_root)
42
+ end
43
+
44
+ def gemfile_lock
45
+ File.join(app_root, "Gemfile.lock")
46
+ end
47
+
48
+ def shebang
49
+ "#!#{Gem.ruby}\n"
50
+ end
51
+
52
+ # A specially formatted comment that documents the format version of the
53
+ # binstub files we generate.
54
+ #
55
+ # This comment should be unusual enough that we can reliably (enough)
56
+ # detect whether a binstub was created by Appbundler and parse it to learn
57
+ # what version of the format it uses. If we ever need to support reading or
58
+ # mutating existing binstubs, we'll know what file version we're starting
59
+ # with.
60
+ def file_format_comment
61
+ "#--APP_BUNDLER_BINSTUB_FORMAT_VERSION=#{BINSTUB_FILE_VERSION}--\n"
62
+ end
63
+
64
+ # Ruby code (as a string) that clears GEM_HOME and GEM_PATH environment
65
+ # variables. In an omnibus context, this is important so users can use
66
+ # things like rvm without accidentally pointing the app at rvm's
67
+ # ruby and gems.
68
+ #
69
+ # Environment sanitization can be skipped by setting the
70
+ # APPBUNDLER_ALLOW_RVM environment variable to "true". This feature
71
+ # exists to make tests run correctly on travis.ci (which uses rvm).
72
+ def env_sanitizer
73
+ %Q{ENV["GEM_HOME"] = ENV["GEM_PATH"] = nil unless ENV["APPBUNDLER_ALLOW_RVM"] == "true"}
74
+ end
75
+
76
+ def runtime_activate
77
+ @runtime_activate ||= begin
78
+ statements = runtime_dep_specs.map {|s| %Q|gem "#{s.name}", "= #{s.version}"|}
79
+ activate_code = ""
80
+ activate_code << env_sanitizer << "\n"
81
+ activate_code << statements.join("\n") << "\n"
82
+ activate_code << %Q|$:.unshift "#{app_lib_dir}"\n|
83
+ activate_code
84
+ end
85
+ end
86
+
87
+ def binstub(bin_file)
88
+ shebang + file_format_comment + runtime_activate + "Kernel.load '#{bin_file}'\n"
89
+ end
90
+
91
+ def executables
92
+ bin_dir_glob = File.join(app_root, "bin", "*")
93
+ Dir[bin_dir_glob]
94
+ end
95
+
96
+ def app_lib_dir
97
+ File.join(app_root, "lib")
98
+ end
99
+
100
+ def runtime_dep_specs
101
+ add_dependencies_from(app_spec)
102
+ end
103
+
104
+ def app_dependency_names
105
+ @app_dependency_names ||= app_spec.dependencies.map(&:name)
106
+ end
107
+
108
+ def app_spec
109
+ spec_for(name)
110
+ end
111
+
112
+ def gemfile_lock_specs
113
+ parsed_gemfile_lock.specs
114
+ end
115
+
116
+ def parsed_gemfile_lock
117
+ @parsed_gemfile_lock ||= Bundler::LockfileParser.new(IO.read(gemfile_lock))
118
+ end
119
+
120
+ private
121
+
122
+ def add_dependencies_from(spec, collected_deps=[])
123
+ spec.dependencies.each do |dep|
124
+ next if collected_deps.any? {|s| s.name == dep.name }
125
+ next_spec = spec_for(dep.name)
126
+ collected_deps << next_spec
127
+ add_dependencies_from(next_spec, collected_deps)
128
+ end
129
+ collected_deps
130
+ end
131
+
132
+ def spec_for(dep_name)
133
+ gemfile_lock_specs.find {|s| s.name == dep_name } or raise "No spec #{dep_name}"
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,79 @@
1
+ require 'appbundler/version'
2
+ require 'appbundler/app'
3
+
4
+ module Appbundler
5
+ class CLI
6
+
7
+ def self.run(argv)
8
+ cli = new(argv)
9
+ cli.validate!
10
+ cli.run
11
+ end
12
+
13
+ attr_reader :argv
14
+
15
+ attr_reader :app_path
16
+ attr_reader :bin_path
17
+
18
+ def initialize(argv)
19
+ @argv = argv
20
+ end
21
+
22
+ def validate!
23
+ if argv.any? {|arg| %w{-h --help help -v --version}.include?(arg) }
24
+ $stdout.print(usage)
25
+ exit 0
26
+ elsif argv.size != 2
27
+ usage_and_exit!
28
+ else
29
+ @app_path = File.expand_path(argv[0])
30
+ @bin_path = File.expand_path(argv[1])
31
+ verify_app_path
32
+ verify_bin_path
33
+ end
34
+ end
35
+
36
+ def verify_app_path
37
+ if !File.directory?(app_path)
38
+ err("APPLICATION_DIR `#{app_path}' is not a directory or doesn't exist")
39
+ usage_and_exit!
40
+ elsif !File.exist?(File.join(app_path, "Gemfile.lock"))
41
+ err("APPLICATION_DIR does not contain require Gemfile.lock")
42
+ usage_and_exit!
43
+ end
44
+ end
45
+
46
+ def verify_bin_path
47
+ if !File.directory?(bin_path)
48
+ err("BINSTUB_DIR `#{bin_path}' is not a directory or doesn't exist")
49
+ usage_and_exit!
50
+ end
51
+ end
52
+
53
+ def run
54
+ created_stubs = App.new(app_path, bin_path).write_executable_stubs
55
+ created_stubs.each do |real_executable_path, stub_path|
56
+ $stdout.puts "Generated binstub #{stub_path} => #{real_executable_path}"
57
+ end
58
+ end
59
+
60
+ def err(message)
61
+ $stderr.print("#{message}\n")
62
+ end
63
+
64
+ def usage_and_exit!
65
+ err(usage)
66
+ exit 1
67
+ end
68
+
69
+ def usage
70
+ <<-E
71
+ Usage: appbundler APPLICATION_DIR BINSTUB_DIR
72
+
73
+ APPLICATION_DIR is the root directory of your app
74
+ BINSTUB_DIR is the directory where you want generated executables to be written
75
+ E
76
+ end
77
+
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ module Appbundler
2
+ VERSION = "0.1.0.beta.0"
3
+ end
@@ -0,0 +1,214 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+ require 'mixlib/shellout'
4
+ require 'appbundler/app'
5
+
6
+ describe Appbundler do
7
+
8
+ def all_specs
9
+ @all_specs ||= []
10
+ end
11
+
12
+ def double_spec(name, version, dep_names)
13
+ deps = dep_names.map {|n| double("Bundler::Dependency #{n}", :name => n.to_s) }
14
+ spec = double("Bundler::LazySpecification '#{name}'", :name => name.to_s, :version => version, :dependencies => deps)
15
+ all_specs << spec
16
+ spec
17
+ end
18
+
19
+ def shellout!(cmd)
20
+ s = Mixlib::ShellOut.new(cmd, :env => {"RUBYOPT" => nil, "BUNDLE_GEMFILE" => nil, "APPBUNDLER_ALLOW_RVM" => "true"})
21
+ s.run_command
22
+ s.error!
23
+ s
24
+ end
25
+
26
+ let(:target_bindir) { File.expand_path("../../test-tmp/bin", __FILE__) }
27
+
28
+ context "given an app with multiple levels of dependencies" do
29
+
30
+ let!(:second_level_dep_a_a) do
31
+ double_spec(:second_level_dep_a_a, "2.1.0", [])
32
+ end
33
+
34
+ let!(:second_level_dep_shared) do
35
+ double_spec(:second_level_dep_shared, "2.3.0", [])
36
+ end
37
+
38
+ let!(:second_level_dep_b_a) do
39
+ double_spec(:second_level_dep_b_a, "2.2.0", [])
40
+ end
41
+
42
+ let!(:first_level_dep_a) do
43
+ double_spec(:first_level_dep_a, "1.1.0", [:second_level_dep_a_a, :second_level_dep_shared])
44
+ end
45
+
46
+ let!(:first_level_dep_b) do
47
+ double_spec(:first_level_dep_b, "1.2.0", [:second_level_dep_b_a, :second_level_dep_shared])
48
+ end
49
+
50
+ let!(:app_spec) do
51
+ double_spec(:app, "1.0.0", [:first_level_dep_a, :first_level_dep_b])
52
+ end
53
+
54
+ let(:app_root) { "/opt/app/embedded/apps/app" }
55
+
56
+ let(:app) do
57
+ Appbundler::App.new(app_root, target_bindir)
58
+ end
59
+
60
+ before do
61
+ app.stub(:gemfile_lock_specs).and_return(all_specs)
62
+ end
63
+
64
+ it "finds all runtime dependencies of the app" do
65
+ # we want to find the minimum set of gems that we need to activate to run
66
+ # the application. To do this, we look at the app's runtime deps and
67
+ # recursively search through the list of gemspecs that we got from the
68
+ # Gemfile.lock, collecting all the runtime deps. This should get us the
69
+ # smallest possible "activate.rb" file that can launch the application
70
+ # with locked gem versions.
71
+ expect(app.runtime_dep_specs).to include(first_level_dep_a)
72
+ expect(app.runtime_dep_specs).to include(first_level_dep_b)
73
+ expect(app.runtime_dep_specs).to include(second_level_dep_a_a)
74
+ expect(app.runtime_dep_specs).to include(second_level_dep_b_a)
75
+ expect(app.runtime_dep_specs).to include(second_level_dep_shared)
76
+ expect(app.runtime_dep_specs.select {|s| s == second_level_dep_shared}).to have(1).item
77
+ end
78
+
79
+ it "generates gem activation code for the app" do
80
+ # this is code with a bunch of gem "foo", "= X.Y.Z" statements. The top
81
+ # level application is _not_ included in this, it's added to the load
82
+ # path instead.
83
+ expect(app.runtime_activate).to include(%q{gem "first_level_dep_a", "= 1.1.0"})
84
+ expect(app.runtime_activate).to include(%q{gem "second_level_dep_a_a", "= 2.1.0"})
85
+ expect(app.runtime_activate).to include(%q{gem "second_level_dep_shared", "= 2.3.0"})
86
+ expect(app.runtime_activate).to include(%q{gem "first_level_dep_b", "= 1.2.0"})
87
+ expect(app.runtime_activate).to include(%q{gem "second_level_dep_b_a", "= 2.2.0"})
88
+ expect(app.runtime_activate).to_not include(%q{gem "app"})
89
+ end
90
+
91
+ it "adds the app code to the load path" do
92
+ expect(app.runtime_activate).to include('$:.unshift "/opt/app/embedded/apps/app/lib"')
93
+ end
94
+
95
+ it "generates code to override GEM_HOME and GEM_PATH (e.g., rvm)" do
96
+ expected = %Q{ENV["GEM_HOME"] = ENV["GEM_PATH"] = nil unless ENV["APPBUNDLER_ALLOW_RVM"] == "true"}
97
+ expect(app.env_sanitizer).to eq(expected)
98
+ expect(app.runtime_activate).to include(expected)
99
+ end
100
+
101
+ end
102
+
103
+ context "when created with the example application" do
104
+ FIXTURES_PATH = File.expand_path("../../fixtures/", __FILE__).freeze
105
+
106
+ APP_ROOT = File.join(FIXTURES_PATH, "example-app").freeze
107
+
108
+ let(:app_root) { APP_ROOT }
109
+
110
+ let(:app) do
111
+ Appbundler::App.new(APP_ROOT, target_bindir)
112
+ end
113
+
114
+ before(:all) do
115
+ Dir.chdir(APP_ROOT) do
116
+ shellout!("bundle install")
117
+ end
118
+ end
119
+
120
+ before do
121
+ FileUtils.rm_rf(target_bindir) if File.exist?(target_bindir)
122
+ FileUtils.mkdir_p(target_bindir)
123
+ end
124
+
125
+ after(:all) do
126
+ FileUtils.rm_rf(target_bindir) if File.exist?(target_bindir)
127
+ end
128
+
129
+ it "initializes ok" do
130
+ app
131
+ end
132
+
133
+ it "names the app using the directory basename" do
134
+ app.name.should == "example-app"
135
+ end
136
+
137
+ it "lists the app's dependencies" do
138
+ # only runtime deps
139
+ expect(app.app_dependency_names).to eq(["chef"])
140
+ end
141
+
142
+ it "generates runtime activation code for the app" do
143
+ expected_gem_activates=<<-E
144
+ ENV["GEM_HOME"] = ENV["GEM_PATH"] = nil unless ENV["APPBUNDLER_ALLOW_RVM"] == "true"
145
+ gem "chef", "= 11.10.4"
146
+ gem "chef-zero", "= 1.7.3"
147
+ gem "hashie", "= 2.0.5"
148
+ gem "json", "= 1.8.1"
149
+ gem "mixlib-log", "= 1.6.0"
150
+ gem "moneta", "= 0.6.0"
151
+ gem "rack", "= 1.5.2"
152
+ gem "diff-lcs", "= 1.2.5"
153
+ gem "erubis", "= 2.7.0"
154
+ gem "highline", "= 1.6.20"
155
+ gem "mime-types", "= 1.25.1"
156
+ gem "mixlib-authentication", "= 1.3.0"
157
+ gem "mixlib-cli", "= 1.4.0"
158
+ gem "mixlib-config", "= 2.1.0"
159
+ gem "mixlib-shellout", "= 1.3.0"
160
+ gem "net-ssh", "= 2.8.0"
161
+ gem "net-ssh-multi", "= 1.2.0"
162
+ gem "net-ssh-gateway", "= 1.2.0"
163
+ gem "ohai", "= 6.20.0"
164
+ gem "ipaddress", "= 0.8.0"
165
+ gem "systemu", "= 2.5.2"
166
+ gem "yajl-ruby", "= 1.2.0"
167
+ gem "pry", "= 0.9.12.6"
168
+ gem "coderay", "= 1.1.0"
169
+ gem "method_source", "= 0.8.2"
170
+ gem "slop", "= 3.4.7"
171
+ gem "puma", "= 1.6.3"
172
+ gem "rest-client", "= 1.6.7"
173
+ E
174
+ expect(app.runtime_activate).to include(expected_gem_activates)
175
+ expected_load_path = '$:.unshift "' + File.join(app_root, "lib") + '"'
176
+ expect(app.runtime_activate).to include(expected_load_path)
177
+ end
178
+
179
+ it "lists the app's executables" do
180
+ expected_executables = %w[app-binary-1 app-binary-2].map do |basename|
181
+ File.join(app_root, "/bin", basename)
182
+ end
183
+ expect(app.executables).to match_array(expected_executables)
184
+ end
185
+
186
+ it "generates an executable 'stub' for an executable in the app" do
187
+ app_binary_1_path = app.executables.grep(/app\-binary\-1/).first
188
+ executable_content = app.binstub(app_binary_1_path)
189
+
190
+ shebang = executable_content.lines.first
191
+ expect(shebang).to match(/^\#\!/)
192
+ expect(shebang).to include(Gem.ruby)
193
+
194
+ expect(executable_content).to include(app.runtime_activate)
195
+
196
+ load_binary = executable_content.lines.to_a.last
197
+ expect(load_binary).to eq(%Q[Kernel.load '#{app_binary_1_path}'\n])
198
+ end
199
+
200
+ it "generates executable stubs for all executables in the app" do
201
+ app.write_executable_stubs
202
+ binary_1 = File.join(target_bindir, "app-binary-1")
203
+ binary_2 = File.join(target_bindir, "app-binary-2")
204
+ expect(File.exist?(binary_1)).to be_true
205
+ expect(File.exist?(binary_2)).to be_true
206
+ expect(File.executable?(binary_1)).to be_true
207
+ expect(File.executable?(binary_1)).to be_true
208
+ expect(shellout!(binary_1).stdout).to eq("binary 1 ran\n")
209
+ expect(shellout!(binary_2).stdout).to eq("binary 2 ran\n")
210
+ end
211
+
212
+ end
213
+
214
+ end
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
@@ -0,0 +1,3 @@
1
+ # Example App
2
+
3
+ This app is a test fixture. It has no use of its own.
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ruby
2
+ puts "binary 1 ran"
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ruby
2
+ puts "binary 2 ran"
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "example-app"
5
+ spec.version = "1.0.0"
6
+ spec.authors = ["danielsdeleo"]
7
+ spec.email = ["dan@opscode.com"]
8
+ spec.description = %q{test fixture app}
9
+ spec.summary = spec.description
10
+ spec.homepage = ""
11
+ spec.license = "Apache2"
12
+
13
+ spec.files = Dir.glob("{bin,lib,spec}/**/*").reject {|f| File.directory?(f) }
14
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_dependency "chef"
19
+ spec.add_development_dependency "rake"
20
+ spec.add_development_dependency "pry"
21
+ end
22
+
@@ -0,0 +1,3 @@
1
+ module ExampleApp
2
+ IT_WORKS = "yes"
3
+ end
File without changes
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: appbundler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.beta.0
5
+ platform: ruby
6
+ authors:
7
+ - danielsdeleo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mixlib-shellout
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ description: Extracts a dependency solution from bundler's Gemfile.lock to speed gem
70
+ activation
71
+ email:
72
+ - dan@opscode.com
73
+ executables:
74
+ - appbundler
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - .gitignore
79
+ - .rspec
80
+ - .travis.yml
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - appbundler.gemspec
86
+ - bin/appbundler
87
+ - lib/appbundler.rb
88
+ - lib/appbundler/app.rb
89
+ - lib/appbundler/cli.rb
90
+ - lib/appbundler/version.rb
91
+ - spec/appbundler/app_spec.rb
92
+ - spec/fixtures/example-app/Gemfile
93
+ - spec/fixtures/example-app/Gemfile.lock
94
+ - spec/fixtures/example-app/README.md
95
+ - spec/fixtures/example-app/bin/app-binary-1
96
+ - spec/fixtures/example-app/bin/app-binary-2
97
+ - spec/fixtures/example-app/example-app.gemspec
98
+ - spec/fixtures/example-app/lib/example_app.rb
99
+ - spec/spec_helper.rb
100
+ homepage: ''
101
+ licenses:
102
+ - Apache2
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>'
116
+ - !ruby/object:Gem::Version
117
+ version: 1.3.1
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.2.0
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Extracts a dependency solution from bundler's Gemfile.lock to speed gem activation
124
+ test_files:
125
+ - spec/appbundler/app_spec.rb
126
+ - spec/fixtures/example-app/Gemfile
127
+ - spec/fixtures/example-app/Gemfile.lock
128
+ - spec/fixtures/example-app/README.md
129
+ - spec/fixtures/example-app/bin/app-binary-1
130
+ - spec/fixtures/example-app/bin/app-binary-2
131
+ - spec/fixtures/example-app/example-app.gemspec
132
+ - spec/fixtures/example-app/lib/example_app.rb
133
+ - spec/spec_helper.rb
134
+ has_rdoc: