Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Generate Gemfile for plugin runner dynamically
Browse files Browse the repository at this point in the history
Add a template and method for generating the Gemfile needed for
stand alone staging script

Test plan:
- New unit test passes
- Existing unit tests pass

Change-Id: Iab3a188e6b79cbf6843fb536d68b62b3e9beb9c4
  • Loading branch information
mpage committed Nov 7, 2011
1 parent 0d85e28 commit 2211850
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
12 changes: 12 additions & 0 deletions stager/assets/plugin_runner_gemfile.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
source :rubygems

gem 'vcap_logging', '>= 0.1.1'
gem 'vcap_cloud_controller_ipc'

<% for plugin_gem in plugin_gems %>
<% if plugin_gem["version"] %>
gem '<%= plugin_gem["name"] %>', '= <%= plugin_gem["version"] %>'
<% else %>
gem '<%= plugin_gem["name"] %>'
<% end %>
<% end %>
21 changes: 21 additions & 0 deletions stager/lib/vcap/stager/plugin_runner.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'erb'
require 'fileutils'

require 'vcap/cloud_controller/ipc'
Expand All @@ -16,6 +17,8 @@ module Stager
# Responsible for orchestrating the execution of all staging plugins selected
# by the user.
class VCAP::Stager::PluginRunner
GEMFILE_TEMPLATE_PATH = File.join(VCAP::Stager::ASSET_DIR, 'plugin_runner_gemfile.erb')

class << self
attr_reader :registered_plugins

Expand All @@ -28,6 +31,24 @@ def register_plugins(*plugins)
def reset_registered_plugins
@registered_plugins = []
end

# Generates a Gemfile that should be used in conjuction with running the
# standalone script using 'bundle exec'. We assume that every plugin has
# already been installed locally on the system (therefore its individual
# dependencies have been satisfied) and rely on Bundler to resolve possible
# conflicts in plugin dependencies.
#
# @param dest_path String Where to place the generated Gemfile
# @param plugin_gems Array Plugins to be loaded
# [{'name' => GEM_NAME, 'version' => GEM_VERSION}]
def generate_standalone_gemfile(dest_path, plugins)
plugin_gems = plugins.map {|p| p['gem'] }
template = File.read(GEMFILE_TEMPLATE_PATH)
renderer = ERB.new(template)
gemfile_contents = renderer.result(binding())
File.open(dest_path, 'w+') {|f| f.write(gemfile_contents) }
dest_path
end
end

# @param source_dir String Directory containing application source
Expand Down
1 change: 0 additions & 1 deletion stager/lib/vcap/stager/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,4 @@ def run_logged(command, expected_exitstatus=0, timeout=nil)

ret
end

end
14 changes: 14 additions & 0 deletions stager/spec/unit/plugin_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
require 'tmpdir'

describe VCAP::Stager::PluginRunner do
describe '.generate_standalone_gemfile' do
it 'should write out a Gemfile containing all plugins to be run' do
tmpdir = Dir.mktmpdir
gemfile_path = File.join(tmpdir, 'Gemfile')
plugins = [{'gem' => {'name' => 'test1'}},
{'gem' => {'name' => 'test2', 'version' => '0.0.1'}}]
VCAP::Stager::PluginRunner.generate_standalone_gemfile(gemfile_path, plugins)
File.exist?(gemfile_path).should be_true
gemfile_contents = File.read(gemfile_path)
gemfile_contents.match(/^gem 'test1'$/).should be_true
gemfile_contents.match(/^gem 'test2', '= 0.0.1'$/).should be_true
end
end

describe '#run_plugins' do
before :each do
@src_dir = Dir.mktmpdir
Expand Down

0 comments on commit 2211850

Please sign in to comment.